Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit d7ab9a9

Browse files
authored
Major Releases v1.8.0 to use auto-TZ
### Major Releases v1.8.0 1. Add auto-Timezone feature with variable `_timezoneName` (e.g. `America/New_York`) and function to retrieve TZ (e.g. `EST5EDT,M3.2.0,M11.1.0`) to use directly to configure ESP32/ESP8266 timezone. Check [How to retrieve timezone? #51](#51) 2. Store those `_timezoneName` and `TZ` in LittleFS or SPIFFS config file. 3. Using these new timezone feature is optional. 4. Add checksum in config file to validate data read from LittleFS or SPIFFS config file. 5. Update examples to show how to use the new TZ feature.
1 parent f28252c commit d7ab9a9

File tree

39 files changed

+11572
-545
lines changed

39 files changed

+11572
-545
lines changed

README.md

Lines changed: 598 additions & 56 deletions
Large diffs are not rendered by default.

examples/Async_AutoConnect/Async_AutoConnect.ino

Lines changed: 164 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
Built by Khoi Hoang https://github.com/khoih-prog/ESPAsync_WiFiManager
1515
Licensed under MIT license
16-
Version: 1.7.1
16+
Version: 1.8.0
1717
1818
Version Modified By Date Comments
1919
------- ----------- ---------- -----------
@@ -35,12 +35,13 @@
3535
1.6.3 K Hoang 13/04/2021 Allow captive portal to run more than once by closing dnsServer.
3636
1.7.0 K Hoang 20/04/2021 Add support to new ESP32-C3 using SPIFFS or EEPROM
3737
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.)
3839
*****************************************************************************************************************************/
3940
#if !( defined(ESP8266) || defined(ESP32) )
4041
#error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting.
4142
#endif
4243

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"
4445

4546
// Use from 0 to 4. Higher number, more debugging messages and memory usage.
4647
#define _ESPASYNC_WIFIMGR_LOGLEVEL_ 3
@@ -154,9 +155,16 @@ typedef struct
154155

155156
#define NUM_WIFI_CREDENTIALS 2
156157

158+
// Assuming max 49 chars
159+
#define TZNAME_MAX_LEN 50
160+
#define TIMEZONE_MAX_LEN 50
161+
157162
typedef struct
158163
{
159164
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;
160168
} WM_Config;
161169

162170
WM_Config WM_config;
@@ -179,7 +187,20 @@ bool initialConfig = false;
179187

180188
// Use false to disable NTP config. Advisable when using Cellphone, Tablet to access Config Portal.
181189
// 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
183204

184205
// Use true to enable CloudFlare NTP service. System can hang if you don't have Internet access while accessing CloudFlare
185206
// 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;
227248
IPAddress dns1IP = gatewayIP;
228249
IPAddress dns2IP = IPAddress(8, 8, 8, 8);
229250

251+
#define USE_CUSTOM_AP_IP false
252+
230253
IPAddress APStaticIP = IPAddress(192, 168, 100, 1);
231254
IPAddress APStaticGW = IPAddress(192, 168, 100, 1);
232255
IPAddress APStaticSN = IPAddress(255, 255, 255, 0);
@@ -373,13 +396,49 @@ uint8_t connectMultiWiFi()
373396
LOGERROR3(F("Channel:"), WiFi.channel(), F(",IP address:"), WiFi.localIP() );
374397
}
375398
else
399+
{
376400
LOGERROR(F("WiFi not connected"));
401+
402+
#if ESP8266
403+
ESP.reset();
404+
#else
405+
ESP.restart();
406+
#endif
407+
}
377408

378409
return status;
379410
}
380411

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+
381437
void heartBeatPrint()
382438
{
439+
#if USE_ESP_WIFIMANAGER_NTP
440+
printLocalTime();
441+
#else
383442
static int num = 1;
384443

385444
if (WiFi.status() == WL_CONNECTED)
@@ -396,6 +455,7 @@ void heartBeatPrint()
396455
{
397456
Serial.print(F(" "));
398457
}
458+
#endif
399459
}
400460

401461
void check_WiFi()
@@ -415,7 +475,12 @@ void check_status()
415475
static ulong current_millis;
416476

417477
#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
419484

420485
current_millis = millis();
421486

@@ -434,6 +499,18 @@ void check_status()
434499
}
435500
}
436501

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+
437514
bool loadConfigData()
438515
{
439516
File file = FileFS.open(CONFIG_FILENAME, "r");
@@ -444,18 +521,25 @@ bool loadConfigData()
444521
// New in v1.4.0
445522
memset(&WM_STA_IPconfig, 0, sizeof(WM_STA_IPconfig));
446523
//////
447-
524+
448525
if (file)
449526
{
450527
file.readBytes((char *) &WM_config, sizeof(WM_config));
451528

452529
// New in v1.4.0
453530
file.readBytes((char *) &WM_STA_IPconfig, sizeof(WM_STA_IPconfig));
454531
//////
455-
532+
456533
file.close();
457534
LOGERROR(F("OK"));
458535

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+
459543
// New in v1.4.0
460544
displayIPConfigStruct(WM_STA_IPconfig);
461545
//////
@@ -469,22 +553,24 @@ bool loadConfigData()
469553
return false;
470554
}
471555
}
472-
556+
473557
void saveConfigData()
474558
{
475559
File file = FileFS.open(CONFIG_FILENAME, "w");
476560
LOGERROR(F("SaveWiFiCfgFile "));
477561

478562
if (file)
479563
{
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));
481567

482568
displayIPConfigStruct(WM_STA_IPconfig);
483569

484570
// New in v1.4.0
485571
file.write((uint8_t*) &WM_STA_IPconfig, sizeof(WM_STA_IPconfig));
486572
//////
487-
573+
488574
file.close();
489575
LOGERROR(F("OK"));
490576
}
@@ -559,7 +645,7 @@ void setup()
559645
// Use this to personalize DHCP hostname (RFC952 conformed)
560646
AsyncWebServer webServer(HTTP_PORT);
561647

562-
#if ( ARDUINO_ESP32S2_DEV || ARDUINO_FEATHERS2 || ARDUINO_PROS2 || ARDUINO_MICROS2 )
648+
#if ( USING_ESP32_S2 || USING_ESP32_C3 )
563649
ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, NULL, "AutoConnectAP");
564650
#else
565651
DNSServer dnsServer;
@@ -572,10 +658,12 @@ void setup()
572658
//reset settings - for testing
573659
//ESPAsync_wifiManager.resetSettings();
574660

661+
#if USE_CUSTOM_AP_IP
575662
//set custom ip for portal
576663
// New in v1.4.0
577664
ESPAsync_wifiManager.setAPStaticIPConfig(WM_AP_IPconfig);
578665
//////
666+
#endif
579667

580668
ESPAsync_wifiManager.setMinimumSignalQuality(-1);
581669

@@ -616,12 +704,31 @@ void setup()
616704
ESPAsync_wifiManager.setConfigPortalTimeout(120); //If no access point name has been previously entered disable timeout.
617705
Serial.println(F("Got ESP Self-Stored Credentials. Timeout 120s for Config Portal"));
618706
}
619-
else if (loadConfigData())
707+
708+
if (loadConfigData())
620709
{
621710
configDataLoaded = true;
622711

623712
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
625732
}
626733
else
627734
{
@@ -641,6 +748,19 @@ void setup()
641748
{
642749
Serial.println(F("We haven't got any access point credentials, so get them now"));
643750

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+
644764
// Starts an access point
645765
//if (!ESPAsync_wifiManager.startConfigPortal((const char *) ssid.c_str(), password))
646766
if ( !ESPAsync_wifiManager.startConfigPortal(AP_SSID.c_str(), AP_PASS.c_str()) )
@@ -674,6 +794,38 @@ void setup()
674794
}
675795
}
676796

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+
677829
// New in v1.4.0
678830
ESPAsync_wifiManager.getSTAStaticIPConfig(WM_STA_IPconfig);
679831
//////

0 commit comments

Comments
 (0)