[GH-ISSUE #705] Auto connect after boot using already saved credentials #590

Open
opened 2026-02-28 01:26:01 +03:00 by kerem · 14 comments
Owner

Originally created by @laercionit on GitHub (Aug 22, 2018).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/705

Hello
First congratulations for LIB.

I recently upgraded to the latest version and realized a change that had a big impact.

If I have a power outage, NodeMCU usually starts faster than the router.

This means that the nodemcu will enter AP mode to enter the captive portal until it has the timeout.

In the old days the nodemcu continued to try to connect to the wifi network with the last settings saved.

For some reason I have not been able to track this, it does not happen anymore. When it enters the AP mode by initiating the captive portal, the Wifi settings are lost even if the save does not occur.

Originally created by @laercionit on GitHub (Aug 22, 2018). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/705 Hello First congratulations for LIB. I recently upgraded to the latest version and realized a change that had a big impact. If I have a power outage, NodeMCU usually starts faster than the router. This means that the nodemcu will enter AP mode to enter the captive portal until it has the timeout. In the old days the nodemcu continued to try to connect to the wifi network with the last settings saved. For some reason I have not been able to track this, it does not happen anymore. When it enters the AP mode by initiating the captive portal, the Wifi settings are lost even if the save does not occur.
Author
Owner

@laercionit commented on GitHub (Aug 22, 2018):

Analyzing the LIB that generated this loss of functionality to continue trying to connect to the wifi using the credentials saved after the timeout of the captive portal was the code below.

I commented the change and the code behavior returned to normal.
It keeps trying to connect to the wifi after the timeout of the captive portal

// if(!WiFi.isConnected()){
// WiFi.persistent(false);
// // disconnect sta, start ap
// WiFi.disconnect(); // this alone is not enough to stop the autoconnecter
// WiFi.mode(WIFI_AP);
// WiFi.persistent(true);
// }
// else {
//setup AP
WiFi.mode(WIFI_AP_STA);
DEBUG_WM(F("SET AP STA"));
// }

<!-- gh-comment-id:414871794 --> @laercionit commented on GitHub (Aug 22, 2018): Analyzing the LIB that generated this loss of functionality to continue trying to connect to the wifi using the credentials saved after the timeout of the captive portal was the code below. I commented the change and the code behavior returned to normal. It keeps trying to connect to the wifi after the timeout of the captive portal // if(!WiFi.isConnected()){ // WiFi.persistent(false); // // disconnect sta, start ap // WiFi.disconnect(); // this alone is not enough to stop the autoconnecter // WiFi.mode(WIFI_AP); // WiFi.persistent(true); // } // else { //setup AP WiFi.mode(WIFI_AP_STA); DEBUG_WM(F("SET AP STA")); // }
Author
Owner

@tablatronix commented on GitHub (Aug 22, 2018):

Hmm the settings should not be erased, not even sure how they would be.

<!-- gh-comment-id:414885577 --> @tablatronix commented on GitHub (Aug 22, 2018): Hmm the settings should not be erased, not even sure how they would be.
Author
Owner

@tablatronix commented on GitHub (Aug 22, 2018):

The settings are not lost, sta mode is disabled, ill add abilty to turn that off, but it was changed because ap mode will not work when sta is disconnected.

Your best option for this is to not enter straight into autoconnect, but wait for wifi, or increase wm connect timeout

<!-- gh-comment-id:414886514 --> @tablatronix commented on GitHub (Aug 22, 2018): The settings are not lost, sta mode is disabled, ill add abilty to turn that off, but it was changed because ap mode will not work when sta is disconnected. Your best option for this is to not enter straight into autoconnect, but wait for wifi, or increase wm connect timeout
Author
Owner

@laercionit commented on GitHub (Aug 22, 2018):

But in my code I have to enter STA.
It still does not connect again without commenting on the code in LIB.

Any suggestion?

wifiManager.setTimeout(NetworkConfigTimeOut);
wifiManager.setAPCallback(NetworkConfigMode);
if (!wifiManager.autoConnect(NetworkDefaultSSID, NetworkDefaultPassword)) {
if (DebugCheck(Error)) DebugSerial.println F("NetworkInit: Failed to connect WiFi or Timeout");
LedWifiStop();
return;
} else {
if (DebugCheck(Info)) DebugSerial.println F("NetworkInit: WiFi Connected!");
LedWifiStop();
}
WiFi.persistent(true);
WiFi.mode(WIFI_STA);
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);
WiFi.hostname(System.Hostname);

<!-- gh-comment-id:414890643 --> @laercionit commented on GitHub (Aug 22, 2018): But in my code I have to enter STA. It still does not connect again without commenting on the code in LIB. Any suggestion? wifiManager.setTimeout(NetworkConfigTimeOut); wifiManager.setAPCallback(NetworkConfigMode); if (!wifiManager.autoConnect(NetworkDefaultSSID, NetworkDefaultPassword)) { if (DebugCheck(Error)) DebugSerial.println F("NetworkInit: Failed to connect WiFi or Timeout"); LedWifiStop(); return; } else { if (DebugCheck(Info)) DebugSerial.println F("NetworkInit: WiFi Connected!"); LedWifiStop(); } WiFi.persistent(true); WiFi.mode(WIFI_STA); WiFi.setAutoConnect(true); WiFi.setAutoReconnect(true); WiFi.hostname(System.Hostname);
Author
Owner

@laercionit commented on GitHub (Aug 22, 2018):

The problem was the return, but I think this change would be appropriate:

As timeout occurs, it continues as AP mode indefinitely, I believe we should take some action at the timeout.

//Ajuste Lib
// check if timeout
if(configPortalHasTimeout()){
WiFi.mode(WIFI_STA);
break;
}

//my code:

void NetworkInit() {
WiFiManager wifiManager;
WiFi.persistent(true);
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);
WiFi.hostname(System.Hostname);
if (DebugCheck(Debug)) DebugSerial.println F("NetworkInit: Network inicialization");
wifiManager.setTimeout(NetworkConfigTimeOut);
wifiManager.setAPCallback(NetworkConfigMode);
if (!wifiManager.autoConnect(NetworkDefaultSSID, NetworkDefaultPassword)) {
if (DebugCheck(Error)) DebugSerial.println F("NetworkInit: Failed to connect WiFi or Timeout");
LedWifiStop();
return;
} else {
if (DebugCheck(Info)) DebugSerial.println F("NetworkInit: WiFi Connected!");
LedWifiStop();
}
Waiting();
NetworkShowHostname();
NetworkShowIP();
NetworkShowIPAP();
NetworkShowMAC();
NetworkShowGTW();
NetworkShowDNS();
NetworkMonitor(true);//CheckWifi
NetworkMonitor(true);//CheckInternet
}

<!-- gh-comment-id:414895442 --> @laercionit commented on GitHub (Aug 22, 2018): The problem was the return, but I think this change would be appropriate: As timeout occurs, it continues as AP mode indefinitely, I believe we should take some action at the timeout. **//Ajuste Lib** // check if timeout if(configPortalHasTimeout()){ WiFi.mode(WIFI_STA); break; } **//my code:** void NetworkInit() { WiFiManager wifiManager; WiFi.persistent(true); WiFi.setAutoConnect(true); WiFi.setAutoReconnect(true); WiFi.hostname(System.Hostname); if (DebugCheck(Debug)) DebugSerial.println F("NetworkInit: Network inicialization"); wifiManager.setTimeout(NetworkConfigTimeOut); wifiManager.setAPCallback(NetworkConfigMode); if (!wifiManager.autoConnect(NetworkDefaultSSID, NetworkDefaultPassword)) { if (DebugCheck(Error)) DebugSerial.println F("NetworkInit: Failed to connect WiFi or Timeout"); LedWifiStop(); return; } else { if (DebugCheck(Info)) DebugSerial.println F("NetworkInit: WiFi Connected!"); LedWifiStop(); } Waiting(); NetworkShowHostname(); NetworkShowIP(); NetworkShowIPAP(); NetworkShowMAC(); NetworkShowGTW(); NetworkShowDNS(); NetworkMonitor(true);//CheckWifi NetworkMonitor(true);//CheckInternet }
Author
Owner

@laercionit commented on GitHub (Aug 22, 2018):

One question, if the Wifi is in AP_STA mode should it keep trying to connect to the wifi with the saved credentials or does it just work with STA mode?

<!-- gh-comment-id:414897159 --> @laercionit commented on GitHub (Aug 22, 2018): One question, if the Wifi is in AP_STA mode should it keep trying to connect to the wifi with the saved credentials or does it just work with STA mode?
Author
Owner

@tablatronix commented on GitHub (Aug 22, 2018):

It should

<!-- gh-comment-id:414897568 --> @tablatronix commented on GitHub (Aug 22, 2018): It should
Author
Owner

@tablatronix commented on GitHub (Aug 22, 2018):

I am looking into forcing startap on same channel as sta, so that it remains stable and leaving sta on.
Still testing to see if it fixes the stability issues.

The best option for now is to wait for router to come up based on reset reason , use configportal timeout and fall back to sketch to reconnect, or reboot.
(development branch only)

<!-- gh-comment-id:414898177 --> @tablatronix commented on GitHub (Aug 22, 2018): I am looking into forcing startap on same channel as sta, so that it remains stable and leaving sta on. Still testing to see if it fixes the stability issues. The best option for now is to wait for router to come up based on reset reason , use configportal timeout and fall back to sketch to reconnect, or reboot. (development branch only)
Author
Owner

@laercionit commented on GitHub (Aug 22, 2018):

Unfortunately for AP_STA it is not reconnecting and my timeout setting did not work, either. I'll keep the LIB code commented until we have another solution.

For me it is important to ensure that the equipment maintains the connection attempt after the timeout. This ensures that regardless of the power outage or any problem with the Wifi, as soon as the router is resolved the nodemcu will connect.

<!-- gh-comment-id:414898710 --> @laercionit commented on GitHub (Aug 22, 2018): Unfortunately for AP_STA it is not reconnecting and my timeout setting did not work, either. I'll keep the LIB code commented until we have another solution. For me it is important to ensure that the equipment maintains the connection attempt after the timeout. This ensures that regardless of the power outage or any problem with the Wifi, as soon as the router is resolved the nodemcu will connect.
Author
Owner

@tablatronix commented on GitHub (Aug 22, 2018):

How long does it take your router to come up?

<!-- gh-comment-id:415007927 --> @tablatronix commented on GitHub (Aug 22, 2018): How long does it take your router to come up?
Author
Owner

@laercionit commented on GitHub (Aug 22, 2018):

Hello my friend....

The question is not the time of the router.
Basically if there is an electrical network failure, or even a router
fault, I wish the nodemcu to continue trying to connect to the network
using the last credentials it has. As soon as the wifi router comes back
on, the nodemcu will connect. I do not want to limit time for this, it's
basically a failover logic.

Em qua, 22 de ago de 2018 às 09:00, Shawn A notifications@github.com
escreveu:

How long does it take your router to come up?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/tzapu/WiFiManager/issues/705#issuecomment-415007927,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAT4Hjp5gHDuJeeiS_9NpOLWlAB87cQpks5uTUftgaJpZM4WGy3F
.

<!-- gh-comment-id:415020972 --> @laercionit commented on GitHub (Aug 22, 2018): Hello my friend.... The question is not the time of the router. Basically if there is an electrical network failure, or even a router fault, I wish the nodemcu to continue trying to connect to the network using the last credentials it has. As soon as the wifi router comes back on, the nodemcu will connect. I do not want to limit time for this, it's basically a failover logic. Em qua, 22 de ago de 2018 às 09:00, Shawn A <notifications@github.com> escreveu: > How long does it take your router to come up? > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > <https://github.com/tzapu/WiFiManager/issues/705#issuecomment-415007927>, > or mute the thread > <https://github.com/notifications/unsubscribe-auth/AAT4Hjp5gHDuJeeiS_9NpOLWlAB87cQpks5uTUftgaJpZM4WGy3F> > . >
Author
Owner

@tablatronix commented on GitHub (Aug 22, 2018):

In your testing how long does it take the router to come back up?

<!-- gh-comment-id:415051014 --> @tablatronix commented on GitHub (Aug 22, 2018): In your testing how long does it take the router to come back up?
Author
Owner

@laercionit commented on GitHub (Aug 22, 2018):

Usually 40 seconds, but as I mentioned, I do not believe the best way is to
put a greater timeout for autoconnect.
I think the nodemcu should always keep trying to connect with the
credentials it has.

Laercio Junior
Cel./Pessoal: (21) 99162-7440
Email Pessoal: laercionit@gmail.com
Skype: laercionit

Em qua, 22 de ago de 2018 às 11:26, Shawn A notifications@github.com
escreveu:

In your testing how long does it take the router to come back up?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/tzapu/WiFiManager/issues/705#issuecomment-415051014,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAT4HhMULxiZvxrpQpbdmYTev3mTJl4mks5uTWoxgaJpZM4WGy3F
.

<!-- gh-comment-id:415051964 --> @laercionit commented on GitHub (Aug 22, 2018): Usually 40 seconds, but as I mentioned, I do not believe the best way is to put a greater timeout for autoconnect. I think the nodemcu should always keep trying to connect with the credentials it has. *Laercio Junior* Cel./Pessoal: (21) 99162-7440 Email Pessoal: laercionit@gmail.com Skype: laercionit Em qua, 22 de ago de 2018 às 11:26, Shawn A <notifications@github.com> escreveu: > In your testing how long does it take the router to come back up? > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > <https://github.com/tzapu/WiFiManager/issues/705#issuecomment-415051014>, > or mute the thread > <https://github.com/notifications/unsubscribe-auth/AAT4HhMULxiZvxrpQpbdmYTev3mTJl4mks5uTWoxgaJpZM4WGy3F> > . >
Author
Owner

@tablatronix commented on GitHub (Aug 23, 2018):

#706

<!-- gh-comment-id:415528679 --> @tablatronix commented on GitHub (Aug 23, 2018): #706
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#590
No description provided.