-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP8266_gateway.ino
1068 lines (920 loc) · 33.8 KB
/
ESP8266_gateway.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 required libraries
#include <WiFiClientSecure.h> //Needed for HTTPS connections
#include <FirebaseArduino.h> //Needed for receiving stream from firebase
#include <ESP8266WiFi.h> //Needed to onnect to WiFi
#include <ESP8266HTTPClient.h> //Needed to send / receive data from DB
#include <ESP8266Ping.h> //Needed to check internet conectivity
#include <ESP8266WebServer.h> //Needed for setup mode web server
#include <NTPClient.h> //Needed for NTP time sync
#include <WiFiUdp.h> //Also needed for NTP
#include <EEPROM.h> //Needed to save / restore data using EEPROM
#include <RF24Network.h> //Needed for nRF sensor network
#include <RF24.h> //Needed for nRF sensor network
#include <SPI.h> //Needed for nRF sensor network
#include "FS.h" //Needed for SPIFFS data storing
#include "ESP8266FtpServer.h" //Needed for FTP server to access SPIFFS remotely
//Define required constants
#define DEBUG true
#define FIREBASE_URL "ppp-2019-dominik-polic.firebaseio.com"
#define FIREBASE_ROOT_PATH "/users"
#define FIREBASE_DOWNSTREAM_PATH "/to_gateway"
#define FIREBASE_UPSTREAM_PATH "/from_gateway"
#define DEVICE_TYPE "SENSOR-GATEWAY"
#define HTTP_TIMEOUT 5 //THIS IS IN SECONDS!!!!!!
#define MAX_FIREBASE_RETRIES 5
#define MAX_TOKEN_RETRIES 5
#define MAX_WIFI_RETRIES 30
#define PING_INTERVAL 20000 //20 seconds
#define FIREBASE_FORCE_TOKEN_RELOAD_INTERVAL 3500000 //a bit less then 1 hour
#define FIREBASE_STREAM_RELOAD_INTERVAL 1800000 //30 minutes
#define DEFAULT_AP_SSID "SensorGW" //MAC address is appended to this to make it unique
#define DEFAULT_AP_PASSWORD "12345678"
//Define Firebase realtime database and authentication information
#define ACCESS_REFRESH_URL "https://securetoken.googleapis.com/v1/token?key=AIzaSyBLvDlBWPdulyt4tHrkjSfPK3twKpPV2DQ" //String after "key=" is the firebase Web API key
#define ACCESS_TOKEN_POST_STRING_BEGIN "{\"grant_type\" : \"refresh_token\", \"refresh_token\" : \""
#define ACCESS_TOKEN_POST_STRING_END "\" }"
//Sensor to gateway types (nRF24 bus)
#define TYPE_DHT11 1 //Temperature and humidity sensor
#define TYPE_TEMPERATURE 2 //Temperature only
#define TYPE_HUMIDITY 3 //Humidity only
#define TYPE_LIGHT 4 //Light sensor
#define TYPE_BMP280 5 //Temperature and pressure sensor
#define TYPE_SWITCH 6 //Digital switch
#define TYPE_BUTTON 7 //Pushbutton
#define TYPE_MOTION 8 //Motion sensor
#define TYPE_LIGHT_BTN 9 //Pushbutton and incoming command light
#define GATEWAY_ADDRESS 00 //nRF24Network address of the gateway
#define CHANNEL 90
#define UNAVAILABLE_VALUE 9999;
//Priority definitions
#define PRIORITY_LOW 1
#define PRIORITY_NORMAL 2
#define PRIORITY_HIGH 3
#define PRIORITY_INSTANT_ONLY 4 //WIth this priority data is only sent if the connection is currently available, otherwise it's discarded. Useful for button presses that are only relevant at one specific moment.
#define MAX_CONSECUTIVE_SENDS 10 //How many entries to send to db in a loop without running other checks
#define MAX_DEVICES 6 //How many addresses are allowed to connect to the gateway, used for parsing data from db on connection established... (from 1 to MAX_DEVICES)
//Factory reset button... If this pin is connected to GND on boot, factory reset will be performed (wifi and user credentials will be reeset, pending data will be intact)
#define FACTORY_RESET_BTN 3
//NTP config
#define UTC_OFFSET_IN_SECONDS 0 //This can be changed to any timezone, but I recommend using central time to sync data between timezones
#define NTP_HOST "time.google.com"
//#define NTP_HOST "pool.ntp.org //Some alternative hosts...
#define NTP_REFRESH_INTERVAL 10000 //10 seconds
//SPIFFS log config
#define NO_FILE "no-record"
//Define EEPROM structure and data
#define EEPROM_ADDRESS 0
struct {
boolean firstSetupDone = false;
char wifiSSID[100] = "";
char wifiPassword[100] = "";
char ACCESS_REFRESH_TOKEN[2048] = "NONE";
char ACCESS_UID[200] = "NONE";
char ACCESS_DEVICE_ID[50] = "NONE";
} EEPROMdata;
//Standard nRF24 packet structure - BEWARE the int format difference between AT328 and ESP8266!!
struct {
uint16_t type = UNAVAILABLE_VALUE;
uint16_t priority = UNAVAILABLE_VALUE;
float temperature = UNAVAILABLE_VALUE;
float humidity = UNAVAILABLE_VALUE;
float pressure = UNAVAILABLE_VALUE;
float light = UNAVAILABLE_VALUE;
boolean switchActive = false;
boolean buttonPressed = false;
boolean motionDetected = false;
} sendData;
//Ping google's DNS server
IPAddress PING_IP(8, 8, 8, 8);
//NTP setup
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_HOST, UTC_OFFSET_IN_SECONDS, NTP_REFRESH_INTERVAL);
//Global HTTPS clients to allocate fixed memory to improve performance
WiFiClientSecure clientW;
WiFiClientSecure sendClientW;
HTTPClient sendClient;
HTTPClient tokenClient;
//FTP server config
FtpServer ftpSrv;
#define FTP_USER "a"
#define FTP_PASS "a"
//Settings variables
String wifiSSID = "";
String wifiPassword = "";
char ACCESS_ID_TOKEN[2048] = "";
//Temporary counters and variables
uint8_t tokenRetries = 0;
boolean initSetupWifiDataReceived = false;
boolean wifiConnected = false;
boolean internetConnected = false;
boolean tokenConnected = false;
boolean firebaseStarted = false;
unsigned long lastPing = 0;
unsigned long lastFirebaseReload = 0;
unsigned long lastFirebaseTokenTime = 0;
unsigned long tempCounter = 0;
unsigned long lastNTPSync = 0;
//Define setup web server here and close it if possible.... should maybe find an alternative approach(?) found it, too lazy to comment ahahaha
ESP8266WebServer setupServer(80);
FirebaseArduino *firebaseInstance = new FirebaseArduino();
//Start attached nRF radio
RF24 radio(2,4);
RF24Network network(radio);
//nRF24 gateway config (this device)
const uint16_t this_node = GATEWAY_ADDRESS;
void setup() {
//Allow insecure HTTPS
clientW.setInsecure();
sendClientW.setInsecure();
clientW.setBufferSizes(4096, 4096);
sendClientW.setBufferSizes(512, 512);
sendClient.setReuse(true);
tokenClient.setReuse(true);
//Initiate serial communication
Serial.begin(115200);
//Initialize EEPROM
EEPROM.begin(3072);
//Begin SPIFFS (for saving data)
SPIFFS.begin();
//Begin FTP server (for remote SPIFFS access)
ftpSrv.begin(FTP_USER,FTP_PASS);
//load saved settings from memory
loadSettings();
//If no user data is found, enter initial setup mode
if (!EEPROMdata.firstSetupDone) {
if(DEBUG) Serial.println(EEPROMdata.wifiSSID);
firstSetupMode();
} else {
//Factory reset when holding a button on startup...
pinMode(FACTORY_RESET_BTN,INPUT_PULLUP);
if(digitalRead(FACTORY_RESET_BTN)==LOW){
if(DEBUG)Serial.println("Factory reset on boot caused by RESET BUTTON!!!");
factoryReset(true);
}
normalSetup();
}
}
//This function takes care of the initial setup
void firstSetupMode() {
if(DEBUG) Serial.println(F("ENTERING SETUP MODE!"));
//Create semi-unique hotspot name:
String apSSID = String(DEFAULT_AP_SSID) + " " + String(WiFi.macAddress());
apSSID.replace(':', ' ');
if(DEBUG) Serial.println(String(F("Created setup hotspot with SSID: "))+apSSID);
//Create setup AP
WiFi.softAP(apSSID, DEFAULT_AP_PASSWORD);
WiFi.mode(WIFI_AP);
setupServer.on("/", HTTP_GET, handleRoot);
//This shows the network ESP8266 can see, to be implemented in the Android/web app for the gateway
setupServer.on("/available-networks", HTTP_GET, []() {
if(DEBUG) Serial.println("Scanning for networks.......");
int numberOfNetworks = WiFi.scanNetworks();
String serverResponse = "{\"networks\":[";
for (int i = 0; i < numberOfNetworks; i++) {
if (i > 0)serverResponse += ",";
serverResponse += "{\"name\":\"";
serverResponse += WiFi.SSID(i);
serverResponse += "\",\"rssi\":";
serverResponse += WiFi.RSSI(i);
serverResponse += "}";
}
serverResponse += "]}";
setupServer.send(200, "text/plain", serverResponse);
});
setupServer.onNotFound(handleNotFound);
setupServer.on("/device-setup", HTTP_POST, handleSetup);
setupServer.begin();
while (!EEPROMdata.firstSetupDone && !initSetupWifiDataReceived) {
setupServer.handleClient();
}
setupServer.handleClient();
//Handle wifi data received.....
if(DEBUG) Serial.println(F("Setup received...."));
WiFi.begin(wifiSSID, wifiPassword);
uint8_t counter_wifi = 0;
while (WiFi.status() != WL_CONNECTED && counter_wifi <= MAX_WIFI_RETRIES) {
setupServer.handleClient();
if(DEBUG) Serial.println(F("Connecting to WiFi in SETUP mode....."));
delay(500);
counter_wifi++;
}
//If connection succedded, disable initial setup mode and restart
checkWifiConnection();
if (wifiConnected) {
if(DEBUG) Serial.println(F("SETUP SUCCESS!!"));
EEPROMdata.firstSetupDone = true;
saveSettings();
delay(1000);
ESP.restart();
} else {
if(DEBUG) Serial.println(F("SETUP FAILED!!"));
EEPROMdata.firstSetupDone = false;
saveSettings();
delay(1000);
ESP.restart();
}
}
//This function prepares the device for use if it has been correctly configured
void normalSetup() {
if(DEBUG) Serial.println(F("Starting normal startup...."));
//Connect to WiFi
WiFi.begin(wifiSSID, wifiPassword);
WiFi.enableAP(false);
WiFi.mode(WIFI_STA);
//Wait for connection, and if it fails after limited time enter offline mode!
uint8_t counter_wifi = 0;
while (WiFi.status() != WL_CONNECTED && counter_wifi <= MAX_WIFI_RETRIES) {
if(DEBUG) Serial.println(F("Connecting to WiFi....."));
delay(500);
counter_wifi++;
}
//Update wifi connection status
checkWifiConnection();
//Print IP address to serial if DEBUG enabled
if (wifiConnected) {
if(DEBUG) Serial.println(F("Connected to wifi!"));
//Check for internet connection on local WiFi
checkInternetConnection();
if (internetConnected) {
if(DEBUG) Serial.println(F("Internet connection is available :D"));
} else {
if(DEBUG) Serial.println(F("Internet connection is unavailable :/"));
}
}
//If internet connection was successful
if (internetConnected) {
getTokenWithRetry();
}
//Connect to firebase and subscribe to this device actions
if (tokenConnected) {
reinitFirebase();
}
timeClient.begin();
//nRF radio setup
SPI.begin();
radio.begin();
network.begin(CHANNEL, this_node);
}
void loop() {
//Loop FTP server
ftpSrv.handleFTP();
ftpSrv.handleFTP();
ftpSrv.handleFTP();
//nRF24 netowrk update state
network.update();
//Check for incoming data on the nRF24 bus and add it to queue
nRF24FromProcess();
//Send data to nRF24 from queue if available
nRF24ToProcess();
//START Network communication and stuff ------------------
//Sync time
timeClient.update();
//Loop FTP server
ftpSrv.handleFTP();
ftpSrv.handleFTP();
ftpSrv.handleFTP();
//Refresh ID token every once in a while
if (millis() > lastFirebaseTokenTime + FIREBASE_FORCE_TOKEN_RELOAD_INTERVAL && internetConnected && wifiConnected) {
getTokenWithRetry();
if (!tokenConnected && internetConnected) {
if(DEBUG) Serial.println(F("Unresolvable problem with firebase! User account has probably changed. Initiating factory reset...."));
factoryReset(true);
}
}
//Check WiFi and Internet connection
pingNetwork(false);
//Request new token if needed
if (internetConnected && !tokenConnected) {
getTokenWithRetry();
}
//Reconnect to firebase if needed
if (tokenConnected && !firebaseStarted) {
reinitFirebase();
}
//Check for data from firebase
checkFirebaseData();
//END Netowrk communication and stuff ---------------------
//Loop FTP server
ftpSrv.handleFTP();
ftpSrv.handleFTP();
ftpSrv.handleFTP();
//If full connection to the database is available receive data from it and send from queue to it
if(firebaseStarted){
//Send data from outgoing queue to database
databaseToProcess();
}
}
//Handle setup POST request and update data in memory
void handleSetup() {
//If request is not formatted correctly, refuse it....
if (setupServer.hasArg("wifiSSID") && setupServer.hasArg("wifiPassword")) {
if(DEBUG) Serial.println("Setting up....");
if (setupServer.hasArg("ACCESS_REFRESH_TOKEN") && setupServer.hasArg("ACCESS_DEVICE_ID") && setupServer.hasArg("ACCESS_UID")) {
setupServer.send(200, "text/plain", "SETUP-FULL");
//Accept correct request
wifiSSID = setupServer.arg("wifiSSID");
wifiPassword = setupServer.arg("wifiPassword");
String(ACCESS_TOKEN_POST_STRING_BEGIN + setupServer.arg("ACCESS_REFRESH_TOKEN") + ACCESS_TOKEN_POST_STRING_END).toCharArray(EEPROMdata.ACCESS_REFRESH_TOKEN, 2048);
setupServer.arg("ACCESS_DEVICE_ID").toCharArray(EEPROMdata.ACCESS_DEVICE_ID, 50);
setupServer.arg("ACCESS_UID").toCharArray(EEPROMdata.ACCESS_UID, 200);
if(DEBUG) Serial.println("Setup full");
saveSettings();
//Mark received settings
initSetupWifiDataReceived = true;
} else {
if (String(EEPROMdata.ACCESS_REFRESH_TOKEN) == "NONE") {
setupServer.send(403, "text/plain", "ERROR-WIFI-NO-SAVED-CREDENTIALS");
return;
}
setupServer.send(200, "text/plain", "SETUP-WIFI");
//Mark received settings
initSetupWifiDataReceived = true;
//Accept correct request
wifiSSID = setupServer.arg("wifiSSID");
wifiPassword = setupServer.arg("wifiPassword");
if(DEBUG) Serial.println("Setup wifi only");
}
} else {
setupServer.send(400, "text/plain", "400: Invalid Request");
return;
}
}
//This function tries to get new id token from google auth server.
//If it fails with active internet connection that means user account has changed and new initial setup is required.
boolean getTokenWithRetry() {
boolean gotToken = false;
for (uint8_t i = 0; i < MAX_TOKEN_RETRIES; i++) {
if (requestNewToken()) {
gotToken = true;
lastFirebaseTokenTime = millis();
break;
}
pingNetwork(true);
if (!internetConnected) {
firebaseStarted = false;
gotToken = false;
break;
}
delay(200);
}
if (gotToken) {
if(DEBUG) Serial.println(F("Successfully got connection token!"));
} else {
if(DEBUG) Serial.println(F("Successfully got connection token!"));
}
tokenConnected = gotToken;
return gotToken;
}
//Function to get new ID token from the constant refresh token once the old one has expired
boolean requestNewToken() {
if(DEBUG) Serial.println(F("Requesting new id token....."));
boolean tokenSuccess = false; //Default to failed state
firebaseInstance->endStream();
firebaseStarted = false;
//Try to connect to Firebase auth server
if (tokenClient.begin(clientW, ACCESS_REFRESH_URL)) {
if(DEBUG) Serial.println(F("Established connection to google auth server"));
int httpCode = tokenClient.POST(String(EEPROMdata.ACCESS_REFRESH_TOKEN));
if(DEBUG) Serial.println(String(F("httpCode: "))+String(httpCode));
//If POST was successful, check HTTP status code and if token was granted store it
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
if(DEBUG) Serial.println(String(F("Google auth server responded with code: ")) + String(httpCode));
// if token has been sent, update it!
if (httpCode == 200) {
tokenRetries = 0;
if(DEBUG) Serial.println(F("SUCCESS! Got new token!"));
getTokenFromResponse(tokenClient, "id_token\": \"", '"');
//Update token in firebase library
firebaseInstance->updateAuth(ACCESS_ID_TOKEN);
//Mark successfull token extraction
tokenSuccess = true;
} else if (httpCode == 400) {
if(DEBUG) Serial.println("CODE 400 restart....");
ESP.restart();
} else {
if(DEBUG) Serial.println(F("POST did not return 200 SUCCESS, probably authentication problem?"));
}
} else {
if(DEBUG) Serial.println(F("POST to google auth server failed!"));
}
tokenClient.end();
} else {
if(DEBUG) Serial.println(F("Unable to connect to google auth server!"));
}
return tokenSuccess;
}
//This function resets all settings to factory defaults
void resetSettings(boolean full) {
EEPROMdata.firstSetupDone = false;
wifiSSID = "";
wifiPassword = "";
ACCESS_ID_TOKEN[0] = (char)0;
//This part removes the unique device ID and user identification...
if (full) {
String("NONE").toCharArray(EEPROMdata.ACCESS_REFRESH_TOKEN, 2048);
String("NONE").toCharArray(EEPROMdata.ACCESS_UID, 200);
String("NONE").toCharArray(EEPROMdata.ACCESS_DEVICE_ID, 50);
}
saveSettings();
}
//This part should load settings from memory
void loadSettings() {
EEPROM.get(EEPROM_ADDRESS, EEPROMdata);
//User data
wifiSSID = EEPROMdata.wifiSSID;
wifiPassword = EEPROMdata.wifiPassword;
if(DEBUG) Serial.println("Loaded settings from EEPROM....");
}
//This part should save settings to memory
void saveSettings() {
if(DEBUG) Serial.println(F("Saving settings to EEPROM...."));
//User data
wifiSSID.toCharArray(EEPROMdata.wifiSSID, 100);
wifiPassword.toCharArray(EEPROMdata.wifiPassword, 100);
EEPROM.put(EEPROM_ADDRESS, EEPROMdata);
EEPROM.commit();
}
//Extract tokens and uid from HTTP response. This is a piece of code that somehow finnaly works and we do not want to mess with it!
void getTokenFromResponse(HTTPClient &http, String key, char terminate) {
bool found = false, look = false;
int ind = 0;
int tokenCurrentPos = 0;
int len = http.getSize();
char char_buff[1];
WiFiClient * stream = http.getStreamPtr();
while (http.connected() && (len > 0 || len == -1)) {
size_t size = stream->available();
if (size) {
int c = stream->readBytes(char_buff, ((size > sizeof(char_buff)) ? sizeof(char_buff) : size));
if (len > 0)
len -= c;
if (found) {
if (char_buff[0] == terminate) {
break;
} else {
ACCESS_ID_TOKEN[tokenCurrentPos] = char_buff[0];
tokenCurrentPos++;
}
}
else if ((!look) && (char_buff[0] == key[0])) {
look = true;
ind = 1;
} else if (look && (char_buff[0] == key[ind])) {
ind ++;
if (ind == key.length()) found = true;
} else if (look && (char_buff[0] != key[ind])) {
ind = 0;
look = false;
}
}
}
ACCESS_ID_TOKEN[tokenCurrentPos] = 0; //Terminate token string
return;
}
//Return formatted string from supplied epoch time
String getTimeString(unsigned long epochTime) {
time_t rawtime = epochTime;
struct tm ts;
char buf[80];
ts = *localtime(&rawtime);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
return String(buf);
}
//Handle root GET request
void handleRoot() {
setupServer.send(200, "text/html", "Please send user credentials and wifi data to the gateway.");
}
//Handle wrong requests
void handleNotFound() {
setupServer.send(404, "text/plain", "404: Not found");
}
void checkWifiConnection() {
wifiConnected = (WiFi.status() == WL_CONNECTED ) ? true : false;
}
void checkInternetConnection() {
if (Ping.ping(PING_IP, 1)) {
internetConnected = true;
} else {
internetConnected = false;
}
}
//Check for updated data from firebase
void checkFirebaseData() {
//Reinitialize firebase connection periodically
if (millis() > lastFirebaseReload + FIREBASE_STREAM_RELOAD_INTERVAL) {
lastFirebaseReload = millis();
firebaseStarted = false;
}
if (tokenConnected && firebaseStarted == false) {
reinitFirebase();
}
//Check for error
if (firebaseInstance->failed()) {
if(DEBUG) Serial.println(String(F("Firebase error: ")) + firebaseInstance->error());
checkWifiConnection();
if (wifiConnected) {
checkInternetConnection();
if (internetConnected) {
getTokenWithRetry();
if (tokenConnected) {
if(DEBUG) Serial.println(F("Restored token connection!"));
reinitFirebase();
if (firebaseStarted) {
if(DEBUG) Serial.println(F("Restored firebase connection!"));
} else {
if(DEBUG) Serial.println(F("FAILED to restore firebase connection!"));
}
}
} else {
if(DEBUG) Serial.println(F("Lost internet connection!"));
tokenConnected = false;
}
} else {
if(DEBUG) Serial.println(F("Lost WiFi connection!"));
internetConnected = false;
tokenConnected = false;
}
delay(500);
}
//When new data is available....
if (firebaseInstance->available()) {
FirebaseObject event = firebaseInstance->readEvent();
String eventType = event.getString("type");
eventType.toLowerCase();
if(DEBUG) Serial.println("Firebase event: " + eventType);
//If new data is written to db, process it
if (eventType == "put") {
String path = event.getString("path");
if(DEBUG) Serial.println("Firebase path: " + path);
//More than one packet is incomming...
if (path == "/") {
for(int i = 1; i<=MAX_DEVICES; i++){
String currentdata = event.getString("data/"+String(i));
Serial.println(String("Data for address ")+String(i)+":"+currentdata);
if(currentdata!="NOT-STRING"){
addRecordToFile(String(i)+"-"+splitString(currentdata,':',0),splitString(currentdata,':',1).toInt(),false);
}
}
deleteFirebaseTree();
} else {
String data = event.getString("data");
if (data == "NOT-STRING") {
if(DEBUG) Serial.println(String(F("ERROR PARSING DATA!!!! WRONG FORMAT AT PATH: ")) + path);
} else {
addRecordToFile(splitString(path,'/',1)+"-"+splitString(data,':',0),splitString(data,':',1).toInt(),false);
deleteFirebaseString(path);
}
}
}
}
}
//This resets the ESP8266 to factory setting and it enters setup mode
void factoryReset(boolean full) {
if(DEBUG) Serial.println(F("Initiating factory reset!!!!!"));
resetSettings(full);
ESP.restart();
}
//This reinitialises firebase stream and stuff
void reinitFirebase() {
firebaseInstance->endStream();
for (uint8_t i = 0; i < MAX_FIREBASE_RETRIES; i++) {
if(DEBUG) Serial.println(String(F("Heap before firebase:"))+String(ESP.getFreeHeap()));
if(DEBUG) Serial.println(F("INIT/REINIT firebase connection"));
firebaseInstance->begin(FIREBASE_URL, String(ACCESS_ID_TOKEN));
if(DEBUG) Serial.println(String(F("Heap before stream but after begin:"))+String(ESP.getFreeHeap()));
firebaseInstance->stream(String(FIREBASE_ROOT_PATH) + "/" + String(EEPROMdata.ACCESS_UID) + "/" + String(DEVICE_TYPE) + "/" + String(EEPROMdata.ACCESS_DEVICE_ID) + String(FIREBASE_DOWNSTREAM_PATH));
long startTime = millis();
while (millis() <= startTime + 1000 && !firebaseInstance->success());
if(DEBUG) Serial.println(String(F("Heap after firebase:"))+String(ESP.getFreeHeap()));
if (firebaseInstance->success()) {
firebaseStarted = true;
if(DEBUG) Serial.println(F("SUCCESS! Firebase reinitialised :D"));
break;
} else {
firebaseStarted = false;
if(DEBUG) Serial.println(F("FAIL! Firebase reinit failed!"));
}
}
}
//This function checks network conectivity
void pingNetwork(boolean force) {
if (millis() > lastPing + PING_INTERVAL || force) {
lastPing = millis();
checkWifiConnection();
if (!wifiConnected) {
if(DEBUG) Serial.println(F("PING determined no wifi connection!"));
wifiConnected = false;
internetConnected = false;
tokenConnected = false;
firebaseStarted = false;
return;
}
checkInternetConnection();
if (!internetConnected) {
if(DEBUG) Serial.println(F("No network! Mark firebase as offline!"));
firebaseStarted = false;
tokenConnected = false;
}
}
}
void databaseToProcess(){
//Check for data to be sent to WiFi bus, send one by one....
String path;
String data;
String filedata;
int counter = 0;
//Send priority 1
while(filedata = readRecordFromFile(PRIORITY_HIGH,true),filedata != NO_FILE){
if(DEBUG)Serial.println("Priority 1 for FB: "+filedata);
path = splitString(filedata,'-',0);
data = splitString(filedata,'-',1);
sendFirebaseStringNoDataFormatting(path,data);
counter++;
if(counter>MAX_CONSECUTIVE_SENDS) return;
}
while(filedata = readRecordFromFile(PRIORITY_NORMAL,true),filedata != NO_FILE){
if(DEBUG)Serial.println("Priority 2 for FB: "+filedata);
path = splitString(filedata,'-',0);
data = splitString(filedata,'-',1);
sendFirebaseStringNoDataFormatting(path,data);
counter++;
if(counter>MAX_CONSECUTIVE_SENDS) return;
}
while(filedata = readRecordFromFile(PRIORITY_LOW,true),filedata != NO_FILE){
if(DEBUG)Serial.println("Priority 3 for FB: "+filedata);
path = splitString(filedata,'-',0);
data = splitString(filedata,'-',1);
sendFirebaseStringNoDataFormatting(path,data);
counter++;
if(counter>MAX_CONSECUTIVE_SENDS) return;
}
}
void nRF24ToProcess(){ //Data for nRF24 in format {address}-{int_value}
String data;
int address;
int value;
//Send instant priority
while(data = readRecordFromFile(PRIORITY_INSTANT_ONLY,false),data != NO_FILE){
address = splitString(data,'-',0).toInt();
value = splitString(data,'-',1).toInt();
nRF24Send(address,value);
}
//Send priority 3
while(data = readRecordFromFile(PRIORITY_HIGH,false),data != NO_FILE){
address = splitString(data,'-',0).toInt();
value = splitString(data,'-',1).toInt();
if(!nRF24Send(address,value)){
addRecordToFile(data,PRIORITY_HIGH,false);
break;
}
}
//Send priority 2
while(data = readRecordFromFile(PRIORITY_NORMAL,false),data != NO_FILE){
address = splitString(data,'-',0).toInt();
value = splitString(data,'-',1).toInt();
if(!nRF24Send(address,value)){
addRecordToFile(data,PRIORITY_NORMAL,false);
Serial.print("BEFORE");
Serial.print(data);
Serial.print("AFTER");
break;
}
}
//Send priority 1
while(data = readRecordFromFile(PRIORITY_LOW,false),data != NO_FILE){
address = splitString(data,'-',0).toInt();
value = splitString(data,'-',1).toInt();
nRF24Send(address,value);
if(!nRF24Send(address,value)){
addRecordToFile(data,PRIORITY_LOW,false);
break;
}
}
}
void nRF24FromProcess(){
//If data is available process it...
if(network.available()) {
RF24NetworkHeader header;
network.read(header,&sendData,sizeof(sendData));
//DEBUG prints....
if(DEBUG){
Serial.print("Received type :");
Serial.print(sendData.type);
Serial.print(", prio: ");
Serial.print(sendData.priority);
Serial.print(", temp: ");
Serial.print(sendData.temperature);
Serial.print(", hum: ");
Serial.print(sendData.humidity);
Serial.print(", pres: ");
Serial.print(sendData.pressure);
Serial.print(", lig: ");
Serial.print(sendData.light);
Serial.print(", SW: ");
Serial.print(sendData.switchActive);
Serial.print(", BTN: ");
Serial.print(sendData.buttonPressed);
Serial.print(", MOT: ");
Serial.print(sendData.motionDetected);
Serial.print(", from: ");
Serial.println(header.from_node);
}
int priority = sendData.priority;
int from_address=header.from_node;
//Read data and calculate priority
String data = processnRF24Data(from_address);
//Add message to outgoing queue
if(priority == PRIORITY_HIGH || priority == PRIORITY_NORMAL || priority == PRIORITY_LOW){
if(DEBUG)Serial.println("HIGH/NORMAL/LOW PRIORITY");
addRecordToFile(data,priority,true); //toFirebase = true
}else if(priority == PRIORITY_INSTANT_ONLY){
if(firebaseStarted){
if(DEBUG)Serial.println("PRIORITY INSTANT ONLY, SENDING INSTANTLY!!!");
String path = splitString(data,'-',0);
String datasend = splitString(data,'-',1);
sendFirebaseStringNoDataFormatting(path,datasend);
}else{
if(DEBUG)Serial.println("PRIORITY INSTANT ONLY, BUT NO NETWORK, IGNORING!!!");
}
}else{
if(DEBUG)Serial.println("ERROR: UNKNOWN PRIORITY");
}
}
}
String processnRF24Data(int from_address){
int type = sendData.type;
boolean awaitingZarez = false;
String data = "NODE";
data+=String(from_address)+"/"+String(timeClient.getEpochTime())+"-{";
if(type == TYPE_DHT11 || type == TYPE_TEMPERATURE || type == TYPE_BMP280){
if(awaitingZarez){
data+=",";
awaitingZarez=false;
}
data+="\"temperature\":"+String(sendData.temperature);
awaitingZarez = true;
}
if(type == TYPE_DHT11 || type == TYPE_HUMIDITY){
if(awaitingZarez){
data+=",";
awaitingZarez=false;
}
data+="\"humidity\":"+String(sendData.humidity);
awaitingZarez = true;
}
if(type == TYPE_BMP280){
if(awaitingZarez){
data+=",";
awaitingZarez=false;
}
data+="\"pressure\":"+String(sendData.pressure);
awaitingZarez = true;
}
if(type == TYPE_LIGHT){
if(awaitingZarez){
data+=",";
awaitingZarez=false;
}
data+="\"light\":"+String(sendData.light);
awaitingZarez = true;
}
if(type == TYPE_SWITCH){
if(awaitingZarez){
data+=",";
awaitingZarez=false;
}
data+="\"switchActive\":"+String(sendData.switchActive?"true":"false");
awaitingZarez = true;
}
if(type == TYPE_BUTTON || type == TYPE_LIGHT_BTN){
if(awaitingZarez){
data+=",";
awaitingZarez=false;
}
data+="\"buttonPressed\":"+String(sendData.buttonPressed?"true":"false");
awaitingZarez = true;
}
if(type == TYPE_MOTION){
if(awaitingZarez){
data+=",";
awaitingZarez=false;
}
data+="\"motionDetected\":"+String(sendData.motionDetected?"true":"false");
awaitingZarez = true;
}
data+="}";
return data;
}
String databaseRead(int* priority_address){
//Read data from database
//Calculate data priority
//Convert to String in format: [priority] [device_id] [timestamp] [data]
priority_address = 0;
return "";
}
boolean nRF24Send(int device_id, int data){//ONLY ALLOW SENDING OF INTS TO NODES!!
//Send data to device
RF24NetworkHeader header(device_id);
bool ok = network.write(header,&data,sizeof(data));
if(DEBUG){
if (ok)
Serial.println("Send data to nRF24 ok:"+data);
else
Serial.println("Send data to nRF24 failed.");
}
return ok;
}
//START Firebase helper functions
void sendFirebaseString(String path, String data) {
sendFirebaseDataHelper(String(FIREBASE_UPSTREAM_PATH) + "/" + path, "\"" + data + "\"");
}
void sendFirebaseStringNoDataFormatting(String path, String data) {
sendFirebaseDataHelper(String(FIREBASE_UPSTREAM_PATH) + "/" + path, data);
}
void sendFirebaseInt(String path, int data) {
sendFirebaseDataHelper(String(FIREBASE_UPSTREAM_PATH) + "/" + path, String(data));
}
void deleteFirebaseString(String path) {
sendFirebaseDataHelper(String(FIREBASE_DOWNSTREAM_PATH) + "/" + path, "null");
}
void deleteFirebaseTree() {
sendFirebaseDataHelper(String(FIREBASE_DOWNSTREAM_PATH), "null");
}
void sendFirebaseDataHelper(String path, String data) {
if(DEBUG) Serial.print(String(F("Sending to: "))+path+", data: "+data+".......");
if (sendClient.begin(sendClientW, String("https://") + String(FIREBASE_URL) + String(FIREBASE_ROOT_PATH) + "/" + String(EEPROMdata.ACCESS_UID) + "/" + String(DEVICE_TYPE) + "/" + String(EEPROMdata.ACCESS_DEVICE_ID) + path + ".json?auth=" + String(ACCESS_ID_TOKEN))) {
sendClient.addHeader("Content-Type", "text/plain");
//sendClient.addHeader("Content-Type", "application/json");
sendClient.addHeader("Connection", "close");
sendClient.setTimeout(HTTP_TIMEOUT * 1000);
if(DEBUG) Serial.println(String(F("just before PUT! HEAP: "))+String(ESP.getFreeHeap()));
int httpCode = sendClient.PUT(data);
if (httpCode == 200) {
if(DEBUG) Serial.println(F("SUCCESS"));
} else if (httpCode > 0) {
if(DEBUG) Serial.println(String(F("ERROR.... remote server problem(?), HTTPCODE: "))+String(httpCode));
} else {
if(DEBUG) Serial.println(F("ERROR... network problem(?)"));