From d64ce2d2430e9fdabbd12692dc88da8656da3390 Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Sat, 8 Jan 2022 14:29:25 -0500 Subject: [PATCH] v1.7.0 to fix the blocking issue in loop() ### 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. Clean up --- CONTRIBUTING.md | 8 +- README.md | 101 +++++++++++++++++----- changelog.md | 8 ++ examples/ESP_WiFi/defines.h | 41 +++++++-- examples/ESP_WiFi_MQTT/defines.h | 31 +++++-- library.json | 2 +- library.properties | 2 +- src/ESP_WiFiManager_Lite.h | 144 +++++++++++++++++++++++++++---- src/ESP_WiFiManager_Lite_Debug.h | 3 +- 9 files changed, 279 insertions(+), 61 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9cb1e6f..3d41c87 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 Inspiron 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 Inspiron 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 ab19c0b..3da8d68 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,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. ESP_WiFi](examples/ESP_WiFi) * [ 2. ESP_WiFi_MQTT](examples/ESP_WiFi_MQTT) @@ -138,8 +141,8 @@ This [**ESP_WiFiManager_Lite** library](https://github.com/khoih-prog/ESP_WiFiMa ## Prerequisites - 1. [`Arduino IDE 1.8.16+` for Arduino](https://www.arduino.cc/en/Main/Software) - 2. [`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/) + 1. [`Arduino IDE 1.8.19+` for Arduino](https://www.arduino.cc/en/Main/Software) + 2. [`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/) 3. [`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/). SPIFFS is deprecated from ESP8266 core 2.7.1+, to use LittleFS. 4. [`ESP_DoubleResetDetector v1.2.1+`](https://github.com/khoih-prog/ESP_DoubleResetDetector) if using DRD feature. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/ESP_DoubleResetDetector.svg?)](https://www.ardu-badge.com/ESP_DoubleResetDetector). 5. [`ESP_MultiResetDetector v1.2.1+`](https://github.com/khoih-prog/ESP_MultiResetDetector) if using MRD feature. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/ESP_MultiResetDetector.svg?)](https://www.ardu-badge.com/ESP_MultiResetDetector). @@ -470,6 +473,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 +``` --- --- @@ -913,7 +945,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 @@ -923,7 +958,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 ///////////////////////////////////////////// @@ -947,10 +985,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 @@ -963,9 +997,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 ///////////////////////////////////////////// @@ -975,7 +1032,7 @@ void loop() #define MANUAL_SSID_INPUT_ALLOWED true // From 2-15 -#define MAX_SSID_IN_LIST 8 + #define MAX_SSID_IN_LIST 8 ///////////////////////////////////////////// @@ -1171,7 +1228,7 @@ This is the terminal output when running [**ESP_WiFi**](examples/ESP_WiFi) examp ``` Starting ESP_WiFi using LittleFS on ESP32_DEV -ESP_WiFiManager_Lite v1.6.0 +ESP_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFC0003 multiResetDetectorFlag = 0xFFFC0003 @@ -1242,7 +1299,7 @@ FFFFFFFFF ``` Starting ESP_WiFi using LittleFS on ESP32_DEV -ESP_WiFiManager_Lite v1.6.0 +ESP_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1314,7 +1371,7 @@ This is the terminal output when running [**ESP_WiFi_MQTT**](examples/ESP_WiFi_M ``` Starting ESP_WiFi_MQTT using LittleFS on ESP8266_NODEMCU -ESP_WiFiManager_Lite v1.6.0 +ESP_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1389,7 +1446,7 @@ NNN Starting ESP_WiFi_MQTT using LittleFS on ESP8266_NODEMCU -ESP_WiFiManager_Lite v1.6.0 +ESP_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1481,7 +1538,7 @@ This is the terminal output when running [**ESP_WiFi_MQTT**](examples/ESP_WiFi_M ``` Starting ESP_WiFi_MQTT using LittleFS on ESP32S2_DEV -ESP_WiFiManager_Lite v1.6.0 +ESP_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1594,7 +1651,7 @@ entry 0x4004c190 Starting ESP_WiFi_MQTT using LittleFS on ESP32S2_DEV -ESP_WiFiManager_Lite v1.6.0 +ESP_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1696,7 +1753,7 @@ This is the terminal output when running [**ESP_WiFi_MQTT**](examples/ESP_WiFi_M ``` Starting ESP_WiFi_MQTT using LittleFS on ESP32S2_DEV -ESP_WiFiManager_Lite v1.6.0 +ESP_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFC0003 multiResetDetectorFlag = 0xFFFC0003 @@ -1724,7 +1781,7 @@ NNNN NNNNN NNNNN NNNNN NN[WML] h:UpdLittleFS ``` Starting ESP_WiFi_MQTT using LittleFS on ESP32S2_DEV -ESP_WiFiManager_Lite v1.6.0 +ESP_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1780,7 +1837,7 @@ This is the terminal output when running [**ESP_WiFi**](examples/ESP_WiFi) examp ``` Starting ESP_WiFi_MQTT using LittleFS on ESP32_DEV -ESP_WiFiManager_Lite v1.6.0 +ESP_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFC0003 multiResetDetectorFlag = 0xFFFC0003 @@ -1824,7 +1881,7 @@ N ``` Starting ESP_WiFi_MQTT using LittleFS on ESP32_DEV -ESP_WiFiManager_Lite v1.6.0 +ESP_WiFiManager_Lite v1.7.0 ESP_MultiResetDetector v1.2.1 LittleFS Flag read = 0xFFFE0001 multiResetDetectorFlag = 0xFFFE0001 @@ -1935,6 +1992,8 @@ Submit issues to: [ESP_WiFiManager_Lite issues](https://github.com/khoih-prog/ES 23. Add support to **ESP32-C3 using EEPROM and SPIFFS** 24. Enable **scan of WiFi networks** for selection in Configuration Portal 25. Ready for ESP32 core v2.0.0+ +26. Fix ESP8266 bug not easy to connect to Config Portal for ESP8266 core v3.0.0+ +27. Fix the blocking issue in loop() with configurable `WIFI_RECON_INTERVAL` --- --- diff --git a/changelog.md b/changelog.md index 31113d1..c518b4e 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Release v1.7.0](#release-v170) * [Release v1.6.0](#release-v160) * [Release v1.5.1](#release-v151) * [Major Release v1.5.0](#major-release-v150) @@ -26,6 +27,13 @@ ## Changelog +### 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. 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/ESP_WiFi/defines.h b/examples/ESP_WiFi/defines.h index db1e6e2..237cae9 100644 --- a/examples/ESP_WiFi/defines.h +++ b/examples/ESP_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/ESP_WiFi_MQTT/defines.h b/examples/ESP_WiFi_MQTT/defines.h index e2c55a1..eccf0ea 100644 --- a/examples/ESP_WiFi_MQTT/defines.h +++ b/examples/ESP_WiFi_MQTT/defines.h @@ -67,10 +67,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 +79,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 73e8ec9..2b8ad96 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ESP_WiFiManager_Lite", - "version": "1.6.0", + "version": "1.7.0", "keywords": "wifi, wi-fi, MultiWiFi, multi-wifi, WiFiManager, esp8266, esp32, esp32-s2, esp32-c3, Communication, iot, credentials, persistent, config-portal, DoubleReset, MultiReset, DoubleResetDetector, littlefs, spiffs, eeprom, light-weight", "description": "Library to configure MultiWiFi/Credentials at runtime for ESP32 (including ESP32-S2 and ESP32-C3) 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 afe0f40..3c5a96c 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ESP_WiFiManager_Lite -version=1.6.0 +version=1.7.0 author=Khoi Hoang maintainer=Khoi Hoang license=MIT diff --git a/src/ESP_WiFiManager_Lite.h b/src/ESP_WiFiManager_Lite.h index 5185b68..2d4d38a 100644 --- a/src/ESP_WiFiManager_Lite.h +++ b/src/ESP_WiFiManager_Lite.h @@ -9,7 +9,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/ESP_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 08/01/2022 Fix the blocking issue in loop() with configurable WIFI_RECON_INTERVAL *****************************************************************************************************************************/ #pragma once @@ -40,7 +41,7 @@ #define USING_ESP32_C3 true #endif -#define ESP_WIFI_MANAGER_LITE_VERSION "ESP_WiFiManager_Lite v1.6.0" +#define ESP_WIFI_MANAGER_LITE_VERSION "ESP_WiFiManager_Lite v1.7.0" #ifdef ESP8266 @@ -179,7 +180,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 ///////////// @@ -300,13 +303,18 @@ 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 @@ -507,7 +515,9 @@ class ESP_WiFiManager_Lite #define REQUIRE_ONE_SET_SSID_PW false #endif -#define PASSWORD_MIN_LEN 8 +#define PASSWORD_MIN_LEN 8 + +#define RETRY_TIMES_CONNECT_WIFI 3 void begin(const char *iHostname = "") { @@ -565,7 +575,6 @@ class ESP_WiFiManager_Lite //// New DRD/MRD //// // noConfigPortal when getConfigData() OK and no MRD/DRD'ed - //if (getConfigData() && noConfigPortal) if (hadConfigData && noConfigPortal && (!isForcedConfigPortal) ) { hadConfigData = true; @@ -634,6 +643,21 @@ class ESP_WiFiManager_Lite #endif #endif +#ifndef RETRY_TIMES_RECONNECT_WIFI + #define RETRY_TIMES_RECONNECT_WIFI 2 +#else + // Force range of user-defined RETRY_TIMES_RECONNECT_WIFI between 2-5 times + #if (RETRY_TIMES_RECONNECT_WIFI < 2) + #warning RETRY_TIMES_RECONNECT_WIFI too low. Reseting to 2 + #undef RETRY_TIMES_RECONNECT_WIFI + #define RETRY_TIMES_RECONNECT_WIFI 2 + #elif (RETRY_TIMES_RECONNECT_WIFI > 5) + #warning RETRY_TIMES_RECONNECT_WIFI too high. Reseting to 5 + #undef RETRY_TIMES_RECONNECT_WIFI + #define RETRY_TIMES_RECONNECT_WIFI 5 + #endif +#endif + #ifndef RESET_IF_CONFIG_TIMEOUT #define RESET_IF_CONFIG_TIMEOUT true #endif @@ -653,10 +677,32 @@ class ESP_WiFiManager_Lite #endif #endif +#if !defined(WIFI_RECON_INTERVAL) + #define WIFI_RECON_INTERVAL 0 // default 0s between reconnecting WiFi +#else + #if (WIFI_RECON_INTERVAL < 0) + #define WIFI_RECON_INTERVAL 0 + #elif (WIFI_RECON_INTERVAL > 600000) + #define WIFI_RECON_INTERVAL 600000 // Max 10min + #endif +#endif + void run() { static int retryTimes = 0; + static bool wifiDisconnectedOnce = false; + + // Lost connection in running. Give chance to reconfig. + // Check WiFi status every 5s and update status + // Check twice to be sure wifi disconnected is real + static unsigned long checkstatus_timeout = 0; + #define WIFI_STATUS_CHECK_INTERVAL 5000L + + static uint32_t curMillis; + + curMillis = millis(); + #if USING_MRD //// New MRD //// // Call the mulyi reset detector loop method every so often, @@ -675,6 +721,29 @@ class ESP_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 ) { @@ -718,8 +787,27 @@ class ESP_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) @@ -729,6 +817,7 @@ class ESP_WiFiManager_Lite ESP_WML_LOGINFO(F("run: WiFi reconnected")); } +#endif } //ESP_WML_LOGINFO(F("run: Lost connection => configMode")); @@ -756,7 +845,6 @@ class ESP_WiFiManager_Lite // Check cores/esp32/esp_arduino_version.h and cores/esp32/core_version.h - //#if ( ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(2, 0, 0) ) //(ESP_ARDUINO_VERSION_MAJOR >= 2) #if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) ) WiFi.setHostname(RFC952_hostname); #else @@ -922,10 +1010,7 @@ class ESP_WiFiManager_Lite // Forced CP => Flag = 0xBEEFBEEF. Else => No forced CP // Flag to be stored at (EEPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE) - // to avoid corruption to current data - //#define FORCED_CONFIG_PORTAL_FLAG_DATA ( (uint32_t) 0xDEADBEEF) - //#define FORCED_PERS_CONFIG_PORTAL_FLAG_DATA ( (uint32_t) 0xBEEFDEAD) - + // to avoid corruption to current data const uint32_t FORCED_CONFIG_PORTAL_FLAG_DATA = 0xDEADBEEF; const uint32_t FORCED_PERS_CONFIG_PORTAL_FLAG_DATA = 0xBEEFDEAD; @@ -2214,6 +2299,17 @@ class ESP_WiFiManager_Lite ////////////////////////////////////////////// +// 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 @@ -2243,10 +2339,11 @@ class ESP_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 ) @@ -2265,6 +2362,8 @@ class ESP_WiFiManager_Lite { ESP_WML_LOGERROR(F("WiFi not connected")); +#if RESET_IF_NO_WIFI + #if USING_MRD // To avoid unnecessary MRD mrd->loop(); @@ -2277,12 +2376,14 @@ class ESP_WiFiManager_Lite ESP.reset(); #else ESP.restart(); - #endif + #endif + +#endif } return status; } - + ////////////////////////////////////////////// // NEW @@ -2614,7 +2715,6 @@ class ESP_WiFiManager_Lite ESP_WML_LOGERROR(F("h:UpdEEPROM")); #endif - //saveConfigData(); saveAllConfigData(); // Done with CP, Clear CP Flag here if forced @@ -2674,6 +2774,10 @@ class ESP_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); @@ -2681,7 +2785,9 @@ class ESP_WiFiManager_Lite 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) { diff --git a/src/ESP_WiFiManager_Lite_Debug.h b/src/ESP_WiFiManager_Lite_Debug.h index c5c4b23..d27db3f 100644 --- a/src/ESP_WiFiManager_Lite_Debug.h +++ b/src/ESP_WiFiManager_Lite_Debug.h @@ -9,7 +9,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/ESP_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 08/01/2022 Fix the blocking issue in loop() with configurable WIFI_RECON_INTERVAL *****************************************************************************************************************************/ #ifndef ESP_WiFiManager_Lite_Debug_h