-
Notifications
You must be signed in to change notification settings - Fork 4
/
sen5x_i2c_example_usage.c
195 lines (177 loc) · 7.21 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
194
195
/*
* 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 <stdio.h> // printf
#include <string.h> // strcmp
#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(...)
*/
int main(void) {
int16_t error = 0;
sensirion_i2c_hal_init();
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 - 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 in degrees celsius to account for additional
// temperature offsets exceeding the SEN module's self heating.
float temp_offset = 0.0f;
int16_t default_slope = 0;
uint16_t default_time_constant = 0;
error = sen5x_set_temperature_offset_parameters(
(int16_t)(200 * temp_offset), default_slope, default_time_constant);
if (error) {
printf(
"Error executing sen5x_set_temperature_offset_parameters(): %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 < 60; i++) {
// Read Measurement
sensirion_i2c_hal_sleep_usec(1000000);
uint16_t mass_concentration_pm1p0;
uint16_t mass_concentration_pm2p5;
uint16_t mass_concentration_pm4p0;
uint16_t mass_concentration_pm10p0;
int16_t ambient_humidity;
int16_t ambient_temperature;
int16_t voc_index;
int16_t 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 / 10.0f);
printf("Mass concentration pm2p5: %.1f µg/m³\n",
mass_concentration_pm2p5 / 10.0f);
printf("Mass concentration pm4p0: %.1f µg/m³\n",
mass_concentration_pm4p0 / 10.0f);
printf("Mass concentration pm10p0: %.1f µg/m³\n",
mass_concentration_pm10p0 / 10.0f);
if (ambient_humidity == 0x7fff) {
printf("Ambient humidity: n/a\n");
} else {
printf("Ambient humidity: %.1f %%RH\n",
ambient_humidity / 100.0f);
}
if (ambient_temperature == 0x7fff) {
printf("Ambient temperature: n/a\n");
} else {
printf("Ambient temperature: %.1f °C\n",
ambient_temperature / 200.0f);
}
if (voc_index == 0x7fff) {
printf("Voc index: n/a\n");
} else {
printf("Voc index: %.1f\n", voc_index / 10.0f);
}
if (nox_index == 0x7fff) {
printf("Nox index: n/a\n");
} else {
printf("Nox index: %.1f\n", nox_index / 10.0f);
}
}
}
error = sen5x_stop_measurement();
if (error) {
printf("Error executing sen5x_stop_measurement(): %i\n", error);
}
return 0;
}