-
Notifications
You must be signed in to change notification settings - Fork 0
/
FRIDA.ino
1143 lines (1059 loc) · 42.5 KB
/
FRIDA.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
// #define _TASK_TIMECRITICAL // Enable monitoring scheduling overruns
//#define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between tasks if no callback methods were invoked during the pass
//#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
// #define _TASK_WDT_IDS // Compile with support for wdt control points and task ids
// #define _TASK_LTS_POINTER // Compile with support for local task storage pointer
// #define _TASK_PRIORITY // Support for layered scheduling priority
// #define _TASK_MICRO_RES // Support for microsecond resolution
// #define _TASK_STD_FUNCTION // Support for std::function (ESP8266 and ESP32 ONLY)
// #define _TASK_DEBUG // Make all methods and variables public for debug purposes
// #define _TASK_INLINE // Make all methods "inline" - needed to support some multi-tab, multi-file implementations
// #define _TASK_TIMEOUT // Support for overall task timeout
// #define _TASK_OOCallBackS // Support for dynamic callback method binding
// #define _TASK_DEFINE_MILLIS // Force forward declaration of millis() and micros() "C" style
// #define _TASK_EXPOSE_CHAIN // Methods to access tasks in the task chain
// Debug and Test options
/*
// /!\ LES DEFINE RELATIFS AUX THREADS DOIVENT ETRE AVANT "include <TaskScheduler>" SINON APPAREMMENT CA PLANTE LES TASKS !
*/
#include <TaskScheduler.h>
#include "Adafruit_SHT31.h"
#include "AdafruitIO_WiFi.h"
#include <M5Stack.h>
#include <Wire.h> // Used to establish serial communication on the I2C bus
#include <SparkFun_TMP117.h> // Used to send and receive specific information from our sensor
#include <MCP41xxx.h>
#include <WiFi.h>
//#include <NTPClient.h>
#include <ezTime.h>
#include <IotWebConf.h>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////VARIABLES DECLARATION///////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Scheduler ts;
//NETWORK
char* WIFI_SSID;
char* WIFI_PASS;
WiFiClient myWiFiClient; //CLIENT USED TO SEND HTTP REQUESTS TO ELSA
const long utcOffsetInSeconds = 3600; //GMT +1
//WiFiUDP ntpUDP;
//NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
Timezone selectedTimeZone;
String defaultTimeRegion = "Europe/Brussels";
bool validTimeZone = true;
HttpClient client = HttpClient(myWiFiClient, "totototo.be", 80); //= HttpClient(myWiFiClient, "phenics.gembloux.ulg.ac.be", 80);
String ElsaServerURL;
String ElsaTemperatureSensorName;
String ElsaHumiditySensorName;
bool ValidElsaCredentials = false;
bool WiFiConnected = false;
uint16_t elsaTimeoutCounter = 0;
enum PlatformUsed { UNDEF, AdafruitIO, Elsa };
//SENSORS
Adafruit_SHT31 sht31 = Adafruit_SHT31(); //Humidity & Temperature sensor
TMP117 sensor48; //Initalize Temperature-only sensor
int i2cAddr = 0x48; //Address of the SHT31 sensor
bool foundSHT31 = false;
bool foundTMP117 = false;
float SHT31_T;
float SHT31_H;
float TMP117_T;
//DISPLAY
bool blank = false;
float prv_SHT31_T = 0.0;
float prv_SHT31_H = 0.0;
int prvColT = WHITE;
int prvColH = WHITE;
int row = 100;
int col = 80;
bool rewrite;
float prv_TMP117_T = 0.0;
int prvCol48 = WHITE;
//--Customisable delays
const unsigned int DEFAULT_DELAY_tReadSensor = 1*TASK_MINUTE;
const unsigned int DEFAULT_DELAY_tShutScreenOff = 5*TASK_MINUTE;
const unsigned int DEFAULT_DELAY_SendOnline = 10*TASK_SECOND;
unsigned int CUSTOM_DELAY_tReadSensor = DEFAULT_DELAY_tReadSensor;
unsigned int CUSTOM_DELAY_tShutScreenOff = DEFAULT_DELAY_tShutScreenOff;
unsigned int CUSTOM_DELAY_SendOnline = DEFAULT_DELAY_SendOnline;
// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point.
const char initialThingName[] = "FRIDA";
// -- Initial password to connect to the Thing, when it creates an own Access Point.
String defaultApPassword = "12345678";
char wifiInitialApPassword[IOTWEBCONF_WORD_LEN] = "12345678";
#define STRING_LEN 128
#define NUMBER_LEN 32
void configSaved();
boolean formValidator();
DNSServer dnsServer;
WebServer server(80);
//-- Time Zone
char TimeZoneValue[STRING_LEN];
//--Server parameters--
char ElsaHostnameValue[STRING_LEN];
char ElsaSensorNameValue_Temperature[STRING_LEN];
char ElsaSensorNameValue_Humidity[STRING_LEN];
//--FRIDA Tasks parameters (keyword "tp")--
char tpNewMeasureDelayValue[NUMBER_LEN];
char tpScreenOffDelayValue[NUMBER_LEN];
char tpSendOnlineDelayValue[NUMBER_LEN];
IotWebConf iotWebConf(initialThingName, &dnsServer, &server, wifiInitialApPassword);
IotWebConfParameter TimeZone_Param = IotWebConfParameter("Time Zone (ex : \"Europe/Brussels\" or \"BE\"", "TimeZone_ParamID", TimeZoneValue, STRING_LEN);
IotWebConfSeparator Separator1 = IotWebConfSeparator();
IotWebConfParameter ElsaHostName = IotWebConfParameter("Elsa Hostname", "ElsaHostNameID", ElsaHostnameValue, STRING_LEN);
IotWebConfParameter ElsaSensorName_Temperature = IotWebConfParameter("Elsa temperature sensor name", "ElsaSensorName_Temperature_ID", ElsaSensorNameValue_Temperature, STRING_LEN);
IotWebConfParameter ElsaSensorName_Humidity = IotWebConfParameter("Elsa optional humidity sensor name", "ElsaSensorName_Humidity_ID", ElsaSensorNameValue_Humidity, STRING_LEN);
IotWebConfSeparator Separator2 = IotWebConfSeparator();
IotWebConfParameter tpNewMeasureDelay = IotWebConfParameter("New measure frequency (in seconds)", "tpNewMeasureDelayID", tpNewMeasureDelayValue, NUMBER_LEN);
IotWebConfParameter tpScreenOffDelay = IotWebConfParameter("Screen active time (in seconds)", "tpScreenOffDelayID", tpScreenOffDelayValue, NUMBER_LEN);
IotWebConfParameter tpSendOnlineDelay = IotWebConfParameter("Communication with the online platform delay (in seconds)", "tpSendOnlineDelayID", tpSendOnlineDelayValue, NUMBER_LEN);
// -- Configuration specific key. The value should be modified if config structure was changed.
//#define CONFIG_VERSION "Alph"
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////TASKS AND CALLBACK METHODS/////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// -- Callback method declarations.
//SENSORS & DISPLAY CALLBACK METHODS
void readSensorCallBack();
void readButtonsStateCallBack();
void shutScreenOffCallBack();
//NETWORK CALLBACK METHODS
void aioTimeUpdBackgroundCallback();
void initElsaCallBack();
void webConfLoopCallBack();
void checkWebConfStatusCallBack();
void getElsaCredentialsCallBack();
//void onElsaEnable();
//void onElsaDisable();
// -- TASKS
//SENSORS & DISPLAY TASKS
Task tReadSensor(DEFAULT_DELAY_tReadSensor, TASK_FOREVER, &readSensorCallBack, &ts, true);
Task tReadButtonsState(50, TASK_FOREVER, &readButtonsStateCallBack, &ts, true); //200 milliseconds delay
Task tShutScreenOff(DEFAULT_DELAY_tShutScreenOff, 1, &shutScreenOffCallBack, &ts, false);
//IotWebConf
Task tWebConfLoop(TASK_SECOND, TASK_FOREVER, &webConfLoopCallBack, &ts, true);
Task tCheckWebConfStatus(10 * TASK_SECOND, TASK_FOREVER, &checkWebConfStatusCallBack, &ts, true);// 60 iterations because we want it to try for 10 minutes then give up. When we give up, the AP will be shut down too.
//NETWORK
Task tIoTimeUpd(10 * TASK_SECOND, TASK_FOREVER, &aioTimeUpdBackgroundCallback, &ts, true);
Task tSendToElsa(DEFAULT_DELAY_SendOnline, 20, &getElsaCredentialsCallBack, &ts, false);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////START OF SETUP//////////////////////////////////////////////////////
// - Launches Serial connection to computer
// - Starts the M5Stack components
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
if(Serial)
{
#define _DEBUG_
}
//#define _TEST_
#ifdef _DEBUG_
#define PP_(a) Serial.print(a);
#define PL_(a) Serial.println(a);
#else
#define PP_(a)
#define PL_(a)
#endif
#if defined(_DEBUG_) || defined(_TEST_)
Serial.begin(115200);
#endif
PL_("Starting up...");
//M5STACK & SENSORS
M5.begin(true,false,true); // init lcd, serial, but don't init sd card
M5.Power.begin();
M5.Lcd.setTextColor(TFT_WHITE,TFT_BLACK);
M5.Lcd.setFreeFont(&FreeSansBold9pt7b);
M5.Lcd.setCursor(0,20);
M5.Lcd.setTextSize(1);
M5.Lcd.setRotation(3); //Invert the display
M5.Lcd.setBrightness(100);
M5.Lcd.clear();
//M5.Lcd.setCursor(0,20);
M5.Lcd.println("Searching sensors...");
if(sht31.begin(0x44)) //Test if Humidity sensor is there
{
foundSHT31 = true;
PL_("Found SHT31");
M5.Lcd.println("Found SHT31");
delay(5000);
//M5.Lcd.clear();
}
//Test if Temperature-Only sensor is there
else if(sensor48.begin(i2cAddr) == true) // Function to check if the sensor will correctly self-identify with the proper Device ID/Address
{
foundTMP117 = true;
sensor48.setContinuousConversionMode();
PL_("Found TMP117");
M5.Lcd.println("Found TMP117");
delay(5000);
//M5.Lcd.clear();
}
else //If nothing was detected then shut down
{
PL_("Couldn't find SHT31");
PL_("Couldn't find TMP117");
M5.Lcd.println("Could not find any sensors...");
M5.Lcd.println("Please connect a sensor and restart Frida.");
foundTMP117 = false;
foundSHT31 = false;
delay (10000);
M5.Lcd.clear();
M5.Lcd.setBrightness(0);
while (1);
}
selectedTimeZone.setLocation(defaultTimeRegion);
//Access Point
M5.Lcd.println("Keep outer buttons pressed to force setup.");
M5.Lcd.println("Keep all buttons pressed to reset AP password.");
M5.update();
delay(10000);//M5.BtnA.PressedFor() doesn't work in the setup so we just add a delay for the user to read the screen then press the buttons.
M5.update();
if (M5.BtnA.isPressed() && M5.BtnB.isPressed() && M5.BtnC.isPressed())
{
PL_("Resetting AP mode");
//iotWebConf.resetApPassword(defaultApPassword);
//iotWebConf._forceDefaultPassword = true;
//resetAPpassword();
iotWebConf.resetApPassword(true);
M5.Lcd.println("Resetting AP mode.");
//PL_(server.arg(iotWebConf._apPasswordParameter.getId()));
delay(2500);
}
else if (M5.BtnA.isPressed() && M5.BtnC.isPressed()) {
PL_("Requested AP mode.");
iotWebConf.forceApMode(true);
M5.Lcd.println("Forcing AP mode.");
delay(2500);
}
iotWebConf.addParameter(&TimeZone_Param);
iotWebConf.addParameter(&Separator1);
iotWebConf.addParameter(&ElsaHostName);
iotWebConf.addParameter(&ElsaSensorName_Temperature);
iotWebConf.addParameter(&ElsaSensorName_Humidity);
iotWebConf.addParameter(&Separator2);
iotWebConf.addParameter(&tpNewMeasureDelay);
iotWebConf.addParameter(&tpScreenOffDelay);
iotWebConf.addParameter(&tpSendOnlineDelay);
iotWebConf.setConfigSavedCallback(&configSaved);
iotWebConf.setFormValidator(&formValidator);
iotWebConf.getApTimeoutParameter()->visible = true;
// -- Initializing the configuration.
iotWebConf.init();
/* USELESS SINCE THE AP WILL FALLBACK TO WIFI BY HIMSELF AFTER SOME TIME
if(M5.BtnA.read() == true && M5.BtnB.read() == true && M5.BtnC.read() == true )
{
//rename ThingName with initial Thing Name
}
if(iotWebConf.getThingNameParameter() != initialThingName)
{
iotWebConf.skipApStartup();
}
*/
// -- Set up required URL handlers on the web server.
server.on("/", handleRoot);
server.on("/config", []{ iotWebConf.handleConfig(); });
server.onNotFound([](){ iotWebConf.handleNotFound(); });
loadTaskParameters();
Serial.println("Ready.");
M5.Lcd.clear();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////START OF MAIN LOOP//////////////////////////////////////////////////
// - Main loop should only execute the task schedulers for a well functioning MultiThreading
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
ts.execute();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////START OF SENSORS METHODS///////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void readSensorCallBack()
{
PL_("tReadSensor");
if (foundSHT31 == true)
{
tReadSensor.yield(&processValuesSHT31);
/*Not quite sure if these should or shouldn't be callbacks.
They probably should in the case where we allow multiple sensors to be connected.
Then we would simply have IFs and not ELSE IFs right below here.
*/
}
else if(foundTMP117 == true)
{
//PL_("readSensorCallBack -- foundTMP117 == true");
tReadSensor.yield(&processValuesTMP117);
}
}
void processValuesSHT31()
{
//PL_("tReadSensor -- processValuesSHT31");
//on lit les informations de ce capteur
SHT31_T = sht31.readTemperature();
SHT31_T = floor(100*SHT31_T)/100; //rounding things to the second decimal.
SHT31_H = sht31.readHumidity();
SHT31_H = floor(100*SHT31_H)/100;
debugReadSHT31();
rewrite = !blank; //if blank = false then rewrite = true => if we are displaying stuff then we want to rewrite the value on the lcd (updates the value on screen)
tReadSensor.yield(&SHT31_DesignateDisplayColor);//if setcallback then delay is x2 for the task.
}
void debugReadSHT31()
{
if (!isnan(SHT31_T)) { // check if 'is not a number'
Serial.print("SHT31_T = ");
Serial.print(SHT31_T);
}
else {
Serial.print("SHT31_T = null");
}
if (!isnan(SHT31_H)) { // check if 'is not a number'
Serial.print("SHT31_H = ");
Serial.print(SHT31_H);
}
else {
Serial.print("SHT31_H = null");
}
}
void processValuesTMP117()
{
//PL_("tReadSensor -- processValuesTMP117");
//on lit les informations de ce capteur
TMP117_T = sensor48.readTempC();
TMP117_T = floor(100*TMP117_T)/100; //rounding things to the second decimal.
debugReadTMP117();
if(TMP117_T <= 0 && TMP117_T > -1.0 && sensor48.begin(i2cAddr) == false)// unplugging TMP117 gives negative value close to 0 whereas unplugging SHT31 gives NAN so we're checking that.
{
TMP117_T = NAN;
}
rewrite = !blank; //if blank == false then rewrite = true => if we are displaying stuff then we want to rewrite the value on the lcd (updates the value on screen)
tReadSensor.yield(&TMP117_DesignateDisplayColor);//if setcallback then delay is x2 for the task.
}
void debugReadTMP117()
{
Serial.print("TMP117_T = ");
if(!isnan(TMP117_T))
PP_(TMP117_T);
PL_("°C");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////START OF DISPLAY METHODS///////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//SHT31
void SHT31_DesignateDisplayColor()
{
if (SHT31_T > prv_SHT31_T) {
prvColT = RED;
}
else if (SHT31_T < prv_SHT31_T) {
prvColT = BLUE;
}
else {
if (prvColT != WHITE && blank == false) rewrite = true;
//else rewrite = false; // If the values didn't change, then we don't want to update the LCD
prvColT = WHITE;
}
if (SHT31_H > prv_SHT31_H) {
prvColH = RED;
}
else if (SHT31_H < prv_SHT31_H) {
prvColH = BLUE;
}
else {
if (prvColH != WHITE && blank == false) rewrite = true;
//else rewrite = false; // If the values didn't change, then we don't want to update the LCD
prvColH = WHITE;
}
tReadSensor.yield(&SHT31_DisplayValues);
}
void SHT31_DisplayValues()
{
if(rewrite)
{
M5.Lcd.clear();
//Display wifiFailedStatus
displayWiFiFailed();
//Display NTP
displayInvalidTimeZone();
M5.Lcd.setCursor(0,130);
M5.Lcd.setTextSize(1);
M5.Lcd.setFreeFont(&FreeSansBold18pt7b);
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Lcd.print(selectedTimeZone.dateTime("H:i:s "));//Display Hour, minutes, seconds
//Display values
M5.Lcd.setCursor(col+100,row);
if(SHT31_T<-30.00 || isnan(SHT31_T))//Set to yellow for errors as we consider anything below -30 as a bad input
{
M5.Lcd.setTextColor(TFT_YELLOW);
}
else
{
M5.Lcd.setTextColor(prvColT);
}
M5.Lcd.print(SHT31_T);
M5.Lcd.println("°C");
M5.Lcd.setCursor(col+100,150);
if(SHT31_H<-30.00 || isnan(SHT31_H))//Set to yellow for errors as we consider anything below -30 as a bad input
{
M5.Lcd.setTextColor(TFT_YELLOW);
}
else
{
M5.Lcd.setTextColor(prvColH);
}
M5.Lcd.print(SHT31_H);
M5.Lcd.println("%H");
//Color designation depends on the previously DISPLAYED value, not the previously measured one.
prv_SHT31_T = SHT31_T; //Used to know if the next measure is gonna be > or < current measure.
prv_SHT31_H = SHT31_H; //Used to know if the next measure is gonna be > or < current measure.
}
if (tReadSensor.getRunCounter() <= 2) //2 because of the two setCallbacks instead of 2 yields
{
//Enable the tShutScreenOff task on the first run AFTER we show stuff to avoid having it just shut the screen off immediately.
tShutScreenOff.enableDelayed(CUSTOM_DELAY_tShutScreenOff);
}
//tReadSensor.yield(&processValuesSHT31); //passes to the scheduler, next pass will be skipping the first step since we now know what sensor we are using.
tReadSensor.setCallback(&processValuesSHT31); //Looping the task
//tReadSensor.delay(5 * TASK_SECOND); //Same
}
//TMP117
void TMP117_DesignateDisplayColor()
{
if (TMP117_T > prv_TMP117_T) {
prvCol48 = RED;
}
else if (TMP117_T < prv_TMP117_T) {
prvCol48 = BLUE;
}
else {
if (prvCol48 != WHITE && blank == false) rewrite = true;
//else rewrite = false; // If the values didn't change, then we don't want to update the LCD
prvCol48 = WHITE;
}
tReadSensor.yield(&TMP117_DisplayValues);
}
void TMP117_DisplayValues()
{
if(rewrite)
{
M5.Lcd.clear();
//Display wifiFailedStatus
displayWiFiFailed();
//Display NTP
displayInvalidTimeZone();
M5.Lcd.setCursor(0,130);
M5.Lcd.setTextSize(1);
M5.Lcd.setFreeFont(&FreeSansBold18pt7b);
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Lcd.print(selectedTimeZone.dateTime("H:i:s "));//Display Hour, minutes, seconds
M5.Lcd.setCursor(col+100,130);
if(TMP117_T<-30.00 || (TMP117_T <= -0.009 && TMP117_T >= -0.011) || isnan(TMP117_T))//Set to yellow for errors as we consider anything below -30 as a bad input. -0.01 being the value when sensor is not correctly connected.
{
M5.Lcd.setTextColor(TFT_YELLOW);
}
else
{
M5.Lcd.setTextColor(prvCol48);
}
M5.Lcd.print(TMP117_T);
M5.Lcd.println("°C");
//Color designation depends on the previously DISPLAYED value, not the previously measured one.
prv_TMP117_T = TMP117_T; //Used to know if the next measure is gonna be > or < current measure.
}
if (tReadSensor.getRunCounter() <= 2)
{
//Enable the tShutScreenOff task on the first run AFTER we show stuff to avoid having it just shut the screen off immediately.
tShutScreenOff.enableDelayed(CUSTOM_DELAY_tShutScreenOff);
}
//tReadSensor.yield(&processValuesTMP117); //passes to the scheduler, next pass will be skipping the first step since we now know what sensor we are using.
tReadSensor.setCallback(&processValuesTMP117); //Looping the task
//tReadSensor.delay(5 * TASK_SECOND); //Same
}
void displayWiFiFailed()
{
if(WiFiConnected != true)
{
M5.Lcd.setCursor(M5.Lcd.width()-200,M5.Lcd.height()-10);
M5.Lcd.setTextSize(1);
M5.Lcd.setFreeFont(&FreeSansBold9pt7b);
M5.Lcd.setTextColor(TFT_YELLOW);
M5.Lcd.println("Not connected to WiFi");
}
else{
M5.Lcd.setCursor(M5.Lcd.width()-170,M5.Lcd.height()-10);
M5.Lcd.setTextSize(1);
M5.Lcd.setFreeFont(&FreeSansBold9pt7b);
M5.Lcd.setTextColor(TFT_GREEN);
M5.Lcd.println("Connected to WiFi");
}
}
void displayInvalidTimeZone()
{
if(validTimeZone != true)
{
M5.Lcd.setCursor(M5.Lcd.width()-170,M5.Lcd.height()-30);
M5.Lcd.setTextSize(1);
M5.Lcd.setFreeFont(&FreeSansBold9pt7b);
M5.Lcd.setTextColor(TFT_YELLOW);
M5.Lcd.println("Invalid time zone");
}
}
void readButtonsStateCallBack()
{
//PL_("T2ReadButtonsState");
M5.update();
if (M5.BtnA.wasReleased() || M5.BtnB.wasReleased() || M5.BtnC.wasReleased()) {
blank = false;
M5.Lcd.setBrightness(100);
tShutScreenOff.restartDelayed(CUSTOM_DELAY_tShutScreenOff);
tReadSensor.forceNextIteration();
}
if(tReadButtonsState.getRunCounter() == 1) //force display at the beginning
{
tReadSensor.forceNextIteration();
}
}
void shutScreenOffCallBack()
{
PL_("tShutScreenOff");
//the screen "shuts down" after having shown the temperature for 60 seconds.
blank = true;
M5.Lcd.clear();
M5.Lcd.setBrightness(0);
//t3DisplayValuesLcd.disable();
tShutScreenOff.disable();//Do not repeat so that we are sure to always wait for 60 seconds
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////START OF IotWebConf CALLBACK METHODS///////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void handleRoot()// Handle web requests to "/" path.
{
// -- Let IotWebConf test and handle captive portal requests.
if (iotWebConf.handleCaptivePortal())
{
// -- Captive portal request were already served.
return;
}
String s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>";
s += "<title>FRIDA Configuration</title></head><body>Welcome to FRIDA configuration !";
s += "<ul>";
s += "<li>ELSA Host: ";
s += ElsaHostnameValue;
s += "<li>ELSA Temperature Sensor : ";
s += ElsaSensorNameValue_Temperature;
s += "<li>ELSA Humidity Sensor : ";
s += ElsaSensorNameValue_Humidity;
s += "</ul>";
s += "Go to <a href='config'>configure page</a> to change values.";
s += "</body></html>\n";
server.send(200, "text/html", s);
}
void configSaved()
{
Serial.println("Configuration was updated.");
iotWebConf.forceApMode(false);
tCheckWebConfStatus.enableIfNot();
}
boolean formValidator()
{
Serial.println("Validating form.");
boolean valid = true;
int l = server.arg(ElsaHostName.getId()).length();
if (l < 5)
{
ElsaHostName.errorMessage = "Please provide at least 5 characters for this field !";
valid = false;
}
return valid;
}
void wifiConnected()
{
Serial.println("WiFi was connected.");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////CONNECTION METHODS////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void webConfLoopCallBack()
{
PL_("WebConf Loop");
// -- doLoop should be called as frequently as possible.
iotWebConf.doLoop();
if(tWebConfLoop.isFirstIteration() == true)
{
PL_("LOAD");
//loadTaskParameters();
}
}
void checkWebConfStatusCallBack()
{
if(iotWebConf.getState() == 4)
{
PL_("WebConf Status == 4 => We are connected");
//M5.Lcd.clear();
M5.Lcd.setCursor(0,40);
M5.Lcd.setTextSize(1);
M5.Lcd.setFreeFont(&FreeSansBold9pt7b);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.println("Connecting...");
tReadSensor.delay(2000);//Delay this task to allow us to actually read what we wrote on the screen.
//timeClient.begin();
initNTP();
//tWebConfLoop.setInterval(3*TASK_SECOND);
tSendToElsa.setCallback(getElsaCredentialsCallBack);
tSendToElsa.setIterations(20);
tSendToElsa.enableDelayed(DEFAULT_DELAY_SendOnline);
//tSendToElsa.enableIfNot();
loadTaskParameters();//load them after so the first tSendToElsa iteration happens after the default time
tWebConfLoop.disable();
tCheckWebConfStatus.disable();
return;//prevents the next IF to happen.
}
else{
PL_("WebConf Status != 4 => We are NOT connected");
M5.Lcd.setCursor(0,20);
M5.Lcd.setTextSize(1);
M5.Lcd.setFreeFont(&FreeSansBold9pt7b);
M5.Lcd.setTextColor(TFT_WHITE);
M5.Lcd.println("Trying to connect to WiFi...");
//tReadSensor.delay(2000);//Delay this task to allow us to actually read what we wrote on the screen.
tWebConfLoop.enableIfNot();
}
/*
if(tCheckWebConfStatus.getRunCounter() % 5 == 0 && isValidDelay(tpNewMeasureDelayValue) && isValidDelay(tpScreenOffDelayValue) && isValidDelay(tpSendOnlineDelayValue))//try every 5 iterations to load new parameters to make sure we end up loading them even if we can't connect to WiFi.
{
tReadSensor.forceNextIteration();
}
*/
}
void initNTP()
{
String TZ = TimeZoneValue;
if(isValidInput(TZ))
{
if(selectedTimeZone.setLocation(TZ))
{
PL_("NTP good");
validTimeZone = true;
selectedTimeZone.setLocation(TZ);
//selectedTimeZone.updateNTP();
}
else{
PL_("NTP invalid");
selectedTimeZone.setLocation(defaultTimeRegion);
validTimeZone = false;
}
}
else{
PL_("NTP Default");
validTimeZone = false;
selectedTimeZone.setLocation(defaultTimeRegion);//don't report error as the user simply left the parameter empty so he wants to use the default setting.
}
}
void getElsaCredentialsCallBack()//initializing phase
{
PL_("getElsaCredentialsCallBack");
WIFI_PASS = iotWebConf.getWifiPasswordParameter()->valueBuffer;
WIFI_SSID = iotWebConf.getWifiSsidParameter()->valueBuffer;
PP_("WIFI credentials : ");
PP_(WIFI_SSID);
PP_(" + ");
PL_(WIFI_PASS);
if(WiFi.status() == WL_CONNECTED)
{
PL_("WiFi connected : WiFi.status == WL_CONNECTED");
WiFiConnected = true;
ElsaTemperatureSensorName = ElsaSensorNameValue_Temperature;
ElsaHumiditySensorName = ElsaSensorNameValue_Humidity;
if(isValidInput(ElsaHostnameValue))//redundant as the web form already asks for a minimum of 5 characters.
{
PP_("ELSA parameters : ");
PP_(ElsaHostnameValue);
PP_(" + ");
PP_(ElsaTemperatureSensorName);
PP_(" + ");
PP_(ElsaHumiditySensorName);
PL_(" .")
ValidElsaCredentials = true;
ElsaServerURL = ElsaHostnameValue;
client = HttpClient(myWiFiClient, ElsaServerURL, 80);
tSendToElsa.set(CUSTOM_DELAY_SendOnline, TASK_FOREVER, &sendDataToElsaCallBack);
//tSendToElsa.yield();
}
else{
PL_("Invalid ELSA parameters");
PP_("Elsa parameters : ");
PP_(ElsaHostnameValue);
PP_(" + ");
PP_(ElsaTemperatureSensorName);
PP_(" + ");
PP_(ElsaHumiditySensorName);
PL_(" .")
//tSendToElsa.disable(); //Disable tasks linked to platforms which don't have valid parameters to avoid unnecessary work.
}
}
else
{
PL_("Trying to connect to WiFi");
WiFiConnected = false;
WiFi.disconnect();
WiFi.begin(WIFI_SSID, WIFI_SSID);
}
if(tSendToElsa.isLastIteration())
{
PL_("Returning to AP mode");
tWebConfLoop.setCallback(webConfLoopCallBack);
tWebConfLoop.enable();
tCheckWebConfStatus.setCallback(checkWebConfStatusCallBack);
tCheckWebConfStatus.enable();
tSendToElsa.disable();
}
}
void aioTimeUpdBackgroundCallback()
{
if(WiFi.status() == WL_CONNECTED)
{
PL_("tIoTimeUpd");
//io.run();
//timeClient.update();
events();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////START OF DATA SENDING METHODS//////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void sendDataToElsaCallBack()
{
//foundTMP117 = true;
if (foundSHT31)
{
sendToElsa_SHT31();
}
if (foundTMP117)
{
if(TMP117_T>=-30.00)//Only send actual data without errors (due to missing Vcc cable ?).
{
sendToElsa_TMP117();
}
}
}
void sendToElsa_SHT31()
{
PL_("sendToElsa_SHT31");
PL_("Before if WiFi status == Connected");
if(WiFi.status() == WL_CONNECTED)//Safety measure since the task will not fall back to initializing phase until something goes wrong.
{
if(SHT31_T>=-30.00)//Only send actual data without errors (due to missing Vcc cable ?).
{
Serial.println("Sending HTTP request...");
// Make a HTTP request:
String url;
url = "/api/put/!s_" + ElsaTemperatureSensorName + "?value=" + String(SHT31_T)
+ "&control=" + String(calculateCheckSUM(ElsaTemperatureSensorName, SHT31_T));
PP_("Sending data : GET ");
PL_(url);
client.get(url);
if(client.available())
{
int statusCode = client.responseStatusCode();
String response = client.responseBody();
PP_("Status code: ");
PL_(statusCode);
PP_("Response: ");
PL_(response);
if(statusCode <199 || statusCode>299)
{
if(statusCode == 403)
{
PL_("403 forbidden, bad checksum.");
}
else
{
elsaTimeoutCounter ++;
PL_("Elsa TimeOut");
if(elsaTimeoutCounter >=10)
{
PL_("10 elsa Timeouts, trying to reconnect");
elsaTimeoutCounter = 0;
resetTaskForWiFiConnection(tSendToElsa, Elsa);//this means the Task will re check WiFi connection then fall back to AP mode if needed. This is just a safety measure in case WiFi connection lost in the middle of the process.
}
client.stop();
return;
}
}
}
else{
PL_("WiFi Client not available");
}
}
tSendToElsa.delay(1000);
if(SHT31_H>=-30.00)//Only send actual data without errors (due to missing Vcc cable ?).
{
Serial.println("Sending HTTP request...");
// Make a HTTP request:
String url;
url = "/api/put/!s_" + ElsaHumiditySensorName + "?value=" + String(SHT31_H)
+ "&control=" + String(calculateCheckSUM(ElsaHumiditySensorName, SHT31_H));
PP_("Sending data : GET ");
PL_(url);
client.get(url);
if(client.available())
{
int statusCode = client.responseStatusCode();
String response = client.responseBody();
PP_("Status code: ");
PL_(statusCode);
PP_("Response: ");
PL_(response);
if(statusCode <199 || statusCode>299)
{
if(statusCode == 403)
{
PL_("403 forbidden, bad checksum.");
}
else
{
elsaTimeoutCounter ++;
PL_("Elsa TimeOut");
if(elsaTimeoutCounter >=10)
{
PL_("10 elsa Timeouts, trying to reconnect");
elsaTimeoutCounter = 0;
resetTaskForWiFiConnection(tSendToElsa, Elsa);//this means the Task will re check WiFi connection then fall back to AP mode if needed. This is just a safety measure in case WiFi connection lost in the middle of the process.
}
client.stop();
return;
}
}
}
else{
PL_("WiFi Client not available");
}
}
PL_();
PL_("closing connection");
elsaTimeoutCounter = 0;
}
else{
PL_("WiFi Status != WL_CONNECTED");
WiFiConnected = false;
resetTaskForWiFiConnection(tSendToElsa, Elsa);
client.stop();
return;
}
client.stop();
tSendToElsa.setCallback(&sendToElsa_SHT31);
}
void sendToElsa_TMP117()
{
PL_("sendToElsa_TMP117");
PL_("Before if WiFi = connected");
if(WiFi.status() == WL_CONNECTED && isValidInput(ElsaTemperatureSensorName))//Safety measure since the task will not fall back to initializing phase until something goes wrong.
{
Serial.println("Sending HTTP request...");
// Make a HTTP request:
String url;
PP_("ElsaTemperatureSensorName : ");
PL_(ElsaTemperatureSensorName);
url = "/api/put/!s_" + ElsaTemperatureSensorName + "?value=" + String(TMP117_T)
+ "&control=" + String(calculateCheckSUM(ElsaTemperatureSensorName, TMP117_T));
/*url = "/api/put/!s_TMP117?value=" + String(25.00)
+ "&control=" + String(calculateCheckSUM("TMP117", 25.00));*/
PP_("Sending data : GET ");
PL_(url);
client.get(url);
if(client.available())
{
int statusCode = client.responseStatusCode();
String response = client.responseBody();
PP_("Status code: ");
PL_(statusCode);
PP_("Response: ");
PL_(response);
if(statusCode <199 || statusCode>299)
{
if(statusCode == 403)
{
PL_("403 forbidden, bad checksum.");
}
else
{
elsaTimeoutCounter ++;
PL_("Elsa TimeOut");
if(elsaTimeoutCounter >=10)
{
PL_("10 elsa Timeouts, trying to reconnect");
elsaTimeoutCounter = 0;
resetTaskForWiFiConnection(tSendToElsa, Elsa);//this means the Task will re check WiFi connection then fall back to AP mode if needed. This is just a safety measure in case WiFi connection lost in the middle of the process.
}
client.stop();
return;
}
}
}
else{
PL_("WiFi Client not available");
}
PL_();
PL_("closing connection");
elsaTimeoutCounter = 0;
}
else{
PL_("WiFi Status != WL_CONNECTED");
WiFiConnected = false;
resetTaskForWiFiConnection(tSendToElsa, Elsa);
client.stop();
return;
}
client.stop();
tSendToElsa.setCallback(&sendToElsa_TMP117);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////START OF GENERIC METHODS/////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool isValidInput(String param) //If we don't put anything or if we type "NULL" then we don't want this parameter
{
if(param != "" && param != "NULL")
{
return true;
}
else{
return false;
}
}
int calculateCheckSUM(String SensorName, float Value)