-
Notifications
You must be signed in to change notification settings - Fork 53
/
SDS011.cpp
136 lines (121 loc) · 3.68 KB
/
SDS011.cpp
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
// SDS011 dust sensor PM2.5 and PM10
// ---------------------
//
// By R. Zschiegner (rz@madavi.de)
// April 2016
//
// Documentation:
// - The iNovaFitness SDS011 datasheet
//
#include "SDS011.h"
static constexpr uint8_t SLEEPCMD[19] = {
0xAA, 0xB4, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x05, 0xAB
};
static constexpr uint8_t WAKEUPCMD[19] = {
0xAA, 0xB4, 0x06, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x06, 0xAB
};
static constexpr uint8_t VERSION_CMD[] PROGMEM = {
0xAA, 0xB4, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x05, 0xAB
};
static constexpr uint8_t CONTINUOUS_MODE_CMD[] PROGMEM = {
0xAA, 0xB4, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x07, 0xAB
};
SDS011::SDS011(void) {
}
// --------------------------------------------------------
// SDS011:read
// --------------------------------------------------------
int SDS011::read(float *p25, float *p10) {
byte buffer;
int value;
int len = 0;
int pm10_serial = 0;
int pm25_serial = 0;
int checksum_is;
int checksum_ok = 0;
int error = 1;
while ((sds_data->available() > 0) && (sds_data->available() >= (10-len))) {
buffer = sds_data->read();
value = int(buffer);
switch (len) {
case (0): if (value != 170) { len = -1; }; break;
case (1): if (value != 192) { len = -1; }; break;
case (2): pm25_serial = value; checksum_is = value; break;
case (3): pm25_serial += (value << 8); checksum_is += value; break;
case (4): pm10_serial = value; checksum_is += value; break;
case (5): pm10_serial += (value << 8); checksum_is += value; break;
case (6): checksum_is += value; break;
case (7): checksum_is += value; break;
case (8): if (value == (checksum_is % 256)) { checksum_ok = 1; } else { len = -1; }; break;
case (9): if (value != 171) { len = -1; }; break;
}
len++;
if (len == 10 && checksum_ok == 1) {
*p10 = (float)pm10_serial/10.0;
*p25 = (float)pm25_serial/10.0;
len = 0; checksum_ok = 0; pm10_serial = 0.0; pm25_serial = 0.0; checksum_is = 0;
error = 0;
}
yield();
}
return error;
}
// --------------------------------------------------------
// SDS011:sleep
// --------------------------------------------------------
void SDS011::sleep() {
for (uint8_t i = 0; i < 19; i++) {
sds_data->write(SLEEPCMD[i]);
}
sds_data->flush();
while (sds_data->available() > 0) {
sds_data->read();
}
}
// --------------------------------------------------------
// SDS011:wakeup
// --------------------------------------------------------
void SDS011::wakeup() {
for (uint8_t i = 0; i < 19; i++) {
sds_data->write(WAKEUPCMD[i]);
}
sds_data->flush();
while (sds_data->available() > 0) {
sds_data->read();
}
}
// --------------------------------------------------------
// SDS011:continuous_mode
// --------------------------------------------------------
void SDS011::continuous_mode() {
for (uint8_t i = 0; i < 19; i++) {
sds_data->write(CONTINUOUS_MODE_CMD[i]);
}
sds_data->flush();
while (sds_data->available() > 0) {
sds_data->read();
}
}
#ifndef ESP32
void SDS011::begin(uint8_t pin_rx, uint8_t pin_tx) {
_pin_rx = pin_rx;
_pin_tx = pin_tx;
SoftwareSerial *softSerial = new SoftwareSerial(_pin_rx, _pin_tx);
softSerial->begin(9600);
sds_data = softSerial;
}
void SDS011::begin(SoftwareSerial* serial) {
serial->begin(9600);
sds_data = serial;
}
#endif
#ifdef ESP32
void SDS011::begin(HardwareSerial* serial) {
serial->begin(9600);
sds_data = serial;
}
void SDS011::begin(HardwareSerial* serial, int8_t rxPin, int8_t txPin) {
serial->begin(9600, SERIAL_8N1, rxPin, txPin);
sds_data = serial;
}
#endif