-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
airrohr-firmware.ino
6320 lines (5710 loc) · 189 KB
/
airrohr-firmware.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************
* *
* This source code needs to be compiled for the board *
* NodeMCU 1.0 (ESP-12E Module) *
* *
************************************************************************
* *
* airRohr firmware *
* Copyright (C) 2016-2021 Code for Stuttgart a.o. *
* Copyright (C) 2021-2024 Sensor.Community a.o. *
* Copyright (C) 2019-2020 Dirk Mueller *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
************************************************************************
* OK LAB Particulate Matter Sensor *
* - nodemcu-LoLin board *
* - Nova SDS0111 *
* http://inovafitness.com/en/Laser-PM2-5-Sensor-SDS011-35.html *
* *
* Wiring Instruction see included Readme.md *
* *
************************************************************************
* *
* Alternative *
* - nodemcu-LoLin board *
* *
* Wiring Instruction: *
* Pin 2 of dust sensor PM2.5 -> Digital 6 (PWM) *
* Pin 3 of dust sensor -> +5V *
* Pin 4 of dust sensor PM1 -> Digital 3 (PMW) *
* *
* *
************************************************************************
* *
* Please check Readme.md for other sensors and hardware *
* *
************************************************************************
*
* latest build
* RAM: [==== ] 41.8% (used 34220 bytes from 81920 bytes)
* Flash: [======= ] 67.1% (used 701191 bytes from 1044464 bytes)
*
************************************************************************/
#include <WString.h>
#include <pgmspace.h>
// increment on change
#define SOFTWARE_VERSION_STR "NRZ-2024-135"
String SOFTWARE_VERSION(SOFTWARE_VERSION_STR);
/*****************************************************************
* Includes *
*****************************************************************/
#if defined(ESP8266)
#include <FS.h> // must be first
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <SoftwareSerial.h>
#include <Hash.h>
#include <ctime>
#include <coredecls.h>
#include <sntp.h>
#endif
#if defined(ESP32)
#define FORMAT_SPIFFS_IF_FAILED true
#include <FS.h>
#include <HTTPClient.h>
#include <SPIFFS.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <HardwareSerial.h>
#include <hwcrypto/sha.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#endif
// includes common to ESP8266 and ESP32 (especially external libraries)
#include "./oledfont.h" // avoids including the default Arial font, needs to be included before SSD1306.h
#include <SSD1306.h>
#include <SH1106.h>
#include <LiquidCrystal_I2C.h>
#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 0
#define ARDUINOJSON_ENABLE_ARDUINO_PRINT 0
#define ARDUINOJSON_DECODE_UNICODE 0
#include <ArduinoJson.h>
#include <DNSServer.h>
#include "./DHT.h"
#include <Adafruit_HTU21DF.h>
#include <Adafruit_BMP085.h>
#include <Adafruit_SHT31.h>
#include <StreamString.h>
#include <DallasTemperature.h>
#include <SparkFun_SCD30_Arduino_Library.h>
#include <TinyGPS++.h>
#include "./bmx280_i2c.h"
#include "./sps30_i2c.h"
#include "./dnms_i2c.h"
#include "./intl.h"
#include "./utils.h"
#include "defines.h"
#include "ext_def.h"
#include "html-content.h"
/******************************************************************
* The variables inside the cfg namespace are persistent *
* configuration values. They have defaults which can be *
* configured at compile-time via the ext_def.h file *
* They can be changed by the user via the web interface, the *
* changes are persisted to the flash and read back after reboot. *
* Note that the names of these variables can't be easily changed *
* as they are part of the json format used to persist the data. *
******************************************************************/
namespace cfg
{
unsigned debug = DEBUG;
unsigned time_for_wifi_config = 600000;
unsigned sending_intervall_ms = 145000;
bool powersave;
char current_lang[3];
// credentials for basic auth of internal web server
bool www_basicauth_enabled = WWW_BASICAUTH_ENABLED;
char www_username[LEN_WWW_USERNAME];
char www_password[LEN_CFG_PASSWORD];
// wifi credentials
char wlanssid[LEN_WLANSSID];
char wlanpwd[LEN_CFG_PASSWORD];
char static_ip[16];
char static_subnet[16];
char static_gateway[16];
char static_dns[16];
// credentials of the sensor in access point mode
char fs_ssid[LEN_FS_SSID] = FS_SSID;
char fs_pwd[LEN_CFG_PASSWORD] = FS_PWD;
// (in)active sensors
bool dht_read = DHT_READ;
bool htu21d_read = HTU21D_READ;
bool ppd_read = PPD_READ;
bool sds_read = SDS_READ;
bool pms_read = PMS_READ;
bool hpm_read = HPM_READ;
bool npm_read = NPM_READ;
bool npm_fulltime = NPM_FULLTIME;
bool ips_read = IPS_READ;
bool sps30_read = SPS30_READ;
bool bmp_read = BMP_READ;
bool bmx280_read = BMX280_READ;
char height_above_sealevel[8] = "0";
bool sht3x_read = SHT3X_READ;
bool scd30_read = SCD30_READ;
bool ds18b20_read = DS18B20_READ;
bool dnms_read = DNMS_READ;
char dnms_correction[LEN_DNMS_CORRECTION] = DNMS_CORRECTION;
bool gps_read = GPS_READ;
char temp_correction[LEN_TEMP_CORRECTION] = TEMP_CORRECTION;
// send to "APIs"
bool send2dusti = SEND2SENSORCOMMUNITY;
bool send2madavi = SEND2MADAVI;
bool send2sensemap = SEND2SENSEMAP;
bool send2fsapp = SEND2FSAPP;
bool send2aircms = SEND2AIRCMS;
bool send2custom = SEND2CUSTOM;
bool send2influx = SEND2INFLUX;
bool send2csv = SEND2CSV;
bool auto_update = AUTO_UPDATE;
bool use_beta = USE_BETA;
// (in)active displays
bool has_display = HAS_DISPLAY; // OLED with SSD1306 and I2C
bool has_sh1106 = HAS_SH1106;
bool has_flipped_display = HAS_FLIPPED_DISPLAY;
bool has_lcd1602 = HAS_LCD1602;
bool has_lcd1602_27 = HAS_LCD1602_27;
bool has_lcd2004 = HAS_LCD2004;
bool has_lcd2004_27 = HAS_LCD2004_27;
bool display_wifi_info = DISPLAY_WIFI_INFO;
bool display_device_info = DISPLAY_DEVICE_INFO;
// API settings
bool ssl_madavi = SSL_MADAVI;
bool ssl_dusti = SSL_SENSORCOMMUNITY;
char senseboxid[LEN_SENSEBOXID] = SENSEBOXID;
char host_influx[LEN_HOST_INFLUX];
char url_influx[LEN_URL_INFLUX];
unsigned port_influx = PORT_INFLUX;
char user_influx[LEN_USER_INFLUX] = USER_INFLUX;
char pwd_influx[LEN_PASS_INFLUX] = PWD_INFLUX;
char measurement_name_influx[LEN_MEASUREMENT_NAME_INFLUX];
bool ssl_influx = SSL_INFLUX;
char host_custom[LEN_HOST_CUSTOM];
char url_custom[LEN_URL_CUSTOM];
bool ssl_custom = SSL_CUSTOM;
unsigned port_custom = PORT_CUSTOM;
char user_custom[LEN_USER_CUSTOM] = USER_CUSTOM;
char pwd_custom[LEN_CFG_PASSWORD] = PWD_CUSTOM;
void initNonTrivials(const char *id)
{
strcpy(cfg::current_lang, CURRENT_LANG);
strcpy_P(www_username, WWW_USERNAME);
strcpy_P(www_password, WWW_PASSWORD);
strcpy_P(wlanssid, WLANSSID);
strcpy_P(wlanpwd, WLANPWD);
strcpy_P(host_custom, HOST_CUSTOM);
strcpy_P(url_custom, URL_CUSTOM);
strcpy_P(host_influx, HOST_INFLUX);
strcpy_P(url_influx, URL_INFLUX);
strcpy_P(measurement_name_influx, MEASUREMENT_NAME_INFLUX);
if (!*fs_ssid)
{
strcpy(fs_ssid, SSID_BASENAME);
strcat(fs_ssid, id);
}
}
}
#define JSON_BUFFER_SIZE 2800
LoggerConfig loggerConfigs[LoggerCount];
long int sample_count = 0;
bool htu21d_init_failed = false;
bool bmp_init_failed = false;
bool bmx280_init_failed = false;
bool sht3x_init_failed = false;
bool scd30_init_failed = false;
bool dnms_init_failed = false;
bool gps_init_failed = false;
bool airrohr_selftest_failed = false;
#if defined(ESP8266)
ESP8266WebServer server(80);
#endif
#if defined(ESP32)
WebServer server(80);
#endif
// default IPv4 address (ex. 192.168.4.1)
const uint8_t default_ip_first_octet = 192;
const uint8_t default_ip_second_octet = 168;
const uint8_t default_ip_third_octet = 4;
const uint8_t default_ip_fourth_octet = 1;
#include "./airrohr-cfg.h"
/*****************************************************************
* Variables for Noise Measurement DNMS *
*****************************************************************/
float last_value_dnms_laeq = -1.0;
float last_value_dnms_la_min = -1.0;
float last_value_dnms_la_max = -1.0;
/*****************************************************************
* Display definitions *
*****************************************************************/
SSD1306 *oled_ssd1306 = nullptr;
SH1106 *oled_sh1106 = nullptr;
LiquidCrystal_I2C *lcd_1602 = nullptr;
LiquidCrystal_I2C *lcd_2004 = nullptr;
const uint8_t lcd_1602_default_i2c_address = 0x3f;
const uint8_t lcd_1602_alternate_i2c_address = 0x27;
const uint8_t lcd_1602_columns = 16;
const uint8_t lcd_1602_rows = 2;
const uint8_t lcd_2004_default_i2c_address = 0x3f;
const uint8_t lcd_2004_alternate_i2c_address = 0x27;
const uint8_t lcd_2004_columns = 20;
const uint8_t lcd_2004_rows = 4;
/*****************************************************************
* Serial declarations *
*****************************************************************/
#if defined(ESP8266)
SoftwareSerial serialSDS;
SoftwareSerial *serialGPS;
SoftwareSerial serialNPM;
SoftwareSerial serialIPS;
#endif
#if defined(ESP32)
#define serialSDS (Serial1)
#define serialGPS (&(Serial2))
#define serialNPM (Serial1)
#define serialIPS (Serial1)
#endif
/*****************************************************************
* DHT declaration *
*****************************************************************/
DHT dht(ONEWIRE_PIN, DHT_TYPE);
/*****************************************************************
* HTU21D declaration *
*****************************************************************/
Adafruit_HTU21DF htu21d;
/*****************************************************************
* BMP declaration *
*****************************************************************/
Adafruit_BMP085 bmp;
/*****************************************************************
* BMP/BME280 declaration *
*****************************************************************/
BMX280 bmx280;
const uint8_t bmx280_default_i2c_address = 0x77;
const uint8_t bmx280_alternate_i2c_address = 0x76;
/*****************************************************************
* SHT3x declaration *
*****************************************************************/
Adafruit_SHT31 sht3x;
/*****************************************************************
* DS18B20 declaration *
*****************************************************************/
OneWire oneWire;
DallasTemperature ds18b20(&oneWire);
/*****************************************************************
* SCD30 declaration *
*****************************************************************/
SCD30 scd30;
/*****************************************************************
* GPS declaration *
*****************************************************************/
TinyGPSPlus gps;
/*****************************************************************
* Variable Definitions for PPD24NS *
* P1 for PM10 & P2 for PM25 *
*****************************************************************/
boolean trigP1 = false;
boolean trigP2 = false;
unsigned long trigOnP1;
unsigned long trigOnP2;
unsigned long lowpulseoccupancyP1 = 0;
unsigned long lowpulseoccupancyP2 = 0;
bool send_now = false;
unsigned long starttime;
unsigned long time_point_device_start_ms;
unsigned long starttime_SDS;
unsigned long starttime_GPS;
unsigned long starttime_NPM;
unsigned long starttime_IPS;
unsigned long act_micro;
unsigned long act_milli;
unsigned long last_micro = 0;
unsigned long min_micro = 1000000000;
unsigned long max_micro = 0;
bool is_SDS_running = true;
enum
{
SDS_REPLY_HDR = 10,
SDS_REPLY_BODY = 8
} SDS_waiting_for;
// To read NPM responses
enum
{
NPM_REPLY_HEADER_16 = 16,
NPM_REPLY_STATE_16 = 14,
NPM_REPLY_BODY_16 = 13,
NPM_REPLY_CHECKSUM_16 = 1
} NPM_waiting_for_16; //for concentration
enum
{
NPM_REPLY_HEADER_4 = 4,
NPM_REPLY_STATE_4 = 2,
NPM_REPLY_CHECKSUM_4 = 1
} NPM_waiting_for_4; //for change
enum
{
NPM_REPLY_HEADER_5 = 5,
NPM_REPLY_STATE_5 = 3,
NPM_REPLY_DATA_5 = 2,
NPM_REPLY_CHECKSUM_5 = 1
} NPM_waiting_for_5; //for fan speed
enum
{
NPM_REPLY_HEADER_6 = 6,
NPM_REPLY_STATE_6 = 4,
NPM_REPLY_DATA_6 = 3,
NPM_REPLY_CHECKSUM_6 = 1
} NPM_waiting_for_6; // for version
enum
{
NPM_REPLY_HEADER_8 = 8,
NPM_REPLY_STATE_8 = 6,
NPM_REPLY_BODY_8 = 5,
NPM_REPLY_CHECKSUM_8 = 1
} NPM_waiting_for_8; // for temperature/humidity
//ENUM POUR IPS??
String current_state_npm;
String current_th_npm;
bool is_PMS_running = true;
bool is_HPM_running = true;
bool is_NPM_running = false;
bool is_IPS_running;
unsigned long sending_time = 0;
unsigned long last_update_attempt;
int last_update_returncode;
int last_sendData_returncode;
float last_value_BMP_T = -128.0;
float last_value_BMP_P = -1.0;
float last_value_BMX280_T = -128.0;
float last_value_BMX280_P = -1.0;
float last_value_BME280_H = -1.0;
float last_value_DHT_T = -128.0;
float last_value_DHT_H = -1.0;
float last_value_DS18B20_T = -1.0;
float last_value_HTU21D_T = -128.0;
float last_value_HTU21D_H = -1.0;
float last_value_SHT3X_T = -128.0;
float last_value_SHT3X_H = -1.0;
float last_value_SCD30_T = -128.0;
float last_value_SCD30_H = -1.0;
uint16_t last_value_SCD30_CO2 = 0;
uint32_t sds_pm10_sum = 0;
uint32_t sds_pm25_sum = 0;
uint32_t sds_val_count = 0;
uint32_t sds_pm10_max = 0;
uint32_t sds_pm10_min = 20000;
uint32_t sds_pm25_max = 0;
uint32_t sds_pm25_min = 20000;
int pms_pm1_sum = 0;
int pms_pm10_sum = 0;
int pms_pm25_sum = 0;
int pms_val_count = 0;
int pms_pm1_max = 0;
int pms_pm1_min = 20000;
int pms_pm10_max = 0;
int pms_pm10_min = 20000;
int pms_pm25_max = 0;
int pms_pm25_min = 20000;
int hpm_pm10_sum = 0;
int hpm_pm25_sum = 0;
int hpm_val_count = 0;
int hpm_pm10_max = 0;
int hpm_pm10_min = 20000;
int hpm_pm25_max = 0;
int hpm_pm25_min = 20000;
uint32_t npm_pm1_sum = 0;
uint32_t npm_pm10_sum = 0;
uint32_t npm_pm25_sum = 0;
uint32_t npm_pm1_sum_pcs = 0;
uint32_t npm_pm10_sum_pcs = 0;
uint32_t npm_pm25_sum_pcs = 0;
uint16_t npm_val_count = 0;
uint16_t npm_pm1_max = 0;
uint16_t npm_pm1_min = 20000;
uint16_t npm_pm10_max = 0;
uint16_t npm_pm10_min = 20000;
uint16_t npm_pm25_max = 0;
uint16_t npm_pm25_min = 20000;
uint16_t npm_pm1_max_pcs = 0;
uint16_t npm_pm1_min_pcs = 60000;
uint16_t npm_pm10_max_pcs = 0;
uint16_t npm_pm10_min_pcs = 60000;
uint16_t npm_pm25_max_pcs = 0;
uint16_t npm_pm25_min_pcs = 60000;
float ips_pm01_sum = 0;
float ips_pm03_sum = 0;
float ips_pm05_sum = 0;
float ips_pm1_sum = 0;
float ips_pm25_sum = 0;
float ips_pm5_sum = 0;
float ips_pm10_sum = 0;
unsigned long ips_pm01_sum_pcs = 0;
unsigned long ips_pm03_sum_pcs = 0;
unsigned long ips_pm05_sum_pcs = 0;
unsigned long ips_pm1_sum_pcs = 0;
unsigned long ips_pm25_sum_pcs = 0;
unsigned long ips_pm5_sum_pcs = 0;
unsigned long ips_pm10_sum_pcs = 0;
uint16_t ips_val_count = 0;
float ips_pm01_max = 0;
float ips_pm01_min = 200;
float ips_pm03_max = 0;
float ips_pm03_min = 200;
float ips_pm05_max = 0;
float ips_pm05_min = 200;
float ips_pm1_max = 0;
float ips_pm1_min = 200;
float ips_pm25_max = 0;
float ips_pm25_min = 200;
float ips_pm5_max = 0;
float ips_pm5_min = 200;
float ips_pm10_max = 0;
float ips_pm10_min = 200;
unsigned long ips_pm01_max_pcs = 0;
unsigned long ips_pm01_min_pcs = 4000000000;
unsigned long ips_pm03_max_pcs = 0;
unsigned long ips_pm03_min_pcs = 4000000000;
unsigned long ips_pm05_max_pcs = 0;
unsigned long ips_pm05_min_pcs = 4000000000;
unsigned long ips_pm1_max_pcs = 0;
unsigned long ips_pm1_min_pcs = 4000000000;
unsigned long ips_pm25_max_pcs = 0;
unsigned long ips_pm25_min_pcs = 4000000000;
unsigned long ips_pm5_max_pcs = 0;
unsigned long ips_pm5_min_pcs = 4000000000;
unsigned long ips_pm10_max_pcs = 0;
unsigned long ips_pm10_min_pcs = 4000000000;
float last_value_SPS30_P0 = -1.0;
float last_value_SPS30_P1 = -1.0;
float last_value_SPS30_P2 = -1.0;
float last_value_SPS30_P4 = -1.0;
float last_value_SPS30_N05 = -1.0;
float last_value_SPS30_N1 = -1.0;
float last_value_SPS30_N25 = -1.0;
float last_value_SPS30_N4 = -1.0;
float last_value_SPS30_N10 = -1.0;
float last_value_SPS30_TS = -1.0;
float value_SPS30_P0 = 0.0;
float value_SPS30_P1 = 0.0;
float value_SPS30_P2 = 0.0;
float value_SPS30_P4 = 0.0;
float value_SPS30_N05 = 0.0;
float value_SPS30_N1 = 0.0;
float value_SPS30_N25 = 0.0;
float value_SPS30_N4 = 0.0;
float value_SPS30_N10 = 0.0;
float value_SPS30_TS = 0.0;
uint16_t SPS30_measurement_count = 0;
unsigned long SPS30_read_counter = 0;
unsigned long SPS30_read_error_counter = 0;
unsigned long SPS30_read_timer = 0;
bool sps30_init_failed = false;
float last_value_PPD_P1 = -1.0;
float last_value_PPD_P2 = -1.0;
float last_value_SDS_P1 = -1.0;
float last_value_SDS_P2 = -1.0;
float last_value_PMS_P0 = -1.0;
float last_value_PMS_P1 = -1.0;
float last_value_PMS_P2 = -1.0;
float last_value_HPM_P1 = -1.0;
float last_value_HPM_P2 = -1.0;
float last_value_NPM_P0 = -1.0;
float last_value_NPM_P1 = -1.0;
float last_value_NPM_P2 = -1.0;
float last_value_NPM_N1 = -1.0;
float last_value_NPM_N10 = -1.0;
float last_value_NPM_N25 = -1.0;
float last_value_IPS_P0 = -1.0; //PM1
float last_value_IPS_P1 = -1.0; //PM10
float last_value_IPS_P2 = -1.0; //PM2.5
float last_value_IPS_P01 = -1.0; //PM0.1
float last_value_IPS_P03 = -1.0; //PM0.3 //ATTENTION P4 = PM4 POUR SPS30
float last_value_IPS_P05 = -1.0; //PM0.5
float last_value_IPS_P5 = -1.0; //PM5
float last_value_IPS_N1 = -1.0;
float last_value_IPS_N10 = -1.0;
float last_value_IPS_N25 = -1.0;
float last_value_IPS_N01 = -1.0;
float last_value_IPS_N03 = -1.0; //ATTENTION P4 = PM4 POUR SPS30
float last_value_IPS_N05 = -1.0;
float last_value_IPS_N5 = -1.0;
float last_value_GPS_alt = -1000.0;
double last_value_GPS_lat = -200.0;
double last_value_GPS_lon = -200.0;
String last_value_GPS_timestamp;
String last_data_string;
int last_signal_strength;
int last_disconnect_reason;
String esp_chipid;
String esp_mac_id;
String last_value_SDS_version;
String last_value_NPM_version;
String last_value_IPS_version;
unsigned long SDS_error_count;
unsigned long NPM_error_count;
unsigned long IPS_error_count;
unsigned long WiFi_error_count;
unsigned long last_page_load = millis();
bool wificonfig_loop = false;
uint8_t sntp_time_set;
unsigned long count_sends = 0;
unsigned long last_display_millis = 0;
uint8_t next_display_count = 0;
struct struct_wifiInfo
{
char ssid[LEN_WLANSSID];
uint8_t encryptionType;
int32_t RSSI;
int32_t channel;
#if defined(ESP8266)
bool isHidden;
uint8_t unused[3];
#endif
};
struct struct_wifiInfo *wifiInfo;
uint8_t count_wifiInfo;
IPAddress addr_static_ip;
IPAddress addr_static_subnet;
IPAddress addr_static_gateway;
IPAddress addr_static_dns;
#define msSince(timestamp_before) (act_milli - (timestamp_before))
const char data_first_part[] PROGMEM = "{\"software_version\": \"" SOFTWARE_VERSION_STR "\", \"sensordatavalues\":[";
const char JSON_SENSOR_DATA_VALUES[] PROGMEM = "sensordatavalues";
/*****************************************************************
* display values *
*****************************************************************/
static void display_debug(const String &text1, const String &text2)
{
debug_outln_info(F("output debug text to displays..."));
if (oled_ssd1306)
{
oled_ssd1306->clear();
oled_ssd1306->displayOn();
oled_ssd1306->setTextAlignment(TEXT_ALIGN_LEFT);
oled_ssd1306->drawString(0, 12, text1);
oled_ssd1306->drawString(0, 24, text2);
oled_ssd1306->display();
}
if (oled_sh1106)
{
oled_sh1106->clear();
oled_sh1106->displayOn();
oled_sh1106->setTextAlignment(TEXT_ALIGN_LEFT);
oled_sh1106->drawString(0, 12, text1);
oled_sh1106->drawString(0, 24, text2);
oled_sh1106->display();
}
if (lcd_1602)
{
lcd_1602->clear();
lcd_1602->setCursor(0, 0);
lcd_1602->print(text1);
lcd_1602->setCursor(0, 1);
lcd_1602->print(text2);
}
if (lcd_2004)
{
lcd_2004->clear();
lcd_2004->setCursor(0, 0);
lcd_2004->print(text1);
lcd_2004->setCursor(0, 1);
lcd_2004->print(text2);
}
}
/*****************************************************************
* read SDS011 sensor serial and firmware date *
*****************************************************************/
static String SDS_version_date()
{
if (cfg::sds_read && !last_value_SDS_version.length())
{
debug_outln_verbose(FPSTR(DBG_TXT_START_READING), FPSTR(DBG_TXT_SDS011_VERSION_DATE));
is_SDS_running = SDS_cmd(PmSensorCmd::Start);
delay(250);
#if defined(ESP8266)
serialSDS.perform_work();
#endif
serialSDS.flush();
// Query Version/Date
SDS_rawcmd(0x07, 0x00, 0x00);
delay(400);
const constexpr uint8_t header_cmd_response[2] = {0xAA, 0xC5};
while (serialSDS.find(header_cmd_response, sizeof(header_cmd_response)))
{
uint8_t data[8];
unsigned r = serialSDS.readBytes(data, sizeof(data));
if (r == sizeof(data) && data[0] == 0x07 && SDS_checksum_valid(data))
{
char tmp[20];
snprintf_P(tmp, sizeof(tmp), PSTR("%02d-%02d-%02d(%02x%02x)"),
data[1], data[2], data[3], data[4], data[5]);
last_value_SDS_version = tmp;
break;
}
}
debug_outln_verbose(FPSTR(DBG_TXT_END_READING), FPSTR(DBG_TXT_SDS011_VERSION_DATE));
}
return last_value_SDS_version;
}
/*****************************************************************
* read Next PM sensor serial and firmware date *
*****************************************************************/
static uint8_t NPM_get_state()
{
uint8_t result;
NPM_waiting_for_4 = NPM_REPLY_HEADER_4;
debug_outln_info(F("State NPM..."));
NPM_cmd(PmSensorCmd2::State);
while (!serialNPM.available())
{
debug_outln("Wait for Serial...", DEBUG_MAX_INFO);
}
while (serialNPM.available() >= NPM_waiting_for_4)
{
const uint8_t constexpr header[2] = {0x81, 0x16};
uint8_t state[1];
uint8_t checksum[1];
uint8_t test[4];
switch (NPM_waiting_for_4)
{
case NPM_REPLY_HEADER_4:
if (serialNPM.find(header, sizeof(header)))
NPM_waiting_for_4 = NPM_REPLY_STATE_4;
break;
case NPM_REPLY_STATE_4:
serialNPM.readBytes(state, sizeof(state));
NPM_state(state[0]);
result = state[0];
NPM_waiting_for_4 = NPM_REPLY_CHECKSUM_4;
break;
case NPM_REPLY_CHECKSUM_4:
serialNPM.readBytes(checksum, sizeof(checksum));
memcpy(test, header, sizeof(header));
memcpy(&test[sizeof(header)], state, sizeof(state));
memcpy(&test[sizeof(header) + sizeof(state)], checksum, sizeof(checksum));
NPM_data_reader(test, 4);
NPM_waiting_for_4 = NPM_REPLY_HEADER_4;
if (NPM_checksum_valid_4(test))
{
debug_outln_info(F("Checksum OK..."));
}
break;
}
}
return result;
}
static bool NPM_start_stop()
{
bool result;
NPM_waiting_for_4 = NPM_REPLY_HEADER_4;
debug_outln_info(F("Switch start/stop NPM..."));
NPM_cmd(PmSensorCmd2::Change);
while (!serialNPM.available())
{
debug_outln("Wait for Serial...", DEBUG_MAX_INFO);
}
while (serialNPM.available() >= NPM_waiting_for_4)
{
const uint8_t constexpr header[2] = {0x81, 0x15};
uint8_t state[1];
uint8_t checksum[1];
uint8_t test[4];
switch (NPM_waiting_for_4)
{
case NPM_REPLY_HEADER_4:
if (serialNPM.find(header, sizeof(header)))
NPM_waiting_for_4 = NPM_REPLY_STATE_4;
break;
case NPM_REPLY_STATE_4:
serialNPM.readBytes(state, sizeof(state));
NPM_state(state[0]);
if (bitRead(state[0], 0) == 0)
{
debug_outln_info(F("NPM start..."));
result = true;
}
else if (bitRead(state[0], 0) == 1)
{
debug_outln_info(F("NPM stop..."));
result = false;
}
else
{
result = !is_NPM_running; //DANGER BECAUSE NON INITIALISED
}
NPM_waiting_for_4 = NPM_REPLY_CHECKSUM_4;
break;
case NPM_REPLY_CHECKSUM_4:
serialNPM.readBytes(checksum, sizeof(checksum));
memcpy(test, header, sizeof(header));
memcpy(&test[sizeof(header)], state, sizeof(state));
memcpy(&test[sizeof(header) + sizeof(state)], checksum, sizeof(checksum));
NPM_data_reader(test, 4);
NPM_waiting_for_4 = NPM_REPLY_HEADER_4;
if (NPM_checksum_valid_4(test))
{
debug_outln_info(F("Checksum OK..."));
}
break;
}
}
return result; //ATTENTION
}
static String NPM_version_date()
{
//debug_outln_verbose(FPSTR(DBG_TXT_START_READING), FPSTR(DBG_TXT_NPM_VERSION_DATE));
delay(250);
NPM_waiting_for_6 = NPM_REPLY_HEADER_6;
debug_outln_info(F("Version NPM..."));
NPM_cmd(PmSensorCmd2::Version);
while (!serialNPM.available())
{
debug_outln("Wait for Serial...", DEBUG_MAX_INFO);
}
while (serialNPM.available() >= NPM_waiting_for_6)
{
const uint8_t constexpr header[2] = {0x81, 0x17};
uint8_t state[1];
uint8_t data[2];
uint8_t checksum[1];
uint8_t test[6];
switch (NPM_waiting_for_6)
{
case NPM_REPLY_HEADER_6:
if (serialNPM.find(header, sizeof(header)))
NPM_waiting_for_6 = NPM_REPLY_STATE_6;
break;
case NPM_REPLY_STATE_6:
serialNPM.readBytes(state, sizeof(state));
NPM_state(state[0]);
NPM_waiting_for_6 = NPM_REPLY_DATA_6;
break;
case NPM_REPLY_DATA_6:
if (serialNPM.readBytes(data, sizeof(data)) == sizeof(data))
{
NPM_data_reader(data, 2);
uint16_t NPMversion = word(data[0], data[1]);
last_value_NPM_version = String(NPMversion);
//debug_outln_verbose(FPSTR(DBG_TXT_END_READING), FPSTR(DBG_TXT_NPM_VERSION_DATE));
debug_outln_info(F("Next PM Firmware: "), last_value_NPM_version);
}
NPM_waiting_for_6 = NPM_REPLY_CHECKSUM_6;
break;
case NPM_REPLY_CHECKSUM_6:
serialNPM.readBytes(checksum, sizeof(checksum));
memcpy(test, header, sizeof(header));
memcpy(&test[sizeof(header)], state, sizeof(state));
memcpy(&test[sizeof(header) + sizeof(state)], data, sizeof(data));
memcpy(&test[sizeof(header) + sizeof(state) + sizeof(data)], checksum, sizeof(checksum));
NPM_data_reader(test, 6);
NPM_waiting_for_6 = NPM_REPLY_HEADER_6;
if (NPM_checksum_valid_6(test))
{
debug_outln_info(F("Checksum OK..."));
}
break;
}
}
return last_value_NPM_version;
}
static void NPM_fan_speed()
{
NPM_waiting_for_5 = NPM_REPLY_HEADER_5;
debug_outln_info(F("Set fan speed to 50 %..."));
NPM_cmd(PmSensorCmd2::Speed);
while (!serialNPM.available())
{
debug_outln("Wait for Serial...", DEBUG_MAX_INFO);
}
while (serialNPM.available() >= NPM_waiting_for_5)
{
const uint8_t constexpr header[2] = {0x81, 0x21};
uint8_t state[1];
uint8_t data[1];
uint8_t checksum[1];
uint8_t test[5];
switch (NPM_waiting_for_5)
{
case NPM_REPLY_HEADER_5:
if (serialNPM.find(header, sizeof(header)))
NPM_waiting_for_5 = NPM_REPLY_STATE_5;
break;
case NPM_REPLY_STATE_5:
serialNPM.readBytes(state, sizeof(state));
NPM_state(state[0]);
NPM_waiting_for_5 = NPM_REPLY_DATA_5;
break;
case NPM_REPLY_DATA_5:
if (serialNPM.readBytes(data, sizeof(data)) == sizeof(data))
{
NPM_data_reader(data, 1);
}
NPM_waiting_for_5 = NPM_REPLY_CHECKSUM_5;
break;
case NPM_REPLY_CHECKSUM_5:
serialNPM.readBytes(checksum, sizeof(checksum));
memcpy(test, header, sizeof(header));
memcpy(&test[sizeof(header)], state, sizeof(state));
memcpy(&test[sizeof(header) + sizeof(state)], data, sizeof(data));
memcpy(&test[sizeof(header) + sizeof(state) + sizeof(data)], checksum, sizeof(checksum));
NPM_data_reader(test, 5);
NPM_waiting_for_5 = NPM_REPLY_HEADER_5;
if (NPM_checksum_valid_5(test))
{
debug_outln_info(F("Checksum OK..."));
}
break;
}
}
}
static String NPM_temp_humi()
{
uint16_t NPM_temp;
uint16_t NPM_humi;
NPM_waiting_for_8 = NPM_REPLY_HEADER_8;
debug_outln_info(F("Temperature/Humidity in Next PM..."));
NPM_cmd(PmSensorCmd2::Temphumi);
while (!serialNPM.available())
{
debug_outln("Wait for Serial...", DEBUG_MAX_INFO);
}
while (serialNPM.available() >= NPM_waiting_for_8)
{
const uint8_t constexpr header[2] = {0x81, 0x14};
uint8_t state[1];
uint8_t data[4];
uint8_t checksum[1];
uint8_t test[8];
switch (NPM_waiting_for_8)
{