-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDS011.cpp
123 lines (109 loc) · 2.33 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
/* Copyright 2018 Dustmap.org. All rights reserved.
*
* This work is licensed under the terms of the MIT license.
* For a copy, see <https://opensource.org/licenses/MIT>.
*/
#include "SDS011.h"
SDS011::SDS011(uint8_t pin_rx, uint8_t pin_tx, uint16_t max_interval) {
_max_interval = max_interval * 1000;
_lastreadtime = -1 * _max_interval;
serial = new SoftwareSerial(pin_rx, pin_tx);
serial->begin(9600);
}
bool SDS011::read(int *pm25, int *pm10, byte *deviceId, bool force) {
uint32_t currenttime = millis();
if (!force && (currenttime-_lastreadtime) < _max_interval) {
return false;
}
_lastreadtime = currenttime;
int length = 0;
int checksum = 0;
while (serial->available() > 0) {
byte buffer = serial->read();
int value = static_cast<int>(buffer);
switch (length) {
// Message header (0xAA)
case 0:
if (buffer != 0xAA) {
length = -1;
}
break;
// Command ID (0xC0)
case 1:
if (buffer != 0xC0) {
length = -1;
}
break;
// DATA 1 (PM2.5 Low byte)
case 2:
*pm25 = value;
checksum = value;
break;
// DATA 2 (PM2.5 High byte)
case 3:
*pm25 += value << 8;
checksum += value;
break;
// DATA 3 (PM10 Low byte)
case 4:
*pm10 = value;
checksum += value;
break;
// DATA 4 (PM10 High byte)
case 5:
*pm10 += value << 8;
checksum += value;
break;
// DATA 5 (ID byte 1)
case 6:
deviceId[0] = buffer;
checksum += value;
break;
// DATA 6 (ID byte 2)
case 7:
deviceId[1] = buffer;
checksum += value;
break;
// Checksum byte
// Low 8bit of the sum result of Data Bytes
// (not including packet head, tail and Command ID)
case 8:
if (value != checksum % 256) {
length = -1;
}
break;
// Message tail (0xAB)
case 9:
if (buffer != 0xAB) {
length = -1;
}
break;
case 10:
return true;
}
length++;
}
return false;
}
void SDS011::sleep() {
static byte sleepBytes[19];
sleepBytes[0] = 0xAA;
sleepBytes[1] = 0xB4;
sleepBytes[2] = 0x06;
sleepBytes[3] = 0x01;
sleepBytes[15] = 0xFF;
sleepBytes[16] = 0xFF;
sleepBytes[17] = 0x05; // Should be 0x08 acording to datasheet?
sleepBytes[18] = 0xAB;
for (uint8_t i = 0; i < 19; i++) {
serial->write(sleepBytes[i]);
}
serial->flush();
while (serial->available() > 0) {
serial->read();
}
}
void SDS011::wakeup() {
serial->write(0x01);
serial->flush();
}