forked from opendata-stuttgart/sensors-software
-
Notifications
You must be signed in to change notification settings - Fork 4
/
airrohr-firmware.ino
4249 lines (3864 loc) · 148 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
#include <Arduino.h>
#define INTL_PL
/************************************************************************
* *
* This source code needs to be compiled for the board *
* NodeMCU 1.0 (ESP-12E Module) *
* *
************************************************************************
* *
* airRohr firmware *
* Copyright (C) 2016-2018 Code for Stuttgart a.o. *
* *
* 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: *
* - SDS011 Pin 1 (TX) -> Pin D1 / GPIO5 *
* - SDS011 Pin 2 (RX) -> Pin D2 / GPIO4 *
* - SDS011 Pin 3 (GND) -> GND *
* - SDS011 Pin 4 (2.5m) -> unused *
* - SDS011 Pin 5 (5V) -> VU *
* - SDS011 Pin 6 (1m) -> unused *
* *
************************************************************************
* *
* Alternative *
* - nodemcu-LoLin board *
* - Shinyei PPD42NS *
* http://www.sca-shinyei.com/pdf/PPD42NS.pdf *
* *
* 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) *
* *
* - PPD42NS Pin 1 (grey or green) => GND *
* - PPD42NS Pin 2 (green or white)) => Pin D5 /GPIO14 *
* counts particles PM25 *
* - PPD42NS Pin 3 (black or yellow) => Vin *
* - PPD42NS Pin 4 (white or black) => Pin D6 / GPIO12 *
* counts particles PM10 *
* - PPD42NS Pin 5 (red) => unused *
* *
************************************************************************
* Extension: DHT22 (AM2303) *
* http://www.aosong.com/en/products/details.asp?id=117 *
* *
* DHT22 Wiring Instruction *
* (left to right, front is perforated side): *
* - DHT22 Pin 1 (VDD) -> Pin 3V3 (3.3V) *
* - DHT22 Pin 2 (DATA) -> Pin D7 (GPIO13) *
* - DHT22 Pin 3 (NULL) -> unused *
* - DHT22 Pin 4 (GND) -> Pin GND *
* *
************************************************************************
* Extensions connected via I2C: *
* HTU21D (https://www.sparkfun.com/products/13763), *
* BMP180, BMP280, BME280, OLED Display with SSD1306 (128x64 px) *
* *
* Wiring Instruction *
* (see labels on display or sensor board) *
* VCC -> Pin 3V3 *
* GND -> Pin GND *
* SCL -> Pin D4 (GPIO2) *
* SDA -> Pin D3 (GPIO0) *
* *
************************************************************************
* *
* Please check Readme.md for other sensors and hardware *
* *
************************************************************************
*
* 06.07.2018
* Der Sketch verwendet 459607 Bytes (44%) des Programmspeicherplatzes. Das Maximum sind 1044464 Bytes.
* Globale Variablen verwenden 48736 Bytes (59%) des dynamischen Speichers, 33184 Bytes für lokale Variablen verbleiben. Das Maximum sind 81920 Bytes.
*
* first version with esp8266 lib 2.4.2
* Der Sketch verwendet 491364 Bytes (47%) des Programmspeicherplatzes. Das Maximum sind 1044464 Bytes.
* Globale Variablen verwenden 37172 Bytes (45%) des dynamischen Speichers, 44748 Bytes für lokale Variablen verbleiben. Das Maximum sind 81920 Bytes.
*
* Der Sketch verwendet 489152 Bytes (46%) des Programmspeicherplatzes. Das Maximum sind 1044464 Bytes.
* Globale Variablen verwenden 37160 Bytes (45%) des dynamischen Speichers, 44760 Bytes für lokale Variablen verbleiben. Das Maximum sind 81920 Bytes.
*
************************************************************************/
// increment on change
#define SOFTWARE_VERSION "NAMF-2019-018I"
#define SPOOF_SOFTWARE_VERSION "NRZ-2018-123B"
/*****************************************************************
* Includes *
*****************************************************************/
#include <FS.h> // must be first
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266httpUpdate.h>
#include <SoftwareSerial.h>
#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>
#include <base64.h>
#include <ArduinoJson.h>
#include "./DHT.h"
#include <Adafruit_HTU21DF.h>
#include <Adafruit_BMP085.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_BME280.h>
#include "ClosedCube_SHT31D.h" // support for Nettigo Air Monitor HECA
#include <DallasTemperature.h>
#include <TinyGPS++.h>
#include <time.h>
#include <coredecls.h>
#include <assert.h>
#if defined(INTL_BG)
#include "intl_bg.h"
#elif defined(INTL_CZ)
#include "intl_cz.h"
#elif defined(INTL_EN)
#include "intl_en.h"
#elif defined(INTL_ES)
#include "intl_es.h"
#elif defined(INTL_FR)
#include "intl_fr.h"
#elif defined(INTL_IT)
#include "intl_it.h"
#elif defined(INTL_LU)
#include "intl_lu.h"
#elif defined(INTL_NL)
#include "intl_nl.h"
#elif defined(INTL_PL)
#include "intl_pl.h"
#elif defined(INTL_PT)
#include "intl_pt.h"
#elif defined(INTL_RU)
#include "intl_ru.h"
#elif defined(INTL_SE)
#include "intl_se.h"
#else
#include "intl_de.h"
#endif
#include "ext_def.h"
#include "html-content.h"
/******************************************************************
* Constants *
******************************************************************/
const unsigned long SAMPLETIME_MS = 30000;
const unsigned long SAMPLETIME_SDS_MS = 1000;
const unsigned long WARMUPTIME_SDS_MS = 15000;
const unsigned long READINGTIME_SDS_MS = 5000;
const unsigned long SAMPLETIME_GPS_MS = 50;
const unsigned long DISPLAY_UPDATE_INTERVAL_MS = 5000;
const unsigned long ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;
const unsigned long PAUSE_BETWEEN_UPDATE_ATTEMPTS_MS = ONE_DAY_IN_MS; // check for firmware updates once a day
const unsigned long DURATION_BEFORE_FORCED_RESTART_MS = ONE_DAY_IN_MS * 28; // force a reboot every ~4 weeks
/******************************************************************
* 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 {
char wlanssid[35] = WLANSSID;
char wlanpwd[65] = WLANPWD;
char current_lang[3] = "PL";
char www_username[65] = WWW_USERNAME;
char www_password[65] = WWW_PASSWORD;
bool www_basicauth_enabled = WWW_BASICAUTH_ENABLED;
char fs_ssid[33] = FS_SSID;
char fs_pwd[65] = FS_PWD;
char version_from_local_config[20] = "";
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 bmp_read = BMP_READ;
bool bmp280_read = BMP280_READ;
bool bme280_read = BME280_READ;
bool heca_read = HECA_READ;
bool ds18b20_read = DS18B20_READ;
bool gps_read = GPS_READ;
bool send2dusti = SEND2DUSTI;
bool send2madavi = SEND2MADAVI;
bool send2sensemap = SEND2SENSEMAP;
bool send2fsapp = SEND2FSAPP;
bool send2custom = SEND2CUSTOM;
bool send2lora = SEND2LORA;
bool send2influx = SEND2INFLUX;
bool send2csv = SEND2CSV;
bool auto_update = AUTO_UPDATE;
bool use_beta = USE_BETA;
bool has_display = HAS_DISPLAY;
bool has_sh1106 = HAS_SH1106;
bool has_lcd1602 = HAS_LCD1602;
bool has_lcd1602_27 = HAS_LCD1602_27;
bool has_lcd2004_27 = HAS_LCD2004_27;
bool has_lcd2004_3f = HAS_LCD2004_3F;
int debug = DEBUG;
bool ssl_madavi = SSL_MADAVI;
bool ssl_dusti = SSL_DUSTI;
char senseboxid[30] = SENSEBOXID;
int port_influx = PORT_INFLUX;
char user_influx[65] = USER_INFLUX;
char pwd_influx[65] = PWD_INFLUX;
char host_custom[100] = HOST_CUSTOM;
char url_custom[100] = URL_CUSTOM;
int port_custom = PORT_CUSTOM;
char user_custom[65] = USER_CUSTOM;
char pwd_custom[65] = PWD_CUSTOM;
char host_influx[100] = HOST_INFLUX;
char url_influx[100] = URL_INFLUX;
unsigned long time_for_wifi_config = 600000;
unsigned long sending_intervall_ms = 145000;
void initNonTrivials(const char* id) {
strcpy(cfg::current_lang, CURRENT_LANG);
if (fs_ssid[0] == '\0') {
strcpy(fs_ssid, "NAM-");
strcat(fs_ssid, id);
}
}
}
#define HOST_MADAVI "api-rrd.madavi.de"
#define URL_MADAVI "/data.php"
#define PORT_MADAVI 80
#define HOST_DUSTI "api.luftdaten.info"
#define URL_DUSTI "/v1/push-sensor-data/"
#define PORT_DUSTI 80
// IMPORTANT: NO MORE CHANGES TO VARIABLE NAMES NEEDED FOR EXTERNAL APIS
#define HOST_SENSEMAP "ingress.opensensemap.org"
#define URL_SENSEMAP "/boxes/BOXID/data?luftdaten=1"
#define PORT_SENSEMAP 443
#define HOST_FSAPP "www.h2801469.stratoserver.net"
#define URL_FSAPP "/data.php"
#define PORT_FSAPP 80
#define UPDATE_HOST "fw.air.nettigo.pl"
#define UPDATE_URL "/update/index.php"
#define UPDATE_PORT 80
#define JSON_BUFFER_SIZE 2000
enum class PmSensorCmd {
Start,
Stop,
ContinuousMode,
VersionDate
};
String basic_auth_influx = "";
String basic_auth_custom = "";
long int sample_count = 0;
bool bmp_init_failed = false;
bool bmp280_init_failed = false;
bool bme280_init_failed = false;
bool heca_init_failed = false;
ESP8266WebServer server(80);
int TimeZone = 1;
/*****************************************************************
* Display definitions *
*****************************************************************/
SSD1306 display(0x3c, I2C_PIN_SDA, I2C_PIN_SCL);
SH1106 display_sh1106(0x3c, I2C_PIN_SDA, I2C_PIN_SCL);
LiquidCrystal_I2C lcd_1602_27(0x27, 16, 2);
LiquidCrystal_I2C lcd_1602_3f(0x3F, 16, 2);
LiquidCrystal_I2C lcd_2004_27(0x27, 20, 4);
LiquidCrystal_I2C lcd_2004_3f(0x3F, 20, 4);
/*****************************************************************
* SDS011 declarations *
*****************************************************************/
SoftwareSerial serialSDS(PM_SERIAL_RX, PM_SERIAL_TX, false, 128);
SoftwareSerial serialGPS(GPS_SERIAL_RX, GPS_SERIAL_TX, false, 512);
/*****************************************************************
* DHT declaration *
*****************************************************************/
DHT dht(ONEWIRE_PIN, DHT_TYPE);
/*****************************************************************
* HTU21D declaration *
*****************************************************************/
Adafruit_HTU21DF htu21d;
/*****************************************************************
* BMP declaration *
*****************************************************************/
Adafruit_BMP085 bmp;
/*****************************************************************
* BMP280 declaration *
*****************************************************************/
Adafruit_BMP280 bmp280;
/*****************************************************************
* BME280 declaration *
*****************************************************************/
Adafruit_BME280 bme280;
/*****************************************************************
* HECA (SHT30) declaration *
*****************************************************************/
ClosedCube_SHT31D heca;
/*****************************************************************
* DS18B20 declaration *
*****************************************************************/
OneWire oneWire(ONEWIRE_PIN);
DallasTemperature ds18b20(&oneWire);
/*****************************************************************
* GPS declaration *
*****************************************************************/
TinyGPSPlus gps;
/*****************************************************************
* Variable Definitions for PPD24NS *
* P1 for PM10 & P2 for PM25 *
*****************************************************************/
unsigned long durationP1;
unsigned long durationP2;
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 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;
bool is_PMS_running = true;
bool is_HPM_running = true;
unsigned long sending_time = 0;
unsigned long last_update_attempt;
int sds_pm10_sum = 0;
int sds_pm25_sum = 0;
int sds_val_count = 0;
int sds_pm10_max = 0;
int sds_pm10_min = 20000;
int sds_pm25_max = 0;
int 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;
double last_value_PPD_P1 = -1.0;
double last_value_PPD_P2 = -1.0;
double last_value_SDS_P1 = -1.0;
double last_value_SDS_P2 = -1.0;
double last_value_PMS_P0 = -1.0;
double last_value_PMS_P1 = -1.0;
double last_value_PMS_P2 = -1.0;
double last_value_HPM_P1 = -1.0;
double last_value_HPM_P2 = -1.0;
double last_value_DHT_T = -128.0;
double last_value_DHT_H = -1.0;
double last_value_HTU21D_T = -128.0;
double last_value_HTU21D_H = -1.0;
double last_value_BMP_T = -128.0;
double last_value_BMP_P = -1.0;
double last_value_BMP280_T = -128.0;
double last_value_BMP280_P = -1.0;
double last_value_BME280_T = -128.0;
double last_value_BME280_H = -1.0;
double last_value_BME280_P = -1.0;
double last_value_HECA_T = -128.0;
double last_value_HECA_H = -1.0;
double last_value_DS18B20_T = -1.0;
double last_value_GPS_lat = -200.0;
double last_value_GPS_lon = -200.0;
double last_value_GPS_alt = -1000.0;
String last_value_GPS_date = "";
String last_value_GPS_time = "";
String last_data_string = "";
String esp_chipid;
long last_page_load = millis();
bool wificonfig_loop = false;
bool first_cycle = true;
bool sntp_time_is_set = false;
bool got_ntp = false;
unsigned long count_sends = 0;
unsigned long next_display_millis = 0;
unsigned long next_display_count = 0;
struct struct_wifiInfo {
char ssid[35];
uint8_t encryptionType;
int32_t RSSI;
int32_t channel;
bool isHidden;
};
struct struct_wifiInfo *wifiInfo;
uint8_t count_wifiInfo;
template<typename T, std::size_t N> constexpr std::size_t array_num_elements(const T(&)[N]) {
return N;
}
template<typename T, std::size_t N> constexpr std::size_t capacity_null_terminated_char_array(const T(&)[N]) {
return N - 1;
}
#define msSince(timestamp_before) (act_milli - (timestamp_before))
const char data_first_part[] PROGMEM = "{\"software_version\": \"{v}\", \"sensordatavalues\":[";
/*****************************************************************
* Debug output *
*****************************************************************/
void debug_out(const String& text, const int level, const bool linebreak) {
if (level <= cfg::debug) {
if (linebreak) {
Serial.println(text);
} else {
Serial.print(text);
}
}
}
/*****************************************************************
* display values *
*****************************************************************/
void display_debug(const String& text1, const String& text2) {
debug_out(F("output debug text to displays..."), DEBUG_MIN_INFO, 1);
debug_out(text1 + "\n" + text2, DEBUG_MAX_INFO, 1);
if (cfg::has_display) {
display.clear();
display.displayOn();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0, 12, text1);
display.drawString(0, 24, text2);
display.display();
}
if (cfg::has_sh1106) {
display_sh1106.clear();
display_sh1106.displayOn();
display_sh1106.setTextAlignment(TEXT_ALIGN_LEFT);
display_sh1106.drawString(0, 12, text1);
display_sh1106.drawString(0, 24, text2);
display_sh1106.display();
}
if (cfg::has_lcd1602) {
lcd_1602_3f.clear();
lcd_1602_3f.setCursor(0, 0);
lcd_1602_3f.print(text1);
lcd_1602_3f.setCursor(0, 1);
lcd_1602_3f.print(text2);
}
if (cfg::has_lcd1602_27) {
lcd_1602_27.clear();
lcd_1602_27.setCursor(0, 0);
lcd_1602_27.print(text1);
lcd_1602_27.setCursor(0, 1);
lcd_1602_27.print(text2);
}
if (cfg::has_lcd2004_27) {
lcd_2004_27.clear();
lcd_2004_27.setCursor(0, 0);
lcd_2004_27.print(text1);
lcd_2004_27.setCursor(0, 1);
lcd_2004_27.print(text2);
}
if (cfg::has_lcd2004_3f) {
lcd_2004_3f.clear();
lcd_2004_3f.setCursor(0, 0);
lcd_2004_3f.print(text1);
lcd_2004_3f.setCursor(0, 1);
lcd_2004_3f.print(text2);
}
}
/*****************************************************************
* check display values, return '-' if undefined *
*****************************************************************/
String check_display_value(double value, double undef, uint8_t len, uint8_t str_len) {
String s = (value != undef ? Float2String(value, len) : "-");
while (s.length() < str_len) {
s = " " + s;
}
return s;
}
/*****************************************************************
* convert float to string with a *
* precision of two (or a given number of) decimal places *
*****************************************************************/
String Float2String(const double value) {
return Float2String(value, 2);
}
String Float2String(const double value, uint8_t digits) {
// Convert a float to String with two decimals.
char temp[15];
dtostrf(value, 13, digits, temp);
String s = temp;
s.trim();
return s;
}
/*****************************************************************
* convert value to json string *
*****************************************************************/
String Value2Json(const String& type, const String& value) {
String s = F("{\"value_type\":\"{t}\",\"value\":\"{v}\"},");
s.replace("{t}", type);
s.replace("{v}", value);
return s;
}
/*****************************************************************
* convert string value to json string *
*****************************************************************/
String Var2Json(const String& name, const String& value) {
String s = F("\"{n}\":\"{v}\",");
String tmp = value;
tmp.replace("\\", "\\\\"); tmp.replace("\"", "\\\"");
s.replace("{n}", name);
s.replace("{v}", tmp);
return s;
}
/*****************************************************************
* convert boolean value to json string *
*****************************************************************/
String Var2Json(const String& name, const bool value) {
String s = F("\"{n}\":\"{v}\",");
s.replace("{n}", name);
s.replace("{v}", (value ? "true" : "false"));
return s;
}
/*****************************************************************
* convert boolean value to json string *
*****************************************************************/
String Var2Json(const String& name, const int value) {
String s = F("\"{n}\":\"{v}\",");
s.replace("{n}", name);
s.replace("{v}", String(value));
return s;
}
/*****************************************************************
* send SDS011 command (start, stop, continuous mode, version *
*****************************************************************/
static bool SDS_cmd(PmSensorCmd cmd) {
static constexpr uint8_t start_cmd[] PROGMEM = {
0xAA, 0xB4, 0x06, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x06, 0xAB
};
static constexpr uint8_t stop_cmd[] PROGMEM = {
0xAA, 0xB4, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x05, 0xAB
};
static constexpr uint8_t continuous_mode_cmd[] PROGMEM = {
0xAA, 0xB4, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x07, 0xAB
};
static constexpr uint8_t version_cmd[] PROGMEM = {
0xAA, 0xB4, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x05, 0xAB
};
constexpr uint8_t cmd_len = array_num_elements(start_cmd);
uint8_t buf[cmd_len];
switch (cmd) {
case PmSensorCmd::Start:
memcpy_P(buf, start_cmd, cmd_len);
break;
case PmSensorCmd::Stop:
memcpy_P(buf, stop_cmd, cmd_len);
break;
case PmSensorCmd::ContinuousMode:
memcpy_P(buf, continuous_mode_cmd, cmd_len);
break;
case PmSensorCmd::VersionDate:
memcpy_P(buf, version_cmd, cmd_len);
break;
}
serialSDS.write(buf, cmd_len);
return cmd != PmSensorCmd::Stop;
}
/*****************************************************************
* send Plantower PMS sensor command start, stop, cont. mode *
*****************************************************************/
static bool PMS_cmd(PmSensorCmd cmd) {
static constexpr uint8_t start_cmd[] PROGMEM = {
0x42, 0x4D, 0xE4, 0x00, 0x01, 0x01, 0x74
};
static constexpr uint8_t stop_cmd[] PROGMEM = {
0x42, 0x4D, 0xE4, 0x00, 0x00, 0x01, 0x73
};
static constexpr uint8_t continuous_mode_cmd[] PROGMEM = {
0x42, 0x4D, 0xE1, 0x00, 0x01, 0x01, 0x71
};
constexpr uint8_t cmd_len = array_num_elements(start_cmd);
uint8_t buf[cmd_len];
switch (cmd) {
case PmSensorCmd::Start:
memcpy_P(buf, start_cmd, cmd_len);
break;
case PmSensorCmd::Stop:
memcpy_P(buf, stop_cmd, cmd_len);
break;
case PmSensorCmd::ContinuousMode:
memcpy_P(buf, continuous_mode_cmd, cmd_len);
break;
case PmSensorCmd::VersionDate:
assert(false && "not supported by this sensor");
break;
}
serialSDS.write(buf, cmd_len);
return cmd != PmSensorCmd::Stop;
}
/*****************************************************************
* send Honeywell PMS sensor command start, stop, cont. mode *
*****************************************************************/
static bool HPM_cmd(PmSensorCmd cmd) {
static constexpr uint8_t start_cmd[] PROGMEM = {
0x68, 0x01, 0x01, 0x96
};
static constexpr uint8_t stop_cmd[] PROGMEM = {
0x68, 0x01, 0x02, 0x95
};
static constexpr uint8_t continuous_mode_cmd[] PROGMEM = {
0x68, 0x01, 0x40, 0x57
};
constexpr uint8_t cmd_len = array_num_elements(start_cmd);
uint8_t buf[cmd_len];
switch (cmd) {
case PmSensorCmd::Start:
memcpy_P(buf, start_cmd, cmd_len);
break;
case PmSensorCmd::Stop:
memcpy_P(buf, stop_cmd, cmd_len);
break;
case PmSensorCmd::ContinuousMode:
memcpy_P(buf, continuous_mode_cmd, cmd_len);
break;
case PmSensorCmd::VersionDate:
assert(false && "not supported by this sensor");
break;
}
serialSDS.write(buf, cmd_len);
return cmd != PmSensorCmd::Stop;
}
/*****************************************************************
* read SDS011 sensor serial and firmware date *
*****************************************************************/
String SDS_version_date() {
String s = "";
char buffer;
int value;
int len = 0;
String version_date = "";
String device_id = "";
int checksum_is = 0;
int checksum_ok = 0;
debug_out(String(FPSTR(DBG_TXT_END_READING)) + FPSTR(DBG_TXT_SDS011_VERSION_DATE), DEBUG_MED_INFO, 1);
is_SDS_running = SDS_cmd(PmSensorCmd::Start);
delay(100);
is_SDS_running = SDS_cmd(PmSensorCmd::VersionDate);
delay(500);
while (serialSDS.available() > 0) {
buffer = serialSDS.read();
debug_out(String(len) + " - " + String(buffer, DEC) + " - " + String(buffer, HEX) + " - " + int(buffer) + " .", DEBUG_MED_INFO, 1);
// "aa" = 170, "ab" = 171, "c0" = 192
value = int(buffer);
switch (len) {
case (0):
if (value != 170) {
len = -1;
};
break;
case (1):
if (value != 197) {
len = -1;
};
break;
case (2):
if (value != 7) {
len = -1;
};
checksum_is = 7;
break;
case (3):
version_date = String(value);
break;
case (4):
version_date += "-" + String(value);
break;
case (5):
version_date += "-" + String(value);
break;
case (6):
if (value < 0x10) {
device_id = "0" + String(value, HEX);
} else {
device_id = String(value, HEX);
};
break;
case (7):
if (value < 0x10) {
device_id += "0";
};
device_id += String(value, HEX);
break;
case (8):
debug_out(FPSTR(DBG_TXT_CHECKSUM_IS), DEBUG_MED_INFO, 0);
debug_out(String(checksum_is % 256), DEBUG_MED_INFO, 0);
debug_out(FPSTR(DBG_TXT_CHECKSUM_SHOULD), DEBUG_MED_INFO, 0);
debug_out(String(value), DEBUG_MED_INFO, 1);
if (value == (checksum_is % 256)) {
checksum_ok = 1;
} else {
len = -1;
};
break;
case (9):
if (value != 171) {
len = -1;
};
break;
}
if (len > 2) { checksum_is += value; }
len++;
if (len == 10 && checksum_ok == 1) {
s = version_date + "(" + device_id + ")";
debug_out(F("SDS version date : "), DEBUG_MIN_INFO, 0);
debug_out(version_date, DEBUG_MIN_INFO, 1);
debug_out(F("SDS device ID: "), DEBUG_MIN_INFO, 0);
debug_out(device_id, DEBUG_MIN_INFO, 1);
len = 0;
checksum_ok = 0;
version_date = "";
device_id = "";
checksum_is = 0;
}
yield();
}
debug_out(String(FPSTR(DBG_TXT_END_READING)) + FPSTR(DBG_TXT_SDS011_VERSION_DATE), DEBUG_MED_INFO, 1);
return s;
}
/*****************************************************************
* disable unneeded NMEA sentences, TinyGPS++ needs GGA, RMC *
*****************************************************************/
void disable_unneeded_nmea() {
serialGPS.println(F("$PUBX,40,GLL,0,0,0,0*5C")); // Geographic position, latitude / longitude
// serialGPS.println(F("$PUBX,40,GGA,0,0,0,0*5A")); // Global Positioning System Fix Data
serialGPS.println(F("$PUBX,40,GSA,0,0,0,0*4E")); // GPS DOP and active satellites
// serialGPS.println(F("$PUBX,40,RMC,0,0,0,0*47")); // Recommended minimum specific GPS/Transit data
serialGPS.println(F("$PUBX,40,GSV,0,0,0,0*59")); // GNSS satellites in view
serialGPS.println(F("$PUBX,40,VTG,0,0,0,0*5E")); // Track made good and ground speed
}
/*****************************************************************
* read config from spiffs *
*****************************************************************/
void readConfig() {
using namespace cfg;
String json_string = "";
debug_out(F("mounting FS..."), DEBUG_MIN_INFO, 1);
bool pms24_read = 0;
bool pms32_read = 0;
if (SPIFFS.begin()) {
debug_out(F("mounted file system..."), DEBUG_MIN_INFO, 1);
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
debug_out(F("reading config file..."), DEBUG_MIN_INFO, 1);
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
debug_out(F("opened config file..."), DEBUG_MIN_INFO, 1);
const 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);
StaticJsonBuffer<JSON_BUFFER_SIZE> jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(json_string);
debug_out(F("File content: "), DEBUG_MAX_INFO, 0);
debug_out(String(buf.get()), DEBUG_MAX_INFO, 1);
debug_out(F("JSON Buffer content: "), DEBUG_MAX_INFO, 0);
debug_out(json_string, DEBUG_MAX_INFO, 1);
if (json.success()) {
debug_out(F("parsed json..."), DEBUG_MIN_INFO, 1);
if (json.containsKey("SOFTWARE_VERSION")) {
strcpy(version_from_local_config, json["SOFTWARE_VERSION"]);
}
#define setFromJSON(key) if (json.containsKey(#key)) key = json[#key];
#define strcpyFromJSON(key) if (json.containsKey(#key)) strcpy(key, json[#key]);
strcpyFromJSON(current_lang);
strcpyFromJSON(wlanssid);
strcpyFromJSON(wlanpwd);
strcpyFromJSON(www_username);
strcpyFromJSON(www_password);
strcpyFromJSON(fs_ssid);
strcpyFromJSON(fs_pwd);
setFromJSON(www_basicauth_enabled);
setFromJSON(dht_read);
setFromJSON(htu21d_read);
setFromJSON(ppd_read);
setFromJSON(sds_read);
setFromJSON(pms_read);
setFromJSON(pms24_read);
setFromJSON(pms32_read);
setFromJSON(hpm_read);
setFromJSON(bmp_read);
setFromJSON(bmp280_read);
setFromJSON(bme280_read);
setFromJSON(heca_read);
setFromJSON(ds18b20_read);
setFromJSON(gps_read);
setFromJSON(send2dusti);
setFromJSON(ssl_dusti);
setFromJSON(send2madavi);
setFromJSON(ssl_madavi);
setFromJSON(send2sensemap);
setFromJSON(send2fsapp);
setFromJSON(send2lora);
setFromJSON(send2csv);
setFromJSON(auto_update);
setFromJSON(use_beta);
setFromJSON(has_display);
setFromJSON(has_sh1106);
setFromJSON(has_lcd1602);
setFromJSON(has_lcd1602_27);
setFromJSON(has_lcd2004_27);
setFromJSON(has_lcd2004_3f);
setFromJSON(debug);
setFromJSON(sending_intervall_ms);
setFromJSON(time_for_wifi_config);
strcpyFromJSON(senseboxid);
if (strcmp(senseboxid, "00112233445566778899aabb") == 0) {
strcpy(senseboxid, "");
send2sensemap = 0;
}
setFromJSON(send2custom);
strcpyFromJSON(host_custom);
strcpyFromJSON(url_custom);
setFromJSON(port_custom);
strcpyFromJSON(user_custom);
strcpyFromJSON(pwd_custom);
setFromJSON(send2influx);
strcpyFromJSON(host_influx);
strcpyFromJSON(url_influx);
setFromJSON(port_influx);
strcpyFromJSON(user_influx);
strcpyFromJSON(pwd_influx);
if (strcmp(host_influx, "api.luftdaten.info") == 0) {
strcpy(host_influx, "");
send2influx = 0;
}
configFile.close();
if (pms24_read || pms32_read) {
pms_read = 1;
writeConfig();
}
#undef setFromJSON
#undef strcpyFromJSON
} else {
debug_out(F("failed to load json config"), DEBUG_ERROR, 1);
}
}
} else {
debug_out(F("config file not found ..."), DEBUG_ERROR, 1);
}
} else {
debug_out(F("failed to mount FS"), DEBUG_ERROR, 1);
}
}
/*****************************************************************
* write config to spiffs *
*****************************************************************/
void writeConfig() {
using namespace cfg;
String json_string = "{";
debug_out(F("saving config..."), DEBUG_MIN_INFO, 1);
#define copyToJSON_Bool(varname) json_string += Var2Json(#varname,varname);
#define copyToJSON_Int(varname) json_string += Var2Json(#varname,varname);
#define copyToJSON_String(varname) json_string += Var2Json(#varname,String(varname));
copyToJSON_String(current_lang);
copyToJSON_String(SOFTWARE_VERSION);
copyToJSON_String(wlanssid);
copyToJSON_String(wlanpwd);
copyToJSON_String(www_username);
copyToJSON_String(www_password);
copyToJSON_String(fs_ssid);
copyToJSON_String(fs_pwd);
copyToJSON_Bool(www_basicauth_enabled);
copyToJSON_Bool(dht_read);
copyToJSON_Bool(htu21d_read);
copyToJSON_Bool(ppd_read);
copyToJSON_Bool(sds_read);
copyToJSON_Bool(pms_read);
copyToJSON_Bool(hpm_read);
copyToJSON_Bool(bmp_read);
copyToJSON_Bool(bmp280_read);
copyToJSON_Bool(bme280_read);
copyToJSON_Bool(heca_read);
copyToJSON_Bool(ds18b20_read);
copyToJSON_Bool(gps_read);
copyToJSON_Bool(send2dusti);