-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoduleair.cpp
More file actions
6125 lines (5366 loc) · 170 KB
/
moduleair.cpp
File metadata and controls
6125 lines (5366 loc) · 170 KB
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 <WString.h>
#include <pgmspace.h>
#define SOFTWARE_VERSION_STR "ModuleAirV2-V1-042022"
#define SOFTWARE_VERSION_STR_SHORT "V1-042022"
String SOFTWARE_VERSION(SOFTWARE_VERSION_STR);
String SOFTWARE_VERSION_SHORT(SOFTWARE_VERSION_STR_SHORT);
#include <Arduino.h>
#include "PxMatrix.h"
#include <arduino_lmic.h>
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
/*****************************************************************
* IMPORTANT *
*****************************************************************/
//On force l'utilisation des 2 SPI
//Dans SPI.cpp
// #if CONFIG_IDF_TARGET_ESP32
// SPIClass SPI(VSPI);
// SPIClass SPI_H(HSPI);
// #else
// SPIClass SPI(FSPI);
// #endif
// Dans SPI.h
// extern SPIClass SPI_H; en bas
//Dans PXMatrix
//On remplace tous les SPI. par SPI_H.
//On définit les pins:
// // HW SPI PINS
// #define SPI_BUS_CLK 14
// #define SPI_BUS_MOSI 13
// #define SPI_BUS_MISO 12
// #define SPI_BUS_SS 4
//on remplace la glcdfont.c original dans AdaFruitGFX => mod dans le dossier Fonts
/*****************************************************************
* IMPORTANT FIN *
*****************************************************************/
#include <MHZ16_uart.h> // CO2
#include <MHZ19.h>
// includes ESP32 libraries
#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 <esp32/sha.h> //pour https ? remplacer par #include <esp32/sha.h> ? #include "sha/sha_parallel_engine.h" ?
#include <WebServer.h>
#include <ESPmDNS.h>
// includes external libraries
#include "./Fonts/oledfont.h" // avoids including the default Arial font, needs to be included before SSD1306.h
#include "./Fonts/Font4x7Fixed.h" // modified Pour l'affichage des unités
#include "./Fonts/Font4x5Fixed.h" //pour l'affichage des infos de debug
#include <SSD1306Wire.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 <StreamString.h>
#include "./bmx280_i2c.h"
#include "./SensirionI2CSgp40.h"
#include "./configuration.h"
// includes files
#include "./intl.h"
#include "./utils.h"
#include "defines.h"
#include "ext_def.h"
#include "html-content.h"
/*****************************************************************
* CONFIGURATION *
*****************************************************************/
namespace cfg
{
unsigned debug = DEBUG;
unsigned time_for_wifi_config = 600000;
unsigned sending_intervall_ms = 145000;
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];
// credentials of the sensor in access point mode
char fs_ssid[LEN_FS_SSID] = FS_SSID;
char fs_pwd[LEN_CFG_PASSWORD] = FS_PWD;
// main config
bool has_wifi = HAS_WIFI;
bool has_lora = HAS_LORA;
char appeui[LEN_APPEUI];
char deveui[LEN_DEVEUI];
char appkey[LEN_APPKEY];
// (in)active sensors
bool sds_read = SDS_READ;
bool npm_read = NPM_READ;
bool npm_fulltime = NPM_FULLTIME;
bool bmx280_read = BMX280_READ;
bool mhz16_read = MHZ16_READ;
bool mhz19_read = MHZ19_READ;
bool sgp40_read = SGP40_READ;
// Location
char latitude[LEN_GEOCOORDINATES];
char longitude[LEN_GEOCOORDINATES];
char height_above_sealevel[8] = "0";
// send to "APIs"
bool send2dusti = SEND2SENSORCOMMUNITY;
bool send2madavi = SEND2MADAVI;
bool send2custom = SEND2CUSTOM;
bool send2custom2 = SEND2CUSTOM2;
bool send2csv = SEND2CSV;
// (in)active displays
bool has_ssd1306 = HAS_SSD1306;
bool has_matrix = HAS_MATRIX;
bool display_measure = DISPLAY_MEASURE;
bool display_forecast = DISPLAY_FORECAST;
bool display_wifi_info = DISPLAY_WIFI_INFO;
bool display_lora_info = DISPLAY_LORA_INFO;
bool display_device_info = DISPLAY_DEVICE_INFO;
// API settings
bool ssl_madavi = SSL_MADAVI;
bool ssl_dusti = SSL_SENSORCOMMUNITY;
// API AirCarto
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;
// API AtmoSud
char host_custom2[LEN_HOST_CUSTOM2];
char url_custom2[LEN_URL_CUSTOM2];
bool ssl_custom2 = SSL_CUSTOM2;
unsigned port_custom2 = PORT_CUSTOM2;
char user_custom2[LEN_USER_CUSTOM2] = USER_CUSTOM2;
char pwd_custom2[LEN_CFG_PASSWORD] = PWD_CUSTOM2;
// First load
void initNonTrivials(const char *id)
{
strcpy(cfg::current_lang, CURRENT_LANG);
strcpy_P(appeui, APPEUI);
strcpy_P(deveui, DEVEUI);
strcpy_P(appkey, APPKEY);
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_custom2, HOST_CUSTOM2);
strcpy_P(url_custom2, URL_CUSTOM2);
strcpy_P(latitude, LATITUDE);
strcpy_P(longitude, LONGITUDE);
if (!*fs_ssid)
{
strcpy(fs_ssid, SSID_BASENAME);
strcat(fs_ssid, id);
}
}
}
//configuration summary for LoRaWAN
bool configlorawan[8] = {false, false, false, false, false, false, false, false};
// configlorawan[0] = cfg::sds_read;
// configlorawan[1] = cfg::npm_read ;
// configlorawan[2] = cfg::bmx280_read;
// configlorawan[3] = cfg::mhz16_read;
// configlorawan[4] = cfg::mhz19_read;
// configlorawan[5] = cfg::sgp40_read;
// configlorawan[6] = cfg::display_forecast;
// configlorawan[7] = cfg::has_wifi;
static byte booltobyte(bool array[8])
{
byte result = 0;
for (int i = 0; i < 8; i++)
{
if (array[i])
{
result |= (byte)(1 << (7 - i));
}
}
return result;
}
// define size of the config JSON
#define JSON_BUFFER_SIZE 2300
// define size of the AtmoSud Forecast API JSON
#define JSON_BUFFER_SIZE2 200
LoggerConfig loggerConfigs[LoggerCount];
// test variables
long int sample_count = 0;
bool bmx280_init_failed = false;
bool sgp40_init_failed = false;
bool moduleair_selftest_failed = false;
WebServer server(80);
// include JSON config reader
#include "./moduleair-cfg.h"
/*****************************************************************
* Display definitions *
*****************************************************************/
SSD1306Wire *oled_ssd1306 = nullptr; // as pointer
//For the matrix
hw_timer_t *timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
#define matrix_width 64
#define matrix_height 32
uint8_t display_draw_time = 30; //10-50 is usually fine
PxMATRIX display(64, 32, P_LAT, P_OE, P_A, P_B, P_C, P_D, P_E);
uint8_t logos[6] = {0,0,0,0,0,0};
uint8_t logo_index = -1;
bool has_logo;
extern const uint8_t gamma8[]; //for gamma correction
struct RGB
{
byte R;
byte G;
byte B;
};
struct RGB displayColor
{
0, 0, 0
};
uint16_t myRED = display.color565(255, 0, 0);
uint16_t myGREEN = display.color565(0, 255, 0);
uint16_t myBLUE = display.color565(0, 0, 255);
uint16_t myWHITE = display.color565(255, 255, 255);
uint16_t myYELLOW = display.color565(255, 255, 0);
uint16_t myCYAN = display.color565(0, 255, 255);
uint16_t myMAGENTA = display.color565(255, 0, 255);
uint16_t myBLACK = display.color565(0, 0, 0);
uint16_t myCUSTOM = display.color565(displayColor.R, displayColor.G, displayColor.B);
uint16_t myCOLORS[8] = {myRED, myGREEN, myBLUE, myWHITE, myYELLOW, myCYAN, myMAGENTA, myBLACK};
void IRAM_ATTR display_updater()
{
// Increment the counter and set the time of ISR
portENTER_CRITICAL_ISR(&timerMux);
display.display(display_draw_time);
portEXIT_CRITICAL_ISR(&timerMux);
}
void display_update_enable(bool is_enable)
{
if (is_enable)
{
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &display_updater, true);
timerAlarmWrite(timer, 4000, true);
timerAlarmEnable(timer);
}
else
{
timerDetachInterrupt(timer);
timerAlarmDisable(timer);
}
}
void drawImage(int x, int y, int h, int w, uint16_t image[])
{
int imageHeight = h;
int imageWidth = w;
int counter = 0;
for (int yy = 0; yy < imageHeight; yy++)
{
for (int xx = 0; xx < imageWidth; xx++)
{
display.drawPixel(xx + x, yy + y, image[counter]);
counter++;
}
}
}
bool gamma_correction = true; //Gamma correction
struct RGB interpolate(float valueSensor, int step1, int step2, int step3, int step4, int step5, bool correction)
{
byte endColorValueR;
byte startColorValueR;
byte endColorValueG;
byte startColorValueG;
byte endColorValueB;
byte startColorValueB;
int valueLimitHigh;
int valueLimitLow;
struct RGB result;
uint16_t rgb565;
if (valueSensor == 0)
{
result.R = 80;
result.G = 240; //blue
result.B = 230;
}
else if (valueSensor > 0 && valueSensor <= step5)
{
if (valueSensor <= step1)
{
valueLimitHigh = step1;
valueLimitLow = 0;
endColorValueR = 80;
startColorValueR = 80; //blue to green
endColorValueG = 204;
startColorValueG = 240;
endColorValueB = 170;
startColorValueB = 230;
}
else if (valueSensor > step1 && valueSensor <= step2)
{
valueLimitHigh = step2;
valueLimitLow = step1;
endColorValueR = 237;
startColorValueR = 80;
endColorValueG = 230; //green to yellow
startColorValueG = 204;
endColorValueB = 97;
startColorValueB = 170;
}
else if (valueSensor > step2 && valueSensor <= step3)
{
valueLimitHigh = step3;
valueLimitLow = step2;
endColorValueR = 237;
startColorValueR = 237;
endColorValueG = 94; //yellow to orange
startColorValueG = 230;
endColorValueB = 88;
startColorValueB = 97;
}
else if (valueSensor > step3 && valueSensor <= step4)
{
valueLimitHigh = step4;
valueLimitLow = step3;
endColorValueR = 136;
startColorValueR = 237;
endColorValueG = 26; // orange to red
startColorValueG = 94;
endColorValueB = 51;
startColorValueB = 88;
}
else if (valueSensor > step4 && valueSensor <= step5)
{
valueLimitHigh = step5;
valueLimitLow = step4;
endColorValueR = 115;
startColorValueR = 136;
endColorValueG = 40; // red to violet
startColorValueG = 26;
endColorValueB = 125;
startColorValueB = 51;
}
result.R = (byte)(((endColorValueR - startColorValueR) * ((valueSensor - valueLimitLow) / (valueLimitHigh - valueLimitLow))) + startColorValueR);
result.G = (byte)(((endColorValueG - startColorValueG) * ((valueSensor - valueLimitLow) / (valueLimitHigh - valueLimitLow))) + startColorValueG);
result.B = (byte)(((endColorValueB - startColorValueB) * ((valueSensor - valueLimitLow) / (valueLimitHigh - valueLimitLow))) + startColorValueB);
}
else if (valueSensor > step5)
{
result.R = 115;
result.G = 40; //violet
result.B = 125;
}
else
{
result.R = 0;
result.G = 0;
result.B = 0;
}
// Debug.println(result.R);
// Debug.println(result.G);
// Debug.println(result.B);
// Debug.println("Value in");
// Debug.println(valueSensor);
// Debug.println("Color in low RGB:");
// Debug.print(startColorValueR);
// Debug.print(" ");
// Debug.print(startColorValueG);
// Debug.print(" ");
// Debug.print(startColorValueB);
// Debug.printf("\n");
// Debug.println("Color in high RGB:");
// Debug.print(endColorValueR);
// Debug.print(" ");
// Debug.print(endColorValueG);
// Debug.print(" ");
// Debug.print(endColorValueB);
// Debug.printf("\n");
// Debug.println("Color out RGB:");
// Debug.print(result.R);
// Debug.print(" ");
// Debug.print(result.G);
// Debug.print(" ");
// Debug.print(result.B);
// Debug.printf("\n");
//Gamma Correction
if (correction == true){
result.R = pgm_read_byte(&gamma8[result.R]);
result.G = pgm_read_byte(&gamma8[result.G]);
result.B = pgm_read_byte(&gamma8[result.B]);
}
rgb565 = ((result.R & 0b11111000) << 8) | ((result.G & 0b11111100) << 3) | (result.B >> 3);
Debug.println(rgb565); // to get list of color if drawGradient is acitvated
return result;
}
struct RGB interpolate2(float valueSensor, int step1, int step2, int step3, bool correction)
{
byte endColorValueR;
byte startColorValueR;
byte endColorValueG;
byte startColorValueG;
byte endColorValueB;
byte startColorValueB;
int valueLimitHigh;
int valueLimitLow;
struct RGB result;
uint16_t rgb565;
if (valueSensor == 0)
{
result.R = 0;
result.G = 0; //blue
result.B = 255;
}
else if (valueSensor > 0 && valueSensor <= step3)
{
if (valueSensor <= step1)
{
valueLimitHigh = step1;
valueLimitLow = 0;
endColorValueR = 0;
startColorValueR = 0; //blue to green
endColorValueG = 255;
startColorValueG = 0;
endColorValueB = 0;
startColorValueB = 255;
}
else if (valueSensor > step1 && valueSensor <= step2)
{
valueLimitHigh = step2;
valueLimitLow = step1;
endColorValueR = 255;
startColorValueR = 0;
endColorValueG = 255; //green to yellow
startColorValueG = 255;
endColorValueB = 0;
startColorValueB = 0;
}
else if (valueSensor > step2 && valueSensor <= step3)
{
valueLimitHigh = step3;
valueLimitLow = step2;
endColorValueR = 255;
startColorValueR = 255;
endColorValueG = 0; //yellow to red
startColorValueG = 255;
endColorValueB = 0;
startColorValueB = 0;
}
result.R = (byte)(((endColorValueR - startColorValueR) * ((valueSensor - valueLimitLow) / (valueLimitHigh - valueLimitLow))) + startColorValueR);
result.G = (byte)(((endColorValueG - startColorValueG) * ((valueSensor - valueLimitLow) / (valueLimitHigh - valueLimitLow))) + startColorValueG);
result.B = (byte)(((endColorValueB - startColorValueB) * ((valueSensor - valueLimitLow) / (valueLimitHigh - valueLimitLow))) + startColorValueB);
}
else if (valueSensor > step3)
{
result.R = 255;
result.G = 0; //red
result.B = 0;
}
else
{
result.R = 0;
result.G = 0;
result.B = 0;
}
// Debug.println(result.R);
// Debug.println(result.G);
// Debug.println(result.B);
// Debug.println("Value in");
// Debug.println(valueSensor);
// Debug.println("Color in low RGB:");
// Debug.print(startColorValueR);
// Debug.print(" ");
// Debug.print(startColorValueG);
// Debug.print(" ");
// Debug.print(startColorValueB);
// Debug.printf("\n");
// Debug.println("Color in high RGB:");
// Debug.print(endColorValueR);
// Debug.print(" ");
// Debug.print(endColorValueG);
// Debug.print(" ");
// Debug.print(endColorValueB);
// Debug.printf("\n");
// Debug.println("Color out RGB:");
// Debug.print(result.R);
// Debug.print(" ");
// Debug.print(result.G);
// Debug.print(" ");
// Debug.print(result.B);
// Debug.printf("\n");
//Gamma Correction
if (correction == true){
result.R = pgm_read_byte(&gamma8[result.R]);
result.G = pgm_read_byte(&gamma8[result.G]);
result.B = pgm_read_byte(&gamma8[result.B]);
}
rgb565 = ((result.R & 0b11111000) << 8) | ((result.G & 0b11111100) << 3) | (result.B >> 3);
Debug.println(rgb565); // to get list of color if drawGradient is acitvated
return result;
}
//You can use drawGradient once in order to get the list of colors and then create an image which is much faster to display
void drawgradient(int x, int y, float valueSensor, int step1, int step2, int step3, int step4, int step5)
{
int gradientHeight = 7;
int gradientWidth = 64;
int pixelvalue[64];
RGB pixelcolors[64];
Debug.println("Pixel values");
for (uint8_t i = 0; i < gradientWidth; i++){
pixelvalue[i]= (int)((i*step5)/(gradientWidth-1));
Debug.print(" ");
Debug.print(pixelvalue[i]);
if (i == 63)
{
Debug.printf("\n");
}
}
for (uint8_t j = 0; j < gradientWidth; j++){
int value = pixelvalue[j];
pixelcolors[j] = interpolate(value, step1, step2, step3, step4, step5, true);
}
for (uint8_t k = 0; k < gradientHeight; k++){
for (int l = 0; l < gradientWidth; l++){
uint16_t myPIXEL = display.color565(pixelcolors[l].R, pixelcolors[l].G, pixelcolors[l].B);
display.drawPixel(x + l, y + k, myPIXEL);
}
}
}
//REVOIR POUR LE TRAITEMENT DES CARACTERES SPECIAUX
void messager1(float valueSensor, int step1, int step2, int step3, int step4, int step5)
{
//MESSAGES FIXES => CENTRER à la main
display.setFont(NULL);
//display.setCursor(0, 25); //voir les position car caractères spéciaux
display.setTextSize(1);
// if (valueSensor >= 0 && valueSensor <= step1)
if (valueSensor >= -1 && valueSensor <= step1)
{
display.setCursor(23, 25);
display.print("Bon");
}
else if (valueSensor > step1 && valueSensor <= step5)
{
if (valueSensor <= step2)
{
display.setCursor(17, 25);
display.print("Moyen");
}
else if (valueSensor > step2 && valueSensor <= step3)
{
display.setCursor(11, 25);
display.print("D");
display.write(130);
display.print("grad");
display.write(130);
}
else if (valueSensor > step3 && valueSensor <= step4)
{
display.setCursor(11, 25);
display.print("Mauvais");
}
else
{
display.setCursor(2, 25);
display.print("Tr");
display.write(138);
display.print("s mauvais");
}
}
else if (valueSensor > step5)
{
display.setCursor(2, 25);
display.print("Ext. mauvais");
}
else
{
display.setCursor(14, 25);
display.setTextSize(1);
display.print("Erreur");
}
}
void messager2(float valueSensor, int step1, int step2, int step3)
{
//MESSAGES FIXES => CENTRER à la main
display.setFont(NULL);
//display.setCursor(0, 25); //voir les position?
display.setTextSize(1);
// if (valueSensor >= 0 && valueSensor <= step1)
if (valueSensor >= -1 && valueSensor <= step1)
{
display.setCursor(20, 25);
display.print("Bien");
}
else if (valueSensor > step1 && valueSensor <= step3)
{
if (valueSensor <= step2)
{
display.setCursor(20, 25);
display.print("Bien");
}
else if (valueSensor > step2 && valueSensor <= step3)
{
display.setCursor(5, 25);
display.print("A");
display.write(130);
display.print("rer SVP");
}
}
else if (valueSensor > step3)
{
display.setCursor(2, 25);
display.print("A");
display.write(130);
display.print("rer vite");
}
else
{
display.setCursor(14, 25);
display.setTextSize(1);
display.print("Erreur");
}
}
void drawCentreString(const String &buf, int x, int y, int offset)
{
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string
display.setCursor(((64-offset)-w)/ 2, y); //si 1 seul chiffre => taille de 2 chiffres !!!
display.print(buf);
}
/*****************************************************************
* Forecast Atmosud *
*****************************************************************/
struct forecast
{
float multi;
float no2;
float o3;
float pm10;
float pm2_5;
};
struct forecast atmoSud
{
- 1.0, -1.0, -1.0, -1.0, -1.0
};
uint8_t arrayDownlink[5];
uint8_t forecast_selector;
/*****************************************************************
* Serial declarations *
*****************************************************************/
#define serialSDS (Serial1)
#define serialNPM (Serial1)
#define serialMHZ (Serial2)
/*****************************************************************
* BMP/BME280 declaration *
*****************************************************************/
BMX280 bmx280;
/*****************************************************************
* MH-Z16 declaration *
*****************************************************************/
MHZ16_uart mhz16;
/*****************************************************************
* MH-Z19 declaration *
*****************************************************************/
MHZ19 mhz19;
/*****************************************************************
* SGP40 declaration *
*****************************************************************/
SensirionI2CSgp40 sgp40;
/*****************************************************************
* Time *
*****************************************************************/
// time management varialbles
bool send_now = false;
unsigned long starttime;
unsigned long time_point_device_start_ms;
unsigned long starttime_SDS;
unsigned long starttime_NPM;
unsigned long last_NPM;
unsigned long act_micro;
unsigned long act_milli;
unsigned long last_micro = 0;
unsigned long min_micro = 1000000000;
unsigned long max_micro = 0;
unsigned long sending_time = 0;
unsigned long last_update_attempt;
int last_update_returncode;
int last_sendData_returncode;
/*****************************************************************
* SDS variables and enums *
*****************************************************************/
bool is_SDS_running;
// To read SDS responses
enum
{
SDS_REPLY_HDR = 10,
SDS_REPLY_BODY = 8
} SDS_waiting_for;
/*****************************************************************
* NPM variables and enums *
*****************************************************************/
bool is_NPM_running = false;
bool nextpmconnected; //important to test nextpm and avoid endless loops
// 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
String current_state_npm;
String current_th_npm;
/*****************************************************************
* Data variables *
*****************************************************************/
float last_value_BMX280_T = -128.0;
float last_value_BMX280_P = -1.0;
float last_value_BME280_H = -1.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;
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 last_value_SDS_P1 = -1.0;
float last_value_SDS_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_MHZ16 = -1.0;
float last_value_MHZ19 = -1.0;
float last_value_SGP40 = -1.0;
String last_data_string;
int last_signal_strength;
int last_disconnect_reason;
String esp_chipid;
String last_value_SDS_version;
String last_value_NPM_version;
unsigned long SDS_error_count;
unsigned long NPM_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_oled = 0;
unsigned long last_display_millis_matrix = 0;
uint8_t next_display_count = 0;
struct struct_wifiInfo
{
char ssid[LEN_WLANSSID];
uint8_t encryptionType;
int32_t RSSI;
int32_t channel;
};
struct struct_wifiInfo *wifiInfo;
uint8_t count_wifiInfo;
#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";
static String displayGenerateFooter(unsigned int screen_count)
{
String display_footer;
for (unsigned int i = 0; i < screen_count; ++i)
{
display_footer += (i != (next_display_count % screen_count)) ? " . " : " o ";
}
return display_footer;
}
/*****************************************************************
* display values *
*****************************************************************/
static void display_debug(const String &text1, const String &text2)
{
debug_outln_info(F("output debug text to displays..."));
if(cfg::has_ssd1306){
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();
}