This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathESP_WiFiManager_Lite.h
3315 lines (2561 loc) · 94.5 KB
/
ESP_WiFiManager_Lite.h
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
/****************************************************************************************************************************
ESP_WiFiManager_Lite.h
For ESP8266 / ESP32 boards
ESP_WiFiManager_Lite (https://github.com/khoih-prog/ESP_WiFiManager_Lite) is a library
for the ESP32/ESP8266 boards to enable store Credentials in EEPROM/SPIFFS/LittleFS for easy
configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services without Hardcoding.
Built by Khoi Hoang https://github.com/khoih-prog/ESP_WiFiManager_Lite
Licensed under MIT license
Version: 1.10.5
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 04/02/2021 Initial coding for ESP32/ESP8266
...
1.8.1 K Hoang 11/02/2022 Add LittleFS support to ESP32-C3. Use core LittleFS instead of Lorol's LITTLEFS for v2.0.0+
1.8.2 K Hoang 21/02/2022 Optional Board_Name in Menu. Optimize code by using passing by reference
1.9.0 K Hoang 09/09/2022 Fix ESP32 chipID and add ESP_getChipOUI()
1.10.0 K Hoang 10/01/2023 Add Captive-Portal feature
1.10.1 K Hoang 12/01/2023 Added public methods to load and save dynamic data
1.10.2 K Hoang 15/01/2023 Add Config Portal scaling support to mobile devices
1.10.3 K Hoang 19/01/2023 Fix compiler error if EEPROM is used
1.10.4 K Hoang 27/01/2023 Using PROGMEM for HTML strings
1.10.5 K Hoang 28/01/2023 Using PROGMEM for strings in examples
*****************************************************************************************************************************/
#pragma once
#ifndef ESP_WiFiManager_Lite_h
#define ESP_WiFiManager_Lite_h
#if !( defined(ESP8266) || defined(ESP32) )
#error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting.
#elif ( ARDUINO_ESP32S2_DEV || ARDUINO_FEATHERS2 || ARDUINO_ESP32S2_THING_PLUS || ARDUINO_MICROS2 || \
ARDUINO_METRO_ESP32S2 || ARDUINO_MAGTAG29_ESP32S2 || ARDUINO_FUNHOUSE_ESP32S2 || \
ARDUINO_ADAFRUIT_FEATHER_ESP32S2_NOPSRAM )
#warning Using ESP32_S2. To follow library instructions to install esp32-s2 core and WebServer Patch
#define USING_ESP32_S2 true
#elif ( ARDUINO_ESP32C3_DEV )
#if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) )
#warning Using ESP32_C3 using core v2.0.0+. Either LittleFS, SPIFFS or EEPROM OK.
#else
#warning Using ESP32_C3 using core v1.0.6-. To follow library instructions to install esp32-c3 core. Only SPIFFS and EEPROM OK.
#endif
#warning You have to select Flash size 2MB and Minimal APP (1.3MB + 700KB) for some boards
#define USING_ESP32_C3 true
#elif ( defined(ARDUINO_ESP32S3_DEV) || defined(ARDUINO_ESP32_S3_BOX) || defined(ARDUINO_TINYS3) || \
defined(ARDUINO_PROS3) || defined(ARDUINO_FEATHERS3) )
#warning Using ESP32_S3. To install esp32-s3-support branch if using core v2.0.2-.
#define USING_ESP32_S3 true
#endif
///////////////////////////////////////////
#ifndef ESP_WIFI_MANAGER_LITE_VERSION
#define ESP_WIFI_MANAGER_LITE_VERSION "ESP_WiFiManager_Lite v1.10.5"
#define ESP_WIFI_MANAGER_LITE_VERSION_MAJOR 1
#define ESP_WIFI_MANAGER_LITE_VERSION_MINOR 10
#define ESP_WIFI_MANAGER_LITE_VERSION_PATCH 5
#define ESP_WIFI_MANAGER_LITE_VERSION_INT 1010005
#endif
///////////////////////////////////////////
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
//default to use EEPROM, otherwise, use LittleFS or SPIFFS
#if ( USE_LITTLEFS || USE_SPIFFS )
#if USE_LITTLEFS
#define FileFS LittleFS
#define FS_Name "LittleFS"
#warning Using LittleFS in ESP_WiFiManager_Lite.h
#else
#define FileFS SPIFFS
#define FS_Name "SPIFFS"
#warning Using SPIFFS in ESP_WiFiManager_Lite.h
#endif
#include <FS.h>
#include <LittleFS.h>
#else
#include <EEPROM.h>
#define FS_Name "EEPROM"
#define EEPROM_SIZE 2048
#warning Using EEPROM in ESP_WiFiManager_Lite.h
#endif
#else //ESP32
#include <WiFi.h>
#include <WiFiMulti.h>
#include <WebServer.h>
// To be sure no LittleFS for ESP32-C3 for core v1.0.6-
#if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) )
// For core v2.0.0+, ESP32-C3 can use LittleFS, SPIFFS or EEPROM
// LittleFS has higher priority than SPIFFS.
// For core v2.0.0+, if not specified any, use better LittleFS
#if ! (defined(USE_LITTLEFS) || defined(USE_SPIFFS) )
#define USE_LITTLEFS true
#define USE_SPIFFS false
#endif
#elif defined(ARDUINO_ESP32C3_DEV)
// For core v1.0.6-, ESP32-C3 only supporting SPIFFS and EEPROM. To use v2.0.0+ for LittleFS
#if USE_LITTLEFS
#undef USE_LITTLEFS
#define USE_LITTLEFS false
#undef USE_SPIFFS
#define USE_SPIFFS true
#endif
#else
// For core v1.0.6-, if not specified any, use SPIFFS to not forcing user to install LITTLEFS library
#if ! (defined(USE_LITTLEFS) || defined(USE_SPIFFS) )
#define USE_SPIFFS true
#endif
#endif
#if USE_LITTLEFS
// Use LittleFS
#include "FS.h"
// Check cores/esp32/esp_arduino_version.h and cores/esp32/core_version.h
//#if ( ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(2, 0, 0) ) //(ESP_ARDUINO_VERSION_MAJOR >= 2)
#if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) )
#warning Using ESP32 Core 1.0.6 or 2.0.0+
// The library has been merged into esp32 core from release 1.0.6
#include <LittleFS.h> // https://github.com/espressif/arduino-esp32/tree/master/libraries/LittleFS
FS* filesystem = &LittleFS;
#define FileFS LittleFS
#define FS_Name "LittleFS"
#else
#warning Using ESP32 Core 1.0.5-. You must install LITTLEFS library
// The library has been merged into esp32 core from release 1.0.6
#include <LITTLEFS.h> // https://github.com/lorol/LITTLEFS
FS* filesystem = &LITTLEFS;
#define FileFS LITTLEFS
#define FS_Name "LittleFS"
#endif
#elif USE_SPIFFS
#include "FS.h"
#include <SPIFFS.h>
FS* filesystem = &SPIFFS;
#define FileFS SPIFFS
#define FS_Name "SPIFFS"
#warning Using SPIFFS in ESP_WiFiManager_Lite.h
#else
#include <EEPROM.h>
#define FS_Name "EEPROM"
#define EEPROM_SIZE 2048
#warning Using EEPROM in ESP_WiFiManager_Lite.h
#endif
#endif
#define DNS_PORT 53
///////////////////////////////////////////
#include <DNSServer.h>
#include <memory>
#undef min
#undef max
#include <algorithm>
//KH, for ESP32
#ifdef ESP8266
extern "C"
{
#include "user_interface.h"
}
#define ESP_getChipId() (ESP.getChipId())
#else //ESP32
#include <esp_wifi.h>
uint32_t getChipID();
uint32_t getChipOUI();
#if defined(ESP_getChipId)
#undef ESP_getChipId
#endif
#if defined(ESP_getChipOUI)
#undef ESP_getChipOUI
#endif
#define ESP_getChipId() getChipID()
#define ESP_getChipOUI() getChipOUI()
#endif
#include <ESP_WiFiManager_Lite_Debug.h>
//////////////////////////////////////////////
// New from v1.3.0
// KH, Some minor simplification
#if !defined(SCAN_WIFI_NETWORKS)
#define SCAN_WIFI_NETWORKS true //false
#endif
#if SCAN_WIFI_NETWORKS
#if !defined(MANUAL_SSID_INPUT_ALLOWED)
#define MANUAL_SSID_INPUT_ALLOWED true
#endif
#if !defined(MAX_SSID_IN_LIST)
#define MAX_SSID_IN_LIST 10
#elif (MAX_SSID_IN_LIST < 2)
#warning Parameter MAX_SSID_IN_LIST defined must be >= 2 - Reset to 10
#undef MAX_SSID_IN_LIST
#define MAX_SSID_IN_LIST 10
#elif (MAX_SSID_IN_LIST > 15)
#warning Parameter MAX_SSID_IN_LIST defined must be <= 15 - Reset to 10
#undef MAX_SSID_IN_LIST
#define MAX_SSID_IN_LIST 10
#endif
#else
#if (_ESP_WM_LITE_LOGLEVEL_ > 3)
#warning SCAN_WIFI_NETWORKS disabled
#endif
#endif
///////// NEW for DRD /////////////
#if !defined(USING_MRD)
#define USING_MRD false
#endif
#if USING_MRD
///////// NEW for MRD /////////////
// These defines must be put before #include <ESP_DoubleResetDetector.h>
// to select where to store DoubleResetDetector's variable.
// For ESP32, You must select one to be true (EEPROM or SPIFFS/LittleFS)
// For ESP8266, You must select one to be true (RTC, EEPROM or SPIFFS/LittleFS)
// Otherwise, library will use default EEPROM storage
#define ESP8266_MRD_USE_RTC false //true
#if USE_LITTLEFS
#define ESP_MRD_USE_LITTLEFS true
#define ESP_MRD_USE_SPIFFS false
#define ESP_MRD_USE_EEPROM false
#elif USE_SPIFFS
#define ESP_MRD_USE_LITTLEFS false
#define ESP_MRD_USE_SPIFFS true
#define ESP_MRD_USE_EEPROM false
#else
#define ESP_MRD_USE_LITTLEFS false
#define ESP_MRD_USE_SPIFFS false
#define ESP_MRD_USE_EEPROM true
#endif
#ifndef MULTIRESETDETECTOR_DEBUG
#define MULTIRESETDETECTOR_DEBUG false
#endif
// These definitions must be placed before #include <ESP_MultiResetDetector.h> to be used
// Otherwise, default values (MRD_TIMES = 3, MRD_TIMEOUT = 10 seconds and MRD_ADDRESS = 0) will be used
// Number of subsequent resets during MRD_TIMEOUT to activate
#ifndef MRD_TIMES
#define MRD_TIMES 3
#endif
// Number of seconds after reset during which a
// subsequent reset will be considered a double reset.
#ifndef MRD_TIMEOUT
#define MRD_TIMEOUT 10
#endif
// EEPROM Memory Address for the MultiResetDetector to use
#ifndef MRD_TIMEOUT
#define MRD_ADDRESS 0
#endif
#include <ESP_MultiResetDetector.h> //https://github.com/khoih-prog/ESP_MultiResetDetector
//MultiResetDetector mrd(MRD_TIMEOUT, MRD_ADDRESS);
MultiResetDetector* mrd;
///////// NEW for MRD /////////////
#else
///////// NEW for DRD /////////////
// These defines must be put before #include <ESP_DoubleResetDetector.h>
// to select where to store DoubleResetDetector's variable.
// For ESP32, You must select one to be true (EEPROM or SPIFFS/LittleFS)
// For ESP8266, You must select one to be true (RTC, EEPROM or SPIFFS/LittleFS)
// Otherwise, library will use default EEPROM storage
#define ESP8266_DRD_USE_RTC false //true
#if USE_LITTLEFS
#define ESP_DRD_USE_LITTLEFS true
#define ESP_DRD_USE_SPIFFS false
#define ESP_DRD_USE_EEPROM false
#elif USE_SPIFFS
#define ESP_DRD_USE_LITTLEFS false
#define ESP_DRD_USE_SPIFFS true
#define ESP_DRD_USE_EEPROM false
#else
#define ESP_DRD_USE_LITTLEFS false
#define ESP_DRD_USE_SPIFFS false
#define ESP_DRD_USE_EEPROM true
#endif
#ifndef DOUBLERESETDETECTOR_DEBUG
#define DOUBLERESETDETECTOR_DEBUG false
#endif
// Number of seconds after reset during which a
// subsequent reset will be considered a double reset.
#define DRD_TIMEOUT 10
// RTC Memory Address for the DoubleResetDetector to use
#define DRD_ADDRESS 0
#include <ESP_DoubleResetDetector.h> //https://github.com/khoih-prog/ESP_DoubleResetDetector
//DoubleResetDetector drd(DRD_TIMEOUT, DRD_ADDRESS);
DoubleResetDetector* drd;
///////// NEW for DRD /////////////
#endif
///////////////////////////////////////////
//NEW
#define MAX_ID_LEN 5
#define MAX_DISPLAY_NAME_LEN 16
///////////////////////////////////////////
typedef struct
{
char id [MAX_ID_LEN + 1];
char displayName [MAX_DISPLAY_NAME_LEN + 1];
char *pdata;
uint8_t maxlen;
} MenuItem;
///////////////////////////////////////////
#if USE_DYNAMIC_PARAMETERS
#if (_ESP_WM_LITE_LOGLEVEL_ > 3)
#warning Using Dynamic Parameters
#endif
///NEW
extern uint16_t NUM_MENU_ITEMS;
extern MenuItem myMenuItems [];
bool *menuItemUpdated = NULL;
#else
#if (_ESP_WM_LITE_LOGLEVEL_ > 3)
#warning Not using Dynamic Parameters
#endif
#endif
///////////////////////////////////////////
#define SSID_MAX_LEN 32
// WPA2 passwords can be up to 63 characters long.
#define PASS_MAX_LEN 64
typedef struct
{
char wifi_ssid[SSID_MAX_LEN];
char wifi_pw [PASS_MAX_LEN];
} WiFi_Credentials;
#define NUM_WIFI_CREDENTIALS 2
#if USING_BOARD_NAME
// Configurable items besides fixed Header, just add board_name
#define NUM_CONFIGURABLE_ITEMS ( ( 2 * NUM_WIFI_CREDENTIALS ) + 1 )
#else
// Configurable items besides fixed Header, just add board_name
#define NUM_CONFIGURABLE_ITEMS ( ( 2 * NUM_WIFI_CREDENTIALS ))
#endif
///////////////////////////////////////////
#define HEADER_MAX_LEN 16
#define BOARD_NAME_MAX_LEN 24
typedef struct Configuration
{
char header [HEADER_MAX_LEN];
WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS];
char board_name [BOARD_NAME_MAX_LEN];
int checkSum;
} ESP_WM_LITE_Configuration;
// Currently CONFIG_DATA_SIZE = 236 = (16 + 96 * 2 + 4 + 24)
uint16_t CONFIG_DATA_SIZE = sizeof(ESP_WM_LITE_Configuration);
///////////////////////////////////////////
extern bool LOAD_DEFAULT_CONFIG_DATA;
extern ESP_WM_LITE_Configuration defaultConfig;
///////////////////////////////////////////
// -- HTML page fragments
const char ESP_WM_LITE_HTML_HEAD_START[] PROGMEM = "<!DOCTYPE html><html><head><title>ESP_WM_LITE</title><meta name='viewport' content='width=device-width, initial-scale=1'>";
const char ESP_WM_LITE_HTML_HEAD_STYLE[] PROGMEM =
"<style>div,input{padding:5px;font-size:1em;}input{width:95%;}body{text-align: center;}button{background-color:#16A1E7;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%;}fieldset{border-radius:0.3rem;margin:0px;}</style>";
#if USING_BOARD_NAME
const char ESP_WM_LITE_HTML_HEAD_END[] PROGMEM =
"</head><div style='text-align:left;display:inline-block;min-width:260px;'>\
<fieldset><div><label>*WiFi SSID</label><div>[[input_id]]</div></div>\
<div><label>*PWD (8+ chars)</label><input value='[[pw]]' id='pw'><div></div></div>\
<div><label>*WiFi SSID1</label><div>[[input_id1]]</div></div>\
<div><label>*PWD1 (8+ chars)</label><input value='[[pw1]]' id='pw1'><div></div></div></fieldset>\
<fieldset><div><label>Board Name</label><input value='[[nm]]' id='nm'><div></div></div></fieldset>"; // DO NOT CHANGE THIS STRING EVER!!!!
#else
const char ESP_WM_LITE_HTML_HEAD_END[] PROGMEM =
"</head><div style='text-align:left;display:inline-block;min-width:260px;'>\
<fieldset><div><label>*WiFi SSID</label><div>[[input_id]]</div></div>\
<div><label>*PWD (8+ chars)</label><input value='[[pw]]' id='pw'><div></div></div>\
<div><label>*WiFi SSID1</label><div>[[input_id1]]</div></div>\
<div><label>*PWD1 (8+ chars)</label><input value='[[pw1]]' id='pw1'><div></div></div></fieldset>"; // DO NOT CHANGE THIS STRING EVER!!!!
#endif
const char ESP_WM_LITE_HTML_INPUT_ID[] PROGMEM = "<input value='[[id]]' id='id'>";
const char ESP_WM_LITE_HTML_INPUT_ID1[] PROGMEM = "<input value='[[id1]]' id='id1'>";
const char ESP_WM_LITE_FLDSET_START[] PROGMEM = "<fieldset>";
const char ESP_WM_LITE_FLDSET_END[] PROGMEM = "</fieldset>";
const char ESP_WM_LITE_HTML_PARAM[] PROGMEM =
"<div><label>{b}</label><input value='[[{v}]]'id='{i}'><div></div></div>";
const char ESP_WM_LITE_HTML_BUTTON[] PROGMEM = "<button onclick=\"sv()\">Save</button></div>";
#if USING_BOARD_NAME
const char ESP_WM_LITE_HTML_SCRIPT[] PROGMEM = "<script id=\"jsbin-javascript\">\
function udVal(key,val){var request=new XMLHttpRequest();var url='/?key='+key+'&value='+encodeURIComponent(val);\
request.open('GET',url,false);request.send(null);}\
function sv(){udVal('id',document.getElementById('id').value);udVal('pw',document.getElementById('pw').value);\
udVal('id1',document.getElementById('id1').value);udVal('pw1',document.getElementById('pw1').value);\
udVal('nm',document.getElementById('nm').value);";
#else
const char ESP_WM_LITE_HTML_SCRIPT[] PROGMEM = "<script id=\"jsbin-javascript\">\
function udVal(key,val){var request=new XMLHttpRequest();var url='/?key='+key+'&value='+encodeURIComponent(val);\
request.open('GET',url,false);request.send(null);}\
function sv(){udVal('id',document.getElementById('id').value);udVal('pw',document.getElementById('pw').value);\
udVal('id1',document.getElementById('id1').value);udVal('pw1',document.getElementById('pw1').value);";
#endif
const char ESP_WM_LITE_HTML_SCRIPT_ITEM[] PROGMEM = "udVal('{d}',document.getElementById('{d}').value);";
const char ESP_WM_LITE_HTML_SCRIPT_END[] PROGMEM = "alert('Updated');}</script>";
const char ESP_WM_LITE_HTML_END[] PROGMEM = "</html>";
#if SCAN_WIFI_NETWORKS
const char ESP_WM_LITE_SELECT_START[] PROGMEM = "<select id=";
const char ESP_WM_LITE_SELECT_END[] PROGMEM = "</select>";
const char ESP_WM_LITE_DATALIST_START[] PROGMEM = "<datalist id=";
const char ESP_WM_LITE_DATALIST_END[] PROGMEM = "</datalist>";
const char ESP_WM_LITE_OPTION_START[] PROGMEM = "<option>";
const char ESP_WM_LITE_OPTION_END[] PROGMEM = ""; // "</option>"; is not required
const char ESP_WM_LITE_NO_NETWORKS_FOUND[] PROGMEM = "No suitable WiFi networks available!";
#endif
//////////////////////////////////////////
//KH Add repeatedly used const
const char WM_HTTP_HEAD_CL[] PROGMEM = "Content-Length";
const char WM_HTTP_HEAD_TEXT_HTML[] PROGMEM = "text/html";
const char WM_HTTP_HEAD_TEXT_PLAIN[] PROGMEM = "text/plain";
const char WM_HTTP_CACHE_CONTROL[] PROGMEM = "Cache-Control";
const char WM_HTTP_NO_STORE[] PROGMEM = "no-cache, no-store, must-revalidate";
const char WM_HTTP_PRAGMA[] PROGMEM = "Pragma";
const char WM_HTTP_NO_CACHE[] PROGMEM = "no-cache";
const char WM_HTTP_EXPIRES[] PROGMEM = "Expires";
const char WM_HTTP_CORS[] PROGMEM = "Access-Control-Allow-Origin";
const char WM_HTTP_CORS_ALLOW_ALL[] PROGMEM = "*";
//////////////////////////////////////////
#if (ESP32)
uint32_t getChipID()
{
uint64_t chipId64 = 0;
for (int i = 0; i < 6; i++)
{
chipId64 |= ( ( (uint64_t) ESP.getEfuseMac() >> (40 - (i * 8)) ) & 0xff ) << (i * 8);
}
return (uint32_t) (chipId64 & 0xFFFFFF);
}
//////////////////////////////////////////
uint32_t getChipOUI()
{
uint64_t chipId64 = 0;
for (int i = 0; i < 6; i++)
{
chipId64 |= ( ( (uint64_t) ESP.getEfuseMac() >> (40 - (i * 8)) ) & 0xff ) << (i * 8);
}
return (uint32_t) (chipId64 >> 24);
}
#endif
//////////////////////////////////////////
String IPAddressToString(const IPAddress& _address)
{
String str = String(_address[0]);
str += ".";
str += String(_address[1]);
str += ".";
str += String(_address[2]);
str += ".";
str += String(_address[3]);
return str;
}
//////////////////////////////////////////
class ESP_WiFiManager_Lite
{
public:
ESP_WiFiManager_Lite()
{
}
//////////////////////////////////////////
~ESP_WiFiManager_Lite()
{
if (dnsServer)
{
delete dnsServer;
}
if (server)
{
delete server;
#if SCAN_WIFI_NETWORKS
if (indices)
{
free(indices); //indices array no longer required so free memory
}
#endif
}
}
//////////////////////////////////////////
void connectWiFi(const char* ssid, const char* pass)
{
ESP_WML_LOGINFO1(F("Con2:"), ssid);
WiFi.mode(WIFI_STA);
if (static_IP != IPAddress(0, 0, 0, 0))
{
ESP_WML_LOGINFO(F("UseStatIP"));
WiFi.config(static_IP, static_GW, static_SN, static_DNS1, static_DNS2);
}
setHostname();
if (WiFi.status() != WL_CONNECTED)
{
if (pass && strlen(pass))
{
WiFi.begin(ssid, pass);
}
else
{
WiFi.begin(ssid);
}
}
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
ESP_WML_LOGINFO(F("Conn2WiFi"));
displayWiFiData();
}
//////////////////////////////////////////
void begin(const char* ssid,
const char* pass )
{
ESP_WML_LOGERROR(F("conW"));
connectWiFi(ssid, pass);
}
//////////////////////////////////////////
#if !defined(USE_LED_BUILTIN)
#define USE_LED_BUILTIN true // use builtin LED to show configuration mode
#endif
#if ESP8266
// For ESP8266
#ifndef LED_BUILTIN
#define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
#endif
#define LED_ON LOW
#define LED_OFF HIGH
#else
// For ESP32
#ifndef LED_BUILTIN
#define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
#endif
#define LED_OFF LOW
#define LED_ON HIGH
#endif
///////////////////////////////////////////
#if !defined(REQUIRE_ONE_SET_SSID_PW)
#define REQUIRE_ONE_SET_SSID_PW false
#endif
#define PASSWORD_MIN_LEN 8
//////////////////////////////////////////
void begin(const char *iHostname = "")
{
#define TIMEOUT_CONNECT_WIFI 30000
#if USE_LED_BUILTIN
// Turn OFF
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LED_OFF);
#endif
#if USING_MRD
//// New MRD ////
mrd = new MultiResetDetector(MRD_TIMEOUT, MRD_ADDRESS);
bool noConfigPortal = true;
if (mrd->detectMultiReset())
#else
//// New DRD ////
drd = new DoubleResetDetector(DRD_TIMEOUT, DRD_ADDRESS);
bool noConfigPortal = true;
if (drd->detectDoubleReset())
#endif
{
ESP_WML_LOGINFO(F("Multi or Double Reset Detected"));
noConfigPortal = false;
}
//// New DRD/MRD ////
if (LOAD_DEFAULT_CONFIG_DATA)
{
ESP_WML_LOGDEBUG(F("======= Start Default Config Data ======="));
displayConfigData(defaultConfig);
}
WiFi.mode(WIFI_STA);
if (iHostname[0] == 0)
{
String _hostname = "ESP_" + String(ESP_getChipId(), HEX);
_hostname.toUpperCase();
getRFC952_hostname(_hostname.c_str());
}
else
{
// Prepare and store the hostname only not NULL
getRFC952_hostname(iHostname);
}
ESP_WML_LOGINFO1(F("Hostname="), RFC952_hostname);
hadConfigData = getConfigData();
isForcedConfigPortal = isForcedCP();
//// New DRD/MRD ////
// noConfigPortal when getConfigData() OK and no MRD/DRD'ed
if (hadConfigData && noConfigPortal && (!isForcedConfigPortal) )
{
hadConfigData = true;
ESP_WML_LOGDEBUG(noConfigPortal ? F("bg: noConfigPortal = true") : F("bg: noConfigPortal = false"));
for (uint16_t i = 0; i < NUM_WIFI_CREDENTIALS; i++)
{
if ( strlen(ESP_WM_LITE_config.WiFi_Creds[i].wifi_pw) >= PASSWORD_MIN_LEN )
{
ESP_WML_LOGDEBUG5(F("bg: addAP : index="), i, F(", SSID="), ESP_WM_LITE_config.WiFi_Creds[i].wifi_ssid, F(", PWD="),
ESP_WM_LITE_config.WiFi_Creds[i].wifi_pw);
wifiMulti.addAP(ESP_WM_LITE_config.WiFi_Creds[i].wifi_ssid, ESP_WM_LITE_config.WiFi_Creds[i].wifi_pw);
}
else
{
ESP_WML_LOGWARN3(F("bg: Ignore invalid WiFi PWD : index="), i, F(", PWD="), ESP_WM_LITE_config.WiFi_Creds[i].wifi_pw);
}
}
if (connectMultiWiFi() == WL_CONNECTED)
{
ESP_WML_LOGINFO(F("bg: WiFi OK."));
}
else
{
ESP_WML_LOGINFO(F("bg: Fail2connect WiFi"));
// failed to connect to WiFi, will start configuration mode
startConfigurationMode();
}
}
else
{
ESP_WML_LOGDEBUG(isForcedConfigPortal ? F("bg: isForcedConfigPortal = true") : F("bg: isForcedConfigPortal = false"));
// If not persistent => clear the flag so that after reset. no more CP, even CP not entered and saved
if (persForcedConfigPortal)
{
ESP_WML_LOGINFO1(F("bg:Stay forever in CP:"),
isForcedConfigPortal ? F("Forced-Persistent") : (noConfigPortal ? F("No ConfigDat") : F("DRD/MRD")));
}
else
{
ESP_WML_LOGINFO1(F("bg:Stay forever in CP:"),
isForcedConfigPortal ? F("Forced-non-Persistent") : (noConfigPortal ? F("No ConfigDat") : F("DRD/MRD")));
clearForcedCP();
}
hadConfigData = isForcedConfigPortal ? true : (noConfigPortal ? false : true);
// failed to connect to WiFi, will start configuration mode
startConfigurationMode();
}
}
//////////////////////////////////////////
#ifndef TIMEOUT_RECONNECT_WIFI
#define TIMEOUT_RECONNECT_WIFI 10000L
#else
// Force range of user-defined TIMEOUT_RECONNECT_WIFI between 10-60s
#if (TIMEOUT_RECONNECT_WIFI < 10000L)
#warning TIMEOUT_RECONNECT_WIFI too low. Reseting to 10000
#undef TIMEOUT_RECONNECT_WIFI
#define TIMEOUT_RECONNECT_WIFI 10000L
#elif (TIMEOUT_RECONNECT_WIFI > 60000L)
#warning TIMEOUT_RECONNECT_WIFI too high. Reseting to 60000
#undef TIMEOUT_RECONNECT_WIFI
#define TIMEOUT_RECONNECT_WIFI 60000L
#endif
#endif
#ifndef RETRY_TIMES_RECONNECT_WIFI
#define RETRY_TIMES_RECONNECT_WIFI 2
#else
// Force range of user-defined RETRY_TIMES_RECONNECT_WIFI between 2-5 times
#if (RETRY_TIMES_RECONNECT_WIFI < 2)
#warning RETRY_TIMES_RECONNECT_WIFI too low. Reseting to 2
#undef RETRY_TIMES_RECONNECT_WIFI
#define RETRY_TIMES_RECONNECT_WIFI 2
#elif (RETRY_TIMES_RECONNECT_WIFI > 5)
#warning RETRY_TIMES_RECONNECT_WIFI too high. Reseting to 5
#undef RETRY_TIMES_RECONNECT_WIFI
#define RETRY_TIMES_RECONNECT_WIFI 5
#endif
#endif
#ifndef RESET_IF_CONFIG_TIMEOUT
#define RESET_IF_CONFIG_TIMEOUT true
#endif
#ifndef CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET
#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 10
#else
// Force range of user-defined TIMES_BEFORE_RESET between 2-100
#if (CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET < 2)
#warning CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET too low. Reseting to 2
#undef CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET
#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 2
#elif (CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET > 100)
#warning CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET too high. Reseting to 100
#undef CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET
#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 100
#endif
#endif
//////////////////////////////////////////
#if !defined(WIFI_RECON_INTERVAL)
#define WIFI_RECON_INTERVAL 0 // default 0s between reconnecting WiFi
#else
#if (WIFI_RECON_INTERVAL < 0)
#define WIFI_RECON_INTERVAL 0
#elif (WIFI_RECON_INTERVAL > 600000)
#define WIFI_RECON_INTERVAL 600000 // Max 10min
#endif
#endif
//////////////////////////////////////////
void run()
{
static int retryTimes = 0;
static bool wifiDisconnectedOnce = false;
// Lost connection in running. Give chance to reconfig.
// Check WiFi status every 5s and update status
// Check twice to be sure wifi disconnected is real
static unsigned long checkstatus_timeout = 0;
#define WIFI_STATUS_CHECK_INTERVAL 5000L
static uint32_t curMillis;
curMillis = millis();
#if USING_MRD
//// New MRD ////
// Call the multi reset detector loop method every so often,
// so that it can recognise when the timeout expires.
// You can also call mrd.stop() when you wish to no longer
// consider the next reset as a multi reset.
mrd->loop();
//// New MRD ////
#else
//// New DRD ////
// Call the double reset detector loop method every so often,
// so that it can recognise when the timeout expires.
// You can also call drd.stop() when you wish to no longer
// consider the next reset as a double reset.
drd->loop();
//// New DRD ////
#endif
if (configuration_mode && dnsServer)
{
dnsServer->processNextRequest();
}
if ( !configuration_mode && (curMillis > checkstatus_timeout) )
{
if (WiFi.status() == WL_CONNECTED)
{
wifi_connected = true;
}
else
{
if (wifiDisconnectedOnce)
{
wifiDisconnectedOnce = false;
wifi_connected = false;
ESP_WML_LOGERROR(F("r:Check&WLost"));
}
else
{
wifiDisconnectedOnce = true;
}
}
checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL;
}
// Lost connection in running. Give chance to reconfig.
if ( WiFi.status() != WL_CONNECTED )
{
// If configTimeout but user hasn't connected to configWeb => try to reconnect WiFi
// But if user has connected to configWeb, stay there until done, then reset hardware
if ( configuration_mode && ( configTimeout == 0 || millis() < configTimeout ) )
{
retryTimes = 0;
if (server)
{
server->handleClient();
// Fix ESP32-S2 issue with WebServer (https://github.com/espressif/arduino-esp32/issues/4348)
if ( String(ARDUINO_BOARD) == "ESP32S2_DEV" )
{
delay(1);
}
}
return;
}
else
{
#if RESET_IF_CONFIG_TIMEOUT
// If we're here but still in configuration_mode, permit running TIMES_BEFORE_RESET times before reset hardware
// to permit user another chance to config.
if ( configuration_mode && (configTimeout != 0) )
{
ESP_WML_LOGDEBUG(F("r:Check RESET_IF_CONFIG_TIMEOUT"));
if (++retryTimes <= CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET)
{
ESP_WML_LOGINFO1(F("run: WiFi lost, configTimeout. Connect WiFi. Retry#:"), retryTimes);
}
else
{
resetFunc();
}
}
#endif
// Not in config mode, try reconnecting before forcing to config mode
if ( WiFi.status() != WL_CONNECTED )
{
#if (WIFI_RECON_INTERVAL > 0)
static uint32_t lastMillis = 0;
if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL )
{
lastMillis = curMillis;
ESP_WML_LOGERROR(F("r:WLost.ReconW"));
if (connectMultiWiFi() == WL_CONNECTED)
{
#if USE_LED_BUILTIN
// turn the LED_BUILTIN OFF to tell us we exit configuration mode.
digitalWrite(LED_BUILTIN, LED_OFF);
#endif
ESP_WML_LOGINFO(F("run: WiFi reconnected"));
}
}
#else
ESP_WML_LOGINFO(F("run: WiFi lost. Reconnect WiFi"));
if (connectMultiWiFi() == WL_CONNECTED)
{
#if USE_LED_BUILTIN
// turn the LED_BUILTIN OFF to tell us we exit configuration mode.
digitalWrite(LED_BUILTIN, LED_OFF);
#endif
ESP_WML_LOGINFO(F("run: WiFi reconnected"));
}
#endif
}
//ESP_WML_LOGINFO(F("run: Lost connection => configMode"));
//startConfigurationMode();
}
}
else if (configuration_mode)
{
// WiFi is connected and we are in configuration_mode
configuration_mode = false;
ESP_WML_LOGINFO(F("run: got WiFi back"));
#if USE_LED_BUILTIN
// turn the LED_BUILTIN OFF to tell us we exit configuration mode.
digitalWrite(LED_BUILTIN, LED_OFF);
#endif