13
13
14
14
Built by Khoi Hoang https://github.com/khoih-prog/ESPAsync_WiFiManager
15
15
Licensed under MIT license
16
- Version: 1.7.1
16
+ Version: 1.8.0
17
17
18
18
Version Modified By Date Comments
19
19
------- ----------- ---------- -----------
35
35
1.6.3 K Hoang 13/04/2021 Allow captive portal to run more than once by closing dnsServer.
36
36
1.7.0 K Hoang 20/04/2021 Add support to new ESP32-C3 using SPIFFS or EEPROM
37
37
1.7.1 K Hoang 25/04/2021 Fix MultiWiFi bug. Fix captive-portal bug if CP AP address is not default 192.168.4.1
38
+ 1.8.0 K Hoang 30/04/2021 Set _timezoneName. Add support to new ESP32-S2 (METRO_ESP32S2, FUNHOUSE_ESP32S2, etc.)
38
39
*****************************************************************************************************************************/
39
40
#if !( defined(ESP8266) || defined(ESP32) )
40
41
#error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting.
41
42
#endif
42
43
43
- #define ESP_ASYNC_WIFIMANAGER_VERSION_MIN_TARGET " ESPAsync_WiFiManager v1.7.1 "
44
+ #define ESP_ASYNC_WIFIMANAGER_VERSION_MIN_TARGET " ESPAsync_WiFiManager v1.8.0 "
44
45
45
46
// Use from 0 to 4. Higher number, more debugging messages and memory usage.
46
47
#define _ESPASYNC_WIFIMGR_LOGLEVEL_ 3
@@ -154,9 +155,16 @@ typedef struct
154
155
155
156
#define NUM_WIFI_CREDENTIALS 2
156
157
158
+ // Assuming max 49 chars
159
+ #define TZNAME_MAX_LEN 50
160
+ #define TIMEZONE_MAX_LEN 50
161
+
157
162
typedef struct
158
163
{
159
164
WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS];
165
+ char TZ_Name[TZNAME_MAX_LEN]; // "America/Toronto"
166
+ char TZ[TIMEZONE_MAX_LEN]; // "EST5EDT,M3.2.0,M11.1.0"
167
+ uint16_t checksum;
160
168
} WM_Config;
161
169
162
170
WM_Config WM_config;
@@ -179,7 +187,20 @@ bool initialConfig = false;
179
187
180
188
// Use false to disable NTP config. Advisable when using Cellphone, Tablet to access Config Portal.
181
189
// See Issue 23: On Android phone ConfigPortal is unresponsive (https://github.com/khoih-prog/ESP_WiFiManager/issues/23)
182
- #define USE_ESP_WIFIMANAGER_NTP false
190
+ #define USE_ESP_WIFIMANAGER_NTP true
191
+
192
+ // Just use enough to save memory. On ESP8266, can cause blank ConfigPortal screen
193
+ // if using too much memory
194
+ #define USING_AFRICA false
195
+ #define USING_AMERICA true
196
+ #define USING_ANTARCTICA false
197
+ #define USING_ASIA false
198
+ #define USING_ATLANTIC false
199
+ #define USING_AUSTRALIA false
200
+ #define USING_EUROPE false
201
+ #define USING_INDIAN false
202
+ #define USING_PACIFIC false
203
+ #define USING_ETC_GMT false
183
204
184
205
// Use true to enable CloudFlare NTP service. System can hang if you don't have Internet access while accessing CloudFlare
185
206
// See Issue #21: CloudFlare link in the default portal (https://github.com/khoih-prog/ESP_WiFiManager/issues/21)
@@ -227,6 +248,8 @@ bool initialConfig = false;
227
248
IPAddress dns1IP = gatewayIP;
228
249
IPAddress dns2IP = IPAddress(8 , 8 , 8 , 8 );
229
250
251
+ #define USE_CUSTOM_AP_IP false
252
+
230
253
IPAddress APStaticIP = IPAddress(192 , 168 , 100 , 1 );
231
254
IPAddress APStaticGW = IPAddress(192 , 168 , 100 , 1 );
232
255
IPAddress APStaticSN = IPAddress(255 , 255 , 255 , 0 );
@@ -373,13 +396,49 @@ uint8_t connectMultiWiFi()
373
396
LOGERROR3 (F (" Channel:" ), WiFi.channel (), F (" ,IP address:" ), WiFi.localIP () );
374
397
}
375
398
else
399
+ {
376
400
LOGERROR (F (" WiFi not connected" ));
401
+
402
+ #if ESP8266
403
+ ESP.reset ();
404
+ #else
405
+ ESP.restart ();
406
+ #endif
407
+ }
377
408
378
409
return status;
379
410
}
380
411
412
+ #if USE_ESP_WIFIMANAGER_NTP
413
+
414
+ void printLocalTime ()
415
+ {
416
+ #if ESP8266
417
+ static time_t now;
418
+
419
+ now = time (nullptr );
420
+
421
+ if ( now > 1000000 )
422
+ {
423
+ Serial.print (" Local Date/Time: " );
424
+ Serial.print (ctime (&now));
425
+ }
426
+ #else
427
+ struct tm timeinfo;
428
+
429
+ getLocalTime ( &timeinfo );
430
+ Serial.print (" Local Date/Time: " );
431
+ Serial.print ( asctime ( &timeinfo ) );
432
+ #endif
433
+ }
434
+
435
+ #endif
436
+
381
437
void heartBeatPrint ()
382
438
{
439
+ #if USE_ESP_WIFIMANAGER_NTP
440
+ printLocalTime ();
441
+ #else
383
442
static int num = 1 ;
384
443
385
444
if (WiFi.status () == WL_CONNECTED)
@@ -396,6 +455,7 @@ void heartBeatPrint()
396
455
{
397
456
Serial.print (F (" " ));
398
457
}
458
+ #endif
399
459
}
400
460
401
461
void check_WiFi ()
@@ -415,7 +475,12 @@ void check_status()
415
475
static ulong current_millis;
416
476
417
477
#define WIFICHECK_INTERVAL 1000L
418
- #define HEARTBEAT_INTERVAL 10000L
478
+
479
+ #if USE_ESP_WIFIMANAGER_NTP
480
+ #define HEARTBEAT_INTERVAL 60000L
481
+ #else
482
+ #define HEARTBEAT_INTERVAL 10000L
483
+ #endif
419
484
420
485
current_millis = millis ();
421
486
@@ -434,6 +499,18 @@ void check_status()
434
499
}
435
500
}
436
501
502
+ int calcChecksum (uint8_t * address, uint16_t sizeToCalc)
503
+ {
504
+ uint16_t checkSum = 0 ;
505
+
506
+ for (uint16_t index = 0 ; index < sizeToCalc; index++)
507
+ {
508
+ checkSum += * ( ( (byte*) address ) + index);
509
+ }
510
+
511
+ return checkSum;
512
+ }
513
+
437
514
bool loadConfigData ()
438
515
{
439
516
File file = FileFS.open (CONFIG_FILENAME, " r" );
@@ -444,18 +521,25 @@ bool loadConfigData()
444
521
// New in v1.4.0
445
522
memset (&WM_STA_IPconfig, 0 , sizeof (WM_STA_IPconfig));
446
523
// ////
447
-
524
+
448
525
if (file)
449
526
{
450
527
file.readBytes ((char *) &WM_config, sizeof (WM_config));
451
528
452
529
// New in v1.4.0
453
530
file.readBytes ((char *) &WM_STA_IPconfig, sizeof (WM_STA_IPconfig));
454
531
// ////
455
-
532
+
456
533
file.close ();
457
534
LOGERROR (F (" OK" ));
458
535
536
+ if ( WM_config.checksum != calcChecksum ( (uint8_t *) &WM_config, sizeof (WM_config) - sizeof (WM_config.checksum ) ) )
537
+ {
538
+ LOGERROR (F (" WM_config checksum wrong" ));
539
+
540
+ return false ;
541
+ }
542
+
459
543
// New in v1.4.0
460
544
displayIPConfigStruct (WM_STA_IPconfig);
461
545
// ////
@@ -469,22 +553,24 @@ bool loadConfigData()
469
553
return false ;
470
554
}
471
555
}
472
-
556
+
473
557
void saveConfigData ()
474
558
{
475
559
File file = FileFS.open (CONFIG_FILENAME, " w" );
476
560
LOGERROR (F (" SaveWiFiCfgFile " ));
477
561
478
562
if (file)
479
563
{
480
- file.write ((uint8_t *) &WM_config, sizeof (WM_config));
564
+ WM_config.checksum = calcChecksum ( (uint8_t *) &WM_config, sizeof (WM_config) - sizeof (WM_config.checksum ) );
565
+
566
+ file.write ((uint8_t *) &WM_config, sizeof (WM_config));
481
567
482
568
displayIPConfigStruct (WM_STA_IPconfig);
483
569
484
570
// New in v1.4.0
485
571
file.write ((uint8_t *) &WM_STA_IPconfig, sizeof (WM_STA_IPconfig));
486
572
// ////
487
-
573
+
488
574
file.close ();
489
575
LOGERROR (F (" OK" ));
490
576
}
@@ -559,7 +645,7 @@ void setup()
559
645
// Use this to personalize DHCP hostname (RFC952 conformed)
560
646
AsyncWebServer webServer (HTTP_PORT);
561
647
562
- #if ( ARDUINO_ESP32S2_DEV || ARDUINO_FEATHERS2 || ARDUINO_PROS2 || ARDUINO_MICROS2 )
648
+ #if ( USING_ESP32_S2 || USING_ESP32_C3 )
563
649
ESPAsync_WiFiManager ESPAsync_wifiManager (&webServer, NULL , " AutoConnectAP" );
564
650
#else
565
651
DNSServer dnsServer;
@@ -572,10 +658,12 @@ void setup()
572
658
// reset settings - for testing
573
659
// ESPAsync_wifiManager.resetSettings();
574
660
661
+ #if USE_CUSTOM_AP_IP
575
662
// set custom ip for portal
576
663
// New in v1.4.0
577
664
ESPAsync_wifiManager.setAPStaticIPConfig (WM_AP_IPconfig);
578
665
// ////
666
+ #endif
579
667
580
668
ESPAsync_wifiManager.setMinimumSignalQuality (-1 );
581
669
@@ -616,12 +704,31 @@ void setup()
616
704
ESPAsync_wifiManager.setConfigPortalTimeout (120 ); // If no access point name has been previously entered disable timeout.
617
705
Serial.println (F (" Got ESP Self-Stored Credentials. Timeout 120s for Config Portal" ));
618
706
}
619
- else if (loadConfigData ())
707
+
708
+ if (loadConfigData ())
620
709
{
621
710
configDataLoaded = true ;
622
711
623
712
ESPAsync_wifiManager.setConfigPortalTimeout (120 ); // If no access point name has been previously entered disable timeout.
624
- Serial.println (F (" Got stored Credentials. Timeout 120s for Config Portal" ));
713
+ Serial.println (F (" Got stored Credentials. Timeout 120s for Config Portal" ));
714
+
715
+ #if USE_ESP_WIFIMANAGER_NTP
716
+ if ( strlen (WM_config.TZ_Name ) > 0 )
717
+ {
718
+ LOGERROR3 (F (" Current TZ_Name =" ), WM_config.TZ_Name , F (" , TZ = " ), WM_config.TZ );
719
+
720
+ #if ESP8266
721
+ configTime (WM_config.TZ , " pool.ntp.org" );
722
+ #else
723
+ // configTzTime(WM_config.TZ, "pool.ntp.org" );
724
+ configTzTime (WM_config.TZ , " time.nist.gov" , " 0.pool.ntp.org" , " 1.pool.ntp.org" );
725
+ #endif
726
+ }
727
+ else
728
+ {
729
+ Serial.println (F (" Current Timezone is not set. Enter Config Portal to set." ));
730
+ }
731
+ #endif
625
732
}
626
733
else
627
734
{
@@ -641,6 +748,19 @@ void setup()
641
748
{
642
749
Serial.println (F (" We haven't got any access point credentials, so get them now" ));
643
750
751
+ Serial.print (F (" Starting configuration portal @ " ));
752
+
753
+ #if USE_CUSTOM_AP_IP
754
+ Serial.print (APStaticIP);
755
+ #else
756
+ Serial.print (F (" 192.168.4.1" ));
757
+ #endif
758
+
759
+ Serial.print (F (" , SSID = " ));
760
+ Serial.print (AP_SSID);
761
+ Serial.print (F (" , PWD = " ));
762
+ Serial.println (AP_PASS);
763
+
644
764
// Starts an access point
645
765
// if (!ESPAsync_wifiManager.startConfigPortal((const char *) ssid.c_str(), password))
646
766
if ( !ESPAsync_wifiManager.startConfigPortal (AP_SSID.c_str (), AP_PASS.c_str ()) )
@@ -674,6 +794,38 @@ void setup()
674
794
}
675
795
}
676
796
797
+ #if USE_ESP_WIFIMANAGER_NTP
798
+ String tempTZ = ESPAsync_wifiManager.getTimezoneName ();
799
+
800
+ if (strlen (tempTZ.c_str ()) < sizeof (WM_config.TZ_Name ) - 1 )
801
+ strcpy (WM_config.TZ_Name , tempTZ.c_str ());
802
+ else
803
+ strncpy (WM_config.TZ_Name , tempTZ.c_str (), sizeof (WM_config.TZ_Name ) - 1 );
804
+
805
+ const char * TZ_Result = ESPAsync_wifiManager.getTZ (WM_config.TZ_Name );
806
+
807
+ if (strlen (TZ_Result) < sizeof (WM_config.TZ ) - 1 )
808
+ strcpy (WM_config.TZ , TZ_Result);
809
+ else
810
+ strncpy (WM_config.TZ , TZ_Result, sizeof (WM_config.TZ_Name ) - 1 );
811
+
812
+ if ( strlen (WM_config.TZ_Name ) > 0 )
813
+ {
814
+ LOGERROR3 (F (" Saving current TZ_Name =" ), WM_config.TZ_Name , F (" , TZ = " ), WM_config.TZ );
815
+
816
+ #if ESP8266
817
+ configTime (WM_config.TZ , " pool.ntp.org" );
818
+ #else
819
+ // configTzTime(WM_config.TZ, "pool.ntp.org" );
820
+ configTzTime (WM_config.TZ , " time.nist.gov" , " 0.pool.ntp.org" , " 1.pool.ntp.org" );
821
+ #endif
822
+ }
823
+ else
824
+ {
825
+ LOGERROR (F (" Current Timezone Name is not set. Enter Config Portal to set." ));
826
+ }
827
+ #endif
828
+
677
829
// New in v1.4.0
678
830
ESPAsync_wifiManager.getSTAStaticIPConfig (WM_STA_IPconfig);
679
831
// ////
0 commit comments