-
Notifications
You must be signed in to change notification settings - Fork 11
/
sen5x_i2c_example_usage.c
193 lines (175 loc) · 7.07 KB
/
sen5x_i2c_example_usage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* I2C-Generator: 0.3.0
* Yaml Version: 2.1.3
* Template Version: 0.7.0-109-gb259776
*/
/*
* Copyright (c) 2021, Sensirion AG
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Sensirion AG nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <math.h> // NAN
#include <stdio.h> // printf
#include "sen5x_i2c.h"
#include "sensirion_common.h"
#include "sensirion_i2c_hal.h"
/*
* TO USE CONSOLE OUTPUT (PRINTF) YOU MAY NEED TO ADAPT THE INCLUDE ABOVE OR
* DEFINE IT ACCORDING TO YOUR PLATFORM:
* #define printf(...)
*/
/**
* Linux specific configuration. Adjust the following define to the device path
* of your sensor.
*/
#define I2C_DEVICE_PATH "/dev/i2c-1"
int main(void) {
int16_t error = 0;
sensirion_i2c_hal_init(I2C_DEVICE_PATH);
error = sen5x_device_reset();
if (error) {
printf("Error executing sen5x_device_reset(): %i\n", error);
}
unsigned char serial_number[32];
uint8_t serial_number_size = 32;
error = sen5x_get_serial_number(serial_number, serial_number_size);
if (error) {
printf("Error executing sen5x_get_serial_number(): %i\n", error);
} else {
printf("Serial number: %s\n", serial_number);
}
unsigned char product_name[32];
uint8_t product_name_size = 32;
error = sen5x_get_product_name(product_name, product_name_size);
if (error) {
printf("Error executing sen5x_get_product_name(): %i\n", error);
} else {
printf("Product name: %s\n", product_name);
}
uint8_t firmware_major;
uint8_t firmware_minor;
bool firmware_debug;
uint8_t hardware_major;
uint8_t hardware_minor;
uint8_t protocol_major;
uint8_t protocol_minor;
error = sen5x_get_version(&firmware_major, &firmware_minor, &firmware_debug,
&hardware_major, &hardware_minor, &protocol_major,
&protocol_minor);
if (error) {
printf("Error executing sen5x_get_version(): %i\n", error);
} else {
printf("Firmware: %u.%u, Hardware: %u.%u\n", firmware_major,
firmware_minor, hardware_major, hardware_minor);
}
// set a temperature offset in degrees celsius
// Note: supported by SEN54 and SEN55 sensors
// By default, the temperature and humidity outputs from the sensor
// are compensated for the modules self-heating. If the module is
// designed into a device, the temperature compensation might need
// to be adapted to incorporate the change in thermal coupling and
// self-heating of other device components.
//
// A guide to achieve optimal performance, including references
// to mechanical design-in examples can be found in the app note
// “SEN5x – Temperature Compensation Instruction” at www.sensirion.com.
// Please refer to those application notes for further information
// on the advanced compensation settings used in
// `sen5x_set_temperature_offset_parameters`,
// `sen5x_set_warm_start_parameter` and `sen5x_set_rht_acceleration_mode`.
//
// Adjust temp_offset to account for additional temperature offsets
// exceeding the SEN module's self heating.
float temp_offset = 0.0f;
error = sen5x_set_temperature_offset_simple(temp_offset);
if (error) {
printf("Error executing sen5x_set_temperature_offset_simple(): %i\n",
error);
} else {
printf("Temperature Offset set to %.2f °C (SEN54/SEN55 only)\n",
temp_offset);
}
// Start Measurement
error = sen5x_start_measurement();
if (error) {
printf("Error executing sen5x_start_measurement(): %i\n", error);
}
for (int i = 0; i < 600; i++) {
// Read Measurement
sensirion_i2c_hal_sleep_usec(1000000);
float mass_concentration_pm1p0;
float mass_concentration_pm2p5;
float mass_concentration_pm4p0;
float mass_concentration_pm10p0;
float ambient_humidity;
float ambient_temperature;
float voc_index;
float nox_index;
error = sen5x_read_measured_values(
&mass_concentration_pm1p0, &mass_concentration_pm2p5,
&mass_concentration_pm4p0, &mass_concentration_pm10p0,
&ambient_humidity, &ambient_temperature, &voc_index, &nox_index);
if (error) {
printf("Error executing sen5x_read_measured_values(): %i\n", error);
} else {
printf("Mass concentration pm1p0: %.1f µg/m³\n",
mass_concentration_pm1p0);
printf("Mass concentration pm2p5: %.1f µg/m³\n",
mass_concentration_pm2p5);
printf("Mass concentration pm4p0: %.1f µg/m³\n",
mass_concentration_pm4p0);
printf("Mass concentration pm10p0: %.1f µg/m³\n",
mass_concentration_pm10p0);
if (isnan(ambient_humidity)) {
printf("Ambient humidity: n/a\n");
} else {
printf("Ambient humidity: %.1f %%RH\n", ambient_humidity);
}
if (isnan(ambient_temperature)) {
printf("Ambient temperature: n/a\n");
} else {
printf("Ambient temperature: %.1f °C\n", ambient_temperature);
}
if (isnan(voc_index)) {
printf("Voc index: n/a\n");
} else {
printf("Voc index: %.1f\n", voc_index);
}
if (isnan(nox_index)) {
printf("Nox index: n/a\n");
} else {
printf("Nox index: %.1f\n", nox_index);
}
}
}
error = sen5x_stop_measurement();
if (error) {
printf("Error executing sen5x_stop_measurement(): %i\n", error);
}
return 0;
}