diff --git a/README.md b/README.md index e2df32cd..1c2ce3d2 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,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. SAMD_WiFiNINA](examples/SAMD_WiFiNINA) * [ 2. SAMD_WiFiNINA_MQTT](examples/SAMD_WiFiNINA_MQTT) @@ -219,7 +222,7 @@ This [**WiFiManager_NINA_Lite** library](https://github.com/khoih-prog/WiFiManag 7. [`Seeeduino SAMD core 1.8.2+`](https://github.com/Seeed-Studio/ArduinoCore-samd) for SAMD21/SAMD51 boards (XIAO M0, Wio Terminal, etc.). [![Latest release](https://img.shields.io/github/release/Seeed-Studio/ArduinoCore-samd.svg)](https://github.com/Seeed-Studio/ArduinoCore-samd/releases/latest/) 8. [`Adafruit nRF52 v1.2.0+`](https://www.adafruit.com) for nRF52 boards such as Adafruit NRF52840_FEATHER, NRF52832_FEATHER, NRF52840_FEATHER_SENSE, NRF52840_ITSYBITSY, NRF52840_CIRCUITPLAY, NRF52840_CLUE, NRF52840_METRO, NRF52840_PCA10056, PARTICLE_XENON, **NINA_B302_ublox**, etc. [![GitHub release](https://img.shields.io/github/release/adafruit/Adafruit_nRF52_Arduino.svg)](https://github.com/adafruit/Adafruit_nRF52_Arduino/releases/latest) 9. [`Arduino mbed_rp2040 core 2.6.1+`](https://github.com/arduino/ArduinoCore-mbed) for Arduino (Use Arduino Board Manager) RP2040-based boards, such as **Arduino Nano RP2040 Connect, RASPBERRY_PI_PICO, etc.**. [![GitHub release](https://img.shields.io/github/release/arduino/ArduinoCore-mbed.svg)](https://github.com/arduino/ArduinoCore-mbed/releases/latest) -10. [`Earle Philhower's arduino-pico core v1.9.12+`](https://github.com/earlephilhower/arduino-pico) for RP2040-based boards such as **RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040**, etc. [![GitHub release](https://img.shields.io/github/release/earlephilhower/arduino-pico.svg)](https://github.com/earlephilhower/arduino-pico/releases/latest) +10. [`Earle Philhower's arduino-pico core v1.9.13+`](https://github.com/earlephilhower/arduino-pico) for RP2040-based boards such as **RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040**, etc. [![GitHub release](https://img.shields.io/github/release/earlephilhower/arduino-pico.svg)](https://github.com/earlephilhower/arduino-pico/releases/latest) 11. [`Functional-VLPP library v1.0.2+`](https://github.com/khoih-prog/functional-vlpp) to use server's lambda function. To install. check [![arduino-library-badge](https://www.ardu-badge.com/badge/Functional-Vlpp.svg?)](https://www.ardu-badge.com/Functional-Vlpp) 12. [`WiFiNINA_Generic library v1.8.14-3+`](https://github.com/khoih-prog/WiFiNINA_Generic). To install. check [![arduino-library-badge](https://www.ardu-badge.com/badge/WiFiNINA_Generic.svg?)](https://www.ardu-badge.com/WiFiNINA_Generic) 13. [`Modified WiFi101 Library v0.16.1+`](https://github.com/khoih-prog/WiFi101) if using WINC1500/WiFi101 shields (MKR1000, etc.). @@ -742,6 +745,38 @@ 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 +``` + + --- --- @@ -1309,6 +1344,12 @@ void 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 + ///////////////////////////////////////////// #define USE_DYNAMIC_PARAMETERS true @@ -1500,7 +1541,7 @@ This is the terminal output when running [**SAMD_WiFiNINA**](examples/SAMD_WiFiN ``` Starting SAMD_WiFiNINA on SAMD NANO_33_IOT -WiFiManager_NINA_Lite v1.5.0 +WiFiManager_NINA_Lite v1.6.0 [WN] Hostname=SAMD-Master-Controller Flag read = 0xffffffff No doubleResetDetected @@ -1552,7 +1593,7 @@ FFFFFFFFF ``` Start SAMD_WiFiNINA on SAMD NANO_33_IOT -WiFiManager_NINA_Lite v1.5.0 +WiFiManager_NINA_Lite v1.6.0 [WN] Hostname=SAMD-WIFININA51F485 [WN] CrCCSum=44880,CrRCSum=-1 [WN] CCSum=53040,RCSum=-1 @@ -1599,7 +1640,7 @@ FFFFFFFFF ``` Start SAMD_WiFiNINA on SAMD NANO_33_IOT -WiFiManager_NINA_Lite v1.5.0 +WiFiManager_NINA_Lite v1.6.0 [WN] Hostname=SAMD-Master-Controller Flag read = 0xd0d04321 No doubleResetDetected @@ -1676,7 +1717,7 @@ HHHHHHHHHH HHHHHHHHHH ``` Start SAMD_WiFiNINA on SAMD NANO_33_IOT -WiFiManager_NINA_Lite v1.5.0 +WiFiManager_NINA_Lite v1.6.0 [WN] Hostname=SAMD-Master-Controller Flag read = 0xd0d04321 No doubleResetDetected @@ -1740,7 +1781,7 @@ FF ``` Start SAMD_WiFiNINA on SAMD NANO_33_IOT -WiFiManager_NINA_Lite v1.5.0 +WiFiManager_NINA_Lite v1.6.0 [WN] Hostname=SAMD-Master-Controller Flag read = 0xd0d04321 No doubleResetDetected @@ -1796,7 +1837,7 @@ HHHHHHHHH HHHHHHHHHH ``` Start SAMD_WiFiNINA on SAMD NANO_33_IOT -WiFiManager_NINA_Lite v1.5.0 +WiFiManager_NINA_Lite v1.6.0 [WN] Hostname=SAMD-Master-Controller Flag read = 0xd0d01234 doubleResetDetected @@ -1852,7 +1893,7 @@ This is the terminal output when running [**RP2040_WiFiNINA_MQTT**](examples/RP2 ``` Starting RP2040_WiFiNINA_MQTT on MBED NANO_RP2040_CONNECT -WiFiManager_NINA_Lite v1.5.0 +WiFiManager_NINA_Lite v1.6.0 [WN] Hostname=RP2040-Master-Controller LittleFS size (KB) = 64 LittleFS Mount OK @@ -1904,7 +1945,7 @@ N ``` Starting RP2040_WiFiNINA_MQTT on MBED NANO_RP2040_CONNECT -WiFiManager_NINA_Lite v1.5.0 +WiFiManager_NINA_Lite v1.6.0 [WN] Hostname=RP2040-Master-Controller LittleFS size (KB) = 64 LittleFS Mount OK @@ -2018,7 +2059,7 @@ Sometimes, the library will only work if you update the `WiFiNINA module/shield` 18. Add support to RP2040-based boards, such as **RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040**, using [**Earle Philhower's arduino-pico** core](https://github.com/earlephilhower/arduino-pico). 19. Add support to RP2040-based boards, such as **RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040**, using [**Arduino-mbed RP2040** core](https://github.com/arduino/ArduinoCore-mbed) 20. Add `LibraryPatches` for [**Adafruit_MQTT_Library**](https://github.com/adafruit/Adafruit_MQTT_Library) to fix compiler error for RP2040-based and many other boards. -21. Fix the blocking issue in loop() +21. Fix the blocking issue in loop() with configurable `WIFI_RECON_INTERVAL` --- --- @@ -2042,7 +2083,9 @@ Submit issues to: [WiFiManager_NINA_Lite issues](https://github.com/khoih-prog/W - [WiFiManager connection attempt to unconfigured ("blank") SSID after restart on SAMD #8](https://github.com/khoih-prog/WiFiManager_NINA_Lite/issues/8) leading to v1.1.3 and v1.2.0 6. Again thanks to [Michael H. "bizprof"](https://github.com/bizprof) to be `collaborator, co-author/maintainer` of this library. With the impressive new introducing feature : - `Enable scan of WiFi networks for selection in Configuration Portal`. Check [PR for v1.3.0 - Enable scan of WiFi networks #10](https://github.com/khoih-prog/WiFiManager_NINA_Lite/pull/10) leading to v1.3.0 -7. Thanks to [tomtobback](https://github.com/tomtobback) to report issue [retries block the main loop #18](https://github.com/khoih-prog/WiFiManager_NINA_Lite/issues/18) leading to version v1.5.0 to fix the blocking issue in loop(). +7. Thanks to [tomtobback](https://github.com/tomtobback) to report issue [retries block the main loop #18](https://github.com/khoih-prog/WiFiManager_NINA_Lite/issues/18) leading to version v1.5.0 and v1.6.0 to fix the blocking issue in loop() with `WIFI_RECON_INTERVAL`. + + diff --git a/changelog.md b/changelog.md index abddc97f..d87b3b42 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Major Release v1.6.0](#major-release-v160) * [Release v1.5.0](#release-v150) * [Release v1.4.1](#release-v141) * [Major Release v1.4.0](#major-release-v140) @@ -35,6 +36,11 @@ ## Changelog +### Major Release v1.6.0 + +1. Add support to RP2040-based boards, such as **NANO_RP2040_CONNECT**, using [**Earle Philhower's arduino-pico** core](https://github.com/earlephilhower/arduino-pico) +2. Configurable `WIFI_RECON_INTERVAL`. Check [retries block the main loop #18](https://github.com/khoih-prog/WiFiManager_NINA_Lite/issues/18#issuecomment-1006197561) + ### 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) diff --git a/examples/RP2040_WiFiNINA/RP2040_WiFiNINA.ino b/examples/RP2040_WiFiNINA/RP2040_WiFiNINA.ino index 68dc3938..e9d85ff6 100644 --- a/examples/RP2040_WiFiNINA/RP2040_WiFiNINA.ino +++ b/examples/RP2040_WiFiNINA/RP2040_WiFiNINA.ino @@ -129,7 +129,7 @@ void displayCredentialsInLoop() #endif void loop() -{ +{ WiFiManager_NINA->run(); check_status(); diff --git a/examples/RP2040_WiFiNINA/defines.h b/examples/RP2040_WiFiNINA/defines.h index a5eb6e56..5b23e811 100644 --- a/examples/RP2040_WiFiNINA/defines.h +++ b/examples/RP2040_WiFiNINA/defines.h @@ -134,12 +134,18 @@ // 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 +#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 + ///////////////////////////////////////////// #define USE_DYNAMIC_PARAMETERS true diff --git a/examples/RP2040_WiFiNINA_MQTT/defines.h b/examples/RP2040_WiFiNINA_MQTT/defines.h index 6a5230b3..2315256e 100644 --- a/examples/RP2040_WiFiNINA_MQTT/defines.h +++ b/examples/RP2040_WiFiNINA_MQTT/defines.h @@ -140,6 +140,12 @@ // 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 + ///////////////////////////////////////////// #define USE_DYNAMIC_PARAMETERS true diff --git a/examples/SAMD_WiFiNINA/defines.h b/examples/SAMD_WiFiNINA/defines.h index 82e795cf..14bef0c3 100644 --- a/examples/SAMD_WiFiNINA/defines.h +++ b/examples/SAMD_WiFiNINA/defines.h @@ -166,6 +166,12 @@ // 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 + ///////////////////////////////////////////// #define USE_DYNAMIC_PARAMETERS true diff --git a/examples/SAMD_WiFiNINA_MQTT/defines.h b/examples/SAMD_WiFiNINA_MQTT/defines.h index b896bd83..7912f9c3 100644 --- a/examples/SAMD_WiFiNINA_MQTT/defines.h +++ b/examples/SAMD_WiFiNINA_MQTT/defines.h @@ -171,6 +171,18 @@ // 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 + +// 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 + ///////////////////////////////////////////// #define USE_DYNAMIC_PARAMETERS true diff --git a/examples/SAM_DUE_WiFiNINA/defines.h b/examples/SAM_DUE_WiFiNINA/defines.h index 30a207c4..8e72b00b 100644 --- a/examples/SAM_DUE_WiFiNINA/defines.h +++ b/examples/SAM_DUE_WiFiNINA/defines.h @@ -128,6 +128,12 @@ // 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 + ///////////////////////////////////////////// #define USE_DYNAMIC_PARAMETERS true diff --git a/examples/SAM_DUE_WiFiNINA_MQTT/defines.h b/examples/SAM_DUE_WiFiNINA_MQTT/defines.h index e8589f77..0fb731b8 100644 --- a/examples/SAM_DUE_WiFiNINA_MQTT/defines.h +++ b/examples/SAM_DUE_WiFiNINA_MQTT/defines.h @@ -128,6 +128,12 @@ // 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 + ///////////////////////////////////////////// #define USE_DYNAMIC_PARAMETERS true diff --git a/examples/STM32_WiFiNINA/defines.h b/examples/STM32_WiFiNINA/defines.h index 543539d4..7b7578b9 100644 --- a/examples/STM32_WiFiNINA/defines.h +++ b/examples/STM32_WiFiNINA/defines.h @@ -142,6 +142,12 @@ // 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 + ///////////////////////////////////////////// #define USE_DYNAMIC_PARAMETERS true diff --git a/examples/STM32_WiFiNINA_MQTT/defines.h b/examples/STM32_WiFiNINA_MQTT/defines.h index ee076d34..f86de4e4 100644 --- a/examples/STM32_WiFiNINA_MQTT/defines.h +++ b/examples/STM32_WiFiNINA_MQTT/defines.h @@ -142,6 +142,12 @@ // 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 + ///////////////////////////////////////////// #define USE_DYNAMIC_PARAMETERS true diff --git a/examples/Teensy_WiFiNINA/defines.h b/examples/Teensy_WiFiNINA/defines.h index 4d84f2d9..b215285c 100644 --- a/examples/Teensy_WiFiNINA/defines.h +++ b/examples/Teensy_WiFiNINA/defines.h @@ -130,6 +130,12 @@ // 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 + ///////////////////////////////////////////// #define USE_DYNAMIC_PARAMETERS true diff --git a/examples/Teensy_WiFiNINA_MQTT/defines.h b/examples/Teensy_WiFiNINA_MQTT/defines.h index 165fd15c..fe0e59ba 100644 --- a/examples/Teensy_WiFiNINA_MQTT/defines.h +++ b/examples/Teensy_WiFiNINA_MQTT/defines.h @@ -133,6 +133,12 @@ // 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 + ///////////////////////////////////////////// #define USE_DYNAMIC_PARAMETERS true diff --git a/examples/nRF52_WiFiNINA/defines.h b/examples/nRF52_WiFiNINA/defines.h index 1699ca57..ceb2ef31 100644 --- a/examples/nRF52_WiFiNINA/defines.h +++ b/examples/nRF52_WiFiNINA/defines.h @@ -150,6 +150,18 @@ // 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 + +// 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 + ///////////////////////////////////////////// #define USE_DYNAMIC_PARAMETERS true diff --git a/examples/nRF52_WiFiNINA_MQTT/defines.h b/examples/nRF52_WiFiNINA_MQTT/defines.h index 08e20761..83416fb8 100644 --- a/examples/nRF52_WiFiNINA_MQTT/defines.h +++ b/examples/nRF52_WiFiNINA_MQTT/defines.h @@ -150,6 +150,12 @@ // 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 + ///////////////////////////////////////////// #define USE_DYNAMIC_PARAMETERS true diff --git a/library.json b/library.json index fce1d1f0..5331f3ba 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "WiFiManager_NINA_Lite", - "version": "1.5.0", + "version": "1.6.0", "keywords": "wifi, wi-fi, MutiWiFi, WiFiNINA, SAM DUE, SAMD, nRF52, STM32, rpi-pico, rp2040, nano-rp2040-connect, Credentials, config-portal, dynamic-params, FlashStorage-SAMD, FlashStorage-STM32, DueFlashStorage, LittleFS, Double-Reset, FlashStorage, light-weight, EEPROM, AVR Mega", "description": "Library to configure MultiWiFi/Credentials at runtime for AVR Mega, Teensy, SAM DUE, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, nRF52, RP2040-based (Nano RP2040 Connect, RASPBERRY_PI_PICO) boards, etc. using WiFiNINA modules/shields. 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, EEPROM, FlashStorage_SAMD, FlashStorage_STM32 or DueFlashStorage. DoubleDetectDetector feature permits entering Config Portal as requested", "authors": diff --git a/library.properties b/library.properties index 6d55f50e..f09143a4 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=WiFiManager_NINA_Lite -version=1.5.0 +version=1.6.0 author=Khoi Hoang maintainer=Khoi Hoang license=MIT diff --git a/src/WiFiManager_NINA_Lite_DUE.h b/src/WiFiManager_NINA_Lite_DUE.h index f4790cbc..0c676f83 100644 --- a/src/WiFiManager_NINA_Lite_DUE.h +++ b/src/WiFiManager_NINA_Lite_DUE.h @@ -8,7 +8,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.5.0 + Version: 1.6.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -30,6 +30,7 @@ 1.4.0 K Hoang 28/05/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using Arduino mbed or Arduino-pico core 1.4.1 K Hoang 12/10/2021 Update `platform.ini` and `library.json` 1.5.0 K Hoang 05/01/2022 Fix the blocking issue in loop() + 1.6.0 K Hoang 05/01/2022 Configurable WIFI_RECON_INTERVAL. Add support to RP2040 using arduino-pico core **********************************************************************************************************************************/ #ifndef WiFiManager_NINA_Lite_DUE_h @@ -48,7 +49,7 @@ #error This code is intended to run on the SAM DUE platform! Please check your Tools->Board setting. #endif -#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1.5.0" +#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1.6.0" #include #include @@ -444,6 +445,16 @@ class WiFiManager_NINA_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; @@ -455,6 +466,10 @@ class WiFiManager_NINA_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. @@ -463,7 +478,7 @@ class WiFiManager_NINA_Lite drd->loop(); //// New DRD //// - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -483,7 +498,7 @@ class WiFiManager_NINA_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; } // Lost connection in running. Give chance to reconfig. @@ -524,12 +539,31 @@ class WiFiManager_NINA_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + WN_LOGERROR(F("r:WLost.ReconW")); + + if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) + { + WN_LOGERROR(F("r:WOK")); + } + } +#else WN_LOGERROR(F("r:WLost.ReconW")); if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) { WN_LOGERROR(F("r:WOK")); } +#endif } } } diff --git a/src/WiFiManager_NINA_Lite_Debug.h b/src/WiFiManager_NINA_Lite_Debug.h index 3f596178..04c901a2 100644 --- a/src/WiFiManager_NINA_Lite_Debug.h +++ b/src/WiFiManager_NINA_Lite_Debug.h @@ -8,7 +8,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.5.0 + Version: 1.6.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -30,6 +30,7 @@ 1.4.0 K Hoang 28/05/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using Arduino mbed or Arduino-pico core 1.4.1 K Hoang 12/10/2021 Update `platform.ini` and `library.json` 1.5.0 K Hoang 05/01/2022 Fix the blocking issue in loop() + 1.6.0 K Hoang 05/01/2022 Configurable WIFI_RECON_INTERVAL. Add support to RP2040 using arduino-pico core **********************************************************************************************************************************/ #ifndef WiFiManager_NINA_Lite_Debug_h diff --git a/src/WiFiManager_NINA_Lite_RP2040.h b/src/WiFiManager_NINA_Lite_RP2040.h index 4acd2e2f..356e2264 100644 --- a/src/WiFiManager_NINA_Lite_RP2040.h +++ b/src/WiFiManager_NINA_Lite_RP2040.h @@ -8,7 +8,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.5.0 + Version: 1.6.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -30,6 +30,7 @@ 1.4.0 K Hoang 28/05/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using Arduino mbed or Arduino-pico core 1.4.1 K Hoang 12/10/2021 Update `platform.ini` and `library.json` 1.5.0 K Hoang 05/01/2022 Fix the blocking issue in loop() + 1.6.0 K Hoang 05/01/2022 Configurable WIFI_RECON_INTERVAL. Add support to RP2040 using arduino-pico core **********************************************************************************************************************************/ #ifndef WiFiManager_NINA_Lite_RP2040_h @@ -45,7 +46,7 @@ #error This code is intended to run on the RP2040 platform! Please check your Tools->Board setting. #endif -#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1.5.0" +#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1.6.0" #include @@ -442,6 +443,16 @@ class WiFiManager_NINA_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; @@ -453,6 +464,10 @@ class WiFiManager_NINA_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. @@ -461,7 +476,7 @@ class WiFiManager_NINA_Lite drd->loop(); //// New DRD //// - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -481,7 +496,7 @@ class WiFiManager_NINA_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; } // Lost connection in running. Give chance to reconfig. @@ -522,12 +537,31 @@ class WiFiManager_NINA_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + WN_LOGERROR(F("r:WLost.ReconW")); + + if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) + { + WN_LOGERROR(F("r:WOK")); + } + } +#else WN_LOGERROR(F("r:WLost.ReconW")); if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) { WN_LOGERROR(F("r:WOK")); } +#endif } } } @@ -950,6 +984,10 @@ class WiFiManager_NINA_Lite ////////////////////////////////////////////// +#if defined(ARDUINO_ARCH_MBED) + #warning Using Arduino-mbed core + // For boards using Arduino-mbed core + void saveForcedCP(uint32_t value) { // Mbed RP2040 code @@ -1332,63 +1370,6 @@ class WiFiManager_NINA_Lite } #endif - ////////////////////////////////////////////// - - void NULLTerminateConfig() - { - //#define HEADER_MAX_LEN 16 - //#define SERVER_MAX_LEN 32 - //#define TOKEN_MAX_LEN 36 - - // NULL Terminating to be sure - WIFININA_config.header[HEADER_MAX_LEN - 1] = 0; - WIFININA_config.WiFi_Creds[0].wifi_ssid[SSID_MAX_LEN - 1] = 0; - WIFININA_config.WiFi_Creds[0].wifi_pw [PASS_MAX_LEN - 1] = 0; - WIFININA_config.WiFi_Creds[1].wifi_ssid[SSID_MAX_LEN - 1] = 0; - WIFININA_config.WiFi_Creds[1].wifi_pw [PASS_MAX_LEN - 1] = 0; - WIFININA_config.board_name[BOARD_NAME_MAX_LEN - 1] = 0; - } - - ////////////////////////////////////////////// - - bool isWiFiConfigValid() - { - #if REQUIRE_ONE_SET_SSID_PW - // If SSID ="blank" or NULL, or PWD length < 8 (as required by standard) => return false - // Only need 1 set of valid SSID/PWD - if (!( ( ( strncmp(WIFININA_config.WiFi_Creds[0].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG)) && - strlen(WIFININA_config.WiFi_Creds[0].wifi_ssid) > 0 ) && - ( strlen(WIFININA_config.WiFi_Creds[0].wifi_pw) >= PASSWORD_MIN_LEN ) ) || - ( ( strncmp(WIFININA_config.WiFi_Creds[1].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG)) && - strlen(WIFININA_config.WiFi_Creds[1].wifi_ssid) > 0 ) && - ( strlen(WIFININA_config.WiFi_Creds[1].wifi_pw) >= PASSWORD_MIN_LEN ) ) )) - #else - // If SSID ="blank" or NULL, or PWD length < 8 (as required by standard) => invalid set - // Need both sets of valid SSID/PWD - if ( !strncmp(WIFININA_config.WiFi_Creds[0].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || - !strncmp(WIFININA_config.WiFi_Creds[0].wifi_pw, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || - !strncmp(WIFININA_config.WiFi_Creds[1].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || - !strncmp(WIFININA_config.WiFi_Creds[1].wifi_pw, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || - ( strlen(WIFININA_config.WiFi_Creds[0].wifi_ssid) == 0 ) || - ( strlen(WIFININA_config.WiFi_Creds[1].wifi_ssid) == 0 ) || - ( strlen(WIFININA_config.WiFi_Creds[0].wifi_pw) < PASSWORD_MIN_LEN ) || - ( strlen(WIFININA_config.WiFi_Creds[1].wifi_pw) < PASSWORD_MIN_LEN ) ) - #endif - { - // If SSID, PW ="blank" or NULL, set the flag - WN_LOGERROR(F("Invalid Stored WiFi Config Data")); - - // Nullify the invalid data to avoid displaying garbage - memset(&WIFININA_config, 0, sizeof(WIFININA_config)); - - hadConfigData = false; - - return false; - } - - return true; - } - ////////////////////////////////////////////// bool loadConfigData() @@ -1414,8 +1395,8 @@ class WiFiManager_NINA_Lite } } - fseek(file, 0, SEEK_SET); - fread((uint8_t *) &WIFININA_config, 1, sizeof(WIFININA_config), file); + fseek(0, SeekSet); + fread((uint8_t *) &WIFININA_config, 1, sizeof(WIFININA_config)); fclose(file); WN_LOGDEBUG(F("OK")); @@ -1437,8 +1418,8 @@ class WiFiManager_NINA_Lite if (file) { - fseek(file, 0, SEEK_SET); - fwrite((uint8_t *) &WIFININA_config, 1, sizeof(WIFININA_config), file); + fseek(0, SeekSet); + fwrite((uint8_t *) &WIFININA_config, 1, sizeof(WIFININA_config)); fclose(file); WN_LOGDEBUG(F("OK")); @@ -1455,8 +1436,8 @@ class WiFiManager_NINA_Lite if (file) { - fseek(file, 0, SEEK_SET); - fwrite((uint8_t *) &WIFININA_config, 1, sizeof(WIFININA_config), file); + fseek(0, SeekSet); + fwrite((uint8_t *) &WIFININA_config, 1, sizeof(WIFININA_config)); fclose(file); WN_LOGDEBUG(F("OK")); @@ -1470,60 +1451,599 @@ class WiFiManager_NINA_Lite saveDynamicData(); #endif } + +////////////////////////////////////////////// + +#else // #if defined(ARDUINO_ARCH_MBED) + +////////////////////////////////////////////// + + #warning Using arduino-pico core + // For boards using arduino-pico core + // Already defined in DoubleResetDetector_Generic.h + // FS* filesystem = &LittleFS; + // #define FileFS LittleFS + + //#define SEEK_SET SeekSet + + void saveForcedCP(uint32_t value) + { + // Mbed RP2040 code + File file = filesystem->open(CONFIG_PORTAL_FILENAME, "w"); + + WN_LOGERROR(F("SaveCPFile ")); + + if (file) + { + file.seek(0, SeekSet); + file.write((uint8_t *) &value, sizeof(value)); + file.close(); + + WN_LOGERROR(F("OK")); + } + else + { + WN_LOGERROR(F("failed")); + } + + // Trying open redundant CP file + file = filesystem->open(CONFIG_PORTAL_FILENAME_BACKUP, "w"); + + WN_LOGERROR(F("SaveBkUpCPFile ")); + + if (file) + { + file.seek(0, SeekSet); + file.write((uint8_t *) &value, sizeof(value)); + file.close(); + + WN_LOGERROR(F("OK")); + } + else + { + WN_LOGERROR(F("failed")); + } + } ////////////////////////////////////////////// - // New from v1.0.5 - void loadAndSaveDefaultConfigData() + void setForcedCP(bool isPersistent) { - // Load Default Config Data from Sketch - memcpy(&WIFININA_config, &defaultConfig, sizeof(WIFININA_config)); - strcpy(WIFININA_config.header, WIFININA_BOARD_TYPE); + uint32_t readForcedConfigPortalFlag = isPersistent? FORCED_PERS_CONFIG_PORTAL_FLAG_DATA : FORCED_CONFIG_PORTAL_FLAG_DATA; + + WN_LOGERROR(isPersistent ? F("setForcedCP Persistent") : F("setForcedCP non-Persistent")); - // Including config and dynamic data, and assume valid - saveConfigData(); - - WN_LOGDEBUG(F("======= Start Loaded Config Data =======")); - displayConfigData(WIFININA_config); + saveForcedCP(readForcedConfigPortalFlag); } ////////////////////////////////////////////// - // Return false if init new EEPROM or SPIFFS. No more need trying to connect. Go directly to config mode - bool getConfigData() + void clearForcedCP() { - bool dynamicDataValid = true; - int calChecksum; + uint32_t readForcedConfigPortalFlag = 0; + + WN_LOGERROR(F("clearForcedCP")); - hadConfigData = false; + saveForcedCP(readForcedConfigPortalFlag); + } + + ////////////////////////////////////////////// + + bool isForcedCP() + { + uint32_t readForcedConfigPortalFlag; + + WN_LOGDEBUG(F("Check if isForcedCP")); - // Use new LOAD_DEFAULT_CONFIG_DATA logic - if (LOAD_DEFAULT_CONFIG_DATA) - { - // Load Config Data from Sketch - loadAndSaveDefaultConfigData(); + File file = filesystem->open(CONFIG_PORTAL_FILENAME, "r"); + + WN_LOGDEBUG(F("LoadCPFile ")); + + if (!file) + { + WN_LOGDEBUG(F("failed")); + + // Trying open redundant config file + file = filesystem->open(CONFIG_PORTAL_FILENAME_BACKUP, "r"); - // Don't need Config Portal anymore - return true; + WN_LOGDEBUG(F("LoadBkUpCPFile ")); + + if (!file) + { + WN_LOGDEBUG(F("failed")); + return false; + } + } + + file.seek(0, SeekSet); + file.read((uint8_t *) &readForcedConfigPortalFlag, sizeof(readForcedConfigPortalFlag)); + file.close(); + WN_LOGDEBUG(F("OK")); + + + // Return true if forced CP (0xDEADBEEF read at offset EPROM_START + DRD_FLAG_DATA_SIZE + CONFIG_DATA_SIZE) + // => set flag noForcedConfigPortal = false + if (readForcedConfigPortalFlag == FORCED_CONFIG_PORTAL_FLAG_DATA) + { + persForcedConfigPortal = false; + return true; + } + else if (readForcedConfigPortalFlag == FORCED_PERS_CONFIG_PORTAL_FLAG_DATA) + { + persForcedConfigPortal = true; + return true; } else - { - // Load stored config data from LittleFS - // Get config data. If "blank" or NULL, set false flag and exit - if (!loadConfigData()) + { + return false; + } + } + + ////////////////////////////////////////////// + +#if USE_DYNAMIC_PARAMETERS + + bool checkDynamicData() + { + int checkSum = 0; + int readCheckSum; + char* readBuffer = nullptr; + + File file = filesystem->open(CREDENTIALS_FILENAME, "r"); + + WN_LOGDEBUG(F("LoadCredFile ")); + + if (!file) + { + WN_LOGDEBUG(F("failed")); + + // Trying open redundant config file + file = filesystem->open(CREDENTIALS_FILENAME_BACKUP, "r"); + + WN_LOGDEBUG(F("LoadBkUpCredFile ")); + + if (!file) { + WN_LOGDEBUG(F("failed")); return false; } + } + + // Find the longest pdata, then dynamically allocate buffer. Remember to free when done + // This is used to store tempo data to calculate checksum to see of data is valid + // We dont like to destroy myMenuItems[i].pdata with invalid data + + uint16_t maxBufferLength = 0; + + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) + { + if (myMenuItems[i].maxlen > maxBufferLength) + maxBufferLength = myMenuItems[i].maxlen; + } + + if (maxBufferLength > 0) + { + readBuffer = new char[ maxBufferLength + 1 ]; - // Verify ChkSum - calChecksum = calcChecksum(); - - WN_LOGERROR3(F("CCSum=0x"), String(calChecksum, HEX), - F(",RCSum=0x"), String(WIFININA_config.checkSum, HEX)); + // check to see NULL => stop and return false + if (readBuffer == NULL) + { + WN_LOGERROR(F("ChkCrR: Error can't allocate buffer.")); + return false; + } + else + { + WN_LOGDEBUG1(F("ChkCrR: Buffer allocated, Sz="), maxBufferLength + 1); + } + + uint16_t offset = 0; -#if USE_DYNAMIC_PARAMETERS - // Load stored dynamic data from LittleFS - dynamicDataValid = checkDynamicData(); + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) + { + uint8_t * _pointer = (uint8_t *) readBuffer; + + // Actual size of pdata is [maxlen + 1] + memset(readBuffer, 0, myMenuItems[i].maxlen + 1); + + // Redundant, but to be sure correct position + file.seek(offset, SeekSet); + file.read(_pointer, myMenuItems[i].maxlen); + + offset += myMenuItems[i].maxlen; + + WN_LOGDEBUG3(F("ChkCrR:pdata="), readBuffer, F(",len="), myMenuItems[i].maxlen); + + for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++,_pointer++) + { + checkSum += *_pointer; + } + } + + file.read((uint8_t *) &readCheckSum, sizeof(readCheckSum)); + + WN_LOGDEBUG(F("OK")); + + file.close(); + + WN_LOGERROR3(F("CrCCsum=0x"), String(checkSum, HEX), F(",CrRCsum=0x"), String(readCheckSum, HEX)); + + if (readBuffer != nullptr) + { + // Free buffer + delete [] readBuffer; + WN_LOGDEBUG(F("Buffer freed")); + } + + if ( checkSum == readCheckSum) + { + return true; + } + } + + return false; + } + + ////////////////////////////////////////////// + + bool loadDynamicData() + { + int checkSum = 0; + int readCheckSum; + totalDataSize = sizeof(WIFININA_config) + sizeof(readCheckSum); + + File file = filesystem->open(CREDENTIALS_FILENAME, "r"); + + WN_LOGDEBUG(F("LoadCredFile ")); + + if (!file) + { + WN_LOGDEBUG(F("failed")); + + // Trying open redundant config file + file = filesystem->open(CREDENTIALS_FILENAME_BACKUP, "r"); + + WN_LOGDEBUG(F("LoadBkUpCredFile ")); + + if (!file) + { + WN_LOGDEBUG(F("failed")); + return false; + } + } + + uint16_t offset = 0; + + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) + { + uint8_t * _pointer = (uint8_t *) myMenuItems[i].pdata; + totalDataSize += myMenuItems[i].maxlen; + + // Actual size of pdata is [maxlen + 1] + memset(myMenuItems[i].pdata, 0, myMenuItems[i].maxlen + 1); + + // Redundant, but to be sure correct position + file.seek(offset, SeekSet); + file.read(_pointer, myMenuItems[i].maxlen); + + offset += myMenuItems[i].maxlen; + + WN_LOGDEBUG3(F("CrR:pdata="), myMenuItems[i].pdata, F(",len="), myMenuItems[i].maxlen); + + for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++,_pointer++) + { + checkSum += *_pointer; + } + } + + file.read((uint8_t *) &readCheckSum, sizeof(readCheckSum)); + + WN_LOGDEBUG(F("OK")); + + file.close(); + + WN_LOGDEBUG3(F("CrCCsum=0x"), String(checkSum, HEX), F(",CrRCsum=0x"), String(readCheckSum, HEX)); + + if ( checkSum != readCheckSum) + { + return false; + } + + return true; + } + + ////////////////////////////////////////////// + + void saveDynamicData() + { + int checkSum = 0; + + File file = filesystem->open(CREDENTIALS_FILENAME, "w"); + + WN_LOGDEBUG(F("SaveCredFile ")); + + uint16_t offset = 0; + + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) + { + uint8_t* _pointer = (uint8_t *) myMenuItems[i].pdata; + + WN_LOGDEBUG3(F("CW1:pdata="), myMenuItems[i].pdata, F(",len="), myMenuItems[i].maxlen); + + if (file) + { + // Redundant, but to be sure correct position + file.seek(offset, SeekSet); + file.write(_pointer, myMenuItems[i].maxlen); + + offset += myMenuItems[i].maxlen; + } + else + { + WN_LOGDEBUG(F("failed")); + } + + for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++,_pointer++) + { + checkSum += *_pointer; + } + } + + if (file) + { + file.write((uint8_t *) &checkSum, sizeof(checkSum)); + file.close(); + + WN_LOGDEBUG(F("OK")); + } + else + { + WN_LOGDEBUG(F("failed")); + } + + WN_LOGDEBUG1(F("CrWCSum=0x"), String(checkSum, HEX)); + + // Trying open redundant Auth file + file = filesystem->open(CREDENTIALS_FILENAME_BACKUP, "w"); + + WN_LOGDEBUG(F("SaveBkUpCredFile ")); + + offset = 0; + + for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++) + { + uint8_t* _pointer = (uint8_t *) myMenuItems[i].pdata; + + WN_LOGDEBUG3(F("CW2:pdata="), myMenuItems[i].pdata, F(",len="), myMenuItems[i].maxlen); + + if (file) + { + file.seek(offset, SeekSet); + file.write(_pointer, myMenuItems[i].maxlen); + + // Redundant, but to be sure correct position + offset += myMenuItems[i].maxlen; + } + else + { + WN_LOGDEBUG(F("failed")); + } + + for (uint16_t j = 0; j < myMenuItems[i].maxlen; j++,_pointer++) + { + checkSum += *_pointer; + } + } + + if (file) + { + file.write((uint8_t *) &checkSum, sizeof(checkSum)); + file.close(); + + WN_LOGDEBUG(F("OK")); + } + else + { + WN_LOGDEBUG(F("failed")); + } + } +#endif + + ////////////////////////////////////////////// + + bool loadConfigData() + { + WN_LOGDEBUG(F("LoadCfgFile ")); + + // file existed + File file = filesystem->open(CONFIG_FILENAME, "r"); + + if (!file) + { + WN_LOGDEBUG(F("failed")); + + // Trying open redundant config file + file = filesystem->open(CONFIG_FILENAME_BACKUP, "r"); + + WN_LOGDEBUG(F("LoadBkUpCfgFile ")); + + if (!file) + { + WN_LOGDEBUG(F("failed")); + return false; + } + } + + file.seek(0, SeekSet); + file.read((uint8_t *) &WIFININA_config, sizeof(WIFININA_config)); + file.close(); + + WN_LOGDEBUG(F("OK")); + + return isWiFiConfigValid(); + } + + ////////////////////////////////////////////// + + void saveConfigData() + { + WN_LOGDEBUG(F("SaveCfgFile ")); + + int calChecksum = calcChecksum(); + WIFININA_config.checkSum = calChecksum; + WN_LOGDEBUG1(F("WCSum=0x"), String(calChecksum, HEX)); + + File file = filesystem->open(CONFIG_FILENAME, "w"); + + if (file) + { + file.seek(0, SeekSet); + file.write((uint8_t *) &WIFININA_config, sizeof(WIFININA_config)); + file.close(); + + WN_LOGDEBUG(F("OK")); + } + else + { + WN_LOGDEBUG(F("failed")); + } + + WN_LOGDEBUG(F("SaveBkUpCfgFile ")); + + // Trying open redundant Auth file + file = filesystem->open(CONFIG_FILENAME_BACKUP, "w"); + + if (file) + { + file.seek(0, SeekSet); + file.write((uint8_t *) &WIFININA_config, sizeof(WIFININA_config)); + file.close(); + + WN_LOGDEBUG(F("OK")); + } + else + { + WN_LOGDEBUG(F("failed")); + } + +#if USE_DYNAMIC_PARAMETERS + saveDynamicData(); +#endif + } + +////////////////////////////////////////////// + +#endif // #if defined(ARDUINO_ARCH_MBED) + + ////////////////////////////////////////////// + + void NULLTerminateConfig() + { + //#define HEADER_MAX_LEN 16 + //#define SERVER_MAX_LEN 32 + //#define TOKEN_MAX_LEN 36 + + // NULL Terminating to be sure + WIFININA_config.header[HEADER_MAX_LEN - 1] = 0; + WIFININA_config.WiFi_Creds[0].wifi_ssid[SSID_MAX_LEN - 1] = 0; + WIFININA_config.WiFi_Creds[0].wifi_pw [PASS_MAX_LEN - 1] = 0; + WIFININA_config.WiFi_Creds[1].wifi_ssid[SSID_MAX_LEN - 1] = 0; + WIFININA_config.WiFi_Creds[1].wifi_pw [PASS_MAX_LEN - 1] = 0; + WIFININA_config.board_name[BOARD_NAME_MAX_LEN - 1] = 0; + } + + ////////////////////////////////////////////// + + bool isWiFiConfigValid() + { + #if REQUIRE_ONE_SET_SSID_PW + // If SSID ="blank" or NULL, or PWD length < 8 (as required by standard) => return false + // Only need 1 set of valid SSID/PWD + if (!( ( ( strncmp(WIFININA_config.WiFi_Creds[0].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG)) && + strlen(WIFININA_config.WiFi_Creds[0].wifi_ssid) > 0 ) && + ( strlen(WIFININA_config.WiFi_Creds[0].wifi_pw) >= PASSWORD_MIN_LEN ) ) || + ( ( strncmp(WIFININA_config.WiFi_Creds[1].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG)) && + strlen(WIFININA_config.WiFi_Creds[1].wifi_ssid) > 0 ) && + ( strlen(WIFININA_config.WiFi_Creds[1].wifi_pw) >= PASSWORD_MIN_LEN ) ) )) + #else + // If SSID ="blank" or NULL, or PWD length < 8 (as required by standard) => invalid set + // Need both sets of valid SSID/PWD + if ( !strncmp(WIFININA_config.WiFi_Creds[0].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WIFININA_config.WiFi_Creds[0].wifi_pw, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WIFININA_config.WiFi_Creds[1].wifi_ssid, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + !strncmp(WIFININA_config.WiFi_Creds[1].wifi_pw, WM_NO_CONFIG, strlen(WM_NO_CONFIG) ) || + ( strlen(WIFININA_config.WiFi_Creds[0].wifi_ssid) == 0 ) || + ( strlen(WIFININA_config.WiFi_Creds[1].wifi_ssid) == 0 ) || + ( strlen(WIFININA_config.WiFi_Creds[0].wifi_pw) < PASSWORD_MIN_LEN ) || + ( strlen(WIFININA_config.WiFi_Creds[1].wifi_pw) < PASSWORD_MIN_LEN ) ) + #endif + { + // If SSID, PW ="blank" or NULL, set the flag + WN_LOGERROR(F("Invalid Stored WiFi Config Data")); + + // Nullify the invalid data to avoid displaying garbage + memset(&WIFININA_config, 0, sizeof(WIFININA_config)); + + hadConfigData = false; + + return false; + } + + return true; + } + + ////////////////////////////////////////////// + + // New from v1.0.5 + void loadAndSaveDefaultConfigData() + { + // Load Default Config Data from Sketch + memcpy(&WIFININA_config, &defaultConfig, sizeof(WIFININA_config)); + strcpy(WIFININA_config.header, WIFININA_BOARD_TYPE); + + // Including config and dynamic data, and assume valid + saveConfigData(); + + WN_LOGDEBUG(F("======= Start Loaded Config Data =======")); + displayConfigData(WIFININA_config); + } + + ////////////////////////////////////////////// + + // Return false if init new EEPROM or SPIFFS. No more need trying to connect. Go directly to config mode + bool getConfigData() + { + bool dynamicDataValid = true; + int calChecksum; + + hadConfigData = false; + + // Use new LOAD_DEFAULT_CONFIG_DATA logic + if (LOAD_DEFAULT_CONFIG_DATA) + { + // Load Config Data from Sketch + loadAndSaveDefaultConfigData(); + + // Don't need Config Portal anymore + return true; + } + else + { + // Load stored config data from LittleFS + // Get config data. If "blank" or NULL, set false flag and exit + if (!loadConfigData()) + { + return false; + } + + // Verify ChkSum + calChecksum = calcChecksum(); + + WN_LOGERROR3(F("CCSum=0x"), String(calChecksum, HEX), + F(",RCSum=0x"), String(WIFININA_config.checkSum, HEX)); + +#if USE_DYNAMIC_PARAMETERS + // Load stored dynamic data from LittleFS + dynamicDataValid = checkDynamicData(); #endif // If checksum = 0 => LittleFS has been cleared (by uploading new FW, etc) => force to CP @@ -1636,7 +2156,7 @@ class WiFiManager_NINA_Lite #if (MAX_NUM_WIFI_RECON_TRIES_PER_LOOP < 1) #define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1 #endif -#endif +#endif // New connection logic from v1.2.0 bool connectMultiWiFi(int retry_time) @@ -1698,6 +2218,23 @@ class WiFiManager_NINA_Lite uint8_t numIndexTried = 0; uint8_t numWiFiReconTries = 0; + +#if 0 + static uint32_t lastMillis = 0; + + //if (delayBetweenReconnect) + { + if ( (millis() - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = millis(); + } + else + { + // Don't recon WiFi + numWiFiReconTries = MAX_NUM_WIFI_RECON_TRIES_PER_LOOP; + } + } +#endif while ( !wifi_connected && (numIndexTried++ < NUM_WIFI_CREDENTIALS) && (numWiFiReconTries++ < MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) ) { diff --git a/src/WiFiManager_NINA_Lite_SAMD.h b/src/WiFiManager_NINA_Lite_SAMD.h index f9929a21..2894e02e 100644 --- a/src/WiFiManager_NINA_Lite_SAMD.h +++ b/src/WiFiManager_NINA_Lite_SAMD.h @@ -8,7 +8,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.5.0 + Version: 1.6.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -30,6 +30,7 @@ 1.4.0 K Hoang 28/05/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using Arduino mbed or Arduino-pico core 1.4.1 K Hoang 12/10/2021 Update `platform.ini` and `library.json` 1.5.0 K Hoang 05/01/2022 Fix the blocking issue in loop() + 1.6.0 K Hoang 05/01/2022 Configurable WIFI_RECON_INTERVAL. Add support to RP2040 using arduino-pico core **********************************************************************************************************************************/ #ifndef WiFiManager_NINA_Lite_SAMD_h @@ -48,7 +49,7 @@ #error This code is intended to run on the SAMD platform! Please check your Tools->Board setting. #endif -#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1.5.0" +#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1.6.0" #include @@ -440,6 +441,16 @@ class WiFiManager_NINA_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; @@ -451,6 +462,10 @@ class WiFiManager_NINA_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. @@ -459,7 +474,7 @@ class WiFiManager_NINA_Lite drd->loop(); //// New DRD //// - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -479,7 +494,7 @@ class WiFiManager_NINA_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; } // Lost connection in running. Give chance to reconfig. @@ -520,12 +535,31 @@ class WiFiManager_NINA_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + WN_LOGERROR(F("r:WLost.ReconW")); + + if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) + { + WN_LOGERROR(F("r:WOK")); + } + } +#else WN_LOGERROR(F("r:WLost.ReconW")); if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) { WN_LOGERROR(F("r:WOK")); } +#endif } } } diff --git a/src/WiFiManager_NINA_Lite_SAMD_CP.h b/src/WiFiManager_NINA_Lite_SAMD_CP.h index 46f51bc5..7b94c509 100644 --- a/src/WiFiManager_NINA_Lite_SAMD_CP.h +++ b/src/WiFiManager_NINA_Lite_SAMD_CP.h @@ -8,7 +8,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.5.0 + Version: 1.6.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -30,6 +30,7 @@ 1.4.0 K Hoang 28/05/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using Arduino mbed or Arduino-pico core 1.4.1 K Hoang 12/10/2021 Update `platform.ini` and `library.json` 1.5.0 K Hoang 05/01/2022 Fix the blocking issue in loop() + 1.6.0 K Hoang 05/01/2022 Configurable WIFI_RECON_INTERVAL. Add support to RP2040 using arduino-pico core *****************************************************************************************************************************/ #ifndef WiFiManager_NINA_Lite_SAMD_h @@ -48,7 +49,7 @@ #error This code is intended to run on the SAMD platform! Please check your Tools->Board setting. #endif -#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1.5.0" +#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1.6.0" #include // Include EEPROM-like API for FlashStorage @@ -426,6 +427,16 @@ class WiFiManager_NINA_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; @@ -437,6 +448,10 @@ class WiFiManager_NINA_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 WiFiManager_NINA_Lite drd->loop(); //// New DRD //// - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -465,7 +480,7 @@ class WiFiManager_NINA_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; } // Lost connection in running. Give chance to reconfig. @@ -506,12 +521,31 @@ class WiFiManager_NINA_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + WN_LOGERROR(F("r:WLost.ReconW")); + + if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) + { + WN_LOGERROR(F("r:WOK")); + } + } +#else WN_LOGERROR(F("r:WLost.ReconW")); if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) { WN_LOGERROR(F("r:WOK")); } +#endif } } } diff --git a/src/WiFiManager_NINA_Lite_STM32.h b/src/WiFiManager_NINA_Lite_STM32.h index 05e6e603..c0c78598 100644 --- a/src/WiFiManager_NINA_Lite_STM32.h +++ b/src/WiFiManager_NINA_Lite_STM32.h @@ -8,7 +8,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.5.0 + Version: 1.6.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -30,6 +30,7 @@ 1.4.0 K Hoang 28/05/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using Arduino mbed or Arduino-pico core 1.4.1 K Hoang 12/10/2021 Update `platform.ini` and `library.json` 1.5.0 K Hoang 05/01/2022 Fix the blocking issue in loop() + 1.6.0 K Hoang 05/01/2022 Configurable WIFI_RECON_INTERVAL. Add support to RP2040 using arduino-pico core **********************************************************************************************************************************/ #ifndef WiFiManager_NINA_Lite_STM32_h @@ -48,7 +49,7 @@ #error This code is intended to run on STM32 platform! Please check your Tools->Board setting. #endif -#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1.5.0" +#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1." #include #include @@ -437,6 +438,16 @@ class WiFiManager_NINA_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; @@ -448,6 +459,10 @@ class WiFiManager_NINA_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. @@ -456,7 +471,7 @@ class WiFiManager_NINA_Lite drd->loop(); //// New DRD //// - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -476,7 +491,7 @@ class WiFiManager_NINA_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; } // Lost connection in running. Give chance to reconfig. @@ -517,12 +532,31 @@ class WiFiManager_NINA_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + WN_LOGERROR(F("r:WLost.ReconW")); + + if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) + { + WN_LOGERROR(F("r:WOK")); + } + } +#else WN_LOGERROR(F("r:WLost.ReconW")); if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) { WN_LOGERROR(F("r:WOK")); } +#endif } } } diff --git a/src/WiFiManager_NINA_Lite_Teensy.h b/src/WiFiManager_NINA_Lite_Teensy.h index f7bc72af..5127d6cf 100644 --- a/src/WiFiManager_NINA_Lite_Teensy.h +++ b/src/WiFiManager_NINA_Lite_Teensy.h @@ -8,7 +8,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.5.0 + Version: 1.6.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -30,6 +30,7 @@ 1.4.0 K Hoang 28/05/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using Arduino mbed or Arduino-pico core 1.4.1 K Hoang 12/10/2021 Update `platform.ini` and `library.json` 1.5.0 K Hoang 05/01/2022 Fix the blocking issue in loop() + 1.6.0 K Hoang 05/01/2022 Configurable WIFI_RECON_INTERVAL. Add support to RP2040 using arduino-pico core **********************************************************************************************************************************/ #ifndef WiFiManager_NINA_Lite_Teensy_h @@ -45,7 +46,7 @@ #error Teensy 2.0 not supported yet #endif -#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1.5.0" +#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1.6.0" #include #include @@ -435,6 +436,16 @@ class WiFiManager_NINA_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; @@ -446,6 +457,10 @@ class WiFiManager_NINA_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. @@ -454,7 +469,7 @@ class WiFiManager_NINA_Lite drd->loop(); //// New DRD //// - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -474,7 +489,7 @@ class WiFiManager_NINA_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; } // Lost connection in running. Give chance to reconfig. @@ -515,12 +530,31 @@ class WiFiManager_NINA_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + WN_LOGERROR(F("r:WLost.ReconW")); + + if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) + { + WN_LOGERROR(F("r:WOK")); + } + } +#else WN_LOGERROR(F("r:WLost.ReconW")); if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) { WN_LOGERROR(F("r:WOK")); } +#endif } } } diff --git a/src/WiFiManager_NINA_Lite_nRF52.h b/src/WiFiManager_NINA_Lite_nRF52.h index f38050eb..73a4801f 100644 --- a/src/WiFiManager_NINA_Lite_nRF52.h +++ b/src/WiFiManager_NINA_Lite_nRF52.h @@ -8,7 +8,7 @@ Built by Khoi Hoang https://github.com/khoih-prog/WiFiManager_NINA_Lite Licensed under MIT license - Version: 1.5.0 + Version: 1.6.0 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -30,6 +30,7 @@ 1.4.0 K Hoang 28/05/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using Arduino mbed or Arduino-pico core 1.4.1 K Hoang 12/10/2021 Update `platform.ini` and `library.json` 1.5.0 K Hoang 05/01/2022 Fix the blocking issue in loop() + 1.6.0 K Hoang 05/01/2022 Configurable WIFI_RECON_INTERVAL. Add support to RP2040 using arduino-pico core **********************************************************************************************************************************/ #ifndef WiFiManager_NINA_Lite_nRF52_h @@ -47,7 +48,7 @@ #error This code is intended to run on the SAMD platform! Please check your Tools->Board setting. #endif -#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1.5.0" +#define WIFIMANAGER_NINA_LITE_VERSION "WiFiManager_NINA_Lite v1.6.0" #include @@ -444,6 +445,16 @@ class WiFiManager_NINA_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; @@ -455,6 +466,10 @@ class WiFiManager_NINA_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. @@ -463,7 +478,7 @@ class WiFiManager_NINA_Lite drd->loop(); //// New DRD //// - if ( !configuration_mode && (millis() > checkstatus_timeout) ) + if ( !configuration_mode && (curMillis > checkstatus_timeout) ) { if (WiFi.status() == WL_CONNECTED) { @@ -483,7 +498,7 @@ class WiFiManager_NINA_Lite } } - checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL; + checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL; } // Lost connection in running. Give chance to reconfig. @@ -524,12 +539,31 @@ class WiFiManager_NINA_Lite // Not in config mode, try reconnecting before forcing to config mode if ( !wifi_connected ) { + + +#if (WIFI_RECON_INTERVAL > 0) + + static uint32_t lastMillis = 0; + + if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL ) + { + lastMillis = curMillis; + + WN_LOGERROR(F("r:WLost.ReconW")); + + if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) + { + WN_LOGERROR(F("r:WOK")); + } + } +#else WN_LOGERROR(F("r:WLost.ReconW")); if (connectMultiWiFi(RETRY_TIMES_RECONNECT_WIFI)) { WN_LOGERROR(F("r:WOK")); } +#endif } } }