-
Notifications
You must be signed in to change notification settings - Fork 1
/
pms7003.ino
436 lines (384 loc) · 13.1 KB
/
pms7003.ino
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
//Rev0 - PMS7003 PM2.5,PM10 air quality reporting. OTA update support.
// HTTP post packet destination configurable via WiFi PMS7003_AP.
// Credits to these include file/code providers:
#include <FS.h> //this needs to be first, or it all crashes and burns...
ADC_MODE(ADC_VCC)
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include <PMS.h>
// Enable debugging monitoring
//#define DEBUG
#ifdef DEBUG
#define DBG(message) Serial.print(message)
#define DBGL(message) Serial.println(message)
#define WRITE_LED
#define TOGGLE_LED
#define SET_LED(x)
#else
#define DBG(message)
#define DBGL(message)
#define set_serial(x)
#define LED_OUTPUT_LEVEL (led_on == true ? LOW : HIGH)
#define WRITE_LED digitalWrite(LED_BUILTIN, LED_OUTPUT_LEVEL)
#define TOGGLE_LED (led_on = !led_on)
#define SET_LED(x) (led_on = x)
bool led_on = false;
#endif
//json defaults. If there are different values in config.json, they are overwritten.
const int host_max_len = 80;
char host[host_max_len] = {0};
const int url_max_len = 80;
char url[url_max_len] = {0};
char send_fail_str[10] = "0";
bool shouldSaveConfig = false;
int serial_select = 0; // 0 = DBG GPIO1, GPIO3, 1 = PMS7003, GPIO15, GPIO13
float vdd = 0.0;
const unsigned max_ten_cnt_for_AP_config = 3; // after this time device idles and waits for pwr reset
const unsigned max_minutes_for_update = 10;
const unsigned int retry_quit_cnt = 4;
const unsigned int retry_delay_sec = 20;
unsigned int send_fail_cnt = 0;
bool send_data_success = false;
bool read_data_success = false;
unsigned long update_timeout = 0;
bool update_in_progress = false;
const uint32_t pms_read_delay_ms = 30000;
const unsigned long sleep_time_in_ms = (10 * 60 * 1000) - pms_read_delay_ms; // approx poll rate of air sample less read delay
const unsigned int retry_reset_connect_parms_cnt = 24*60*60*1000 / (pms_read_delay_ms + sleep_time_in_ms); // ~one day
WiFiClient client;
const int httpPort = 8080;
const char * AP_pwd = "your_pwd";
enum pms7003_action {
pms_wakeup,
pms_getdata,
pms_sleep,
pms_ota
};
pms7003_action pms_next_action = pms_wakeup;
unsigned long pms_next_action_ms = 0;
PMS pms(Serial);
PMS::DATA pmsdata;
//callback notifying us of the need to save config
void saveConfigCallback () {
DBGL("Should save config");
shouldSaveConfig = true;
}
void sleepCallback() {
#ifdef DEBUG
DBGL();
Serial.flush();
#endif
}
void connect(bool clr_connect_info) {
unsigned wait_cnt;
const unsigned connect_seconds_max = 20;
WiFiManager wifiManager;
int ten_cnt = 0;
if(WiFi.status() != WL_CONNECTED) {
DBGL("connecting WiFi...");
//wifi_fpm_do_wakeup();
WiFi.forceSleepWake();
delay(100);
wifi_fpm_close;
wifi_set_sleep_type(MODEM_SLEEP_T);
wifi_set_opmode(STATION_MODE);
wifi_station_connect();
wait_cnt = 0;
while(wait_cnt++ < connect_seconds_max && WiFi.status() != WL_CONNECTED) {
delay(1000);
DBG(wait_cnt);
}
if(WiFi.status() != WL_CONNECTED) {
DBGL("unable to connect");
// The extra parameters to be configured (can be either global or just in the setup)
// After connecting, parameter.getValue() will get you the configured value
// id/name placeholder/prompt default length
WiFiManagerParameter custom_host("host", "Host Website ie. site.com", host, host_max_len);
WiFiManagerParameter custom_url("url", "url", url, url_max_len);
#ifdef DEBUG
wifiManager.setDebugOutput(true);
#else
wifiManager.setDebugOutput(false);
#endif
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
//set static ip
//wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//add all your parameters here
wifiManager.addParameter(&custom_host);
wifiManager.addParameter(&custom_url);
//set minimu quality of signal so it ignores AP's under that quality
//defaults to 8%
//wifiManager.setMinimumSignalQuality();
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
wifiManager.setTimeout(10*60); // 10 minutes
//fetches ssid and pass and tries to send air quality data
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
while(ten_cnt++ < max_ten_cnt_for_AP_config && WiFi.status() != WL_CONNECTED) {
//if (wifiManager.autoConnect("PMS7003_AP")) {
if (wifiManager.autoConnect("PMS7003_AP", AP_pwd)) {
strcpy(host, custom_host.getValue());
strcpy(url, custom_url.getValue());
}
}
}
}
if (clr_connect_info || strlen(host) == 0 || strlen(url) == 0) {
WiFi.disconnect();
delay(2000);
ESP.deepSleep(1000 * 1000);
}
}
void disconnect() {
if (WiFi.status() == WL_CONNECTED) {
DBGL("disconnecting WiFi...");
wifi_set_opmode(NULL_MODE);
wifi_fpm_open();
wifi_fpm_set_wakeup_cb(sleepCallback);
WiFi.forceSleepBegin();
delay(100);
}
}
void get_config() {
//read configuration from FS json
DBGL("mounting FS...");
if (SPIFFS.begin()) {
DBGL("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
DBGL("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
DBGL("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonDocument doc(size + 100);
DeserializationError error = deserializeJson(doc, buf.get());
//doc.printTo(Serial);
if (!error) {
DBGL("\nparsed json");
strcpy(host, doc["host"]);
DBGL(host);
strcpy(url, doc["url"]);
DBGL(url);
strcpy(send_fail_str, doc["send_fail"]);
DBGL(send_fail_str);
} else {
DBGL("failed to load json config");
}
}
}
} else {
DBGL("failed to mount FS");
SPIFFS.format();
DBGL("SPIFFS fortmatted !");
}
send_fail_cnt = atoi(send_fail_str);
DBG("Send Fail Count ");
DBGL(send_fail_cnt);
}
void get_vdd() {
vdd = (float)ESP.getVcc() / 1000.0;
}
void save_config() {
DBGL("saving config");
DynamicJsonDocument doc(1024);
doc["host"] = host;
doc["url"] = url;
itoa(send_fail_cnt, send_fail_str, 10);
doc["send_fail"] = send_fail_str;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
DBGL("failed to open config file for writing");
}
//doc.printTo(Serial);
serializeJson(doc, configFile);
configFile.close();
shouldSaveConfig = false;
}
bool send_data() {
bool success = false;
String senddata = "SP_1_0=" + String(pmsdata.PM_SP_UG_1_0) + "&SP_2_5=" + String(pmsdata.PM_SP_UG_2_5) + "&SP_10_0=" + String(pmsdata.PM_SP_UG_10_0) +
"&AE_1_0=" + String(pmsdata.PM_AE_UG_1_0) + "&AE_2_5=" + String(pmsdata.PM_AE_UG_2_5) + "&AE_10_0=" + String(pmsdata.PM_AE_UG_10_0) +
"&batt=" + String(vdd) + "&fail=" + String(send_fail_cnt);
if (WiFi.status() != WL_CONNECTED) {
connect(false); // Typical connect with valid SSID and URL
}
if (WiFi.status() == WL_CONNECTED) {
DBG("Requesting URL: ");
DBGL(url);
DBGL(senddata);
if (client.connect(host, httpPort)) {
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: " + senddata.length() + "\r\n\r\n" +
senddata + "\r\n");
while (client.connected()) {
if (client.available()) {
String line = client.readStringUntil('\n');
DBGL(line);
if (line.startsWith("success")) {
success = true;
} else if (line.startsWith("update")) {
success = true;
pms_next_action = pms_ota;
pms_next_action_ms = millis();
update_timeout = pms_next_action_ms + max_minutes_for_update*60*1000;
}
}
}
client.stop();
}
}
return success;
}
#ifdef DEBUG
void set_serial(int sel_pms7003) {
if (serial_select != sel_pms7003) {
Serial.flush();
while (Serial.available()) { Serial.read(); }
// esp8266 lib 2.5 Serial.swap is broke - gotta do this
//Serial.end();
if (sel_pms7003) {
//Serial.begin(PMS::BAUD_RATE);
Serial.swap(15);
} else {
//Serial.begin(115200);
Serial.swap(1);
}
//Serial.swap();
//pinMode(15, OUTPUT);
serial_select = sel_pms7003;
}
}
#endif
void setup() {
#ifdef DEBUG
// esp8266 lib 2.5 Serial.swap is broke - gotta have same baud as pms7003
Serial.begin(PMS::BAUD_RATE);
//Serial.begin(115200);
delay(10000); // for USB to instantiate
serial_select = 0;
#else
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LED_OUTPUT_LEVEL);
// GPIO15, GPIO13 (TX/RX pin on ESP-12F)
Serial.begin(PMS::BAUD_RATE);
Serial.swap(15); // switch pins to PMS7003 for full time use - Serial not shared for debug
serial_select = 1;
#endif
ArduinoOTA.begin();
ArduinoOTA.onStart([]() {
DBGL("OTA onstart");
update_in_progress = true;
pms_next_action = pms_ota;
pms_next_action_ms = millis();
update_timeout = pms_next_action_ms + max_minutes_for_update*60*1000;
set_serial(0);
});
// Set modem sleep for accurate ADC reads
disconnect();
get_vdd();
get_config();
}
void loop() {
if (millis() > pms_next_action_ms) {
switch (pms_next_action) {
case pms_wakeup:
DBGL("pms_wakeup");
delay(2000);
set_serial(1);
pms.passiveMode();
delay(1000); // seemed to need this for fan start?
pms.wakeUp();
set_serial(0);
pms_next_action = pms_getdata;
pms_next_action_ms = millis() + pms_read_delay_ms;
break;
case pms_getdata:
DBGL("pms_getdata");
set_serial(1);
pms.requestRead();
delay(1000);
read_data_success = pms.readUntil(pmsdata);
set_serial(0);
if (read_data_success) {
pms_next_action = pms_sleep;
pms_next_action_ms = millis() + 1000;
} else {
DBGL("failed to read data from PMS7003");
//Wait for potential OTA
pms_next_action = pms_ota;
pms_next_action_ms = millis();
update_timeout = pms_next_action_ms + max_minutes_for_update*60*1000;
connect(false);
}
break;
case pms_sleep:
send_data_success = send_data(); // this blocks waiting for connect and send
if (send_data_success == false) {
send_fail_cnt++;
if (send_fail_cnt > retry_reset_connect_parms_cnt) {
set_serial(1);
pms.sleep();
send_fail_cnt = 1;
save_config();
connect(true);
// code path ends here
}
save_config();
if (send_fail_cnt % retry_quit_cnt == 0) {
set_serial(1);
pms.sleep();
delay(2000);
disconnect();
ESP.deepSleep(sleep_time_in_ms * 1000);
// code path ends here
} else {
pms_next_action_ms = millis() + retry_delay_sec * 1000;
}
} else { // this is also pms_ota path after successful send
if (send_fail_cnt != 0) {
send_fail_cnt = 0;
shouldSaveConfig = true;
}
if (shouldSaveConfig) {
save_config();
}
set_serial(1);
pms.sleep();
delay(2000);
set_serial(0);
if (pms_next_action != pms_ota) {
disconnect();
ESP.deepSleep(sleep_time_in_ms * 1000);
// no code path here - resets after timeout
}
}
break;
case pms_ota:
if( !update_in_progress && millis() > update_timeout ) {
disconnect();
ESP.deepSleep(sleep_time_in_ms * 1000);
}
break;
}
}
if (WiFi.status() == WL_CONNECTED) {
ArduinoOTA.handle();
}
}