From c8954701dfb5528bb7eecb9fa7c3f3ab82250bbe Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Sun, 9 Jan 2022 14:36:53 -0500 Subject: [PATCH] v1.7.0 to fix the blocking issue in loop() ### Major Release v1.7.0 1. Fix ESP8266 bug not easy to connect to Config Portal for ESP8266 core v3.0.0+ 2. Fix the blocking issue in loop(). Check [retries block the main loop #18](https://github.com/khoih-prog/WiFiManager_NINA_Lite/issues/18) 3. Configurable `WIFI_RECON_INTERVAL`. Check [retries block the main loop #18](https://github.com/khoih-prog/WiFiManager_NINA_Lite/issues/18#issuecomment-1006197561) 4. Optimize library code by using `reference-passing` instead of `value-passing` 5. Clean up --- CONTRIBUTING.md | 8 +- README.md | 100 ++++++++++--- changelog.md | 9 ++ examples/ESPAsync_WiFi/defines.h | 41 ++++-- examples/ESPAsync_WiFi_MQTT/defines.h | 41 ++++-- library.json | 2 +- library.properties | 2 +- src/ESPAsync_WiFiManager_Lite.h | 194 ++++++++++++++++---------- src/ESPAsync_WiFiManager_Lite_Debug.h | 3 +- 9 files changed, 286 insertions(+), 114 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1a643a4..cf3fea4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,8 +14,8 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p Please ensure to specify the following: -* Arduino IDE version (e.g. 1.8.16) or Platform.io version -* `ESP8266` or `ESP32` Core Version (e.g. ESP8266 core v3.0.2 or ESP32 v2.0.1) +* Arduino IDE version (e.g. 1.8.19) or Platform.io version +* `ESP8266` or `ESP32` Core Version (e.g. ESP8266 core v3.0.2 or ESP32 v2.0.2) * Contextual information (e.g. what you were trying to achieve) * Simplest possible steps to reproduce * Anything that might be relevant in your opinion, such as: @@ -26,10 +26,10 @@ Please ensure to specify the following: ### Example ``` -Arduino IDE version: 1.8.16 +Arduino IDE version: 1.8.19 ESP8266 Core Version 3.0.2 OS: Ubuntu 20.04 LTS -Linux xy-Inspiron-3593 5.4.0-90-generic #101-Ubuntu SMP Fri Oct 15 20:00:55 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux +Linux xy-Inspiron-3593 5.4.0-92-generic #103-Ubuntu SMP Fri Nov 26 16:13:00 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux Context: The board couldn't autoreconnect to Local Blynk Server after router power recycling. diff --git a/README.md b/README.md index 118d5a4..32c9c32 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,9 @@ * [12.1 Enable auto-scan of WiFi networks for selection in Configuration Portal](#121-enable-auto-scan-of-wifi-networks-for-selection-in-configuration-portal) * [12.2 Disable manually input SSIDs](#122-disable-manually-input-ssids) * [12.3 Select maximum number of SSIDs in the list](#123-select-maximum-number-of-ssids-in-the-list) + * [13. To avoid blocking in loop when WiFi is lost](#13-To-avoid-blocking-in-loop-when-wifi-is-lost) + * [13.1 Max times to try WiFi per loop](#131-max-times-to-try-wifi-per-loop) + * [13.2 Interval between reconnection WiFi if lost](#132-interval-between-reconnection-wifi-if-lost) * [Examples](#examples) * [ 1. ESPAsync_WiFi](examples/ESPAsync_WiFi) * [ 2. ESPAsync_WiFi_MQTT](examples/ESPAsync_WiFi_MQTT) @@ -161,9 +164,9 @@ This [**ESPAsync_WiFiManager_Lite** library](https://github.com/khoih-prog/ESPAs ## Prerequisites - 1. [`Arduino IDE 1.8.16+` for Arduino](https://www.arduino.cc/en/Main/Software) + 1. [`Arduino IDE 1.8.19+` for Arduino](https://www.arduino.cc/en/Main/Software) 2. [`ESP8266 Core 3.0.2+`](https://github.com/esp8266/Arduino) for ESP8266-based boards. [![Latest release](https://img.shields.io/github/release/esp8266/Arduino.svg)](https://github.com/esp8266/Arduino/releases/latest/). To use ESP8266 core 2.7.1+ for LittleFS. - 3. [`ESP32 Core 2.0.1+`](https://github.com/espressif/arduino-esp32) for ESP32-based boards. [![Latest release](https://img.shields.io/github/release/espressif/arduino-esp32.svg)](https://github.com/espressif/arduino-esp32/releases/latest/) + 3. [`ESP32 Core 2.0.2+`](https://github.com/espressif/arduino-esp32) for ESP32-based boards. [![Latest release](https://img.shields.io/github/release/espressif/arduino-esp32.svg)](https://github.com/espressif/arduino-esp32/releases/latest/) 4. [`ESPAsyncWebServer v1.2.3+`](https://github.com/me-no-dev/ESPAsyncWebServer) for all ESP32/ESP8266-based boards. 5. [`ESPAsyncTCP v1.2.2+`](https://github.com/me-no-dev/ESPAsyncTCP) for ESP8266-based boards. 6. [`AsyncTCP v1.1.1+`](https://github.com/me-no-dev/AsyncTCP) for ESP32-based boards @@ -494,6 +497,35 @@ The maximum number of SSIDs in the list is seletable from 2 to 15. If invalid nu #define MAX_SSID_IN_LIST 8 ``` +#### 13. To avoid blocking in loop when WiFi is lost + +#### 13.1 Max times to try WiFi per loop + +To define max times to try WiFi per loop() iteration. To avoid blocking issue in loop() + +Default is 1 if not defined, and minimum is forced to be 1. + +To use, uncomment in `defines.h`. + +Check [retries block the main loop #18](https://github.com/khoih-prog/WiFiManager_NINA_Lite/issues/18#issue-1094004380) + +``` +#define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 2 +``` + +#### 13.2 Interval between reconnection WiFi if lost + +Default is no interval between reconnection WiFi times if lost WiFi. Max permitted interval will be 10mins. + +Uncomment to use. Be careful, WiFi reconnection will be delayed if using this method. + +Only use whenever urgent tasks in loop() can't be delayed. But if so, it's better you have to rewrite your code, e.g. using higher priority tasks. + +Check [retries block the main loop #18](https://github.com/khoih-prog/WiFiManager_NINA_Lite/issues/18#issuecomment-1006197561) + +``` +#define WIFI_RECON_INTERVAL 30000 // 30s +``` --- --- @@ -938,7 +970,10 @@ void loop() // RTC Memory Address for the DoubleResetDetector to use #define MRD_ADDRESS 0 - #warning Using MULTI_RESETDETECTOR + + #if (_ESP_WM_LITE_LOGLEVEL_ > 3) + #warning Using MULTI_RESETDETECTOR + #endif #else #define DOUBLERESETDETECTOR_DEBUG true @@ -948,7 +983,10 @@ void loop() // RTC Memory Address for the DoubleResetDetector to use #define DRD_ADDRESS 0 - #warning Using DOUBLE_RESETDETECTOR + + #if (_ESP_WM_LITE_LOGLEVEL_ > 3) + #warning Using DOUBLE_RESETDETECTOR + #endif #endif ///////////////////////////////////////////// @@ -972,10 +1010,6 @@ void loop() ///////////////////////////////////////////// -// Permit input only one set of WiFi SSID/PWD. The other can be "NULL or "blank" -// Default is false (if not defined) => must input 2 sets of SSID/PWD -#define REQUIRE_ONE_SET_SSID_PW false - // Force some params #define TIMEOUT_RECONNECT_WIFI 10000L @@ -988,9 +1022,32 @@ void loop() #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 // Config Timeout 120s (default 60s). Applicable only if Config Data is Valid -#define CONFIG_TIMEOUT 120000L +#define CONFIG_TIMEOUT 120000L + +///////////////////////////////////////////// + +// Permit input only one set of WiFi SSID/PWD. The other can be "NULL or "blank" +// Default is false (if not defined) => must input 2 sets of SSID/PWD +#define REQUIRE_ONE_SET_SSID_PW true //false + +// Max times to try WiFi per loop() iteration. To avoid blocking issue in loop() +// Default 1 if not defined, and minimum 1. +#define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 2 + +// Default no interval between recon WiFi if lost +// Max permitted interval will be 10mins +// Uncomment to use. Be careful, WiFi reconnect will be delayed if using this method +// Only use whenever urgent tasks in loop() can't be delayed. But if so, it's better you have to rewrite your code, e.g. using higher priority tasks. +#define WIFI_RECON_INTERVAL 30000 + +///////////////////////////////////////////// + +// Permit reset hardware if no WiFi to permit user another chance to access Config Portal. +#define RESET_IF_NO_WIFI false + +///////////////////////////////////////////// -#define USE_DYNAMIC_PARAMETERS true +#define USE_DYNAMIC_PARAMETERS true ///////////////////////////////////////////// @@ -1196,7 +1253,7 @@ This is the terminal output when running [**ESPAsync_WiFi_MQTT**](examples/ESPAs ``` Starting ESPAsync_WiFi_MQTT using LittleFS on ESP32_DEV -ESPAsync_WiFiManager_Lite v1.6.0 +ESPAsync_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1271,7 +1328,7 @@ NNN Starting ESPAsync_WiFi_MQTT using LittleFS on ESP32_DEV -ESPAsync_WiFiManager_Lite v1.6.0 +ESPAsync_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1363,7 +1420,7 @@ This is the terminal output when running [**ESPAsync_WiFi_MQTT**](examples/ESPAs ``` Starting ESPAsync_WiFi_MQTT using LittleFS on ESP8266_NODEMCU -ESPAsync_WiFiManager_Lite v1.6.0 +ESPAsync_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1438,7 +1495,7 @@ NNN Starting ESPAsync_WiFi_MQTT using LittleFS on ESP8266_NODEMCU -ESPAsync_WiFiManager_Lite v1.6.0 +ESPAsync_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1529,7 +1586,7 @@ This is the terminal output when running [**ESPAsync_WiFi_MQTT**](examples/ESPAs ``` Starting ESPAsync_WiFi_MQTT using LittleFS on ESP32S2_DEV -ESPAsync_WiFiManager_Lite v1.6.0 +ESPAsync_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1642,7 +1699,7 @@ entry 0x4004c190 Starting ESPAsync_WiFi_MQTT using LittleFS on ESP32S2_DEV -ESPAsync_WiFiManager_Lite v1.6.0 +ESPAsync_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1745,7 +1802,7 @@ This is the terminal output when running [**ESPAsync_WiFi_MQTT**](examples/ESPAs ``` Starting ESPAsync_WiFi_MQTT using LittleFS on ESP32S2_DEV -ESPAsync_WiFiManager_Lite v1.6.0 +ESPAsync_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFC0003 multiResetDetectorFlag = 0xFFFC0003 @@ -1788,7 +1845,7 @@ entry 0x4004c190 ``` Starting ESPAsync_WiFi_MQTT using LittleFS on ESP32S2_DEV -ESPAsync_WiFiManager_Lite v1.6.0 +ESPAsync_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1844,7 +1901,7 @@ This is the terminal output when running [**ESPAsync_WiFi_MQTT**](examples/ESPAs ``` Starting ESPAsync_WiFi_MQTT using LittleFS on ESP32_DEV -ESPAsync_WiFiManager_Lite v1.6.0 +ESPAsync_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFC0003 multiResetDetectorFlag = 0xFFFC0003 @@ -1889,7 +1946,7 @@ NNNN NNNNN NNNNN N ``` Starting ESPAsync_WiFi_MQTT using LittleFS on ESP32_DEV -ESPAsync_WiFiManager_Lite v1.6.0 +ESPAsync_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -2000,6 +2057,9 @@ Submit issues to: [ESPAsync_WiFiManager_Lite issues](https://github.com/khoih-pr 20. Add support to **ESP32-C3 using EEPROM and SPIFFS** 21. Enable **scan of WiFi networks** for selection in Configuration Portal 22. Ready for ESP32 core v2.0.0+ +23. Fix ESP8266 bug not easy to connect to Config Portal for ESP8266 core v3.0.0+ +24. Fix the blocking issue in loop() with configurable `WIFI_RECON_INTERVAL` +25. Optimize library code by using `reference-passing` instead of `value-passing` --- --- diff --git a/changelog.md b/changelog.md index da13603..6684efe 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Major Release v1.7.0](#major-release-v170) * [Release v1.6.0](#release-v160) * [Release v1.5.1](#release-v151) * [Major Release v1.5.0](#major-release-v150) @@ -27,6 +28,14 @@ ## Changelog +### Major Release v1.7.0 + +1. Fix ESP8266 bug not easy to connect to Config Portal for ESP8266 core v3.0.0+ +2. Fix the blocking issue in loop(). Check [retries block the main loop #18](https://github.com/khoih-prog/WiFiManager_NINA_Lite/issues/18) +3. Configurable `WIFI_RECON_INTERVAL`. Check [retries block the main loop #18](https://github.com/khoih-prog/WiFiManager_NINA_Lite/issues/18#issuecomment-1006197561) +4. Optimize library code by using `reference-passing` instead of `value-passing` +5. Clean up + ### Release v1.6.0 1. Auto detect ESP32 core and use either built-in LittleFS or [LITTLEFS](https://github.com/lorol/LITTLEFS) library. diff --git a/examples/ESPAsync_WiFi/defines.h b/examples/ESPAsync_WiFi/defines.h index e15ef46..092c6d2 100644 --- a/examples/ESPAsync_WiFi/defines.h +++ b/examples/ESPAsync_WiFi/defines.h @@ -33,7 +33,10 @@ // RTC Memory Address for the DoubleResetDetector to use #define MRD_ADDRESS 0 - #warning Using MULTI_RESETDETECTOR + + #if (_ESP_WM_LITE_LOGLEVEL_ > 3) + #warning Using MULTI_RESETDETECTOR + #endif #else #define DOUBLERESETDETECTOR_DEBUG true @@ -43,7 +46,10 @@ // RTC Memory Address for the DoubleResetDetector to use #define DRD_ADDRESS 0 - #warning Using DOUBLE_RESETDETECTOR + + #if (_ESP_WM_LITE_LOGLEVEL_ > 3) + #warning Using DOUBLE_RESETDETECTOR + #endif #endif ///////////////////////////////////////////// @@ -67,10 +73,6 @@ ///////////////////////////////////////////// -// Permit input only one set of WiFi SSID/PWD. The other can be "NULL or "blank" -// Default is false (if not defined) => must input 2 sets of SSID/PWD -#define REQUIRE_ONE_SET_SSID_PW false - // Force some params #define TIMEOUT_RECONNECT_WIFI 10000L @@ -83,9 +85,32 @@ #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 // Config Timeout 120s (default 60s). Applicable only if Config Data is Valid -#define CONFIG_TIMEOUT 120000L +#define CONFIG_TIMEOUT 120000L + +///////////////////////////////////////////// + +// Permit input only one set of WiFi SSID/PWD. The other can be "NULL or "blank" +// Default is false (if not defined) => must input 2 sets of SSID/PWD +#define REQUIRE_ONE_SET_SSID_PW true //false + +// Max times to try WiFi per loop() iteration. To avoid blocking issue in loop() +// Default 1 if not defined, and minimum 1. +#define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 2 + +// Default no interval between recon WiFi if lost +// Max permitted interval will be 10mins +// Uncomment to use. Be careful, WiFi reconnect will be delayed if using this method +// Only use whenever urgent tasks in loop() can't be delayed. But if so, it's better you have to rewrite your code, e.g. using higher priority tasks. +#define WIFI_RECON_INTERVAL 30000 + +///////////////////////////////////////////// + +// Permit reset hardware if no WiFi to permit user another chance to access Config Portal. +#define RESET_IF_NO_WIFI false + +///////////////////////////////////////////// -#define USE_DYNAMIC_PARAMETERS true +#define USE_DYNAMIC_PARAMETERS true ///////////////////////////////////////////// diff --git a/examples/ESPAsync_WiFi_MQTT/defines.h b/examples/ESPAsync_WiFi_MQTT/defines.h index d8e8d71..aaed1ff 100644 --- a/examples/ESPAsync_WiFi_MQTT/defines.h +++ b/examples/ESPAsync_WiFi_MQTT/defines.h @@ -33,7 +33,10 @@ // RTC Memory Address for the DoubleResetDetector to use #define MRD_ADDRESS 0 - #warning Using MULTI_RESETDETECTOR + + #if (_ESP_WM_LITE_LOGLEVEL_ > 3) + #warning Using MULTI_RESETDETECTOR + #endif #else #define DOUBLERESETDETECTOR_DEBUG true @@ -43,7 +46,10 @@ // RTC Memory Address for the DoubleResetDetector to use #define DRD_ADDRESS 0 - #warning Using DOUBLE_RESETDETECTOR + + #if (_ESP_WM_LITE_LOGLEVEL_ > 3) + #warning Using DOUBLE_RESETDETECTOR + #endif #endif ///////////////////////////////////////////// @@ -67,10 +73,6 @@ ///////////////////////////////////////////// -// Permit input only one set of WiFi SSID/PWD. The other can be "NULL or "blank" -// Default is false (if not defined) => must input 2 sets of SSID/PWD -#define REQUIRE_ONE_SET_SSID_PW false - // Force some params #define TIMEOUT_RECONNECT_WIFI 10000L @@ -83,9 +85,32 @@ #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET 5 // Config Timeout 120s (default 60s). Applicable only if Config Data is Valid -#define CONFIG_TIMEOUT 120000L +#define CONFIG_TIMEOUT 120000L + +///////////////////////////////////////////// + +// Permit input only one set of WiFi SSID/PWD. The other can be "NULL or "blank" +// Default is false (if not defined) => must input 2 sets of SSID/PWD +#define REQUIRE_ONE_SET_SSID_PW true //false + +// Max times to try WiFi per loop() iteration. To avoid blocking issue in loop() +// Default 1 if not defined, and minimum 1. +#define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 2 + +// Default no interval between recon WiFi if lost +// Max permitted interval will be 10mins +// Uncomment to use. Be careful, WiFi reconnect will be delayed if using this method +// Only use whenever urgent tasks in loop() can't be delayed. But if so, it's better you have to rewrite your code, e.g. using higher priority tasks. +#define WIFI_RECON_INTERVAL 30000 + +///////////////////////////////////////////// + +// Permit reset hardware if no WiFi to permit user another chance to access Config Portal. +#define RESET_IF_NO_WIFI false + +///////////////////////////////////////////// -#define USE_DYNAMIC_PARAMETERS true +#define USE_DYNAMIC_PARAMETERS true ///////////////////////////////////////////// diff --git a/library.json b/library.json index 5af1b76..537e5be 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ESPAsync_WiFiManager_Lite", - "version": "1.6.0", + "version": "1.7.0", "keywords": "wifi, wi-fi, Async, Async-WebServer, Async-WiFiManager, Communication, MultiWiFi, multi-wifi, ESP32, ESP32-S2, esp32-c3, ESP8266, Credentials, config-portal, DoubleReset, MultiReset, Detector, dynamic-params, dynamic, customs-header", "description": "Library using AsyncWebServer to configure MultiWiFi/Credentials at runtime for ESP32 (including ESP32-S2) and ESP8266 boards. You can also specify DHCP HostName, static AP and STA IP. Use much less memory compared to full-fledge WiFiManager. Config Portal will be auto-adjusted to match the number of dynamic custom parameters. Optional default Credentials to be autoloaded into Config Portal to use or change instead of manually input. Credentials are saved in LittleFS, SPIFFS or EEPROM. New powerful-yet-simple-to-use feature to enable adding dynamic custom parameters from sketch and input using the same Config Portal. Double or MultiDetectDetector as well as Virtual Switches feature permits entering Config Portal as requested. Configurable Customs HTML Headers, including Customs Style, Customs Head Elements, CORS Header.", "authors": diff --git a/library.properties b/library.properties index 1438580..02cf931 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ESPAsync_WiFiManager_Lite -version=1.6.0 +version=1.7.0 author=Khoi Hoang maintainer=Khoi Hoang license=MIT diff --git a/src/ESPAsync_WiFiManager_Lite.h b/src/ESPAsync_WiFiManager_Lite.h index 3eaa693..391248e 100644 --- a/src/ESPAsync_WiFiManager_Lite.h +++ b/src/ESPAsync_WiFiManager_Lite.h @@ -9,7 +9,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/ESPAsync_WiFiManager_Lite Licensed under MIT license - Version: 1.6.0 + Version: 1.7.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -21,6 +21,7 @@ 1.5.0 Michael H 24/04/2021 Enable scan of WiFi networks for selection in Configuration Portal 1.5.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json` 1.6.0 K Hoang 26/11/2021 Auto detect ESP32 core and use either built-in LittleFS or LITTLEFS library. Fix bug. + 1.7.0 K Hoang 09/01/2022 Fix the blocking issue in loop() with configurable WIFI_RECON_INTERVAL *****************************************************************************************************************************/ #pragma once @@ -42,7 +43,7 @@ #define USING_ESP32_C3 true #endif -#define ESP_ASYNC_WIFI_MANAGER_LITE_VERSION "ESPAsync_WiFiManager_Lite v1.6.0" +#define ESP_ASYNC_WIFI_MANAGER_LITE_VERSION "ESPAsync_WiFiManager_Lite v1.7.0" #ifdef ESP8266 @@ -183,7 +184,9 @@ #define MAX_SSID_IN_LIST 10 #endif #else - #warning SCAN_WIFI_NETWORKS disabled + #if (_ESP_WM_LITE_LOGLEVEL_ > 3) + #warning SCAN_WIFI_NETWORKS disabled + #endif #endif ///////// NEW for DRD ///////////// @@ -304,13 +307,17 @@ typedef struct // #if USE_DYNAMIC_PARAMETERS - #warning Using 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 - #warning Not using Dynamic Parameters + #if (_ESP_WM_LITE_LOGLEVEL_ > 3) + #warning Not using Dynamic Parameters + #endif #endif @@ -408,7 +415,7 @@ const char WM_HTTP_CORS_ALLOW_ALL[] PROGMEM = "*"; ////////////////////////////////////////// -String IPAddressToString(IPAddress _address) +String IPAddressToString(const IPAddress& _address) { String str = String(_address[0]); str += "."; @@ -666,11 +673,33 @@ class ESPAsync_WiFiManager_Lite #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 mulyi reset detector loop method every so often, @@ -689,6 +718,29 @@ class ESPAsync_WiFiManager_Lite //// New DRD //// #endif + 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 ) { @@ -727,8 +779,27 @@ class ESPAsync_WiFiManager_Lite #endif // Not in config mode, try reconnecting before forcing to config mode - //if ( WiFi.status() != WL_CONNECTED ) + 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) + { + // turn the LED_BUILTIN OFF to tell us we exit configuration mode. + digitalWrite(LED_BUILTIN, LED_OFF); + + ESP_WML_LOGINFO(F("run: WiFi reconnected")); + } + } +#else ESP_WML_LOGINFO(F("run: WiFi lost. Reconnect WiFi")); if (connectMultiWiFi() == WL_CONNECTED) @@ -738,6 +809,7 @@ class ESPAsync_WiFiManager_Lite ESP_WML_LOGINFO(F("run: WiFi reconnected")); } +#endif } //ESP_WML_LOGINFO(F("run: Lost connection => configMode")); @@ -752,7 +824,7 @@ class ESPAsync_WiFiManager_Lite digitalWrite(LED_BUILTIN, LED_OFF); } } - + ////////////////////////////////////////////// void setHostname() @@ -783,14 +855,14 @@ class ESPAsync_WiFiManager_Lite ////////////////////////////////////////////// - void setConfigPortalIP(IPAddress portalIP = IPAddress(192, 168, 4, 1)) + void setConfigPortalIP(const IPAddress& portalIP = IPAddress(192, 168, 4, 1)) { portal_apIP = portalIP; } ////////////////////////////////////////////// - void setConfigPortal(String ssid = "", String pass = "") + void setConfigPortal(const String& ssid = "", const String& pass = "") { portal_ssid = ssid; portal_pass = pass; @@ -816,9 +888,9 @@ class ESPAsync_WiFiManager_Lite ////////////////////////////////////////////// - void setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn = IPAddress(255, 255, 255, 0), - IPAddress dns_address_1 = IPAddress(0, 0, 0, 0), - IPAddress dns_address_2 = IPAddress(0, 0, 0, 0)) + void setSTAStaticIPConfig(const IPAddress& ip, const IPAddress& gw, const IPAddress& sn = IPAddress(255, 255, 255, 0), + const IPAddress& dns_address_1 = IPAddress(0, 0, 0, 0), + const IPAddress& dns_address_2 = IPAddress(0, 0, 0, 0)) { static_IP = ip; static_GW = gw; @@ -839,7 +911,7 @@ class ESPAsync_WiFiManager_Lite ////////////////////////////////////////////// - String getWiFiSSID(uint8_t index) + String getWiFiSSID(const uint8_t index) { if (index >= NUM_WIFI_CREDENTIALS) return String(""); @@ -852,7 +924,7 @@ class ESPAsync_WiFiManager_Lite ////////////////////////////////////////////// - String getWiFiPW(uint8_t index) + String getWiFiPW(const uint8_t index) { if (index >= NUM_WIFI_CREDENTIALS) return String(""); @@ -1141,7 +1213,7 @@ class ESPAsync_WiFiManager_Lite ////////////////////////////////////// - void displayConfigData(ESP_WM_LITE_Configuration configData) + void displayConfigData(const ESP_WM_LITE_Configuration& configData) { ESP_WML_LOGERROR5(F("Hdr="), configData.header, F(",SSID="), configData.WiFi_Creds[0].wifi_ssid, F(",PW="), configData.WiFi_Creds[0].wifi_pw); @@ -1235,7 +1307,7 @@ class ESPAsync_WiFiManager_Lite ////////////////////////////////////////////// - void saveForcedCP(uint32_t value) + void saveForcedCP(const uint32_t value) { File file = FileFS.open(CONFIG_PORTAL_FILENAME, "w"); @@ -1271,7 +1343,7 @@ class ESPAsync_WiFiManager_Lite ////////////////////////////////////////////// - void setForcedCP(bool isPersistent) + void setForcedCP(const bool isPersistent) { uint32_t readForcedConfigPortalFlag = isPersistent? FORCED_PERS_CONFIG_PORTAL_FLAG_DATA : FORCED_CONFIG_PORTAL_FLAG_DATA; @@ -1869,7 +1941,7 @@ class ESPAsync_WiFiManager_Lite ////////////////////////////////////////////// - void setForcedCP(bool isPersistent) + void setForcedCP(const bool isPersistent) { uint32_t readForcedConfigPortalFlag = isPersistent? FORCED_PERS_CONFIG_PORTAL_FLAG_DATA : FORCED_CONFIG_PORTAL_FLAG_DATA; @@ -2218,8 +2290,18 @@ class ESPAsync_WiFiManager_Lite #endif ////////////////////////////////////////////// - -#if 1 + +// New connectMultiWiFi() logic from v1.7.0 +// 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 + uint8_t connectMultiWiFi() { #if ESP32 @@ -2249,10 +2331,11 @@ class ESPAsync_WiFiManager_Lite int i = 0; status = wifiMulti.run(); delay(WIFI_MULTI_1ST_CONNECT_WAITING_MS); + + uint8_t numWiFiReconTries = 0; - while ( ( i++ < 20 ) && ( status != WL_CONNECTED ) ) + while ( ( status != WL_CONNECTED ) && (numWiFiReconTries++ < MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) ) { - //status = wifiMulti.run(); status = WiFi.status(); if ( status == WL_CONNECTED ) @@ -2271,6 +2354,8 @@ class ESPAsync_WiFiManager_Lite { ESP_WML_LOGERROR(F("WiFi not connected")); +#if RESET_IF_NO_WIFI + #if USING_MRD // To avoid unnecessary MRD mrd->loop(); @@ -2283,53 +2368,14 @@ class ESPAsync_WiFiManager_Lite ESP.reset(); #else ESP.restart(); - #endif - } - - return status; - } -#else - - uint8_t connectMultiWiFi() - { - // For ESP8266, this better be 3000 to enable connect the 1st time -#define WIFI_MULTI_CONNECT_WAITING_MS 3000L - - uint8_t status; - - ESP_WML_LOGINFO(F("Connecting MultiWifi...")); - - WiFi.mode(WIFI_STA); - - setHostname(); - - int i = 0; - status = wifiMulti.run(); - delay(WIFI_MULTI_CONNECT_WAITING_MS); - - while ( ( i++ < 10 ) && ( status != WL_CONNECTED ) ) - { - status = wifiMulti.run(); - - if ( status == WL_CONNECTED ) - break; - else - delay(WIFI_MULTI_CONNECT_WAITING_MS); - } - - if ( status == WL_CONNECTED ) - { - ESP_WML_LOGWARN1(F("WiFi connected after time: "), i); - ESP_WML_LOGWARN3(F("SSID="), WiFi.SSID(), F(",RSSI="), WiFi.RSSI()); - ESP_WML_LOGWARN3(F("Channel="), WiFi.channel(), F(",IP="), WiFi.localIP() ); + #endif + +#endif } - else - ESP_WML_LOGERROR(F("WiFi not connected")); return status; } -#endif - + ////////////////////////////////////////////// // NEW @@ -2718,14 +2764,20 @@ class ESPAsync_WiFiManager_Lite } else channel = WiFiAPChannel; + + // softAPConfig() must be put before softAP() for ESP8266 core v3.0.0+ to work. + // ESP32 or ESP8266is core v3.0.0- is OK either way + WiFi.softAPConfig(portal_apIP, portal_apIP, IPAddress(255, 255, 255, 0)); - WiFi.softAP(portal_ssid.c_str(), portal_pass.c_str(), channel); + WiFi.softAP(portal_ssid.c_str(), portal_pass.c_str(), channel); ESP_WML_LOGERROR3(F("\nstConf:SSID="), portal_ssid, F(",PW="), portal_pass); ESP_WML_LOGERROR3(F("IP="), portal_apIP.toString(), ",ch=", channel); delay(100); // ref: https://github.com/espressif/arduino-esp32/issues/985#issuecomment-359157428 - WiFi.softAPConfig(portal_apIP, portal_apIP, IPAddress(255, 255, 255, 0)); + + // Move up for ESP8266 + //WiFi.softAPConfig(portal_apIP, portal_apIP, IPAddress(255, 255, 255, 0)); if (!server) { @@ -2777,7 +2829,7 @@ class ESPAsync_WiFiManager_Lite ////////////////////////////////////////// - void setMinimumSignalQuality(int quality) + void setMinimumSignalQuality(const int quality) { _minimumQuality = quality; } @@ -2785,7 +2837,7 @@ class ESPAsync_WiFiManager_Lite ////////////////////////////////////////// //if this is true, remove duplicate Access Points - default true - void setRemoveDuplicateAPs(bool removeDuplicates) + void setRemoveDuplicateAPs(const bool removeDuplicates) { _removeDuplicateAPs = removeDuplicates; } @@ -2905,7 +2957,7 @@ class ESPAsync_WiFiManager_Lite ////////////////////////////////////////// - int getRSSIasQuality(int RSSI) + int getRSSIasQuality(const int RSSI) { int quality = 0; diff --git a/src/ESPAsync_WiFiManager_Lite_Debug.h b/src/ESPAsync_WiFiManager_Lite_Debug.h index 7beafa6..4831ecb 100644 --- a/src/ESPAsync_WiFiManager_Lite_Debug.h +++ b/src/ESPAsync_WiFiManager_Lite_Debug.h @@ -9,7 +9,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/ESPAsync_WiFiManager_Lite Licensed under MIT license - Version: 1.6.0 + Version: 1.7.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -21,6 +21,7 @@ 1.5.0 Michael H 24/04/2021 Enable scan of WiFi networks for selection in Configuration Portal 1.5.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json` 1.6.0 K Hoang 26/11/2021 Auto detect ESP32 core and use either built-in LittleFS or LITTLEFS library. Fix bug. + 1.7.0 K Hoang 09/01/2022 Fix the blocking issue in loop() with configurable WIFI_RECON_INTERVAL *****************************************************************************************************************************/ #ifndef ESPAsync_WiFiManager_Lite_Debug_h