-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Smogomierz.ino
3356 lines (3060 loc) · 103 KB
/
Smogomierz.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
/*
ESP8266 core for Arduino - 3.0.2 // NodeMCU 1.0; Flash Size: 4MB (FS:1MB OTA:~1019KB)
Arduino core for the ESP32 - 1.0.6!! // 2.0.0 NOT SUPPORTED!
Adafruit Unified Sensor - 1.1.4
DallasTemperature - 3.8.0
*/
// ****** CHOOSE(uncomment) ONLY ONE!!! ******
#define INTL_OLD // old translataion system
// #define INTL_PL // polish translataion // EXPERIMENTAL!
// #define INTL_EN // english translataion // EXPERIMENTAL!
// *******************************************
// ****** CHOOSE(uncomment) ONLY ONE!!! ******
#define DUSTSENSOR_PMS5003_7003_BME280_0x76 // PMS5003 / PMS7003 - BME280_0x76
// #define DUSTSENSOR_PMS5003_7003_BME280_0x77 // PMS5003 / PM§S7003 - BME280_0x77
// #define DUSTSENSOR_SDS011_21 // Nova Fitness SDS011 / SDS021
// #define DUSTSENSOR_HPMA115S0 // Honeywell HPMA115S0
// #define DUSTSENSOR_SPS30 // Sensirion SPS30
// *******************************************
#define ASYNC_WEBSERVER_ON
#define DISABLE_SMOGLIST
/*
ESP8266
Podłączenie czujnikow dla ESP8266 NodeMCU:
BME280/BMP280: VIN - 3V; GND - G; SCL - D4; SDA - D3
SHT1x: VIN - 3V; GND - G; SCL - D5; DATA/SDA - D6 wymaga rezystora 10k podłaczonego do VCC
SHT21/HTU21D: VIN - 3V; GND - G; SCL - D5; SDA - D6
DHT22: VIN - 3V; GND - G; D7
DS18B20: VIN - 3V; GND - G; D5 - 4.7k Ohm resistor!
PMS5003/7003: VIN - VIN/5V; GND - G; Zielony/TX - D1; Niebieski/RX - D2
HPMA115S0: VIN - VIN/5V; GND - G; TX - D1; RX - D2
SDS011/21: VIN - 5V; GND - G; TX - D1; RX - D2
Sensirion SPS30: VIN - 5V; GND - G; TX - D1; RX - D2
Connection of sensors on ESP8266 NodeMCU:
BME280/BMP280: VIN - 3V; GND - G; SCL - D4; SDA - D3
SHT1x: VIN - 3V; GND - G; SCL - D5; DATA/SDA - D6 required pull-up resistor 10k to VCC
SHT21/HTU21D: VIN - 3V; GND - G; SCL - D5; SDA - D6
DHT22: VIN - 3V; GND - G; D7
DS18B20: VIN - 3V; GND - G; D5 - 4.7k Ohm resistor!
PMS5003/7003: VIN - VIN/5V; GND - G; Green/TX - D1; Blue/RX - D2
HPMA115S0: VIN - VIN/5V; GND - G; TX - D1; RX - D2
SDS011/21: VIN - 5V; GND - G; TX - D1; RX - D2
Sensirion SPS30: VIN - 5V; GND - G; TX - D1; RX - D2
ESP32
Podłączenie czujnikow dla ESP32:
BME280/BMP280: VIN - 3V; GND - G; SCL - D17; SDA - D16
SHT1x: VIN - 3V; GND - G; SCL - D5; DATA/SDA - D6 wymaga rezystora 10k podłaczonego do VCC
SHT21/HTU21D: VIN - 3V; GND - G; SCL - D5; SDA - D6
DHT22: VIN - 3V; GND - G; D7
DS18B20: VIN - 3V; GND - G; D5 - 4.7k Ohm resistor!
PMS5003/7003: VIN - VIN/5V; GND - G; TX - D5; RX - D4
HPMA115S0: VIN - VIN/5V; GND - G; TX - D1; RX - D2
SDS011/21: VIN - 5V; GND - G; TX - D5; RX - D4
Sensirion SPS30: VIN - 5V; GND - G; TX - D5; RX - D4
Connection of sensors on ESP32:
BME280/BMP280: VIN - 3V; GND - G; SCL - D17; SDA - D16
SHT1x: VIN - 3V; GND - G; SCL - D5; DATA/SDA - D6 required pull-up resistor 10k to VCC
SHT21/HTU21D: VIN - 3V; GND - G; SCL - D5; SDA - D6
DHT22: VIN - 3V; GND - G; D7
DS18B20: VIN - 3V; GND - G; D5 - 4.7k Ohm resistor!
PMS5003/7003: VIN - VIN/5V; GND - G; TX - D5; RX - D4
HPMA115S0: VIN - VIN/5V; GND - G; TX - D1; RX - D2
SDS011/21: VIN - 5V; GND - G; TX - D5; RX - D4
Sensirion SPS30: VIN - 5V; GND - G; TX - D5; RX - D4
*/
/*
ESP8266 PMS7003/BME280_0x76 - NodeMCU 1.0 - 1M SPIFFS --- FS:1MB OTA: ~1019KB
Szkic używa 580548 bajtów (55%) pamięci programu. Maksimum to 1044464 bajtów.
Zmienne globalne używają 45888 bajtów (56%) pamięci dynamicznej, pozostawiając 36032 bajtów dla zmiennych lokalnych. Maksimum to 81920 bajtów.
ASYNC_WEBSERVER_ON + INTL_OLD
Szkic używa 694529 bajtów (66%) pamięci programu. Maksimum to 1044464 bajtów.
Zmienne globalne używają 46688 bajtów (56%) pamięci dynamicznej, pozostawiając 35232 bajtów dla zmiennych lokalnych. Maksimum to 81920 bajtów.
Szkic używa 695897 bajtów (66%) pamięci programu. Maksimum to 1044464 bajtów.
Zmienne globalne używają 46800 bajtów (57%) pamięci dynamicznej, pozostawiając 35120 bajtów dla zmiennych lokalnych. Maksimum to 81920 bajtów.
Szkic używa 691393 bajtów (66%) pamięci programu. Maksimum to 1044464 bajtów.
Zmienne globalne używają 46876 bajtów (57%) pamięci dynamicznej, pozostawiając 35044 bajtów dla zmiennych lokalnych. Maksimum to 81920 bajtów.
ASYNC_WEBSERVER_ON + INTL_PL
Szkic używa 662541 bajtów (63%) pamięci programu. Maksimum to 1044464 bajtów.
Zmienne globalne używają 40528 bajtów (49%) pamięci dynamicznej, pozostawiając 41392 bajtów dla zmiennych lokalnych. Maksimum to 81920 bajtów.
================================================================================================================================================
ESP32 Dev Module PMS7003/BME280_0x76 - 1.9MB APP with OTA - 190KB SPIFFS
Szkic używa 1375786 bajtów (69%) pamięci programu. Maksimum to 1966080 bajtów.
Zmienne globalne używają 58664 bajtów (17%) pamięci dynamicznej, pozostawiając 269016 bajtów dla zmiennych lokalnych. Maksimum to 327680 bajtów.
Szkic używa 1377866 bajtów (70%) pamięci programu. Maksimum to 1966080 bajtów.
Zmienne globalne używają 58656 bajtów (17%) pamięci dynamicznej, pozostawiając 269024 bajtów dla zmiennych lokalnych. Maksimum to 327680 bajtów.
*** init homekit support:
Szkic używa 1543130 bajtów (78%) pamięci programu. Maksimum to 1966080 bajtów.
Zmienne globalne używają 60808 bajtów (18%) pamięci dynamicznej, pozostawiając 266872 bajtów dla zmiennych lokalnych. Maksimum to 327680 bajtów.
ASYNC_WEBSERVER_ON
Szkic używa 1477490 bajtów (75%) pamięci programu. Maksimum to 1966080 bajtów.
Zmienne globalne używają 58976 bajtów (17%) pamięci dynamicznej, pozostawiając 268704 bajtów dla zmiennych lokalnych. Maksimum to 327680 bajtów.
Szkic używa 1477750 bajtów (75%) pamięci programu. Maksimum to 1966080 bajtów.
Zmienne globalne używają 59024 bajtów (18%) pamięci dynamicznej, pozostawiając 268656 bajtów dla zmiennych lokalnych. Maksimum to 327680 bajtów.
*/
#include <Arduino.h>
#include "FS.h"
#include <ArduinoJson.h> // 6.9.0 or later
#ifdef ASYNC_WEBSERVER_ON
#include "src/WiFiManager/ESPAsyncWiFiManager.h" // https://github.com/alanswx/ESPAsyncWiFiManager // 5.11.2021
#else
#include "src/WiFiManager/WiFiManager.h" // https://github.com/tzapu/WiFiManager/tree/development // 5.11.2021 DEV
#endif
#ifdef ARDUINO_ARCH_ESP8266
#ifndef DUSTSENSOR_PMS5003_7003_BME280_0x77
#include "src/libs/esp8266/bme280_0x76.h" // https://github.com/zen/BME280_light // CUSTOMIZED! 5.11.2021
#else
#include "src/libs/esp8266/bme280_0x77.h" // https://github.com/zen/BME280_light // CUSTOMIZED! 5.11.2021
#endif
#elif defined ARDUINO_ARCH_ESP32
#include "src/libs/esp32/Adafruit_BME280.h" // https://github.com/Takatsuki0204/BME280-I2C-ESP32 // 5.11.2021
#endif
#include "src/libs/HTU2xD_SHT2x_Si70xx.h" // https://github.com/enjoyneering/HTU2xD_SHT2x_Si70xx // 5.11.2021
#include "src/libs/Adafruit_BMP280.h" // https://github.com/adafruit/Adafruit_BMP280_Library // 5.11.2021
#include "src/libs/SHT1x.h" // https://github.com/practicalarduino/SHT1x // 5.11.2021
#include "src/libs/DHT.h" // https://github.com/adafruit/DHT-sensor-library // 5.11.2021
#ifdef DUSTSENSOR_PMS5003_7003_BME280_0x76 or DUSTSENSOR_PMS5003_7003_BME280_0x77
#include "src/libs/pms.h" // https://github.com/fu-hsi/PMS // 5.11.2021
#elif defined DUSTSENSOR_SDS011_21
#ifdef ARDUINO_ARCH_ESP8266
#include "src/libs/esp8266/SdsDustSensor.h" // SDS011/SDS021 - https://github.com/lewapek/sds-dust-sensors-arduino-library // 5.11.2021
#elif defined ARDUINO_ARCH_ESP32
// #include "src/libs/esp8266/SdsDustSensor.h" // SDS011/SDS021 - https://github.com/lewapek/sds-dust-sensors-arduino-library // 5.11.2021
#include "src/libs/esp32/SDS011.h" // https://github.com/ricki-z/SDS011 // 5.11.2021
#endif
#elif defined DUSTSENSOR_HPMA115S0
#include "src/libs/hpma115S0.h" // https://github.com/hpsaturn/HPMA115S0 // 5.11.2021
#elif defined DUSTSENSOR_SPS30
#include "src/libs/sps30.h" // https://github.com/paulvha/sps30 // CUSTOMIZED! 5.11.2021
#else // If no dust sensor has been defined - use DUSTSENSOR_PMS5003_7003_BME280_0x76
#include "src/libs/pms.h" // https://github.com/fu-hsi/PMS // 5.11.2021
#endif
#include "src/smogly_spiffs.h"
#include "config.h"
#include "defaultConfig.h"
#include "src/autoupdate.h"
#ifndef DISABLE_SMOGLIST
#include "src/services/smoglist.h"
#endif
#include "src/services/luftdaten.h"
#ifdef ARDUINO_ARCH_ESP32
#include "src/services/airmonitor.h"
#endif
#include "src/services/thing_speak.h"
#include "src/services/aqieco.h"
#include "src/services/InfluxDbV2.h" // https://github.com/davidgs/ESP8266_Influx_DB_V2 // CUSTOMIZED! 5.11.2021
// it would be nice to update the InfluxDB library to
// #include "src/services/InfluxDB/InfluxDbClient.h" // https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino
#ifdef ARDUINO_ARCH_ESP8266 // ESP8266 core for Arduino - 2.6.3 or later
#ifdef ASYNC_WEBSERVER_ON
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#else
#include <ESP8266WebServer.h>
#endif
#include <ESP8266mDNS.h>
#ifndef ASYNC_WEBSERVER_ON
#include <ESP8266HTTPUpdateServer.h>
#endif
#include <SoftwareSerial.h>
#elif defined ARDUINO_ARCH_ESP32 // Arduino core for the ESP32 - 1.0.4-rc1 or later // at 1.0.3 autoupdate doesn't work !!!
#include <WiFi.h>
#include <WiFiClient.h>
#include <ESPmDNS.h>
#ifdef ASYNC_WEBSERVER_ON
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#else
#include <WebServer.h>
#endif
#include <Update.h>
#include <HTTPClient.h>
#include <HTTPUpdate.h>
#include <time.h>
#include <HardwareSerial.h>
#endif
#include <PubSubClient.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// TEMP/HUMI/PRESS Sensor config - START
// BME280 config
#ifdef ARDUINO_ARCH_ESP8266 // VIN - 3V; GND - G; SCL - D4; SDA - D3
#define ASCII_ESC 27
char bufout[10];
BME280<> BMESensor;
BME280<> BMESensor_2;
#elif defined ARDUINO_ARCH_ESP32 // VIN - 3V; GND - G; SCL - D17; SDA - D16
//#define I2C_SDA = FIRST_THP_SDA
//#define I2C_SCL = FIRST_THP_SCL
Adafruit_BME280 bme((uint8_t)FIRST_THP_SDA, (uint8_t)FIRST_THP_SCL); // I2C -- ONLY THE DEFAULT VALUES WORK
#endif
// BMP280 config
Adafruit_BMP280 bmp; //I2C
// Serial for SHT21/HTU21D config
// HTU21D myHTU21D(HTU21D_RES_RH12_TEMP14);
HTU2xD_SHT2x_SI70xx ht2x(HTU2xD_SENSOR, HUMD_12BIT_TEMP_14BIT); //sensor type, resolution
// DHT22 config
//#define DHTPIN 13 // D7 on NodeMCU/WeMos board
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(FIRST_THP_SDA, DHTTYPE);
// SHT1x – Config
//#define dataPin 14 //D5
//#define clockPin 12 //D6
SHT1x sht1x(FIRST_THP_SDA, FIRST_THP_SCL);
// DS18B20 – Config
//const int DS18B20_WireBus = 14; // D5
//OneWire oneWire(FIRST_THP_SDA);
OneWire oneWire(14);
DallasTemperature DS18B20(&oneWire);
// TEMP/HUMI/PRESS Sensor config - END
// DUST Sensor config - START
#ifdef DUSTSENSOR_PMS5003_7003_BME280_0x76 or DUSTSENSOR_PMS5003_7003_BME280_0x77
//***PMSx003 - START***
#ifdef ARDUINO_ARCH_ESP8266
#ifdef ARDUINO_ESP8266_RELEASE_2_6_0
SoftwareSerial PMS_Serial; // only for esp8266 core 2.6.0
#else
SoftwareSerial PMS_Serial(DUST_TX, DUST_RX); // Change TX - D1 and RX - D2 pins -- esp8266 core 2.6.1 or later
#endif
PMS pms(PMS_Serial);
PMS::DATA data;
#elif defined ARDUINO_ARCH_ESP32
HardwareSerial PMS_Serial(1); // Change TX - D5 and RX - D4 pins
PMS pms(PMS_Serial);
PMS::DATA data;
#endif
//***PMSx003 - END***
#elif defined DUSTSENSOR_SDS011_21
//***SDS0x1 - START***
#ifdef ARDUINO_ARCH_ESP8266
// SDS011/21 config
SdsDustSensor sds(DUST_TX, DUST_RX); // Change TX - D1 and RX - D2 pins
#elif defined ARDUINO_ARCH_ESP32
// SDS011/21 config
HardwareSerial sds_port(2); // Change TX - D5 and RX - D4 pins
SDS011 my_sds;
int err;
float SDSpm25, SDSpm10;
#endif
//***SDS0x1 - END***
#elif defined DUSTSENSOR_HPMA115S0
//***HPMA115S0 - START***
#ifdef ARDUINO_ARCH_ESP8266
SoftwareSerial hpmaSerial(DUST_TX, DUST_RX); // TX/RX – D1/D2
HPMA115S0 hpma115S0(hpmaSerial);
#elif defined ARDUINO_ARCH_ESP32
HardwareSerial hpmaSerial(1); // Change TX - D5 and RX - D4 pins
HPMA115S0 hpma115S0(hpmaSerial);
#endif
unsigned int hpma115S0_pm25, hpma115S0_pm10;
//***HPMA115S0 - END***
#elif defined DUSTSENSOR_SPS30
//***SPS30 - START***
#ifdef ARDUINO_ARCH_ESP8266
#define SP30_COMMS I2C_COMMS
#elif defined ARDUINO_ARCH_ESP32
#define SP30_COMMS SERIALPORT1
#endif
#define SPS30_AUTOCLEANINTERVAL -1
#define SPS30_PERFORMCLEANNOW 1
#define SPS30_DEBUG 0
// function prototypes (sometimes the pre-processor does not create prototypes themself on ESPxx)
void ErrtoMess(char *mess, uint8_t r);
void Errorloop(char *mess, uint8_t r);
void GetDeviceInfo();
bool read_sps30_data();
// create constructor
SPS30 sps30;
float SPS30_PM1, SPS30_PM25, SPS30_PM4, SPS30_PM10;
//***SPS30 - END***
#else // If no dust sensor has been defined - use DUSTSENSOR_PMS5003_7003_BME280_0x76
//***PMSx003 - START***
#ifdef ARDUINO_ARCH_ESP8266
#ifdef ARDUINO_ESP8266_RELEASE_2_6_0
SoftwareSerial PMS_Serial; // only for esp8266 core 2.6.0
#else
SoftwareSerial PMS_Serial(DUST_TX, DUST_RX); // Change TX - D1 and RX - D2 pins -- esp8266 core 2.6.1 or later
#endif
PMS pms(PMS_Serial);
PMS::DATA data;
#elif defined ARDUINO_ARCH_ESP32
HardwareSerial PMS_Serial(1); // Change TX - D5 and RX - D4 pins
PMS pms(PMS_Serial);
PMS::DATA data;
#endif
//***PMSx003 - END***
#endif
// DUST Sensor config - END
static char device_name[20];
static unsigned int DUST_interval = 60 * 1000; // 1 minute
unsigned int previous_DUST_Millis = 0;
static unsigned int SENDING_FREQUENCY_interval = 60 * 1000; // 1 minute
unsigned int previous_SENDING_FREQUENCY_Millis = 0;
static unsigned int SENDING_FREQUENCY_AIRMONITOR_interval = 60 * 1000; // 1 minute
unsigned int previous_SENDING_FREQUENCY_AIRMONITOR_Millis = 0;
static unsigned int SENDING_DB_FREQUENCY_interval = 60 * 1000; // 1 minute
unsigned int previous_SENDING_DB_FREQUENCY_Millis = 0;
static unsigned short TwoSec_interval = 2 * 1000; // 2 second
unsigned int previous_2sec_Millis = 0;
static unsigned int REBOOT_interval = 24 * 60 * 60 * 1000; // 24 hours
unsigned int previous_REBOOT_Millis = 0;
// unsigned long time_now_temp = 0;
#ifdef DUSTSENSOR_SPS30
static unsigned short pmMeasurements[10][4];
#else
static unsigned short pmMeasurements[10][3];
#endif
static unsigned char iPM = 0;
static unsigned short averagePM1, averagePM25, averagePM4, averagePM10 = 0;
static float currentTemperature, currentHumidity, currentPressure = 0;
// float currentTemperature_THP1, currentHumidity_THP1, currentPressure_THP1 = 0;
// float currentTemperature_THP2, currentHumidity_THP2, currentPressure_THP2 = 0;
static float calib = 1;
static bool need_update = false;
char SERVERSOFTWAREVERSION[32] = "";
char CURRENTSOFTWAREVERSION[32] = "";
#ifdef ASYNC_WEBSERVER_ON
AsyncWebServer server(80);
#else
#ifdef ARDUINO_ARCH_ESP8266
ESP8266WebServer WebServer(80);
ESP8266HTTPUpdateServer httpUpdater;
#elif defined ARDUINO_ARCH_ESP32
WebServer WebServer(80);
#endif
#endif
WiFiClient espClient;
PubSubClient mqttclient(espClient);
// https://github.com/Yurik72/ESPHap
// HomeKit -- START
#ifdef ARDUINO_ARCH_ESP32
#include <SPIFFS.h>
#endif
#ifdef ARDUINO_ARCH_ESP8266
#include "coredecls.h"
#endif
#ifdef ARDUINO_ARCH_ESP32
extern "C" {
#include "homeintegration.h" // https://github.com/Yurik72/ESPHap
}
#endif
#ifdef ARDUINO_ARCH_ESP8266
// #include "homekitintegrationcpp.h" // https://github.com/Yurik72/ESPHap/issues/14 << !!!!
#endif
#ifdef ARDUINO_ARCH_ESP32
#include <hapfilestorage/hapfilestorage.hpp>
homekit_service_t* hapservice = {0};
String pair_file_name = "/homekit_pair.dat";
homekit_service_t* homekit_temperature = NULL;
homekit_service_t* homekit_humidity = NULL;
homekit_service_t* homekit_pm10_level = NULL;
homekit_service_t* homekit_pm25_level = NULL;
homekit_characteristic_t* pm10_level_characteristic = NULL;
homekit_characteristic_t* pm25_level_characteristic = NULL;
struct device_data_t {
float homekit_temperature = 22.0;
float homekit_humidity = 50.0;
float homekit_pm10_level = 10.0;
float homekit_pm25_level = 20.0;
};
device_data_t homekit_DeviceData;
#endif
// HomeKit -- END
#ifndef ASYNC_WEBSERVER_ON
WiFiManager wifiManager;
#endif
// check TEMP/HUMI/PRESS Sensor - START
bool checkHTU21DStatus() {
int temperature_HTU21D_Int = int(ht2x.readTemperature());
int humidity_HTU21D_Int = int(ht2x.readHumidity());
if ((temperature_HTU21D_Int == 0 && humidity_HTU21D_Int == 0) || (temperature_HTU21D_Int == 255 && humidity_HTU21D_Int == 255)) {
if (DEBUG) {
#ifdef ARDUINO_ARCH_ESP8266
Serial.println(F("No data from HTU21D sensor!\n"));
#elif defined ARDUINO_ARCH_ESP32
Serial.println(("No data from HTU21D sensor!\n"));
#endif
}
return false;
} else {
return true;
}
}
bool checkBmeStatus() {
#ifdef ARDUINO_ARCH_ESP8266
int temperature_BME280_Int = BMESensor.temperature;
int pressure_BME280_Int = (BMESensor.seaLevelForAltitude(MYALTITUDE));
int humidity_BME280_Int = BMESensor.humidity;
#elif defined ARDUINO_ARCH_ESP32
int temperature_BME280_Int = bme.readTemperature();
int pressure_BME280_Int = (bme.seaLevelForAltitude(MYALTITUDE, (bme.readPressure() / 100.0F)));
int humidity_BME280_Int = bme.readHumidity();
#endif
if (temperature_BME280_Int == 0 && pressure_BME280_Int == 0 && humidity_BME280_Int == 0) {
if (DEBUG) {
#ifdef ARDUINO_ARCH_ESP8266
Serial.println(F("No data from BME280 sensor!\n"));
#elif defined ARDUINO_ARCH_ESP32
Serial.println(("No data from BME280 sensor!\n"));
#endif
}
return false;
} else {
return true;
}
}
bool checkBmpStatus() {
int temperature_BMP_Int = bmp.readTemperature();
int pressure_BMP_Int = bmp.readPressure();
if (temperature_BMP_Int == 0 && pressure_BMP_Int == 0) {
if (DEBUG) {
#ifdef ARDUINO_ARCH_ESP8266
Serial.println(F("No data from BMP280 sensor!\n"));
#elif defined ARDUINO_ARCH_ESP32
Serial.println(("No data from BMP280 sensor!\n"));
#endif
}
return false;
} else {
return true;
}
}
bool checkDHT22Status() {
int humidity_DHT_Int = dht.readHumidity();
int temperature_DHT_Int = dht.readTemperature();
if (humidity_DHT_Int == 0 && temperature_DHT_Int == 0) {
if (DEBUG) {
#ifdef ARDUINO_ARCH_ESP8266
Serial.println(F("No data from DHT22 sensor!\n"));
#elif defined ARDUINO_ARCH_ESP32
Serial.println(("No data from DHT22 sensor!\n"));
#endif
}
return false;
} else if (isnan(humidity_DHT_Int) && isnan(temperature_DHT_Int)) {
if (DEBUG) {
#ifdef ARDUINO_ARCH_ESP8266
Serial.println(F("No data from DHT22 sensor!\n"));
#elif defined ARDUINO_ARCH_ESP32
Serial.println(("No data from DHT22 sensor!\n"));
#endif
}
return false;
} else {
return true;
}
}
bool checkSHT1xStatus() {
int humidity_SHT1x_Int = sht1x.readHumidity();
int temperature_SHT1x_Int = sht1x.readTemperatureC();
if (humidity_SHT1x_Int == 0 && temperature_SHT1x_Int == 0) {
if (DEBUG) {
#ifdef ARDUINO_ARCH_ESP8266
Serial.println(F("No data from SHT1x sensor!\n"));
#elif defined ARDUINO_ARCH_ESP32
Serial.println(("No data from SHT1x sensor!\n"));
#endif
}
return false;
} else {
return true;
}
}
bool checkDS18B20Status() {
/*
DS18B20.requestTemperatures();
int temperature_DS18B20_Int = DS18B20.getTempCByIndex(0);
if (temperature_DS18B20_Int == -127) {
if (DEBUG) {
#ifdef ARDUINO_ARCH_ESP8266
Serial.println(F("No data from DS18B20 sensor!\n"));
#elif defined ARDUINO_ARCH_ESP32
Serial.println(("No data from DS18B20 sensor!\n"));
#endif
}
return false;
} else {
return true;
}
*/
return true;
}
// check TEMP/HUMI/PRESS Sensor - END
void minutesToSeconds() {
DUST_interval = 1000; // 1 second
SENDING_FREQUENCY_interval = 1000;
SENDING_DB_FREQUENCY_interval = 1000;
}
void MQTTreconnect() {
// Loop until we're reconnected
#ifdef ARDUINO_ARCH_ESP8266
if (!mqttclient.connected()) {
Serial.print(F("Attempting MQTT connection..."));
// Attempt to connect
//if (mqttclient.connect("ESP8266Client", MQTT_USER, MQTT_PASSWORD)) {
if (mqttclient.connect(device_name, MQTT_USER, MQTT_PASSWORD)) {
Serial.println(F("connected"));
} else {
Serial.print(F("failed, rc="));
Serial.print(mqttclient.state());
Serial.println(F("\n"));
}
}
#elif defined ARDUINO_ARCH_ESP32
if (!mqttclient.connected()) {
Serial.print(("Attempting MQTT connection..."));
// Attempt to connect
//if (mqttclient.connect("ESP8266Client", MQTT_USER, MQTT_PASSWORD)) {
if (mqttclient.connect(device_name, MQTT_USER, MQTT_PASSWORD)) {
Serial.println(("connected"));
} else {
Serial.print(("failed, rc="));
Serial.print(mqttclient.state());
Serial.println(("\n"));
}
}
#endif
}
void set_I2C_PINS(String THP_PIN, int i) {
#ifdef ARDUINO_ARCH_ESP8266
if (i == 1) {
if (THP_PIN == "D1") {
FIRST_THP_SDA = 5;
} else if (THP_PIN == "D2") {
FIRST_THP_SDA = 4;
} else if (THP_PIN == "D3") {
FIRST_THP_SDA = 0;
} else if (THP_PIN == "D4") {
FIRST_THP_SDA = 2;
} else if (THP_PIN == "D5") {
FIRST_THP_SDA = 14;
} else if (THP_PIN == "D6") {
FIRST_THP_SDA = 12;
} else if (THP_PIN == "D7") {
FIRST_THP_SDA = 13;
} else if (THP_PIN == "D8") {
FIRST_THP_SDA = 15;
} else if (THP_PIN == "D16") {
FIRST_THP_SDA = 16;
} else if (THP_PIN == "D17") {
FIRST_THP_SDA = 17;
}
} else if (i == 2) {
if (THP_PIN == "D1") {
FIRST_THP_SCL = 5;
} else if (THP_PIN == "D2") {
FIRST_THP_SCL = 4;
} else if (THP_PIN == "D3") {
FIRST_THP_SCL = 0;
} else if (THP_PIN == "D4") {
FIRST_THP_SCL = 2;
} else if (THP_PIN == "D5") {
FIRST_THP_SCL = 14;
} else if (THP_PIN == "D6") {
FIRST_THP_SCL = 12;
} else if (THP_PIN == "D7") {
FIRST_THP_SCL = 13;
} else if (THP_PIN == "D8") {
FIRST_THP_SCL = 15;
} else if (THP_PIN == "D16") {
FIRST_THP_SCL = 16;
} else if (THP_PIN == "D17") {
FIRST_THP_SCL = 17;
}
} else if (i == 3) {
if (THP_PIN == "D1") {
SECOND_THP_SDA = 5;
} else if (THP_PIN == "D2") {
SECOND_THP_SDA = 4;
} else if (THP_PIN == "D3") {
SECOND_THP_SDA = 0;
} else if (THP_PIN == "D4") {
SECOND_THP_SDA = 2;
} else if (THP_PIN == "D5") {
SECOND_THP_SDA = 14;
} else if (THP_PIN == "D6") {
SECOND_THP_SDA = 12;
} else if (THP_PIN == "D7") {
SECOND_THP_SDA = 13;
} else if (THP_PIN == "D8") {
SECOND_THP_SDA = 15;
} else if (THP_PIN == "D16") {
SECOND_THP_SDA = 16;
} else if (THP_PIN == "D17") {
SECOND_THP_SDA = 17;
}
} else if (i == 4) {
if (THP_PIN == "D1") {
SECOND_THP_SCL = 5;
} else if (THP_PIN == "D2") {
SECOND_THP_SCL = 4;
} else if (THP_PIN == "D3") {
SECOND_THP_SCL = 0;
} else if (THP_PIN == "D4") {
SECOND_THP_SCL = 2;
} else if (THP_PIN == "D5") {
SECOND_THP_SCL = 14;
} else if (THP_PIN == "D6") {
SECOND_THP_SCL = 12;
} else if (THP_PIN == "D7") {
SECOND_THP_SCL = 13;
} else if (THP_PIN == "D8") {
SECOND_THP_SCL = 15;
} else if (THP_PIN == "D16") {
SECOND_THP_SCL = 16;
} else if (THP_PIN == "D17") {
SECOND_THP_SCL = 17;
}
}
#elif defined ARDUINO_ARCH_ESP32
if (i == 1) {
if (THP_PIN == "D1") {
FIRST_THP_SDA = 8;
} else if (THP_PIN == "D2") {
FIRST_THP_SDA = 9;
} else if (THP_PIN == "D4") {
FIRST_THP_SDA = 4;
} else if (THP_PIN == "D5") {
FIRST_THP_SDA = 5;
} else if (THP_PIN == "D15") {
FIRST_THP_SDA = 15;
} else if (THP_PIN == "D16") {
FIRST_THP_SDA = 16;
} else if (THP_PIN == "D17") {
FIRST_THP_SDA = 17;
} else if (THP_PIN == "D18") {
FIRST_THP_SDA = 18;
} else if (THP_PIN == "D19") {
FIRST_THP_SDA = 19;
} else if (THP_PIN == "D21") {
FIRST_THP_SDA = 21;
} else if (THP_PIN == "D22") {
FIRST_THP_SDA = 22;
} else if (THP_PIN == "D23") {
FIRST_THP_SDA = 23;
}
} else if (i == 2) {
if (THP_PIN == "D1") {
FIRST_THP_SCL = 8;
} else if (THP_PIN == "D2") {
FIRST_THP_SCL = 9;
} else if (THP_PIN == "D4") {
FIRST_THP_SCL = 4;
} else if (THP_PIN == "D5") {
FIRST_THP_SCL = 5;
} else if (THP_PIN == "D15") {
FIRST_THP_SCL = 15;
} else if (THP_PIN == "D16") {
FIRST_THP_SCL = 16;
} else if (THP_PIN == "D17") {
FIRST_THP_SCL = 17;
} else if (THP_PIN == "D18") {
FIRST_THP_SCL = 18;
} else if (THP_PIN == "D19") {
FIRST_THP_SCL = 19;
} else if (THP_PIN == "D21") {
FIRST_THP_SCL = 21;
} else if (THP_PIN == "D22") {
FIRST_THP_SCL = 22;
} else if (THP_PIN == "D23") {
FIRST_THP_SCL = 23;
}
} else if (i == 3) {
if (THP_PIN == "D1") {
SECOND_THP_SDA = 8;
} else if (THP_PIN == "D2") {
SECOND_THP_SDA = 9;
} else if (THP_PIN == "D4") {
SECOND_THP_SDA = 4;
} else if (THP_PIN == "D5") {
SECOND_THP_SDA = 5;
} else if (THP_PIN == "D15") {
SECOND_THP_SDA = 15;
} else if (THP_PIN == "D16") {
SECOND_THP_SDA = 16;
} else if (THP_PIN == "D17") {
SECOND_THP_SDA = 17;
} else if (THP_PIN == "D18") {
SECOND_THP_SDA = 18;
} else if (THP_PIN == "D19") {
SECOND_THP_SDA = 19;
} else if (THP_PIN == "D21") {
SECOND_THP_SDA = 21;
} else if (THP_PIN == "D22") {
SECOND_THP_SDA = 22;
} else if (THP_PIN == "D23") {
SECOND_THP_SDA = 23;
}
} else if (i == 4) {
if (THP_PIN == "D1") {
SECOND_THP_SCL = 8;
} else if (THP_PIN == "D2") {
SECOND_THP_SCL = 9;
} else if (THP_PIN == "D4") {
SECOND_THP_SCL = 4;
} else if (THP_PIN == "D5") {
SECOND_THP_SCL = 5;
} else if (THP_PIN == "D15") {
SECOND_THP_SCL = 15;
} else if (THP_PIN == "D16") {
SECOND_THP_SCL = 16;
} else if (THP_PIN == "D17") {
SECOND_THP_SCL = 17;
} else if (THP_PIN == "D18") {
SECOND_THP_SCL = 18;
} else if (THP_PIN == "D19") {
SECOND_THP_SCL = 19;
} else if (THP_PIN == "D21") {
SECOND_THP_SCL = 21;
} else if (THP_PIN == "D22") {
SECOND_THP_SCL = 22;
} else if (THP_PIN == "D23") {
SECOND_THP_SCL = 23;
}
}
#endif
}
void set_SERIAL_PINS(String DUST_PIN, int i) {
#ifdef ARDUINO_ARCH_ESP8266
if (i == 1) {
if (DUST_PIN == "D1") {
DUST_TX = 5;
} else if (DUST_PIN == "D2") {
DUST_TX = 4;
} else if (DUST_PIN == "D3") {
DUST_TX = 0;
} else if (DUST_PIN == "D4") {
DUST_TX = 2;
} else if (DUST_PIN == "D5") {
DUST_TX = 14;
} else if (DUST_PIN == "D6") {
DUST_TX = 12;
} else if (DUST_PIN == "D7") {
DUST_TX = 13;
} else if (DUST_PIN == "D8") {
DUST_TX = 15;
} else if (DUST_PIN == "D16") {
DUST_TX = 16;
} else if (DUST_PIN == "D17") {
DUST_TX = 17;
}
} else if (i == 2) {
if (DUST_PIN == "D1") {
DUST_RX = 5;
} else if (DUST_PIN == "D2") {
DUST_RX = 4;
} else if (DUST_PIN == "D3") {
DUST_RX = 0;
} else if (DUST_PIN == "D4") {
DUST_RX = 2;
} else if (DUST_PIN == "D5") {
DUST_RX = 14;
} else if (DUST_PIN == "D6") {
DUST_RX = 12;
} else if (DUST_PIN == "D7") {
DUST_RX = 13;
} else if (DUST_PIN == "D8") {
DUST_RX = 15;
} else if (DUST_PIN == "D16") {
DUST_RX = 16;
} else if (DUST_PIN == "D17") {
DUST_RX = 17;
}
}
#elif defined ARDUINO_ARCH_ESP32
if (i == 1) {
if (DUST_PIN == "D1") {
DUST_TX = 8;
} else if (DUST_PIN == "D2") {
DUST_TX = 9;
} else if (DUST_PIN == "D4") {
DUST_TX = 4;
} else if (DUST_PIN == "D5") {
DUST_TX = 5;
} else if (DUST_PIN == "D15") {
DUST_TX = 15;
} else if (DUST_PIN == "D16") {
DUST_TX = 16;
} else if (DUST_PIN == "D17") {
DUST_TX = 17;
} else if (DUST_PIN == "D18") {
DUST_TX = 18;
} else if (DUST_PIN == "D19") {
DUST_TX = 19;
} else if (DUST_PIN == "D21") {
DUST_TX = 21;
} else if (DUST_PIN == "D22") {
DUST_TX = 22;
} else if (DUST_PIN == "D23") {
DUST_TX = 23;
}
} else if (i == 2) {
if (DUST_PIN == "D1") {
DUST_RX = 8;
} else if (DUST_PIN == "D2") {
DUST_RX = 9;
} else if (DUST_PIN == "D4") {
DUST_RX = 4;
} else if (DUST_PIN == "D5") {
DUST_RX = 5;
} else if (DUST_PIN == "D15") {
DUST_RX = 15;
} else if (DUST_PIN == "D16") {
DUST_RX = 16;
} else if (DUST_PIN == "D17") {
DUST_RX = 17;
} else if (DUST_PIN == "D18") {
DUST_RX = 18;
} else if (DUST_PIN == "D19") {
DUST_RX = 19;
} else if (DUST_PIN == "D21") {
DUST_RX = 21;
} else if (DUST_PIN == "D22") {
DUST_RX = 22;
} else if (DUST_PIN == "D23") {
DUST_RX = 23;
}
}
#endif
}
// default translation - english
#ifdef INTL_OLD
#include "intl/default_intl.h"
#include "src/translator.h"
#endif
#ifdef INTL_EN
#include "intl/new_default_intl.h"
#endif
#ifdef INTL_PL
#include "intl/new_intl_pl.h"
#endif
// all HTML content
#include "html/html-content.h"
#ifdef ASYNC_WEBSERVER_ON
#include "html/html-root.h"
#include "html/html-config.h"
#include "html/html-config-device.h"
#include "html/html-config-services.h"
#include "html/html-config-adv-mqtt.h"
#include "html/html-update.h"
#endif
// library doesnt support arguments :/
#ifdef ASYNC_WEBSERVER_ON
#include "src/smogly_asyncwebserver.h"
#else
#include "src/smogly_webserver.h"
#endif
void setup() {
Serial.begin(115200);
yield();
fs_setup();
yield();
// deleteConfig();
// yield();
//temporary solution!
if (SECOND_THP) {
strcpy(SECOND_THP_MODEL, THP_MODEL);
}
#ifdef ARDUINO_ARCH_ESP32
disableCore0WDT();
// disableCore1WDT(); // ESP32-solo-1 so only CORE0!
#endif
#ifdef INTL_OLD
loadtranslation(SELECTED_LANGUAGE);
#endif
yield();
set_I2C_PINS(CONFIG_FIRST_THP_SDA, 1);
set_I2C_PINS(CONFIG_FIRST_THP_SCL, 2);
set_I2C_PINS(CONFIG_SECOND_THP_SDA, 3);
set_I2C_PINS(CONFIG_SECOND_THP_SCL, 4);
set_SERIAL_PINS(CONFIG_DUST_TX, 1);
set_SERIAL_PINS(CONFIG_DUST_RX, 2);
// DUST SENSOR setup - START
#ifdef DUSTSENSOR_PMS5003_7003_BME280_0x76 or DUSTSENSOR_PMS5003_7003_BME280_0x77
if (!strcmp(DUST_MODEL, "PMS7003")) {
#ifdef ARDUINO_ARCH_ESP8266
#ifdef ARDUINO_ESP8266_RELEASE_2_6_0
PMS_Serial.begin(9600, DUST_TX, DUST_RX); // Change TX - D1 and RX - D2 pins -- only for esp8266 core 2.6.0
#else
PMS_Serial.begin(9600); //PMSx003 serial -- esp8266 core 2.6.1 or later
#endif
#elif defined ARDUINO_ARCH_ESP32
PMS_Serial.begin(9600, SERIAL_8N1, DUST_TX, DUST_RX); //PMSx003 serial
#endif
if (FREQUENTMEASUREMENT == true) {
pms.wakeUp();
#ifdef ASYNC_WEBSERVER_ON
yield();
delay(500);
#else
delay(500);
#endif
pms.activeMode();
} else {
pms.passiveMode();