-
Notifications
You must be signed in to change notification settings - Fork 35
/
IOTAppStory.cpp
1610 lines (1253 loc) · 38.8 KB
/
IOTAppStory.cpp
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 "IOTAppStory.h"
IOTAppStory::IOTAppStory(const char *compDate, const int modeButton)
: _compDate(compDate)
, _modeButton(modeButton)
{
#if DEBUG_LVL >= 1
Serial.begin(SERIAL_SPEED);
while (!Serial){
delay(10);
}
DEBUG_PRINT(F("\n\n\n\n\n"));
#endif
}
/**
THIS ONLY RUNS ON THE FIRST BOOT OF A JUST INSTALLED APP (OR AFTER RESET TO DEFAULT SETTINGS)
*/
void IOTAppStory::firstBoot(char ea){
// erase eeprom after config (delete extra field data etc.)
if(ea == 'F'){
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(SER_ERASE_FULL);
#endif
// Wipe out WiFi credentials.
WiFi.disconnect();
delay(200);
// erase full eeprom
eraseFlash(0,EEPROM_SIZE);
String emty = F("000000");
emty.toCharArray(config.actCode, 7);
emty = "";
emty.toCharArray(config.ssid[0], STRUCT_CHAR_ARRAY_SIZE);
emty.toCharArray(config.password[0], STRUCT_PASSWORD_SIZE);
emty.toCharArray(config.ssid[1], STRUCT_CHAR_ARRAY_SIZE);
emty.toCharArray(config.password[1], STRUCT_PASSWORD_SIZE);
emty.toCharArray(config.ssid[2], STRUCT_CHAR_ARRAY_SIZE);
emty.toCharArray(config.password[2], STRUCT_PASSWORD_SIZE);
}else if(ea == 'P'){
#if DEBUG_LVL == 1
DEBUG_PRINTLN(SER_ERASE_PART);
#endif
#if DEBUG_LVL >= 2
DEBUG_PRINTLN(SER_ERASE_PART_EXT);
#endif
// erase eeprom but leave the config settings
eraseFlash((sizeof(config)+2),EEPROM_SIZE);
}
#if DEBUG_LVL >= 1
else{
DEBUG_PRINTLN(SER_ERASE_NONE);
}
#endif
boardMode = 'N';
bootTimes = 0;
boardInfo boardInfo(bootTimes, boardMode);
boardInfo.write();
// update first boot config flag (date)
strcpy(config.compDate, _compDate);
writeConfig();
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(FPSTR(SER_DEV));
#endif
if (_firstBootCallback){
#if DEBUG_LVL >= 3
DEBUG_PRINTLN(SER_CALLBACK_FIRST_BOOT);
#endif
_firstBootCallback();
}
}
void IOTAppStory::preSetAppName(String appName){
if (!_configReaded) {
readConfig();
}
_setDeviceName = true;
SetConfigValueCharArray(config.appName, appName, 33, _setPreSet);
}
void IOTAppStory::preSetAppVersion(String appVersion){
if (!_configReaded) {
readConfig();
}
_setDeviceName = true;
SetConfigValueCharArray(config.appVersion, appVersion, 11, _setPreSet);
}
void IOTAppStory::preSetDeviceName(String deviceName){
if (!_configReaded) {
readConfig();
}
_setDeviceName = true;
SetConfigValueCharArray(config.deviceName, deviceName, STRUCT_BNAME_SIZE, _setPreSet);
}
void IOTAppStory::preSetAutoUpdate(bool automaticUpdate){
_updateOnBoot = automaticUpdate;
}
void IOTAppStory::preSetAutoConfig(bool automaticConfig){
_automaticConfig = automaticConfig;
}
void IOTAppStory::preSetWifi(String ssid, String password){
if (!_configReaded) {
readConfig();
}
_setPreSet = true;
SetConfigValueCharArray(config.ssid[0], ssid, STRUCT_CHAR_ARRAY_SIZE, _setPreSet);
SetConfigValueCharArray(config.password[0], password, STRUCT_PASSWORD_SIZE, _setPreSet);
}
/*
void IOTAppStory::preSetServer(String HOST1, String FILE1){
if (!_configReaded) {
readConfig();
}
SetConfigValueCharArray(config.HOST1, HOST1, STRUCT_CHAR_ARRAY_SIZE, _setPreSet);
SetConfigValueCharArray(config.FILE1, FILE1, STRUCT_CHAR_ARRAY_SIZE, _setPreSet);
}
*/
void IOTAppStory::setCallHome(bool callHome) {
_callHome = callHome;
}
void IOTAppStory::setCallHomeInterval(unsigned long interval) {
_callHomeInterval = interval * 1000; //Convert to millis so users can pass seconds to this function
}
void IOTAppStory::begin(char ea){
// if deviceName is not set, set it to the appName
if(_setDeviceName == false){
preSetDeviceName("yourESP");
}
// read config if needed
if (!_configReaded) {
readConfig();
}
// write config if detected changes
if(_setPreSet == true){
writeConfig();
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(SER_SAVE_CONFIG);
#endif
}
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(FPSTR(SER_DEV));
DEBUG_PRINT(SER_START);
DEBUG_PRINT(config.appName);
DEBUG_PRINT(F(" v"));
DEBUG_PRINTLN(config.appVersion);
#endif
#if DEBUG_LVL >= 2
DEBUG_PRINTLN(FPSTR(SER_DEV));
DEBUG_PRINTF_P(SER_MODE_SEL_BTN, _modeButton, config.deviceName, _updateOnBoot);
#endif
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(FPSTR(SER_DEV));
#endif
// set the input pin for Config/Update mode selection
pinMode(_modeButton, INPUT_PULLUP);
// Read the "bootTime" & "boardMode" from the Non-volatile storage on ESP32 processor
//readPref();
boardInfo boardInfo(bootTimes, boardMode);
boardInfo.read();
// on first boot of the app run the firstBoot() function
if(strcmp(config.compDate,_compDate) != 0){
firstBoot(ea);
}
// BOOT STATISTICS read and increase boot statistics (optional)
#if BOOTSTATISTICS == true && DEBUG_LVL >= 1
bootTimes++;
//writePref();
boardInfo.write();
#if DEBUG_LVL >= 1
printBoardInfo();
#endif
#endif
// process added fields
processField();
// --------- START WIFI --------------------------
connectNetwork();
//---------- SELECT BOARD MODE -----------------------------
#if INC_CONFIG == true
if(boardMode == 'C'){
runConfigServer();
}
#endif
// --------- READ FULL CONFIG --------------------------
//readConfig();
// --------- if automaticUpdate Update --------------------------
if(_updateOnBoot == true){
callHome();
}
_buttonEntry = millis() + MODE_BUTTON_VERY_LONG_PRESS; // make sure the timedifference during startup is bigger than 10 sec. Otherwise it will go either in config mode or calls home
_appState = AppStateNoPress;
// ----------- END SPECIFIC SETUP CODE ----------------------------
#if DEBUG_LVL >= 1
DEBUG_PRINT(F("\n\n\n\n\n"));
#endif
/* */
}
#if DEBUG_LVL >= 1
/** print BoardInfo */
void IOTAppStory::printBoardInfo(){
DEBUG_PRINTF_P(SER_BOOTTIMES_UPDATE, bootTimes, boardMode);
DEBUG_PRINTLN(FPSTR(SER_DEV));
}
#endif
/** send msg to iasLog */
void IOTAppStory::iasLog(String msg) {
// notifi IAS & enduser about the localIP
String url = F("https://");
url += _updateHost;
url += F("/ota/cfg-sta.php");
url += "?msg=";
url += msg;
HTTPClient http;
httpClientSetup(http, url, false);
#if DEBUG_LVL >= 3
DEBUG_PRINTLN(SER_UPDATE_IASLOG);
#endif
if(http.GET() != HTTP_CODE_OK){
#if DEBUG_LVL >= 3
DEBUG_PRINTLN(SER_FAILED_COLON);
DEBUG_PRINTLN(http.getString());
#endif
}
http.end();
}
/** config server */
void IOTAppStory::runConfigServer() {
bool exitConfig = false;
// callback entered config mode
if (_configModeCallback){
_configModeCallback();
}
#if DEBUG_LVL >= 1
DEBUG_PRINT(SER_CONFIG_MODE);
#endif
if(WiFi.status() != WL_CONNECTED){
// when there is no wifi setup server in AP mode
IPAddress apIP(192, 168, 4, 1);
dnsServer.reset(new DNSServer());
server.reset(new AsyncWebServer(80));
WiFi.mode(WIFI_AP_STA);
#if SMARTCONFIG == true
WiFi.beginSmartConfig();
#endif
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(config.deviceName);
dnsServer->start(DNS_PORT, "*", apIP);
#if DEBUG_LVL >= 2
DEBUG_PRINTF_P(SER_CONFIG_AP_MODE, config.deviceName);
#endif
}else{
// notifi IAS & enduser this device went to config mode (also sends localIP)
iasLog("1");
// when there is wifi setup server in STA mode
server.reset(new AsyncWebServer(80));
WiFi.mode(WIFI_STA);
#if DEBUG_LVL >= 2
DEBUG_PRINT(SER_CONFIG_STA_MODE);
DEBUG_PRINTLN(WiFi.localIP());
#endif
}
server->on("/", HTTP_GET, [&](AsyncWebServerRequest *request){ servHdlRoot(request); });
server->on("/i", HTTP_GET, [&](AsyncWebServerRequest *request){ servHdlDevInfo(request); });
#if defined ESP8266
server->on("/fp", HTTP_POST, [&](AsyncWebServerRequest *request){ servHdlFngPrintSave(request); });
#endif
server->on("/wsc", HTTP_GET, [&](AsyncWebServerRequest *request){ servHdlWifiScan(request); });
server->on("/wsa", HTTP_POST, [&](AsyncWebServerRequest *request){ servHdlWifiSave(request); });
server->on("/app", HTTP_GET, [&](AsyncWebServerRequest *request){ servHdlAppInfo(request); });
server->on("/as", HTTP_POST, [&](AsyncWebServerRequest *request){ servHdlAppSave(request); });
server->on("/ds", HTTP_POST, [&](AsyncWebServerRequest *request){
#if DEBUG_LVL >= 3
DEBUG_PRINT(SER_REC_ACT_CODE);
#endif
String retHtml = F("0");
if(request->hasParam("ac", true)){
request->getParam("ac", true)->value().toCharArray(config.actCode, 7); //???
if(!_confirmed && !_tryToConf){
_tryToConf = true;
retHtml = F("1");
}
}
hdlReturn(request, retHtml);
});
server->on("/close", HTTP_GET, [&](AsyncWebServerRequest *request){ exitConfig = true; });
// start the server
server->begin();
// config server loop
while(exitConfig == false){
yield();
if(WiFi.status() != WL_CONNECTED){
//DNS
dnsServer->processNextRequest();
// smartconfig default false / off
#if SMARTCONFIG == true
if(WiFi.smartConfigDone()){
WiFi.mode(WIFI_AP_STA);
isNetworkConnected();
}
#endif
// wifi connect when asked
if(_tryToConn == true){
#if DEBUG_LVL >= 3
DEBUG_PRINT(SER_REC_CREDENTIALS);
DEBUG_PRINT(config.ssid[0]);
DEBUG_PRINT(F(" - "));
DEBUG_PRINTLN(config.password[0]);
#endif
WiFi.begin(config.ssid[0], config.password[0]);
_connected = isNetworkConnected(false);
yield();
if(_connected){
// Saving config to eeprom
_writeConfig = true;
#if DEBUG_LVL >= 3
DEBUG_PRINT(SER_CONN_SAVE_EEPROM);
#endif
}else{
readConfig();
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(SER_FAILED_TRYAGAIN);
#endif
_tryToConnFail = true;
}
_tryToConn = false;
}
}else{
// write actCode to EEPROM
if(!_confirmed && _tryToConf){
_confirmed = true;
_tryToConf = false;
_writeConfig = true;
}
// write EEPROM
if(_writeConfig){
writeConfig();
yield();
_writeConfig = false;
}
// when succesfully added wifi cred in AP mode change to STA mode
if(_changeMode){
delay(1000);
WiFi.mode(WIFI_STA);
delay(100);
_changeMode = false;
// notifi IAS & enduser this device went to config mode (also sends localIP)
iasLog("1");
#if DEBUG_LVL >= 2
//DEBUG_PRINTF_P(PSTR(" \n Changed to STA mode. Open %s\n"), WiFi.localIP().toString());
DEBUG_PRINT(SER_CONFIG_STA_MODE_CHANGE);
DEBUG_PRINTLN(WiFi.localIP());
DEBUG_PRINTLN();
#endif
}
}
}
#if DEBUG_LVL >= 2
DEBUG_PRINTLN(SER_CONFIG_EXIT);
#endif
// notifi IAS & enduser this device has left config mode (also sends localIP)
iasLog("0");
// Return to Normal Operation
espRestart('N');
}
/** Connect to Wifi AP */
void IOTAppStory::connectNetwork() {
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(SER_CONNECTING);
#endif
#if WIFI_MULTI == true
wifiMulti.addAP(config.ssid[0], config.password[0]);
wifiMulti.addAP(config.ssid[1], config.password[1]);
wifiMulti.addAP(config.ssid[2], config.password[2]);
#else
WiFi.begin(config.ssid[0], config.password[0]);
#endif
if(!isNetworkConnected()) {
if(_automaticConfig || boardMode == 'C'){
if(boardMode == 'N'){
boardMode = 'C';
//writePref();
boardInfo boardInfo(bootTimes, boardMode);
boardInfo.write();
}
#if DEBUG_LVL >= 1
DEBUG_PRINT(SER_CONN_NONE_GO_CFG);
#endif
}else{
#if DEBUG_LVL >= 1
// this point is only reached if _automaticConfig = false
DEBUG_PRINT(SER_CONN_NONE_CONTINU);
#endif
}
}else{
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(SER_CONNECTED);
#endif
#if DEBUG_LVL >= 2
DEBUG_PRINT(SER_DEV_MAC);
DEBUG_PRINTLN(WiFi.macAddress());
#endif
#if DEBUG_LVL >= 1
DEBUG_PRINT(SER_DEV_IP);
DEBUG_PRINTLN(WiFi.localIP());
#endif
#if USEMDNS == true
// Register host name in WiFi and mDNS
String hostNameWifi = config.deviceName;
hostNameWifi += ".local";
if(MDNS.begin(config.deviceName)){
#if DEBUG_LVL >= 1
DEBUG_PRINT(SER_DEV_MDNS);
DEBUG_PRINT(hostNameWifi);
#endif
#if DEBUG_LVL >= 2
DEBUG_PRINTLN(SER_DEV_MDNS_INFO);
#endif
#if DEBUG_LVL == 1
DEBUG_PRINTLN(F(""));
#endif
}else{
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(SER_DEV_MDNS_FAIL);
#endif
}
#endif
}
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(FPSTR(SER_DEV));
#endif
}
/**
Wait until network is connected.
Returns false if not connected after MAX_WIFI_RETRIES retries
*/
bool IOTAppStory::isNetworkConnected(bool multi) {
#if defined ESP8266
int retries = MAX_WIFI_RETRIES;
#elif defined ESP32
int retries = (MAX_WIFI_RETRIES/2);
#endif
#if DEBUG_LVL >= 1
DEBUG_PRINT(F(" "));
#endif
#if WIFI_MULTI == true
if(multi){
while (wifiMulti.run() != WL_CONNECTED && retries-- > 0 ) {
delay(500);
#if DEBUG_LVL >= 1
DEBUG_PRINT(F("."));
#endif
}
}else{
#endif
while (WiFi.status() != WL_CONNECTED && retries-- > 0 ) {
delay(500);
#if DEBUG_LVL >= 1
DEBUG_PRINT(F("."));
#endif
}
#if WIFI_MULTI == true
}
#endif
if(retries > 0){
return true;
}else{
return false;
}
}
/**
call home and check for updates
*/
void IOTAppStory::callHome(bool spiffs /*= true*/) {
// update from IOTappStory.com
#if DEBUG_LVL >= 2
DEBUG_PRINTLN(SER_CALLING_HOME);
#endif
if (_firmwareUpdateCheckCallback){
_firmwareUpdateCheckCallback();
}
// try to update from IOTAppStory
iotUpdater(0);
// try to update spiffs from IOTAppStory
if(spiffs){
iotUpdater(1);
}
#if DEBUG_LVL >= 2
DEBUG_PRINTLN(SER_RET_FROM_IAS);
#endif
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(FPSTR(SER_DEV));
#endif
}
/**
IOT updater
*/
void IOTAppStory::iotUpdater(bool spiffs) {
String url = "";
#if DEBUG_LVL >= 2
DEBUG_PRINT(F("\n"));
#endif
#if DEBUG_LVL >= 1
DEBUG_PRINT(SER_CHECK_FOR);
#endif
#if DEBUG_LVL >= 1
if(spiffs == false){
// type = sketch
DEBUG_PRINT(SER_APP_SKETCH);
}
if(spiffs == true){
// type = spiffs
DEBUG_PRINT(SER_SPIFFS);
}
#endif
#if DEBUG_LVL >= 2
DEBUG_PRINT(SER_UPDATES_FROM);
#endif
#if DEBUG_LVL == 1
DEBUG_PRINT(SER_UPDATES);
#endif
#if HTTPS == true
url = F("https://");
#else
url = F("http://");
#endif
// location 1
url += _updateHost;
url += _updateFile;
#if DEBUG_LVL >= 2
DEBUG_PRINTLN(url);
#endif
#if DEBUG_LVL == 1
DEBUG_PRINTLN("");
#endif
HTTPClient http;
httpClientSetup(http, url, spiffs);
// track these headers for later use
const char * headerkeys[] = { "x-MD5", "x-name", "x-ver"};
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
http.collectHeaders(headerkeys, headerkeyssize);
int code = http.GET();
int len = http.getSize();
if(code == HTTP_CODE_OK){
ESP8266HTTPUpdate ESPhttpUpdate;
#if DEBUG_LVL >= 1
DEBUG_PRINT(SER_DOWN_AND_PROC);
#endif
if (_firmwareUpdateDownloadCallback){
_firmwareUpdateDownloadCallback();
}
if(ESPhttpUpdate.handleUpdate(http, len, spiffs)) {
// succesfull update
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(SER_REBOOT_NEC);
#endif
// store received appName & appVersion
strcpy(config.appName, http.header("x-name").c_str());
strcpy(config.appVersion, http.header("x-ver").c_str());
writeConfig();
if (_firmwareUpdateSuccessCallback){
_firmwareUpdateSuccessCallback();
}
// reboot
ESP.restart();
}
}else{
#if DEBUG_LVL >= 1
if(code > 0){
DEBUG_PRINTLN(http.getString());
}else{
// Failed. Server down OR HTTPS problem
DEBUG_PRINTLN(SER_CALLHOME_FAILED);
}
#endif
#if defined ESP32
if(code != 399 && _firmwareUpdateErrorCallback){
_firmwareUpdateErrorCallback();
}
#elif defined ESP8266
if(code != HTTP_CODE_NOT_MODIFIED && _firmwareUpdateErrorCallback){
_firmwareUpdateErrorCallback();
}
#endif
}
http.end();
}
/**
Add fields to the fieldStruct
*/
void IOTAppStory::addField(char* &defaultVal,const char *fieldLabel, int length, char type){
_nrXF++;
// add values to the fieldstruct
fieldStruct[_nrXF-1].fieldLabel = fieldLabel;
fieldStruct[_nrXF-1].varPointer = &defaultVal;
fieldStruct[_nrXF-1].length = length+1;
fieldStruct[_nrXF-1].type = type;
}
/**
Process the added fields
- save to eeprom
- load stored values
*/
void IOTAppStory::processField(){
eepFreeFrom = sizeof(config)+2;
// to prevent longer then default values overwriting each other
// temp save value, overwrite variable with longest value posible
// and then resave the temp value to the original variable
if(_nrXF > 0){
char* tempValue[_nrXF];
for(unsigned int nr = 0; nr < _nrXF; nr++){
tempValue[nr] = (*fieldStruct[nr].varPointer);
char* tmpVal = new char[fieldStruct[nr].length];
for (int i = 0; i < fieldStruct[nr].length-1; i++) {
tmpVal[i] = 't';
}
(*fieldStruct[nr].varPointer) = tmpVal;
}
for(unsigned int nr = 0; nr < _nrXF; nr++){
//String().toCharArray(, fieldStruct[nr].length);
strcpy((*fieldStruct[nr].varPointer), tempValue[nr]);
}
}
if(_nrXF > 0){
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(SER_PROC_FIELDS);
#endif
#if DEBUG_LVL >= 2
DEBUG_PRINTLN(SER_PROC_TBL_HDR);
#endif
EEPROM.begin(EEPROM_SIZE);
for (unsigned int nr = 1; nr <= _nrXF; nr++){
delay(100);
// loop through the fields struct array
int prevTotLength = 0;
for(unsigned int i = 0; i < (nr-1); i++){
prevTotLength += fieldStruct[i].length;
}
const int sizeOfVal = fieldStruct[nr-1].length;
const unsigned int eeBeg = eepFreeFrom+prevTotLength+nr+((nr-1)*2);
const unsigned int eeEnd = eepFreeFrom+(prevTotLength+sizeOfVal)+nr+1+((nr-1)*2);
#if DEBUG_LVL >= 2
DEBUG_PRINTF_P(PSTR(" %02d | %-30s | %03d | %04d to %04d | %-30s | "), nr, fieldStruct[nr-1].fieldLabel, fieldStruct[nr-1].length-1, eeBeg, eeEnd, (*fieldStruct[nr-1].varPointer));
#endif
char* eepVal = new char[fieldStruct[nr-1].length + 1];
char* tmpVal = new char[fieldStruct[nr-1].length + 1];
for (int i = 0; i < fieldStruct[nr-1].length; i++) {
eepVal[i] = 0;
tmpVal[i] = 0;
}
if ((*fieldStruct[nr-1].varPointer) != NULL) {
strncpy(tmpVal, (*fieldStruct[nr-1].varPointer), fieldStruct[nr-1].length);
}
// read eeprom, check for MAGICEEP and get the updated value if present
if(EEPROM.read(eeBeg) == MAGICEEP[0] && EEPROM.read(eeEnd) == '^'){
for (unsigned int t = eeBeg; t <= eeEnd; t++){
// start after MAGICEEP
if(t != eeBeg && t != eeEnd && EEPROM.read(t) != 0){
*((char*)eepVal + (t-eeBeg)-1) = EEPROM.read(t);
}
}
// if eeprom value is different update the ret value
if(strcmp(eepVal, (*fieldStruct[nr-1].varPointer)) != 0){
#if DEBUG_LVL >= 2
DEBUG_PRINTF_P(SER_PROC_TBL_OVRW, eepVal);
#endif
(*fieldStruct[nr-1].varPointer) = eepVal;
}else{
#if DEBUG_LVL >= 2
DEBUG_PRINTF_P(SER_PROC_TBL_DEF, (*fieldStruct[nr-1].varPointer));
#endif
}
}else{
#if DEBUG_LVL >= 2
DEBUG_PRINTF_P(SER_PROC_TBL_WRITE, (*fieldStruct[nr-1].varPointer));
#endif
// add MAGICEEP to value and write to eeprom
for (unsigned int t = eeBeg; t <= eeEnd; t++){
if(t == eeBeg){
EEPROM.put(t, MAGICEEP[0]); // magic begin marker
}else if(t == eeEnd){
EEPROM.put(t, '^'); // magic end marker
}else{
EEPROM.put(t, *((char*)tmpVal + (t-eeBeg)-1)); // byte of value`
}
}
}
#if DEBUG_LVL >= 2
DEBUG_PRINTLN();
#endif
if(nr == _nrXF){
eepFreeFrom = eeEnd+ 2;
}
}
EEPROM.end();
#if DEBUG_LVL >= 1
DEBUG_PRINTLN(FPSTR(SER_DEV));
#endif
}
}
/**
convert dpins to int
*/
int IOTAppStory::dPinConv(String orgVal){
#if defined ESP8266_OAK
// https://github.com/esp8266/Arduino/blob/master/variants/oak/pins_arduino.h
// DEBUG_PRINTLN("- Digistump OAK -");
if (orgVal == "P0" || orgVal == "2") return P0;
else if (orgVal == "P1" || orgVal == "5") return P1;
else if (orgVal == "P2" || orgVal == "0") return P2;
else if (orgVal == "P3" || orgVal == "3") return P3;
else if (orgVal == "P4" || orgVal == "1") return P4;
else if (orgVal == "P5" || orgVal == "4") return P5;
else if (orgVal == "P6" || orgVal == "15") return P6;
else if (orgVal == "P7" || orgVal == "13") return P7;
else if (orgVal == "P8" || orgVal == "12") return P8;
else if (orgVal == "P9" || orgVal == "14") return P9;
else if (orgVal == "P10" || orgVal == "16") return P10;
else if (orgVal == "P11" || orgVal == "17") return P11;
else return P0;
#elif defined ESP8266_WEMOS_D1MINI || defined ESP8266_WEMOS_D1MINILITE || defined ESP8266_WEMOS_D1MINIPRO
// https://github.com/esp8266/Arduino/blob/master/variants/d1_mini/pins_arduino.h
// DEBUG_PRINTLN("- build-variant d1_mini -");
if (orgVal == "D0" || orgVal == "16") return D0;
else if (orgVal == "D1" || orgVal == "5") return D1;
else if (orgVal == "D2" || orgVal == "4") return D2;
else if (orgVal == "D3" || orgVal == "0") return D3;
else if (orgVal == "D4" || orgVal == "2") return D4;
else if (orgVal == "D5" || orgVal == "14") return D5;
else if (orgVal == "D6" || orgVal == "12") return D6;
else if (orgVal == "D7" || orgVal == "13") return D7;
else if (orgVal == "D8" || orgVal == "15") return D8;
else if (orgVal == "RX" || orgVal == "3") return RX;
else if (orgVal == "TX" || orgVal == "1") return TX;
else return D0;
#elif defined ESP8266_NODEMCU || defined WIFINFO
// https://github.com/esp8266/Arduino/blob/master/variants/wifinfo/pins_arduino.h
// https://github.com/esp8266/Arduino/blob/master/variants/nodemcu/pins_arduino.h
// DEBUG_PRINTLN("- build-variant nodemcu and wifinfo -");
if (orgVal == "D0" || orgVal == "16") return D0;
else if (orgVal == "D1" || orgVal == "5") return D1;
else if (orgVal == "D2" || orgVal == "4") return D2;
else if (orgVal == "D3" || orgVal == "0") return D3;
else if (orgVal == "D4" || orgVal == "2") return D4;
else if (orgVal == "D5" || orgVal == "14") return D5;
else if (orgVal == "D6" || orgVal == "12") return D6;
else if (orgVal == "D7" || orgVal == "13") return D7;
else if (orgVal == "D8" || orgVal == "15") return D8;
else if (orgVal == "D9" || orgVal == "3") return D9;
else if (orgVal == "D10" || orgVal == "1") return D10;
else return D0;
#else
// DEBUG_PRINTLN("- Generic ESP's -");
// There are NO constants for the generic eps's!
// But people makes mistakes when entering pin nr's in config
// And if you originally developed your code for "Special ESP's"
// this part makes makes it compatible when compiling for "Generic ESP's"
if (orgVal == "D0" || orgVal == "16") return 16;
else if (orgVal == "D1" || orgVal == "5") return 5;
else if (orgVal == "D2" || orgVal == "4") return 4;
else if (orgVal == "D3" || orgVal == "0") return 0;
else if (orgVal == "D4" || orgVal == "2") return 2;
else if (orgVal == "D5" || orgVal == "14") return 14;
else if (orgVal == "D6" || orgVal == "12") return 12;
else if (orgVal == "D7" || orgVal == "13") return 13;
else if (orgVal == "D8" || orgVal == "15") return 15;
else if (orgVal == "D9" || orgVal == "3") return 3;
else if (orgVal == "D10" || orgVal == "1") return 1;
else return 16;
#endif
}
/**
Set mode and reboot
*/
void IOTAppStory::espRestart(char mmode) {
//while (isModeButtonPressed()) yield(); // wait till GPIOo released
delay(500);
boardMode = mmode;
boardInfo boardInfo(bootTimes, boardMode);
boardInfo.write();
ESP.restart();
}
/**
Erase flash from till
*/
void IOTAppStory::eraseFlash(int eepFrom, int eepTo) {
#if DEBUG_LVL >= 2
DEBUG_PRINTF_P(SER_ERASE_FLASH, eepFrom, eepTo);