-
Notifications
You must be signed in to change notification settings - Fork 0
/
mhz19c.c
446 lines (331 loc) · 11 KB
/
mhz19c.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include "mhz19c.h"
#define BUFFER_SIZE (9)
#define RETRY_MAX (10)
#define TX_START (0)
#define TX_RESERVED (1)
#define TX_COMMAND (2)
#define TX_DATA(i) (3+i)
#define TX_CHECKSUM (8)
#define RX_START (0)
#define RX_COMMAND (1)
#define RX_DATA(i) (2+i)
#define RX_CHECKSUM (8)
#define START_VALUE (0xff)
#define RESERVED_VALUE (0x01)
#define COM_SET_AUTO_CALIB (0x79)
#define COM_GET_AUTO_CALIB (0x7d)
#define COM_GET_TEMPRATURE (0x85)
#define COM_GET_CO2_PPM (0x86)
#define COM_ZERO_CALIBRATION (0x87)
#define COM_GET_VERSION (0xa0)
#define SET_AUTO_CALIB_OFF (0x00)
#define SET_AUTO_CALIB_ON (0xa0)
static bool mhz19c_get_version(struct mhz19c_t *mhz19c);
static uint8_t mhz19c_get_checksum(const uint8_t *buffer);
// ----------------------------------------------------------------------------
// Logging Utility
#define mhz19c_log_verbose(mhz19c, ...) \
do { \
if (mhz19c->verbose) { \
fprintf(stderr, "verbose: "); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
} \
} while(false)
#define mhz19c_log_error(...) \
do { \
fprintf(stderr, "error: "); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
} while(false)
void mhz19c_set_log_verbose(struct mhz19c_t *mhz19c, bool verbose) {
mhz19c->verbose = verbose;
mhz19c_log_verbose(mhz19c, "log level set to verbose = %d.", verbose);
}
// ----------------------------------------------------------------------------
// I/O Utility (UART)
bool mhz19c_open(struct mhz19c_t *mhz19c) {
mhz19c_log_verbose(mhz19c, "open the device.");
const int fd = open("/dev/serial0", O_RDWR);
if (fd < 0) {
mhz19c_log_error("failed to open the device.");
return false;
}
mhz19c_log_verbose(mhz19c, "set the termios state.");
struct termios tio = {};
if (tcgetattr(fd, &tio) < 0) {
mhz19c_log_error("failed to get the termios state.");
close(fd);
return false;
}
// Set serial port baud rate be 9600.
cfsetspeed(&tio, B9600);
// Use raw mode. This also sets data bit to 8 bytes and parity bit null.
cfmakeraw(&tio);
// Set stop bit to 1 byte.
tio.c_cflag &= ~((tcflag_t)CSTOPB);
tio.c_cflag |= CREAD;
tio.c_cflag |= CLOCAL;
// Set min read bytes to 0.
tio.c_cc[VMIN] = 0;
// Set read timeout in 500 ms.
tio.c_cc[VTIME] = 5;
// Note:
// This is equivalent to `ioctl(fd, TCSETS, &tio)`.
// Use of ioctl makes for non-portable programs.
if (tcsetattr(fd, TCSANOW, &tio) < 0) {
mhz19c_log_error("failed to set the termios state.");
close(fd);
return false;
}
mhz19c->fd = fd;
// Get firmware version.
// Note:
// When connecting for the first time after turning on the power, data
// that does not match the checksum may be received. So it will retry
// until it receives correct data.
{
bool success = false;
for (int i = 0; i < RETRY_MAX; i += 1) {
if (mhz19c_get_version(mhz19c)) {
success = true;
break;
}
usleep(25 * 1000); // Wait for 25 ms.
}
if (!success) {
mhz19c_log_error("failed to get the firmware version.");
close(fd);
return false;
}
}
return true;
}
bool mhz19c_close(const struct mhz19c_t *mhz19c) {
mhz19c_log_verbose(mhz19c, "close the device.");
if (close(mhz19c->fd) < 0) {
mhz19c_log_error("failed to close the device.");
return false;
}
return true;
}
static bool mhz19c_write(const struct mhz19c_t *mhz19c, uint8_t command, const uint8_t *data, size_t data_size) {
// Write data.
uint8_t buffer_tx[BUFFER_SIZE] = {};
buffer_tx[TX_START] = START_VALUE;
buffer_tx[TX_RESERVED] = RESERVED_VALUE;
buffer_tx[TX_COMMAND] = command;
if (data != NULL) {
for (size_t i = 0; i < data_size; i += 1) {
buffer_tx[TX_DATA(i)] = data[i];
}
}
buffer_tx[TX_CHECKSUM] = mhz19c_get_checksum(buffer_tx);
if (mhz19c->verbose) {
char message[64] = "send data:";
char temp[8];
for (size_t i = 0; i < BUFFER_SIZE; i += 1) {
sprintf(temp, " %02x", buffer_tx[i]);
strcat(message, temp);
}
mhz19c_log_verbose(mhz19c, "%s", message);
}
ssize_t count = write(mhz19c->fd, buffer_tx, BUFFER_SIZE);
if (count != BUFFER_SIZE) {
mhz19c_log_error("failed to send data.");
return false;
}
if (tcdrain(mhz19c->fd) < 0) {
mhz19c_log_error("failed to drain data.");
return false;
}
return true;
}
static bool mhz19c_read(const struct mhz19c_t *mhz19c, uint8_t *command, uint8_t *data, size_t data_size) {
// Read data.
uint8_t buffer_rx[BUFFER_SIZE] = {};
size_t total_count = 0;
for (int i = 0; i < RETRY_MAX; i += 1) {
const size_t rem = BUFFER_SIZE - total_count;
ssize_t count = read(mhz19c->fd, buffer_rx + total_count, rem);
if (count < 0) {
mhz19c_log_error("failed to read data. (%zd)", count);
return false;
}
mhz19c_log_verbose(mhz19c, "received %zd bytes.", count);
total_count += (size_t)count;
if (total_count == BUFFER_SIZE) {
break;
}
}
if (total_count != BUFFER_SIZE) {
mhz19c_log_error("failed to read data. (%zu)", total_count);
return false;
}
if (mhz19c->verbose) {
char message[64] = "read data:";
char temp[8];
for (size_t i = 0; i < BUFFER_SIZE; i += 1) {
sprintf(temp, " %02x", buffer_rx[i]);
strcat(message, temp);
}
mhz19c_log_verbose(mhz19c, "%s", message);
}
// Validate data with checksum.
const uint8_t actual_checksum = mhz19c_get_checksum(buffer_rx);
if (buffer_rx[RX_CHECKSUM] != actual_checksum) {
mhz19c_log_error("failed to varify checksum. the expected value is %02x, but the actual value is %02x.", buffer_rx[RX_CHECKSUM], actual_checksum);
return false;
}
mhz19c_log_verbose(mhz19c, "the checksum value (%02x) is correct.", actual_checksum);
// Return the received command.
if (command != NULL) {
*command = buffer_rx[RX_COMMAND];
}
// Return the received data.
if (data != NULL) {
for (size_t i = 0; i < data_size; i += 1) {
data[i] = buffer_rx[RX_DATA(i)];
}
}
return true;
}
// ----------------------------------------------------------------------------
// MH-Z19C
bool mhz19c_get_temperature(const struct mhz19c_t *mhz19c, float *temp) {
mhz19c_log_verbose(mhz19c, "mhz19c_get_temperature()");
if (mhz19c->version[1] <= '4') {
mhz19c_log_error("not supported.");
return false;
}
if (tcflush(mhz19c->fd, TCIOFLUSH) < 0) {
mhz19c_log_error("failed to flush data.");
return false;
}
// Send command.
if (!mhz19c_write(mhz19c, COM_GET_TEMPRATURE, NULL, 0)) {
return false;
}
// Read the return value.
const size_t data_size = 2;
uint8_t data[data_size];
if (!mhz19c_read(mhz19c, NULL, data, data_size)) {
return false;
}
// Return temperature (°C).
float _temp = (float)(data[0] << 8 | data[1]) / 100.f;
mhz19c_log_verbose(mhz19c, "temp = %.2f", _temp);
if (temp != NULL) {
*temp = _temp;
}
return true;
}
bool mhz19c_get_co2_ppm(const struct mhz19c_t *mhz19c, int *co2_ppm, int *temp) {
mhz19c_log_verbose(mhz19c, "mhz19c_get_co2_ppm()");
if (tcflush(mhz19c->fd, TCIOFLUSH) < 0) {
mhz19c_log_error("failed to flush data.");
return false;
}
// Send command.
if (!mhz19c_write(mhz19c, COM_GET_CO2_PPM, NULL, 0)) {
return false;
}
// Read the return value.
const size_t data_size = 3;
uint8_t data[data_size];
if (!mhz19c_read(mhz19c, NULL, data, data_size)) {
return false;
}
// Return CO2 concentration (ppm) and temperature (°C).
int _co2_ppm = data[0] << 8 | data[1];
int _temp = data[2] - 40;
mhz19c_log_verbose(mhz19c, "co2_ppm = %d", _co2_ppm);
mhz19c_log_verbose(mhz19c, "temp = %d", _temp);
if (co2_ppm != NULL) {
*co2_ppm = _co2_ppm;
}
if (temp != NULL) {
*temp = _temp;
}
return true;
}
bool mhz19c_zero_calibration(const struct mhz19c_t *mhz19c) {
mhz19c_log_verbose(mhz19c, "mhz19c_start_calibration()");
if (tcflush(mhz19c->fd, TCIOFLUSH) < 0) {
mhz19c_log_error("failed to flush data.");
return false;
}
// Send command.
if (!mhz19c_write(mhz19c, COM_ZERO_CALIBRATION, NULL, 0)) {
return false;
}
return true;
}
bool mhz19c_set_abc(const struct mhz19c_t *mhz19c, bool is_on) {
mhz19c_log_verbose(mhz19c, "mhz19c_set_auto_calib(is_on = %d)", is_on);
if (tcflush(mhz19c->fd, TCIOFLUSH) < 0) {
mhz19c_log_error("failed to flush data.");
return false;
}
// Send command.
const uint8_t data = is_on ? SET_AUTO_CALIB_ON : SET_AUTO_CALIB_OFF;
if (!mhz19c_write(mhz19c, COM_SET_AUTO_CALIB, &data, 1)) {
return false;
}
return true;
}
bool mhz19c_get_abc(const struct mhz19c_t *mhz19c, bool *is_on) {
mhz19c_log_verbose(mhz19c, "mhz19c_get_auto_calib()");
if (tcflush(mhz19c->fd, TCIOFLUSH) < 0) {
mhz19c_log_error("failed to flush data.");
return false;
}
// Send command.
if (!mhz19c_write(mhz19c, COM_GET_AUTO_CALIB, NULL, 0)) {
return false;
}
// Read the return value.
const size_t data_size = 6;
uint8_t data[data_size];
if (!mhz19c_read(mhz19c, NULL, data, data_size)) {
return false;
}
// Return state of auto calibration.
bool _is_on = data[5];
mhz19c_log_verbose(mhz19c, "is_on = %d", _is_on);
if (is_on != NULL) {
*is_on = _is_on;
}
return true;
}
static bool mhz19c_get_version(struct mhz19c_t *mhz19c) {
mhz19c_log_verbose(mhz19c, "mhz19c_get_version()");
if (tcflush(mhz19c->fd, TCIOFLUSH) < 0) {
mhz19c_log_error("failed to flush data.");
return false;
}
// Send command.
if (!mhz19c_write(mhz19c, COM_GET_VERSION, NULL, 0)) {
return false;
}
// Read the return value.
size_t size = sizeof(mhz19c->version);
if (!mhz19c_read(mhz19c, NULL, (uint8_t *)mhz19c->version, size - 1)) {
return false;
}
mhz19c->version[size - 1] = 0x00;
mhz19c_log_verbose(mhz19c, "version = %s", mhz19c->version);
return true;
}
static uint8_t mhz19c_get_checksum(const uint8_t *buffer) {
uint8_t checksum = 0x00;
for (int i = 1; i <= 7; i += 1) {
checksum += buffer[i];
}
return 0xff - checksum + 0x01;
}