From 00f0753d540678c83c78e48622dc0f08caf2f904 Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Sat, 8 Jan 2022 20:47:53 -0500 Subject: [PATCH] v1.5.0 to fix the blocking issue in loop() ### Release v1.5.0 1. Fix the blocking issue in loop(). Check [retries block the main loop #18](https://github.com/khoih-prog/WiFiManager_NINA_Lite/issues/18) 2. Configurable `WIFI_RECON_INTERVAL`. Check [retries block the main loop #18](https://github.com/khoih-prog/WiFiManager_NINA_Lite/issues/18#issuecomment-1006197561) 3. Update `Packages' Patches` --- src/Esp8266_AT_WM_Lite.h | 83 ++++++++++++++++++----- src/Esp8266_AT_WM_Lite_DUE.h | 88 ++++++++++++++++++------ src/Esp8266_AT_WM_Lite_Debug.h | 11 +-- src/Esp8266_AT_WM_Lite_Mbed_RPi_Pico.h | 88 ++++++++++++++++++------ src/Esp8266_AT_WM_Lite_RPi_Pico.h | 94 +++++++++++++++++++------- src/Esp8266_AT_WM_Lite_SAMD.h | 90 ++++++++++++++++++------ src/Esp8266_AT_WM_Lite_STM32.h | 88 ++++++++++++++++++------ src/Esp8266_AT_WM_Lite_nRF52.h | 88 ++++++++++++++++++------ 8 files changed, 483 insertions(+), 147 deletions(-) diff --git a/src/Esp8266_AT_WM_Lite.h b/src/Esp8266_AT_WM_Lite.h index b7d7cc6..cdba822 100644 --- a/src/Esp8266_AT_WM_Lite.h +++ b/src/Esp8266_AT_WM_Lite.h @@ -8,23 +8,24 @@ Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_WM_Lite Licensed under MIT license - Version: 1.4.1 + Version: 1.5.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 09/03/2020 Initial coding 1.0.1 K Hoang 20/03/2020 Add feature to enable adding dynamically more Credentials parameters in sketch 1.0.2 K Hoang 17/04/2020 Fix bug. Add support to SAMD51 and SAMD DUE. WPA2 SSID PW to 63 chars. - Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. + Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. 1.0.3 K Hoang 11/06/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, NINA_B30_ublox, etc. - Add DRD support. Add MultiWiFi support + Add DRD support. Add MultiWiFi support 1.0.4 K Hoang 03/07/2020 Add support to ESP32-AT shields. Modify LOAD_DEFAULT_CONFIG_DATA logic. - Enhance MultiWiFi connection logic. Fix WiFi Status bug. + Enhance MultiWiFi connection logic. Fix WiFi Status bug. 1.1.0 K Hoang 13/04/2021 Fix invalid "blank" Config Data treated as Valid. Optional one set of WiFi Credentials 1.2.0 Michael H 28/04/2021 Enable scan of WiFi networks for selection in Configuration Portal 1.3.0 K Hoang 12/05/2021 Add support to RASPBERRY_PI_PICO using Arduino-pico core 1.4.0 K Hoang 01/06/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using RP2040 Arduino mbed core - 1.4.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json` + 1.4.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json` + 1.5.0 K Hoang 08/01/2022 Fix the blocking issue in loop() with configurable WIFI_RECON_INTERVAL ***************************************************************************************************************************************/ #ifndef Esp8266_AT_WM_Lite_h @@ -38,7 +39,7 @@ #error This code is intended to run on the Mega2560 platform! Please check your Tools->Board setting. #endif -#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.4.1" +#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.5.0" #define DEFAULT_BOARD_NAME "AVR-MEGA" @@ -295,6 +296,16 @@ class ESP_AT_WiFiManager_Lite ////////////////////////////////////////////// +#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; @@ -306,6 +317,10 @@ class ESP_AT_WiFiManager_Lite static unsigned long checkstatus_timeout = 0; #define WIFI_STATUS_CHECK_INTERVAL 5000L + static uint32_t curMillis; + + curMillis = millis(); + //// New DRD //// // Call the double reset detector loop method every so often, // so that it can recognise when the timeout expires. @@ -313,8 +328,8 @@ class ESP_AT_WiFiManager_Lite // consider the next reset as a double reset. drd->loop(); //// New DRD //// - - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -326,7 +341,7 @@ class ESP_AT_WiFiManager_Lite { wifiDisconnectedOnce = false; wifi_connected = false; - ESP_AT_LOGDEBUG(F("r:Check&WLost")); + ESP_AT_LOGERROR(F("r:Check&WLost")); } else { @@ -334,8 +349,8 @@ class ESP_AT_WiFiManager_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; - } + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; + } // Lost connection in running. Give chance to reconfig. if ( !wifi_connected ) @@ -348,7 +363,7 @@ class ESP_AT_WiFiManager_Lite if (server) { - //ESP_AT_LOGDEBUG(F("r:hC")); + //ESP_AT_LOGDEBUG(F("r:handleClient")); server->handleClient(); } @@ -363,7 +378,7 @@ class ESP_AT_WiFiManager_Lite { if (++retryTimes <= CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET) { - ESP_AT_LOGDEBUG1(F("r:Wlost&TOut.ConW.Retry#"), retryTimes); + ESP_AT_LOGERROR1(F("r:WLost&TOut.ConW.Retry#"), retryTimes); } else { @@ -375,18 +390,38 @@ class ESP_AT_WiFiManager_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { - ESP_AT_LOGDEBUG(F("r:Wlost.ReconW")); + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + ESP_AT_LOGERROR(F("r:WLost.ReconW")); + + if (connectToWifi(RETRY_TIMES_RECONNECT_WIFI)) + { + ESP_AT_LOGERROR(F("r:WOK")); + } + } +#else + ESP_AT_LOGERROR(F("r:WLost.ReconW")); + if (connectToWifi(RETRY_TIMES_RECONNECT_WIFI)) { - ESP_AT_LOGDEBUG(F("r:WOK")); + ESP_AT_LOGERROR(F("r:WOK")); } +#endif } } } else if (configuration_mode) { configuration_mode = false; - ESP_AT_LOGDEBUG(F("r:gotWBack")); + ESP_AT_LOGERROR(F("r:gotWBack")); } } @@ -984,8 +1019,18 @@ class ESP_AT_WiFiManager_Lite } ////////////////////////////////////////////// + +// Max times to try WiFi per loop() iteration. To avoid blocking issue in loop() +// Default 1 and minimum 1. +#if !defined(MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 +#else + #if (MAX_NUM_WIFI_RECON_TRIES_PER_LOOP < 1) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 + #endif +#endif -// New connection logic for ESP32-AT from v1.0.6 + // New connection logic for ESP32-AT from v1.0.6 bool connectToWifi(int retry_time) { int sleep_time = 250; @@ -1000,8 +1045,10 @@ class ESP_AT_WiFiManager_Lite } ESP_AT_LOGDEBUG3(F("con2WF:SSID="), ESP8266_AT_config.wifi_ssid, F(",PW="), ESP8266_AT_config.wifi_pw); + + uint8_t numWiFiReconTries = 0; - while ( !wifi_connected && ( 0 < retry_time ) ) + while ( !wifi_connected && ( 0 < retry_time ) && (numWiFiReconTries++ < MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) ) { ESP_AT_LOGDEBUG1(F("Remaining retry_time="), retry_time); diff --git a/src/Esp8266_AT_WM_Lite_DUE.h b/src/Esp8266_AT_WM_Lite_DUE.h index b759a52..5c536dd 100644 --- a/src/Esp8266_AT_WM_Lite_DUE.h +++ b/src/Esp8266_AT_WM_Lite_DUE.h @@ -8,23 +8,24 @@ Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_WM_Lite Licensed under MIT license - Version: 1.4.1 + Version: 1.5.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 09/03/2020 Initial coding 1.0.1 K Hoang 20/03/2020 Add feature to enable adding dynamically more Credentials parameters in sketch 1.0.2 K Hoang 17/04/2020 Fix bug. Add support to SAMD51 and SAMD DUE. WPA2 SSID PW to 63 chars. - Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. + Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. 1.0.3 K Hoang 11/06/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, NINA_B30_ublox, etc. - Add DRD support. Add MultiWiFi support + Add DRD support. Add MultiWiFi support 1.0.4 K Hoang 03/07/2020 Add support to ESP32-AT shields. Modify LOAD_DEFAULT_CONFIG_DATA logic. - Enhance MultiWiFi connection logic. Fix WiFi Status bug. + Enhance MultiWiFi connection logic. Fix WiFi Status bug. 1.1.0 K Hoang 13/04/2021 Fix invalid "blank" Config Data treated as Valid. Optional one set of WiFi Credentials 1.2.0 Michael H 28/04/2021 Enable scan of WiFi networks for selection in Configuration Portal 1.3.0 K Hoang 12/05/2021 Add support to RASPBERRY_PI_PICO using Arduino-pico core 1.4.0 K Hoang 01/06/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using RP2040 Arduino mbed core 1.4.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json` + 1.5.0 K Hoang 08/01/2022 Fix the blocking issue in loop() with configurable WIFI_RECON_INTERVAL ***************************************************************************************************************************************/ #ifndef Esp8266_AT_WM_Lite_DUE_h @@ -40,7 +41,7 @@ #error This code is intended to run on the SAM DUE platform! Please check your Tools->Board setting. #endif -#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.4.1" +#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.5.0" #define DEFAULT_BOARD_NAME "SAM-DUE" @@ -427,6 +428,16 @@ class ESP_AT_WiFiManager_Lite ////////////////////////////////////////////// +#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; @@ -438,6 +449,10 @@ class ESP_AT_WiFiManager_Lite static unsigned long checkstatus_timeout = 0; #define WIFI_STATUS_CHECK_INTERVAL 5000L + static uint32_t curMillis; + + curMillis = millis(); + //// New DRD //// // Call the double reset detector loop method every so often, // so that it can recognise when the timeout expires. @@ -446,7 +461,7 @@ class ESP_AT_WiFiManager_Lite drd->loop(); //// New DRD //// - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -458,7 +473,7 @@ class ESP_AT_WiFiManager_Lite { wifiDisconnectedOnce = false; wifi_connected = false; - ESP_AT_LOGDEBUG(F("r:Check&WLost")); + ESP_AT_LOGERROR(F("r:Check&WLost")); } else { @@ -466,7 +481,7 @@ class ESP_AT_WiFiManager_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; } // Lost connection in running. Give chance to reconfig. @@ -495,7 +510,7 @@ class ESP_AT_WiFiManager_Lite { if (++retryTimes <= CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET) { - ESP_AT_LOGDEBUG1(F("r:WLost&TOut.ConW.Retry#"), retryTimes); + ESP_AT_LOGERROR1(F("r:WLost&TOut.ConW.Retry#"), retryTimes); } else { @@ -507,19 +522,38 @@ class ESP_AT_WiFiManager_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { - ESP_AT_LOGDEBUG(F("r:WLost.ReconW")); + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + ESP_AT_LOGERROR(F("r:WLost.ReconW")); + + if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) + { + ESP_AT_LOGERROR(F("r:WOK")); + } + } +#else + ESP_AT_LOGERROR(F("r:WLost.ReconW")); if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) { - ESP_AT_LOGDEBUG(F("r:WOK")); + ESP_AT_LOGERROR(F("r:WOK")); } +#endif } } } else if (configuration_mode) { configuration_mode = false; - ESP_AT_LOGDEBUG(F("r:gotWBack")); + ESP_AT_LOGERROR(F("r:gotWBack")); } } @@ -1214,6 +1248,16 @@ class ESP_AT_WiFiManager_Lite ////////////////////////////////////////////// +// Max times to try WiFi per loop() iteration. To avoid blocking issue in loop() +// Default 1 and minimum 1. +#if !defined(MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 +#else + #if (MAX_NUM_WIFI_RECON_TRIES_PER_LOOP < 1) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 + #endif +#endif + // New connection logic from v1.2.0 bool connectMultiWiFi(int retry_time) { @@ -1224,11 +1268,11 @@ class ESP_AT_WiFiManager_Lite static int lastConnectedIndex = 255; - ESP_AT_LOGWARN(F("ConMultiWifi")); + ESP_AT_LOGDEBUG(F("ConMultiWifi")); if (static_IP != IPAddress(0, 0, 0, 0)) { - ESP_AT_LOGWARN(F("UseStatIP")); + ESP_AT_LOGDEBUG(F("UseStatIP")); WiFi.config(static_IP); } @@ -1241,7 +1285,7 @@ class ESP_AT_WiFiManager_Lite if ( strlen(ESP8266_AT_config.WiFi_Creds[new_index].wifi_pw) >= PASSWORD_MIN_LEN ) { index = new_index; - ESP_AT_LOGWARN3(F("Using index="), index, F(", lastConnectedIndex="), lastConnectedIndex); + ESP_AT_LOGDEBUG3(F("Using index="), index, F(", lastConnectedIndex="), lastConnectedIndex); } else { @@ -1256,7 +1300,7 @@ class ESP_AT_WiFiManager_Lite // First connection ever, index = 0 if ( strlen(ESP8266_AT_config.WiFi_Creds[0].wifi_pw) >= PASSWORD_MIN_LEN ) { - ESP_AT_LOGWARN(F("First connection, Using index=0")); + ESP_AT_LOGDEBUG(F("First connection, Using index=0")); } else { @@ -1273,11 +1317,13 @@ class ESP_AT_WiFiManager_Lite uint8_t numIndexTried = 0; - while ( !wifi_connected && (numIndexTried++ < NUM_WIFI_CREDENTIALS) ) + uint8_t numWiFiReconTries = 0; + + while ( !wifi_connected && (numIndexTried++ < NUM_WIFI_CREDENTIALS) && (numWiFiReconTries++ < MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) ) { while ( 0 < retry_time ) { - ESP_AT_LOGWARN1(F("Remaining retry_time="), retry_time); + ESP_AT_LOGDEBUG1(F("Remaining retry_time="), retry_time); status = WiFi.begin(ESP8266_AT_config.WiFi_Creds[index].wifi_ssid, ESP8266_AT_config.WiFi_Creds[index].wifi_pw); @@ -1286,7 +1332,7 @@ class ESP_AT_WiFiManager_Lite { wifi_connected = true; lastConnectedIndex = index; - ESP_AT_LOGWARN1(F("WOK, lastConnectedIndex="), lastConnectedIndex); + ESP_AT_LOGDEBUG1(F("WOK, lastConnectedIndex="), lastConnectedIndex); break; } @@ -1324,7 +1370,9 @@ class ESP_AT_WiFiManager_Lite if (wifi_connected) { ESP_AT_LOGERROR(F("con2WF:OK")); - + + ESP_AT_LOGERROR1(F("IP="), WiFi.localIP() ); + displayWiFiData(); } else diff --git a/src/Esp8266_AT_WM_Lite_Debug.h b/src/Esp8266_AT_WM_Lite_Debug.h index fa6da89..17c3477 100644 --- a/src/Esp8266_AT_WM_Lite_Debug.h +++ b/src/Esp8266_AT_WM_Lite_Debug.h @@ -8,23 +8,24 @@ Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_WM_Lite Licensed under MIT license - Version: 1.4.1 + Version: 1.5.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 09/03/2020 Initial coding 1.0.1 K Hoang 20/03/2020 Add feature to enable adding dynamically more Credentials parameters in sketch 1.0.2 K Hoang 17/04/2020 Fix bug. Add support to SAMD51 and SAMD DUE. WPA2 SSID PW to 63 chars. - Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. + Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. 1.0.3 K Hoang 11/06/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, NINA_B30_ublox, etc. - Add DRD support. Add MultiWiFi support + Add DRD support. Add MultiWiFi support 1.0.4 K Hoang 03/07/2020 Add support to ESP32-AT shields. Modify LOAD_DEFAULT_CONFIG_DATA logic. - Enhance MultiWiFi connection logic. Fix WiFi Status bug. + Enhance MultiWiFi connection logic. Fix WiFi Status bug. 1.1.0 K Hoang 13/04/2021 Fix invalid "blank" Config Data treated as Valid. Optional one set of WiFi Credentials 1.2.0 Michael H 28/04/2021 Enable scan of WiFi networks for selection in Configuration Portal 1.3.0 K Hoang 12/05/2021 Add support to RASPBERRY_PI_PICO using Arduino-pico core 1.4.0 K Hoang 01/06/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using RP2040 Arduino mbed core - 1.4.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json` + 1.4.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json` + 1.5.0 K Hoang 08/01/2022 Fix the blocking issue in loop() with configurable WIFI_RECON_INTERVAL ***************************************************************************************************************************************/ #ifndef Esp8266_AT_WM_Lite_Debug_h diff --git a/src/Esp8266_AT_WM_Lite_Mbed_RPi_Pico.h b/src/Esp8266_AT_WM_Lite_Mbed_RPi_Pico.h index 5dfc342..10e2421 100644 --- a/src/Esp8266_AT_WM_Lite_Mbed_RPi_Pico.h +++ b/src/Esp8266_AT_WM_Lite_Mbed_RPi_Pico.h @@ -8,23 +8,24 @@ Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_WM_Lite Licensed under MIT license - Version: 1.4.1 + Version: 1.5.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 09/03/2020 Initial coding 1.0.1 K Hoang 20/03/2020 Add feature to enable adding dynamically more Credentials parameters in sketch 1.0.2 K Hoang 17/04/2020 Fix bug. Add support to SAMD51 and SAMD DUE. WPA2 SSID PW to 63 chars. - Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. + Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. 1.0.3 K Hoang 11/06/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, NINA_B30_ublox, etc. - Add DRD support. Add MultiWiFi support + Add DRD support. Add MultiWiFi support 1.0.4 K Hoang 03/07/2020 Add support to ESP32-AT shields. Modify LOAD_DEFAULT_CONFIG_DATA logic. - Enhance MultiWiFi connection logic. Fix WiFi Status bug. + Enhance MultiWiFi connection logic. Fix WiFi Status bug. 1.1.0 K Hoang 13/04/2021 Fix invalid "blank" Config Data treated as Valid. Optional one set of WiFi Credentials 1.2.0 Michael H 28/04/2021 Enable scan of WiFi networks for selection in Configuration Portal 1.3.0 K Hoang 12/05/2021 Add support to RASPBERRY_PI_PICO using Arduino-pico core 1.4.0 K Hoang 01/06/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using RP2040 Arduino mbed core 1.4.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json` + 1.5.0 K Hoang 08/01/2022 Fix the blocking issue in loop() with configurable WIFI_RECON_INTERVAL ***************************************************************************************************************************************/ #ifndef Esp8266_AT_WM_Lite_Mbed_RPi_Pico_h @@ -40,7 +41,7 @@ #error This code is intended to run on the RP2040 platform! Please check your Tools->Board setting. #endif -#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.4.1" +#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.5.0" #define DEFAULT_BOARD_NAME "MBED-RP2040" @@ -429,6 +430,16 @@ class ESP_AT_WiFiManager_Lite ////////////////////////////////////////////// +#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; @@ -440,6 +451,10 @@ class ESP_AT_WiFiManager_Lite static unsigned long checkstatus_timeout = 0; #define WIFI_STATUS_CHECK_INTERVAL 5000L + static uint32_t curMillis; + + curMillis = millis(); + //// New DRD //// // Call the double reset detector loop method every so often, // so that it can recognise when the timeout expires. @@ -448,7 +463,7 @@ class ESP_AT_WiFiManager_Lite drd->loop(); //// New DRD //// - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -460,7 +475,7 @@ class ESP_AT_WiFiManager_Lite { wifiDisconnectedOnce = false; wifi_connected = false; - ESP_AT_LOGDEBUG(F("r:Check&WLost")); + ESP_AT_LOGERROR(F("r:Check&WLost")); } else { @@ -468,7 +483,7 @@ class ESP_AT_WiFiManager_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; } // Lost connection in running. Give chance to reconfig. @@ -497,7 +512,7 @@ class ESP_AT_WiFiManager_Lite { if (++retryTimes <= CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET) { - ESP_AT_LOGDEBUG1(F("r:WLost&TOut.ConW.Retry#"), retryTimes); + ESP_AT_LOGERROR1(F("r:WLost&TOut.ConW.Retry#"), retryTimes); } else { @@ -509,19 +524,38 @@ class ESP_AT_WiFiManager_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { - ESP_AT_LOGDEBUG(F("r:WLost.ReconW")); + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + ESP_AT_LOGERROR(F("r:WLost.ReconW")); + + if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) + { + ESP_AT_LOGERROR(F("r:WOK")); + } + } +#else + ESP_AT_LOGERROR(F("r:WLost.ReconW")); if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) { - ESP_AT_LOGDEBUG(F("r:WOK")); + ESP_AT_LOGERROR(F("r:WOK")); } +#endif } } } else if (configuration_mode) { configuration_mode = false; - ESP_AT_LOGDEBUG(F("r:gotWBack")); + ESP_AT_LOGERROR(F("r:gotWBack")); } } @@ -1388,6 +1422,16 @@ class ESP_AT_WiFiManager_Lite ////////////////////////////////////////////// +// Max times to try WiFi per loop() iteration. To avoid blocking issue in loop() +// Default 1 and minimum 1. +#if !defined(MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 +#else + #if (MAX_NUM_WIFI_RECON_TRIES_PER_LOOP < 1) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 + #endif +#endif + // New connection logic from v1.2.0 bool connectMultiWiFi(int retry_time) { @@ -1398,11 +1442,11 @@ class ESP_AT_WiFiManager_Lite static int lastConnectedIndex = 255; - ESP_AT_LOGWARN(F("ConMultiWifi")); + ESP_AT_LOGDEBUG(F("ConMultiWifi")); if (static_IP != IPAddress(0, 0, 0, 0)) { - ESP_AT_LOGWARN(F("UseStatIP")); + ESP_AT_LOGDEBUG(F("UseStatIP")); WiFi.config(static_IP); } @@ -1415,7 +1459,7 @@ class ESP_AT_WiFiManager_Lite if ( strlen(ESP8266_AT_config.WiFi_Creds[new_index].wifi_pw) >= PASSWORD_MIN_LEN ) { index = new_index; - ESP_AT_LOGWARN3(F("Using index="), index, F(", lastConnectedIndex="), lastConnectedIndex); + ESP_AT_LOGDEBUG3(F("Using index="), index, F(", lastConnectedIndex="), lastConnectedIndex); } else { @@ -1430,7 +1474,7 @@ class ESP_AT_WiFiManager_Lite // First connection ever, index = 0 if ( strlen(ESP8266_AT_config.WiFi_Creds[0].wifi_pw) >= PASSWORD_MIN_LEN ) { - ESP_AT_LOGWARN(F("First connection, Using index=0")); + ESP_AT_LOGDEBUG(F("First connection, Using index=0")); } else { @@ -1447,11 +1491,13 @@ class ESP_AT_WiFiManager_Lite uint8_t numIndexTried = 0; - while ( !wifi_connected && (numIndexTried++ < NUM_WIFI_CREDENTIALS) ) + uint8_t numWiFiReconTries = 0; + + while ( !wifi_connected && (numIndexTried++ < NUM_WIFI_CREDENTIALS) && (numWiFiReconTries++ < MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) ) { while ( 0 < retry_time ) { - ESP_AT_LOGWARN1(F("Remaining retry_time="), retry_time); + ESP_AT_LOGDEBUG1(F("Remaining retry_time="), retry_time); status = WiFi.begin(ESP8266_AT_config.WiFi_Creds[index].wifi_ssid, ESP8266_AT_config.WiFi_Creds[index].wifi_pw); @@ -1460,7 +1506,7 @@ class ESP_AT_WiFiManager_Lite { wifi_connected = true; lastConnectedIndex = index; - ESP_AT_LOGWARN1(F("WOK, lastConnectedIndex="), lastConnectedIndex); + ESP_AT_LOGDEBUG1(F("WOK, lastConnectedIndex="), lastConnectedIndex); break; } @@ -1498,7 +1544,9 @@ class ESP_AT_WiFiManager_Lite if (wifi_connected) { ESP_AT_LOGERROR(F("con2WF:OK")); - + + ESP_AT_LOGERROR1(F("IP="), WiFi.localIP() ); + displayWiFiData(); } else diff --git a/src/Esp8266_AT_WM_Lite_RPi_Pico.h b/src/Esp8266_AT_WM_Lite_RPi_Pico.h index d5c8c3a..ae56537 100644 --- a/src/Esp8266_AT_WM_Lite_RPi_Pico.h +++ b/src/Esp8266_AT_WM_Lite_RPi_Pico.h @@ -8,23 +8,24 @@ Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_WM_Lite Licensed under MIT license - Version: 1.4.1 + Version: 1.5.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 09/03/2020 Initial coding 1.0.1 K Hoang 20/03/2020 Add feature to enable adding dynamically more Credentials parameters in sketch 1.0.2 K Hoang 17/04/2020 Fix bug. Add support to SAMD51 and SAMD DUE. WPA2 SSID PW to 63 chars. - Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. + Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. 1.0.3 K Hoang 11/06/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, NINA_B30_ublox, etc. - Add DRD support. Add MultiWiFi support + Add DRD support. Add MultiWiFi support 1.0.4 K Hoang 03/07/2020 Add support to ESP32-AT shields. Modify LOAD_DEFAULT_CONFIG_DATA logic. - Enhance MultiWiFi connection logic. Fix WiFi Status bug. + Enhance MultiWiFi connection logic. Fix WiFi Status bug. 1.1.0 K Hoang 13/04/2021 Fix invalid "blank" Config Data treated as Valid. Optional one set of WiFi Credentials 1.2.0 Michael H 28/04/2021 Enable scan of WiFi networks for selection in Configuration Portal 1.3.0 K Hoang 12/05/2021 Add support to RASPBERRY_PI_PICO using Arduino-pico core 1.4.0 K Hoang 01/06/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using RP2040 Arduino mbed core 1.4.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json` + 1.5.0 K Hoang 08/01/2022 Fix the blocking issue in loop() with configurable WIFI_RECON_INTERVAL ***************************************************************************************************************************************/ #ifndef Esp8266_AT_WM_Lite_RPi_Pico_h @@ -40,7 +41,7 @@ #error This code is intended to run on the RP2040 platform! Please check your Tools->Board setting. #endif -#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.4.1" +#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.5.0" #define DEFAULT_BOARD_NAME "RP2040" @@ -431,6 +432,16 @@ class ESP_AT_WiFiManager_Lite ////////////////////////////////////////////// +#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; @@ -442,6 +453,10 @@ class ESP_AT_WiFiManager_Lite static unsigned long checkstatus_timeout = 0; #define WIFI_STATUS_CHECK_INTERVAL 5000L + static uint32_t curMillis; + + curMillis = millis(); + //// New DRD //// // Call the double reset detector loop method every so often, // so that it can recognise when the timeout expires. @@ -450,7 +465,7 @@ class ESP_AT_WiFiManager_Lite drd->loop(); //// New DRD //// - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -462,7 +477,7 @@ class ESP_AT_WiFiManager_Lite { wifiDisconnectedOnce = false; wifi_connected = false; - ESP_AT_LOGDEBUG(F("r:Check&WLost")); + ESP_AT_LOGERROR(F("r:Check&WLost")); } else { @@ -470,7 +485,7 @@ class ESP_AT_WiFiManager_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; } // Lost connection in running. Give chance to reconfig. @@ -499,7 +514,7 @@ class ESP_AT_WiFiManager_Lite { if (++retryTimes <= CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET) { - ESP_AT_LOGDEBUG1(F("r:WLost&TOut.ConW.Retry#"), retryTimes); + ESP_AT_LOGERROR1(F("r:WLost&TOut.ConW.Retry#"), retryTimes); } else { @@ -511,22 +526,41 @@ class ESP_AT_WiFiManager_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { - ESP_AT_LOGDEBUG(F("r:WLost.ReconW")); + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + ESP_AT_LOGERROR(F("r:WLost.ReconW")); + + if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) + { + ESP_AT_LOGERROR(F("r:WOK")); + } + } +#else + ESP_AT_LOGERROR(F("r:WLost.ReconW")); if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) { - ESP_AT_LOGDEBUG(F("r:WOK")); + ESP_AT_LOGERROR(F("r:WOK")); } +#endif } } } else if (configuration_mode) { configuration_mode = false; - ESP_AT_LOGDEBUG(F("r:gotWBack")); + ESP_AT_LOGERROR(F("r:gotWBack")); } } - + ////////////////////////////////////////////// void setConfigPortalIP(IPAddress portalIP = IPAddress(192, 168, 4, 1)) @@ -1391,7 +1425,17 @@ class ESP_AT_WiFiManager_Lite } ////////////////////////////////////////////// - + +// Max times to try WiFi per loop() iteration. To avoid blocking issue in loop() +// Default 1 and minimum 1. +#if !defined(MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 +#else + #if (MAX_NUM_WIFI_RECON_TRIES_PER_LOOP < 1) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 + #endif +#endif + // New connection logic from v1.2.0 bool connectMultiWiFi(int retry_time) { @@ -1402,11 +1446,11 @@ class ESP_AT_WiFiManager_Lite static int lastConnectedIndex = 255; - ESP_AT_LOGWARN(F("ConMultiWifi")); + ESP_AT_LOGDEBUG(F("ConMultiWifi")); if (static_IP != IPAddress(0, 0, 0, 0)) { - ESP_AT_LOGWARN(F("UseStatIP")); + ESP_AT_LOGDEBUG(F("UseStatIP")); WiFi.config(static_IP); } @@ -1419,7 +1463,7 @@ class ESP_AT_WiFiManager_Lite if ( strlen(ESP8266_AT_config.WiFi_Creds[new_index].wifi_pw) >= PASSWORD_MIN_LEN ) { index = new_index; - ESP_AT_LOGWARN3(F("Using index="), index, F(", lastConnectedIndex="), lastConnectedIndex); + ESP_AT_LOGDEBUG3(F("Using index="), index, F(", lastConnectedIndex="), lastConnectedIndex); } else { @@ -1434,7 +1478,7 @@ class ESP_AT_WiFiManager_Lite // First connection ever, index = 0 if ( strlen(ESP8266_AT_config.WiFi_Creds[0].wifi_pw) >= PASSWORD_MIN_LEN ) { - ESP_AT_LOGWARN(F("First connection, Using index=0")); + ESP_AT_LOGDEBUG(F("First connection, Using index=0")); } else { @@ -1451,11 +1495,13 @@ class ESP_AT_WiFiManager_Lite uint8_t numIndexTried = 0; - while ( !wifi_connected && (numIndexTried++ < NUM_WIFI_CREDENTIALS) ) + uint8_t numWiFiReconTries = 0; + + while ( !wifi_connected && (numIndexTried++ < NUM_WIFI_CREDENTIALS) && (numWiFiReconTries++ < MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) ) { while ( 0 < retry_time ) { - ESP_AT_LOGWARN1(F("Remaining retry_time="), retry_time); + ESP_AT_LOGDEBUG1(F("Remaining retry_time="), retry_time); status = WiFi.begin(ESP8266_AT_config.WiFi_Creds[index].wifi_ssid, ESP8266_AT_config.WiFi_Creds[index].wifi_pw); @@ -1464,7 +1510,7 @@ class ESP_AT_WiFiManager_Lite { wifi_connected = true; lastConnectedIndex = index; - ESP_AT_LOGWARN1(F("WOK, lastConnectedIndex="), lastConnectedIndex); + ESP_AT_LOGDEBUG1(F("WOK, lastConnectedIndex="), lastConnectedIndex); break; } @@ -1502,7 +1548,9 @@ class ESP_AT_WiFiManager_Lite if (wifi_connected) { ESP_AT_LOGERROR(F("con2WF:OK")); - + + ESP_AT_LOGERROR1(F("IP="), WiFi.localIP() ); + displayWiFiData(); } else @@ -1514,7 +1562,7 @@ class ESP_AT_WiFiManager_Lite return wifi_connected; } - + ////////////////////////////////////////////// // NEW diff --git a/src/Esp8266_AT_WM_Lite_SAMD.h b/src/Esp8266_AT_WM_Lite_SAMD.h index b35a1b9..9f8b5a6 100644 --- a/src/Esp8266_AT_WM_Lite_SAMD.h +++ b/src/Esp8266_AT_WM_Lite_SAMD.h @@ -8,23 +8,24 @@ Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_WM_Lite Licensed under MIT license - Version: 1.4.1 + Version: 1.5.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 09/03/2020 Initial coding 1.0.1 K Hoang 20/03/2020 Add feature to enable adding dynamically more Credentials parameters in sketch 1.0.2 K Hoang 17/04/2020 Fix bug. Add support to SAMD51 and SAMD DUE. WPA2 SSID PW to 63 chars. - Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. + Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. 1.0.3 K Hoang 11/06/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, NINA_B30_ublox, etc. - Add DRD support. Add MultiWiFi support + Add DRD support. Add MultiWiFi support 1.0.4 K Hoang 03/07/2020 Add support to ESP32-AT shields. Modify LOAD_DEFAULT_CONFIG_DATA logic. - Enhance MultiWiFi connection logic. Fix WiFi Status bug. + Enhance MultiWiFi connection logic. Fix WiFi Status bug. 1.1.0 K Hoang 13/04/2021 Fix invalid "blank" Config Data treated as Valid. Optional one set of WiFi Credentials 1.2.0 Michael H 28/04/2021 Enable scan of WiFi networks for selection in Configuration Portal 1.3.0 K Hoang 12/05/2021 Add support to RASPBERRY_PI_PICO using Arduino-pico core 1.4.0 K Hoang 01/06/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using RP2040 Arduino mbed core - 1.4.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json` + 1.4.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json` + 1.5.0 K Hoang 08/01/2022 Fix the blocking issue in loop() with configurable WIFI_RECON_INTERVAL ***************************************************************************************************************************************/ #ifndef Esp8266_AT_WM_Lite_SAMD_h @@ -43,7 +44,7 @@ #error This code is intended to run on the SAMD platform! Please check your Tools->Board setting. #endif -#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.4.1" +#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.5.0" #define DEFAULT_BOARD_NAME "SAMD" @@ -428,6 +429,16 @@ class ESP_AT_WiFiManager_Lite ////////////////////////////////////////////// +#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; @@ -439,6 +450,10 @@ class ESP_AT_WiFiManager_Lite static unsigned long checkstatus_timeout = 0; #define WIFI_STATUS_CHECK_INTERVAL 5000L + static uint32_t curMillis; + + curMillis = millis(); + //// New DRD //// // Call the double reset detector loop method every so often, // so that it can recognise when the timeout expires. @@ -447,7 +462,7 @@ class ESP_AT_WiFiManager_Lite drd->loop(); //// New DRD //// - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -459,7 +474,7 @@ class ESP_AT_WiFiManager_Lite { wifiDisconnectedOnce = false; wifi_connected = false; - ESP_AT_LOGDEBUG(F("r:Check&WLost")); + ESP_AT_LOGERROR(F("r:Check&WLost")); } else { @@ -467,7 +482,7 @@ class ESP_AT_WiFiManager_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; } // Lost connection in running. Give chance to reconfig. @@ -496,7 +511,7 @@ class ESP_AT_WiFiManager_Lite { if (++retryTimes <= CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET) { - ESP_AT_LOGDEBUG1(F("r:WLost&TOut.ConW.Retry#"), retryTimes); + ESP_AT_LOGERROR1(F("r:WLost&TOut.ConW.Retry#"), retryTimes); } else { @@ -508,19 +523,38 @@ class ESP_AT_WiFiManager_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { - ESP_AT_LOGDEBUG(F("r:WLost.ReconW")); + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + ESP_AT_LOGERROR(F("r:WLost.ReconW")); + + if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) + { + ESP_AT_LOGERROR(F("r:WOK")); + } + } +#else + ESP_AT_LOGERROR(F("r:WLost.ReconW")); if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) { - ESP_AT_LOGDEBUG(F("r:WOK")); + ESP_AT_LOGERROR(F("r:WOK")); } +#endif } } } else if (configuration_mode) { configuration_mode = false; - ESP_AT_LOGDEBUG(F("r:gotWBack")); + ESP_AT_LOGERROR(F("r:gotWBack")); } } @@ -1175,6 +1209,16 @@ class ESP_AT_WiFiManager_Lite ////////////////////////////////////////////// +// Max times to try WiFi per loop() iteration. To avoid blocking issue in loop() +// Default 1 and minimum 1. +#if !defined(MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 +#else + #if (MAX_NUM_WIFI_RECON_TRIES_PER_LOOP < 1) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 + #endif +#endif + // New connection logic from v1.2.0 bool connectMultiWiFi(int retry_time) { @@ -1185,11 +1229,11 @@ class ESP_AT_WiFiManager_Lite static int lastConnectedIndex = 255; - ESP_AT_LOGWARN(F("ConMultiWifi")); + ESP_AT_LOGDEBUG(F("ConMultiWifi")); if (static_IP != IPAddress(0, 0, 0, 0)) { - ESP_AT_LOGWARN(F("UseStatIP")); + ESP_AT_LOGDEBUG(F("UseStatIP")); WiFi.config(static_IP); } @@ -1202,7 +1246,7 @@ class ESP_AT_WiFiManager_Lite if ( strlen(ESP8266_AT_config.WiFi_Creds[new_index].wifi_pw) >= PASSWORD_MIN_LEN ) { index = new_index; - ESP_AT_LOGWARN3(F("Using index="), index, F(", lastConnectedIndex="), lastConnectedIndex); + ESP_AT_LOGDEBUG3(F("Using index="), index, F(", lastConnectedIndex="), lastConnectedIndex); } else { @@ -1217,7 +1261,7 @@ class ESP_AT_WiFiManager_Lite // First connection ever, index = 0 if ( strlen(ESP8266_AT_config.WiFi_Creds[0].wifi_pw) >= PASSWORD_MIN_LEN ) { - ESP_AT_LOGWARN(F("First connection, Using index=0")); + ESP_AT_LOGDEBUG(F("First connection, Using index=0")); } else { @@ -1234,11 +1278,13 @@ class ESP_AT_WiFiManager_Lite uint8_t numIndexTried = 0; - while ( !wifi_connected && (numIndexTried++ < NUM_WIFI_CREDENTIALS) ) + uint8_t numWiFiReconTries = 0; + + while ( !wifi_connected && (numIndexTried++ < NUM_WIFI_CREDENTIALS) && (numWiFiReconTries++ < MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) ) { while ( 0 < retry_time ) { - ESP_AT_LOGWARN1(F("Remaining retry_time="), retry_time); + ESP_AT_LOGDEBUG1(F("Remaining retry_time="), retry_time); status = WiFi.begin(ESP8266_AT_config.WiFi_Creds[index].wifi_ssid, ESP8266_AT_config.WiFi_Creds[index].wifi_pw); @@ -1247,7 +1293,7 @@ class ESP_AT_WiFiManager_Lite { wifi_connected = true; lastConnectedIndex = index; - ESP_AT_LOGWARN1(F("WOK, lastConnectedIndex="), lastConnectedIndex); + ESP_AT_LOGDEBUG1(F("WOK, lastConnectedIndex="), lastConnectedIndex); break; } @@ -1285,7 +1331,9 @@ class ESP_AT_WiFiManager_Lite if (wifi_connected) { ESP_AT_LOGERROR(F("con2WF:OK")); - + + ESP_AT_LOGERROR1(F("IP="), WiFi.localIP() ); + displayWiFiData(); } else diff --git a/src/Esp8266_AT_WM_Lite_STM32.h b/src/Esp8266_AT_WM_Lite_STM32.h index 8e6af0d..6076b7a 100644 --- a/src/Esp8266_AT_WM_Lite_STM32.h +++ b/src/Esp8266_AT_WM_Lite_STM32.h @@ -8,23 +8,24 @@ Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_WM_Lite Licensed under MIT license - Version: 1.4.1 + Version: 1.5.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 09/03/2020 Initial coding 1.0.1 K Hoang 20/03/2020 Add feature to enable adding dynamically more Credentials parameters in sketch 1.0.2 K Hoang 17/04/2020 Fix bug. Add support to SAMD51 and SAMD DUE. WPA2 SSID PW to 63 chars. - Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. + Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. 1.0.3 K Hoang 11/06/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, NINA_B30_ublox, etc. - Add DRD support. Add MultiWiFi support + Add DRD support. Add MultiWiFi support 1.0.4 K Hoang 03/07/2020 Add support to ESP32-AT shields. Modify LOAD_DEFAULT_CONFIG_DATA logic. - Enhance MultiWiFi connection logic. Fix WiFi Status bug. + Enhance MultiWiFi connection logic. Fix WiFi Status bug. 1.1.0 K Hoang 13/04/2021 Fix invalid "blank" Config Data treated as Valid. Optional one set of WiFi Credentials 1.2.0 Michael H 28/04/2021 Enable scan of WiFi networks for selection in Configuration Portal 1.3.0 K Hoang 12/05/2021 Add support to RASPBERRY_PI_PICO using Arduino-pico core 1.4.0 K Hoang 01/06/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using RP2040 Arduino mbed core 1.4.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json` + 1.5.0 K Hoang 08/01/2022 Fix the blocking issue in loop() with configurable WIFI_RECON_INTERVAL ***************************************************************************************************************************************/ #ifndef Esp8266_AT_WM_Lite_STM32_h @@ -41,7 +42,7 @@ #error This code is intended to run on STM32 platform! Please check your Tools->Board setting. #endif -#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.4.1" +#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.5.0" #define DEFAULT_BOARD_NAME "STM32" @@ -426,6 +427,16 @@ class ESP_AT_WiFiManager_Lite ////////////////////////////////////////////// +#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; @@ -437,6 +448,10 @@ class ESP_AT_WiFiManager_Lite static unsigned long checkstatus_timeout = 0; #define WIFI_STATUS_CHECK_INTERVAL 5000L + static uint32_t curMillis; + + curMillis = millis(); + //// New DRD //// // Call the double reset detector loop method every so often, // so that it can recognise when the timeout expires. @@ -445,7 +460,7 @@ class ESP_AT_WiFiManager_Lite drd->loop(); //// New DRD //// - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -457,7 +472,7 @@ class ESP_AT_WiFiManager_Lite { wifiDisconnectedOnce = false; wifi_connected = false; - ESP_AT_LOGDEBUG(F("r:Check&WLost")); + ESP_AT_LOGERROR(F("r:Check&WLost")); } else { @@ -465,7 +480,7 @@ class ESP_AT_WiFiManager_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; } // Lost connection in running. Give chance to reconfig. @@ -494,7 +509,7 @@ class ESP_AT_WiFiManager_Lite { if (++retryTimes <= CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET) { - ESP_AT_LOGDEBUG1(F("r:WLost&TOut.ConW.Retry#"), retryTimes); + ESP_AT_LOGERROR1(F("r:WLost&TOut.ConW.Retry#"), retryTimes); } else { @@ -506,19 +521,38 @@ class ESP_AT_WiFiManager_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { - ESP_AT_LOGDEBUG(F("r:WLost.ReconW")); + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + ESP_AT_LOGERROR(F("r:WLost.ReconW")); + + if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) + { + ESP_AT_LOGERROR(F("r:WOK")); + } + } +#else + ESP_AT_LOGERROR(F("r:WLost.ReconW")); if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) { - ESP_AT_LOGDEBUG(F("r:WOK")); + ESP_AT_LOGERROR(F("r:WOK")); } +#endif } } } else if (configuration_mode) { configuration_mode = false; - ESP_AT_LOGDEBUG(F("r:gotWBack")); + ESP_AT_LOGERROR(F("r:gotWBack")); } } @@ -1212,6 +1246,16 @@ class ESP_AT_WiFiManager_Lite ////////////////////////////////////////////// +// Max times to try WiFi per loop() iteration. To avoid blocking issue in loop() +// Default 1 and minimum 1. +#if !defined(MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 +#else + #if (MAX_NUM_WIFI_RECON_TRIES_PER_LOOP < 1) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 + #endif +#endif + // New connection logic from v1.2.0 bool connectMultiWiFi(int retry_time) { @@ -1222,11 +1266,11 @@ class ESP_AT_WiFiManager_Lite static int lastConnectedIndex = 255; - ESP_AT_LOGWARN(F("ConMultiWifi")); + ESP_AT_LOGDEBUG(F("ConMultiWifi")); if (static_IP != IPAddress(0, 0, 0, 0)) { - ESP_AT_LOGWARN(F("UseStatIP")); + ESP_AT_LOGDEBUG(F("UseStatIP")); WiFi.config(static_IP); } @@ -1239,7 +1283,7 @@ class ESP_AT_WiFiManager_Lite if ( strlen(ESP8266_AT_config.WiFi_Creds[new_index].wifi_pw) >= PASSWORD_MIN_LEN ) { index = new_index; - ESP_AT_LOGWARN3(F("Using index="), index, F(", lastConnectedIndex="), lastConnectedIndex); + ESP_AT_LOGDEBUG3(F("Using index="), index, F(", lastConnectedIndex="), lastConnectedIndex); } else { @@ -1254,7 +1298,7 @@ class ESP_AT_WiFiManager_Lite // First connection ever, index = 0 if ( strlen(ESP8266_AT_config.WiFi_Creds[0].wifi_pw) >= PASSWORD_MIN_LEN ) { - ESP_AT_LOGWARN(F("First connection, Using index=0")); + ESP_AT_LOGDEBUG(F("First connection, Using index=0")); } else { @@ -1271,11 +1315,13 @@ class ESP_AT_WiFiManager_Lite uint8_t numIndexTried = 0; - while ( !wifi_connected && (numIndexTried++ < NUM_WIFI_CREDENTIALS) ) + uint8_t numWiFiReconTries = 0; + + while ( !wifi_connected && (numIndexTried++ < NUM_WIFI_CREDENTIALS) && (numWiFiReconTries++ < MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) ) { while ( 0 < retry_time ) { - ESP_AT_LOGWARN1(F("Remaining retry_time="), retry_time); + ESP_AT_LOGDEBUG1(F("Remaining retry_time="), retry_time); status = WiFi.begin(ESP8266_AT_config.WiFi_Creds[index].wifi_ssid, ESP8266_AT_config.WiFi_Creds[index].wifi_pw); @@ -1284,7 +1330,7 @@ class ESP_AT_WiFiManager_Lite { wifi_connected = true; lastConnectedIndex = index; - ESP_AT_LOGWARN1(F("WOK, lastConnectedIndex="), lastConnectedIndex); + ESP_AT_LOGDEBUG1(F("WOK, lastConnectedIndex="), lastConnectedIndex); break; } @@ -1322,7 +1368,9 @@ class ESP_AT_WiFiManager_Lite if (wifi_connected) { ESP_AT_LOGERROR(F("con2WF:OK")); - + + ESP_AT_LOGERROR1(F("IP="), WiFi.localIP() ); + displayWiFiData(); } else diff --git a/src/Esp8266_AT_WM_Lite_nRF52.h b/src/Esp8266_AT_WM_Lite_nRF52.h index 7b6907c..9b0935f 100644 --- a/src/Esp8266_AT_WM_Lite_nRF52.h +++ b/src/Esp8266_AT_WM_Lite_nRF52.h @@ -8,23 +8,24 @@ Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_WM_Lite Licensed under MIT license - Version: 1.4.1 + Version: 1.5.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 09/03/2020 Initial coding 1.0.1 K Hoang 20/03/2020 Add feature to enable adding dynamically more Credentials parameters in sketch 1.0.2 K Hoang 17/04/2020 Fix bug. Add support to SAMD51 and SAMD DUE. WPA2 SSID PW to 63 chars. - Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. + Permit to input special chars such as !,@,#,$,%,^,&,* into data fields. 1.0.3 K Hoang 11/06/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, NINA_B30_ublox, etc. - Add DRD support. Add MultiWiFi support + Add DRD support. Add MultiWiFi support 1.0.4 K Hoang 03/07/2020 Add support to ESP32-AT shields. Modify LOAD_DEFAULT_CONFIG_DATA logic. - Enhance MultiWiFi connection logic. Fix WiFi Status bug. + Enhance MultiWiFi connection logic. Fix WiFi Status bug. 1.1.0 K Hoang 13/04/2021 Fix invalid "blank" Config Data treated as Valid. Optional one set of WiFi Credentials 1.2.0 Michael H 28/04/2021 Enable scan of WiFi networks for selection in Configuration Portal 1.3.0 K Hoang 12/05/2021 Add support to RASPBERRY_PI_PICO using Arduino-pico core 1.4.0 K Hoang 01/06/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using RP2040 Arduino mbed core 1.4.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json` + 1.5.0 K Hoang 08/01/2022 Fix the blocking issue in loop() with configurable WIFI_RECON_INTERVAL ***************************************************************************************************************************************/ #ifndef Esp8266_AT_WM_Lite_nRF52_h @@ -42,7 +43,7 @@ #error This code is intended to run on the nRF52 platform! Please check your Tools->Board setting. #endif -#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.4.1" +#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.5.0" #define DEFAULT_BOARD_NAME "nRF52" @@ -432,6 +433,16 @@ class ESP_AT_WiFiManager_Lite ////////////////////////////////////////////// +#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; @@ -443,6 +454,10 @@ class ESP_AT_WiFiManager_Lite static unsigned long checkstatus_timeout = 0; #define WIFI_STATUS_CHECK_INTERVAL 5000L + static uint32_t curMillis; + + curMillis = millis(); + //// New DRD //// // Call the double reset detector loop method every so often, // so that it can recognise when the timeout expires. @@ -451,7 +466,7 @@ class ESP_AT_WiFiManager_Lite drd->loop(); //// New DRD //// - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -463,7 +478,7 @@ class ESP_AT_WiFiManager_Lite { wifiDisconnectedOnce = false; wifi_connected = false; - ESP_AT_LOGDEBUG(F("r:Check&WLost")); + ESP_AT_LOGERROR(F("r:Check&WLost")); } else { @@ -471,7 +486,7 @@ class ESP_AT_WiFiManager_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; } // Lost connection in running. Give chance to reconfig. @@ -500,7 +515,7 @@ class ESP_AT_WiFiManager_Lite { if (++retryTimes <= CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET) { - ESP_AT_LOGDEBUG1(F("r:WLost&TOut.ConW.Retry#"), retryTimes); + ESP_AT_LOGERROR1(F("r:WLost&TOut.ConW.Retry#"), retryTimes); } else { @@ -512,19 +527,38 @@ class ESP_AT_WiFiManager_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { - ESP_AT_LOGDEBUG(F("r:WLost.ReconW")); + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + ESP_AT_LOGERROR(F("r:WLost.ReconW")); + + if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) + { + ESP_AT_LOGERROR(F("r:WOK")); + } + } +#else + ESP_AT_LOGERROR(F("r:WLost.ReconW")); if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) { - ESP_AT_LOGDEBUG(F("r:WOK")); + ESP_AT_LOGERROR(F("r:WOK")); } +#endif } } } else if (configuration_mode) { configuration_mode = false; - ESP_AT_LOGDEBUG(F("r:gotWBack")); + ESP_AT_LOGERROR(F("r:gotWBack")); } } @@ -1345,6 +1379,16 @@ class ESP_AT_WiFiManager_Lite ////////////////////////////////////////////// +// Max times to try WiFi per loop() iteration. To avoid blocking issue in loop() +// Default 1 and minimum 1. +#if !defined(MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 +#else + #if (MAX_NUM_WIFI_RECON_TRIES_PER_LOOP < 1) + #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 + #endif +#endif + // New connection logic from v1.2.0 bool connectMultiWiFi(int retry_time) { @@ -1355,11 +1399,11 @@ class ESP_AT_WiFiManager_Lite static int lastConnectedIndex = 255; - ESP_AT_LOGWARN(F("ConMultiWifi")); + ESP_AT_LOGDEBUG(F("ConMultiWifi")); if (static_IP != IPAddress(0, 0, 0, 0)) { - ESP_AT_LOGWARN(F("UseStatIP")); + ESP_AT_LOGDEBUG(F("UseStatIP")); WiFi.config(static_IP); } @@ -1372,7 +1416,7 @@ class ESP_AT_WiFiManager_Lite if ( strlen(ESP8266_AT_config.WiFi_Creds[new_index].wifi_pw) >= PASSWORD_MIN_LEN ) { index = new_index; - ESP_AT_LOGWARN3(F("Using index="), index, F(", lastConnectedIndex="), lastConnectedIndex); + ESP_AT_LOGDEBUG3(F("Using index="), index, F(", lastConnectedIndex="), lastConnectedIndex); } else { @@ -1387,7 +1431,7 @@ class ESP_AT_WiFiManager_Lite // First connection ever, index = 0 if ( strlen(ESP8266_AT_config.WiFi_Creds[0].wifi_pw) >= PASSWORD_MIN_LEN ) { - ESP_AT_LOGWARN(F("First connection, Using index=0")); + ESP_AT_LOGDEBUG(F("First connection, Using index=0")); } else { @@ -1404,11 +1448,13 @@ class ESP_AT_WiFiManager_Lite uint8_t numIndexTried = 0; - while ( !wifi_connected && (numIndexTried++ < NUM_WIFI_CREDENTIALS) ) + uint8_t numWiFiReconTries = 0; + + while ( !wifi_connected && (numIndexTried++ < NUM_WIFI_CREDENTIALS) && (numWiFiReconTries++ < MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) ) { while ( 0 < retry_time ) { - ESP_AT_LOGWARN1(F("Remaining retry_time="), retry_time); + ESP_AT_LOGDEBUG1(F("Remaining retry_time="), retry_time); status = WiFi.begin(ESP8266_AT_config.WiFi_Creds[index].wifi_ssid, ESP8266_AT_config.WiFi_Creds[index].wifi_pw); @@ -1417,7 +1463,7 @@ class ESP_AT_WiFiManager_Lite { wifi_connected = true; lastConnectedIndex = index; - ESP_AT_LOGWARN1(F("WOK, lastConnectedIndex="), lastConnectedIndex); + ESP_AT_LOGDEBUG1(F("WOK, lastConnectedIndex="), lastConnectedIndex); break; } @@ -1455,7 +1501,9 @@ class ESP_AT_WiFiManager_Lite if (wifi_connected) { ESP_AT_LOGERROR(F("con2WF:OK")); - + + ESP_AT_LOGERROR1(F("IP="), WiFi.localIP() ); + displayWiFiData(); } else