-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDS011.cpp
226 lines (200 loc) · 5.32 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
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
#include <SoftwareSerial.h>
#include <NovaSDS011.h>
#include "sensordata.h"
#define SDS_PIN_RX 16
#define SDS_PIN_TX 17
float levelP25; // P2.5 level
float levelP10; // P10 level
NovaSDS011 sds011;
void testDataReportingMode(uint16_t device_id = 0xFFFF)
{
Serial.println("Start: testDataReportingMode with ID " + String(device_id));
// Test Set to Active
if (!sds011.setDataReportingMode(DataReportingMode::active, device_id))
{
Serial.println("Fail: setting SDS011 Report Mode to active.");
return;
}
DataReportingMode reportMode = sds011.getDataReportingMode(device_id);
if (reportMode == DataReportingMode::report_error)
{
Serial.println("Fail: getting SDS011 Report Mode.");
return;
}
if (reportMode == DataReportingMode::query)
{
Serial.println("Fail: SDS011 Report Mode not as set.");
return;
}
// Test Set to Query
if (!sds011.setDataReportingMode(DataReportingMode::query, device_id))
{
Serial.println("Fail: setting SDS011 Report Mode to query.");
return;
}
reportMode = sds011.getDataReportingMode(device_id);
if (reportMode == DataReportingMode::report_error)
{
Serial.println("Fail: getting SDS011 Report Mode.");
return;
}
if (reportMode == DataReportingMode::active)
{
Serial.println("Fail: SDS011 Report Mode not as set.");
return;
}
// Test Set to Active Again
if (!sds011.setDataReportingMode(DataReportingMode::active, device_id))
{
Serial.println("Fail: setting SDS011 Report Mode to active.");
return;
}
reportMode = sds011.getDataReportingMode(device_id);
if (reportMode == DataReportingMode::report_error)
{
Serial.println("Fail: getting SDS011 Report Mode.");
return;
}
if (reportMode == DataReportingMode::query)
{
Serial.println("Fail: SDS011 Report Mode not as set.");
return;
}
Serial.println("Success: testDataReportingMod");
}
void testDataWorkingMode(uint16_t device_id = 0xFFFF)
{
Serial.println("Start: testDataWorkingMode with ID " + String(device_id));
// Test Set to Sleep
if (!sds011.setWorkingMode(WorkingMode::sleeping, device_id))
{
Serial.println("Fail: setting SDS011 Working Mode to sleep.");
return;
}
delay(4000); //Hear fan turning off
//Test Set to Work
if (!sds011.setWorkingMode(WorkingMode::work, device_id))
{
Serial.println("Fail: setting SDS011 Working Mode to work.");
return;
}
delay(400);
WorkingMode workingMode = sds011.getWorkingMode(device_id);
if (workingMode == WorkingMode::working_error)
{
Serial.println("Fail: getting SDS011 Working Mode.");
return;
}
if (workingMode == WorkingMode::sleeping)
{
Serial.println("Fail: SDS011 Working Mode not as set.");
return;
}
Serial.println("Success: testDataWorkingMode");
}
void testDataDutyCycle(uint16_t device_id = 0xFFFF)
{
Serial.println("Start: testDataDutyCycle with ID " + String(device_id));
// Test Set to 20
if (!sds011.setDutyCycle(20, device_id))
{
Serial.println("Fail: setting SDS011 duty cycle to 20.");
return;
}
uint8_t dutyCycle = sds011.getDutyCycle(device_id);
if (dutyCycle == 0xFF)
{
Serial.println("Fail: getting SDS011 duty cycle.");
return;
}
else if (dutyCycle != 20)
{
Serial.println("Fail: SDS011 duty cycle not set. Expected 20 get " + String(dutyCycle));
return;
}
// Test Set to 0
if (!sds011.setDutyCycle(0, device_id))
{
Serial.println("Fail: setting SDS011 duty cycle to 0.");
return;
}
Serial.println("Success: testDataDutyCycle");
}
void testSetDeviceID(uint16_t device_id = 0xFFFF)
{
Serial.println("Start: testSetDeviceID to " + String(device_id));
if (!sds011.setDeviceID(device_id))
{
Serial.println("Fail setting SDS011 Device ID to 0xAAAA.");
return;
}
Serial.println("Success: testSetDeviceID");
}
void setupSDS(void)
{
sds011.begin(SDS_PIN_RX, SDS_PIN_TX);
//testDataReportingMode();
//testDataWorkingMode();
//testDataDutyCycle();
//testSetDeviceID(0xAAAA);
if (sds011.setWorkingMode(WorkingMode::work))
{
Serial.println("SDS011 working mode \"Work\"");
}
else
{
Serial.println("FAIL: Unable to set working mode \"Work\"");
}
SDS011Version version = sds011.getVersionDate();
if (version.valid)
{
Serial.println("SDS011 Firmware Vesion:\nYear: " + String(version.year) + "\nMonth: " +
String(version.month) + "\nDay: " + String(version.day));
}
else
{
Serial.println("FAIL: Unable to obtain Software Version");
}
if (sds011.setDutyCycle(5))
{
Serial.println("SDS011 Duty Cycle set to 5min");
}
else
{
Serial.println("FAIL: Unable to set Duty Cycle");
}
}
void printError(QuerryError err){
switch (err)
{
case no_error:
Serial.println("no_error");
break;
case no_new_data:
Serial.println("no_new_data");
break;
case response_error:
Serial.println("response_error");
break;
case call_to_often:
Serial.println("call_to_often");
break;
default:
Serial.println("Unknown Error");
break;
}
}
void loopSDS(void)
{
float p25, p10;
QuerryError err = sds011.queryData(p25, p10);
if (err == QuerryError::no_error)
{
levelP25 = p25;
levelP10 = p10;
//Serial.println(String(millis() / 1000) + "s:PM2.5=" + String(p25) + ", PM10=" + String(p10));
} else {
//Serial.print("Could not read values from sensor, reason: ");
//printError(err);
}
}