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

Commit 00f0753

Browse files
authored
v1.5.0 to fix the blocking issue in loop()
### Release v1.5.0 1. Fix the blocking issue in loop(). Check [retries block the main loop #18](khoih-prog/WiFiManager_NINA_Lite#18) 2. Configurable `WIFI_RECON_INTERVAL`. Check [retries block the main loop #18](khoih-prog/WiFiManager_NINA_Lite#18 (comment)) 3. Update `Packages' Patches`
1 parent 87ed7ec commit 00f0753

8 files changed

+483
-147
lines changed

src/Esp8266_AT_WM_Lite.h

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@
88
99
Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_WM_Lite
1010
Licensed under MIT license
11-
Version: 1.4.1
11+
Version: 1.5.0
1212
1313
Version Modified By Date Comments
1414
------- ----------- ---------- -----------
1515
1.0.0 K Hoang 09/03/2020 Initial coding
1616
1.0.1 K Hoang 20/03/2020 Add feature to enable adding dynamically more Credentials parameters in sketch
1717
1.0.2 K Hoang 17/04/2020 Fix bug. Add support to SAMD51 and SAMD DUE. WPA2 SSID PW to 63 chars.
18-
Permit to input special chars such as !,@,#,$,%,^,&,* into data fields.
18+
Permit to input special chars such as !,@,#,$,%,^,&,* into data fields.
1919
1.0.3 K Hoang 11/06/2020 Add support to nRF52 boards, such as AdaFruit Feather nRF52832, NINA_B30_ublox, etc.
20-
Add DRD support. Add MultiWiFi support
20+
Add DRD support. Add MultiWiFi support
2121
1.0.4 K Hoang 03/07/2020 Add support to ESP32-AT shields. Modify LOAD_DEFAULT_CONFIG_DATA logic.
22-
Enhance MultiWiFi connection logic. Fix WiFi Status bug.
22+
Enhance MultiWiFi connection logic. Fix WiFi Status bug.
2323
1.1.0 K Hoang 13/04/2021 Fix invalid "blank" Config Data treated as Valid. Optional one set of WiFi Credentials
2424
1.2.0 Michael H 28/04/2021 Enable scan of WiFi networks for selection in Configuration Portal
2525
1.3.0 K Hoang 12/05/2021 Add support to RASPBERRY_PI_PICO using Arduino-pico core
2626
1.4.0 K Hoang 01/06/2021 Add support to Nano_RP2040_Connect, RASPBERRY_PI_PICO using RP2040 Arduino mbed core
27-
1.4.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json`
27+
1.4.1 K Hoang 10/10/2021 Update `platform.ini` and `library.json`
28+
1.5.0 K Hoang 08/01/2022 Fix the blocking issue in loop() with configurable WIFI_RECON_INTERVAL
2829
***************************************************************************************************************************************/
2930

3031
#ifndef Esp8266_AT_WM_Lite_h
@@ -38,7 +39,7 @@
3839
#error This code is intended to run on the Mega2560 platform! Please check your Tools->Board setting.
3940
#endif
4041

41-
#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.4.1"
42+
#define ESP_AT_WM_LITE_VERSION "ESP_AT_WM_Lite v1.5.0"
4243

4344
#define DEFAULT_BOARD_NAME "AVR-MEGA"
4445

@@ -295,6 +296,16 @@ class ESP_AT_WiFiManager_Lite
295296

296297
//////////////////////////////////////////////
297298

299+
#if !defined(WIFI_RECON_INTERVAL)
300+
#define WIFI_RECON_INTERVAL 0 // default 0s between reconnecting WiFi
301+
#else
302+
#if (WIFI_RECON_INTERVAL < 0)
303+
#define WIFI_RECON_INTERVAL 0
304+
#elif (WIFI_RECON_INTERVAL > 600000)
305+
#define WIFI_RECON_INTERVAL 600000 // Max 10min
306+
#endif
307+
#endif
308+
298309
void run()
299310
{
300311
static int retryTimes = 0;
@@ -306,15 +317,19 @@ class ESP_AT_WiFiManager_Lite
306317
static unsigned long checkstatus_timeout = 0;
307318
#define WIFI_STATUS_CHECK_INTERVAL 5000L
308319

320+
static uint32_t curMillis;
321+
322+
curMillis = millis();
323+
309324
//// New DRD ////
310325
// Call the double reset detector loop method every so often,
311326
// so that it can recognise when the timeout expires.
312327
// You can also call drd.stop() when you wish to no longer
313328
// consider the next reset as a double reset.
314329
drd->loop();
315330
//// New DRD ////
316-
317-
if ( !configuration_mode && (millis() > checkstatus_timeout) )
331+
332+
if ( !configuration_mode && (curMillis > checkstatus_timeout) )
318333
{
319334
if (WiFi.status() == WL_CONNECTED)
320335
{
@@ -326,16 +341,16 @@ class ESP_AT_WiFiManager_Lite
326341
{
327342
wifiDisconnectedOnce = false;
328343
wifi_connected = false;
329-
ESP_AT_LOGDEBUG(F("r:Check&WLost"));
344+
ESP_AT_LOGERROR(F("r:Check&WLost"));
330345
}
331346
else
332347
{
333348
wifiDisconnectedOnce = true;
334349
}
335350
}
336351

337-
checkstatus_timeout = millis() + WIFI_STATUS_CHECK_INTERVAL;
338-
}
352+
checkstatus_timeout = curMillis + WIFI_STATUS_CHECK_INTERVAL;
353+
}
339354

340355
// Lost connection in running. Give chance to reconfig.
341356
if ( !wifi_connected )
@@ -348,7 +363,7 @@ class ESP_AT_WiFiManager_Lite
348363

349364
if (server)
350365
{
351-
//ESP_AT_LOGDEBUG(F("r:hC"));
366+
//ESP_AT_LOGDEBUG(F("r:handleClient"));
352367
server->handleClient();
353368
}
354369

@@ -363,7 +378,7 @@ class ESP_AT_WiFiManager_Lite
363378
{
364379
if (++retryTimes <= CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET)
365380
{
366-
ESP_AT_LOGDEBUG1(F("r:Wlost&TOut.ConW.Retry#"), retryTimes);
381+
ESP_AT_LOGERROR1(F("r:WLost&TOut.ConW.Retry#"), retryTimes);
367382
}
368383
else
369384
{
@@ -375,18 +390,38 @@ class ESP_AT_WiFiManager_Lite
375390
// Not in config mode, try reconnecting before forcing to config mode
376391
if ( !wifi_connected )
377392
{
378-
ESP_AT_LOGDEBUG(F("r:Wlost.ReconW"));
393+
394+
395+
#if (WIFI_RECON_INTERVAL > 0)
396+
397+
static uint32_t lastMillis = 0;
398+
399+
if ( (lastMillis == 0) || (curMillis - lastMillis) > WIFI_RECON_INTERVAL )
400+
{
401+
lastMillis = curMillis;
402+
403+
ESP_AT_LOGERROR(F("r:WLost.ReconW"));
404+
405+
if (connectToWifi(RETRY_TIMES_RECONNECT_WIFI))
406+
{
407+
ESP_AT_LOGERROR(F("r:WOK"));
408+
}
409+
}
410+
#else
411+
ESP_AT_LOGERROR(F("r:WLost.ReconW"));
412+
379413
if (connectToWifi(RETRY_TIMES_RECONNECT_WIFI))
380414
{
381-
ESP_AT_LOGDEBUG(F("r:WOK"));
415+
ESP_AT_LOGERROR(F("r:WOK"));
382416
}
417+
#endif
383418
}
384419
}
385420
}
386421
else if (configuration_mode)
387422
{
388423
configuration_mode = false;
389-
ESP_AT_LOGDEBUG(F("r:gotWBack"));
424+
ESP_AT_LOGERROR(F("r:gotWBack"));
390425
}
391426
}
392427

@@ -984,8 +1019,18 @@ class ESP_AT_WiFiManager_Lite
9841019
}
9851020

9861021
//////////////////////////////////////////////
1022+
1023+
// Max times to try WiFi per loop() iteration. To avoid blocking issue in loop()
1024+
// Default 1 and minimum 1.
1025+
#if !defined(MAX_NUM_WIFI_RECON_TRIES_PER_LOOP)
1026+
#define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1
1027+
#else
1028+
#if (MAX_NUM_WIFI_RECON_TRIES_PER_LOOP < 1)
1029+
#define MAX_NUM_WIFI_RECON_TRIES_PER_LOOP 1
1030+
#endif
1031+
#endif
9871032

988-
// New connection logic for ESP32-AT from v1.0.6
1033+
// New connection logic for ESP32-AT from v1.0.6
9891034
bool connectToWifi(int retry_time)
9901035
{
9911036
int sleep_time = 250;
@@ -1000,8 +1045,10 @@ class ESP_AT_WiFiManager_Lite
10001045
}
10011046

10021047
ESP_AT_LOGDEBUG3(F("con2WF:SSID="), ESP8266_AT_config.wifi_ssid, F(",PW="), ESP8266_AT_config.wifi_pw);
1048+
1049+
uint8_t numWiFiReconTries = 0;
10031050

1004-
while ( !wifi_connected && ( 0 < retry_time ) )
1051+
while ( !wifi_connected && ( 0 < retry_time ) && (numWiFiReconTries++ < MAX_NUM_WIFI_RECON_TRIES_PER_LOOP) )
10051052
{
10061053
ESP_AT_LOGDEBUG1(F("Remaining retry_time="), retry_time);
10071054

0 commit comments

Comments
 (0)