mirror of
https://github.com/tzapu/WiFiManager.git
synced 2026-04-28 01:25:49 +03:00
[GH-ISSUE #1383] Calling autoConnect() in loop() causes lag #1185
Labels
No labels
📶 WiFi
🕸️ HTTP
Branch
DEV Help Wanted
Discussion
Documentation
ESP32
Example
Good First Issue
Hotfix
In Progress
Incomplete
Needs Feeback
Priority
QA
Question
Task
Upstream/Dependancy
bug
duplicate
enhancement
invalid
pull-request
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/WiFiManager#1185
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @OekoSolveMG on GitHub (Mar 29, 2022).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1383
Basic Infos
Hardware
WiFiManager Branch/Release: Master 2.0.5-beta
Esp8266/Esp32: ESP32
Hardware: M5 Core 2
Description
When attempting to reconect in the loop, for the case that the device loses WiFi connection and automatically attempts to reconnect (with delays or not) the device becomes laggy and blocks input after a while.
The main reason from what I could debug is that the
autoConnect()subscribes to a callback inWiFi_autoReconnect(). In this case that would the theWiFi.onEvent.The problem is this subscription get's pushed back to a
vectorof callbacks in theWiFiGenericclass.Meaning that if you call
autoConnect()in a loop then over a shorter or longer period depeding on how fast you recallautoConnect(), the system will be subscribed to that many callbacks that the rest of the system starts to lag.Possible fixes
Make WiFiManager::WiFiEvent actually reconnect to the WiFi when using the ESP32. This does not seem to be the case right now
Make
autoConnect()removeEvent instead of just the deconstructor.Add a check that only subscribed to the callback if it isn't subscribed already.
A method like
wifiConnectDefault()could be made public instead of private so that this method could be used to reconnectSketch (Quite big so only relevant wifi parts have been included)
Sketch with fix
Both
wifiConnectDefault()andwaitForConnectResult()have been made public to ensure this fix works.@tablatronix commented on GitHub (Mar 29, 2022):
hmm, I did not expect people to call autoconnect in a loop
We can just check
wm_event_idand avoid recreating multiple events@tablatronix commented on GitHub (Mar 29, 2022):
Try that
I can add a better auto reconnect, ideally esp32 and 8266 are supposed to do this automatically.
@OekoSolveMG commented on GitHub (Mar 30, 2022):
Thanks a lot, my use case stems from me wanting to uphold the connection for a long time and because it is an enduser device the wifi could be turned off / not have enough signal strength for a while and lost the connection etc.
In this case I want to reconnect the device, as soon as possible because I am uploading data over MQTT.
I am still wondering if there is a cleaner way to reconnect the device at runtime if it ever looses connection, additionally do I have to turn on any features with the setters to make sure it reconnects or should that be done automatically anyway? Because
setWiFiAutoReconnect(true)seems to set a boolean that is never used. Only with the ESP8266, does it actually set theautoReconnectof the underlyingWiFiSTA.h.This is not done however for the ESP32 which only subscribed to the WiFiEvent, which also does not seem to reconnect.
Perhaps using something like
wifiConnectDefault()could be used in the Event callback when the connection has been lost, when using ESP32 or if possibleWiFi.setAutoReconnect()could be set for the ESP32 as well?I'll add my settings as well perhaps I missed one that needs to be set so that reconnection works?
Because using
autoConnect()does not seem to actually reconnect after the inital attempt, when the AP is connectable again. This happened even when bothsetConnectRetriesandsetConnectTImeoutwere commented out.Example build output of the WifiEvent, as we can see the WiFiEvent clearly realises the wifi is theoretically existing again and could be connected too. But the WiFiManager does not do that atleast for me.
@tablatronix commented on GitHub (Mar 30, 2022):
Well my point is that the esp libraries are supposed to have autoreconnect enabled by default but I have found they are not always working in the background, not sure why.
@OekoSolveMG commented on GitHub (Mar 31, 2022):
Okay thanks a lot for your help. I'll probably try to add a custom reconnect that you can enable with
setWiFiAutoReconnect()and a comment to the method that explains that you should enable it if the auto reconnect doesn't work for the ESP32.To do that I'll probably use the
WiFiEventmethod to check if the WiFi is disconnected and the error isn'tAP NOT FOUND, I should be able to then simply useconnectToDefaultAP()to reconnect to the saved access point if there is any and if there isn't reconnecting will be skipped.I'm currently busy with other stuff but as soon as I tested it on my device and implemented it I will make a pr with a reference to this issue and close this issue if that is fine for you :D.
Thanks a lot for your fast response.
@tablatronix commented on GitHub (Mar 31, 2022):
Thats what i do, I have not really had time to look into it, I usually just add a timer and check if wifi is connected and try a simple WiFi.reconnect(); if not, or reboot after a timeout
@tablatronix commented on GitHub (Apr 7, 2022):
ill create a new issue for autoreconnect testing and solutions
@OekoSolveMG commented on GitHub (Apr 8, 2022):
After looking into the WiFiClient / WiFiClientGeneric it seems that the
WiFi.setAutoReconnect()does do something, if you loose connection because of authentication or a lot of other reasons etc. You seem to automatically reconnect. This is not the case tough for the case where the access point is turned off and then turned on again. Because it doesn't register aWiFiEventwhen an access point that was previously turned off is turned on again.Meaning this case needs to be polled by the end user to reconnect yourself. Additionaly the
WiFiEventof the WiFiManager doesn't seem to register any WiFiEvents even ifsetWiFiAutoReconnect()was set.@tablatronix commented on GitHub (Apr 8, 2022):
Ahh that makes sense, it should still emit a disconnected event , to at least start a reconnect loop, so maybe I can improve this. For some reason I forgot it only does a single reconnect attempt I think.
Should be easy to add to non blocking, since we already have a process..
@tablatronix commented on GitHub (Apr 12, 2022):
I fixed this wrong, and backwards. oops