[GH-ISSUE #1122] Autoconnect problem after network loss - need some help #958

Open
opened 2026-02-28 01:27:51 +03:00 by kerem · 15 comments
Owner

Originally created by @FilipDem on GitHub (Sep 10, 2020).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1122

Basic Infos

Hardware

WiFimanager Branch/Release:
WifiManager 2.0.3-alpha from the Arduino Library Manager

Esp8266/Esp32: ESP8266
Hardware: ESP-12F
ESP Core Version: 2.7.4

Description

I am using the WifiManager... Now it happens that the network is interrupted. I see then that the AP is activated (from LED behavior and network scan). The network comes up again (because my PC has again a WIFI connection. Afterwards the AP is quit (probably after timeout), however there is no network access. After reboot all comes back OK.

Is it correct that the StartConfigPortal is started if a network disconnection is detected? Don't understand where in the code this happens, seen I don't do sth special in the loop(). Who could help me to the behavior of the WifiManager to detect that the network is interrupted and up again?
Or somebody has some ideas what could be the problem, always welcome.

Concept Sketch

void setup() {
...
		WiFiManager wifiManager;
		wifiManager.setDebugOutput(DEBUG_WIFIMANAGER);
		wifiManager.setShowStaticFields(true);
		static const char* menu[] PROGMEM = {"wifi", "wifinoscan", "restart", "exit"};
		wifiManager.setMenu(menu, 4);
		wifiManager.setAPCallback(APconfigModeCallback);
		wifiManager.setAPStaticIPConfig(AP_IP_ADDRESS, AP_GATEWAY, AP_SUBNET_MASK);
		wifiManager.setConfigPortalTimeout(TIMEOUT_AP_PORTAL);

		// Connect to network or active AP if no connection can be made
		if (wifiManager.autoConnect(APname, APpassword)) {
			// Send the IP address of the ESP8266 to the computer
			DEBUG(Serial.print(F("\nConnected to: "));Serial.println(WiFi.SSID());Serial.print(F("IP address: "));Serial.println(WiFi.localIP());)
	
			// Update status of the connection
			LastConnectionStatus = true;
			LastConnectionDateTime = CurrentDateTime;
	
			// Led on
			ticker.detach();
			digitalWrite(LED, LOW);
	
			return true;
		}
		// Active AP has quit after timeout; no network connection
		else {
			DEBUG(Serial.print(F("\nNo network connection!"));)
	
			// Led slowly blinking if no network
			ticker.attach(1, toggle_led);
			
			return false;
		}

	}
...

}

void loop() {

...
		if (WiFi.status() == WL_CONNECTED) {
			// Connection is just restored
			if (LastConnectionStatus == false) {
				// Toggle status
				LastConnectionStatus = true;
				LastConnectionDateTime = CurrentDateTime;
				// Led on
				ticker.detach();
				digitalWrite(LED, LOW);
				DEBUG(Serial.println(F("WIFI connected again!"));)
			}
			return true;
		}
		// Not connected to network
		else {
			// Connection is just lost
			if (LastConnectionStatus == true) {
				// Toggle status and count disconnections
				LastConnectionStatus = false;
				NbrDeconnections++;
				DEBUG(Serial.println(F("WIFI disconnected!"));)
				// Led slowly blinking if no network
				ticker.attach(1, toggle_led);
			}
			// Connection stay lost: reboot after a certain time
			else {
				if ( CurrentDateTime-LastConnectionDateTime > MAX_TIME_WITHOUT_INTERNET ) {
					ESP.restart();
				}
			}
			return false;
		}
...

}

Originally created by @FilipDem on GitHub (Sep 10, 2020). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1122 #### Basic Infos #### Hardware **WiFimanager Branch/Release:** WifiManager 2.0.3-alpha from the Arduino Library Manager **Esp8266/Esp32: ESP8266** **Hardware: ESP-12F** **ESP Core Version: 2.7.4** ### Description I am using the WifiManager... Now it happens that the network is interrupted. I see then that the AP is activated (from LED behavior and network scan). The network comes up again (because my PC has again a WIFI connection. Afterwards the AP is quit (probably after timeout), however there is no network access. After reboot all comes back OK. Is it correct that the StartConfigPortal is started if a network disconnection is detected? Don't understand where in the code this happens, seen I don't do sth special in the loop(). Who could help me to the behavior of the WifiManager to detect that the network is interrupted and up again? Or somebody has some ideas what could be the problem, always welcome. ### Concept Sketch ``` void setup() { ... WiFiManager wifiManager; wifiManager.setDebugOutput(DEBUG_WIFIMANAGER); wifiManager.setShowStaticFields(true); static const char* menu[] PROGMEM = {"wifi", "wifinoscan", "restart", "exit"}; wifiManager.setMenu(menu, 4); wifiManager.setAPCallback(APconfigModeCallback); wifiManager.setAPStaticIPConfig(AP_IP_ADDRESS, AP_GATEWAY, AP_SUBNET_MASK); wifiManager.setConfigPortalTimeout(TIMEOUT_AP_PORTAL); // Connect to network or active AP if no connection can be made if (wifiManager.autoConnect(APname, APpassword)) { // Send the IP address of the ESP8266 to the computer DEBUG(Serial.print(F("\nConnected to: "));Serial.println(WiFi.SSID());Serial.print(F("IP address: "));Serial.println(WiFi.localIP());) // Update status of the connection LastConnectionStatus = true; LastConnectionDateTime = CurrentDateTime; // Led on ticker.detach(); digitalWrite(LED, LOW); return true; } // Active AP has quit after timeout; no network connection else { DEBUG(Serial.print(F("\nNo network connection!"));) // Led slowly blinking if no network ticker.attach(1, toggle_led); return false; } } ... } ``` void loop() { ``` ... if (WiFi.status() == WL_CONNECTED) { // Connection is just restored if (LastConnectionStatus == false) { // Toggle status LastConnectionStatus = true; LastConnectionDateTime = CurrentDateTime; // Led on ticker.detach(); digitalWrite(LED, LOW); DEBUG(Serial.println(F("WIFI connected again!"));) } return true; } // Not connected to network else { // Connection is just lost if (LastConnectionStatus == true) { // Toggle status and count disconnections LastConnectionStatus = false; NbrDeconnections++; DEBUG(Serial.println(F("WIFI disconnected!"));) // Led slowly blinking if no network ticker.attach(1, toggle_led); } // Connection stay lost: reboot after a certain time else { if ( CurrentDateTime-LastConnectionDateTime > MAX_TIME_WITHOUT_INTERNET ) { ESP.restart(); } } return false; } ... ``` } ```
Author
Owner

@tablatronix commented on GitHub (Sep 10, 2020):

autoconnect autostarts configportal on fail, it should only start on reboot, unless you disable that (its optional)

esp should auto reconnect, so why it is not is the question.

It sounds like your esp is having a problem reconnecting, it could be another bug or unrelated to wm.

You need to enable debugging and see what its doing.

Also make sure you set wifi.mode properly in your code, sta only if you do not want ap running

<!-- gh-comment-id:690687238 --> @tablatronix commented on GitHub (Sep 10, 2020): autoconnect autostarts configportal on fail, it should only start on reboot, unless you disable that (its optional) esp should auto reconnect, so why it is not is the question. It sounds like your esp is having a problem reconnecting, it could be another bug or unrelated to wm. You need to enable debugging and see what its doing. Also make sure you set wifi.mode properly in your code, sta only if you do not want ap running
Author
Owner

@ddweber456 commented on GitHub (Oct 28, 2020):

I have had the same issue with a power outages. Router takes 30 - 40 sec to connect to the web, mean time the ESP wifiManager.autoConnect is already in AP mode waiting for SSID/Password.

I fixed the problem with wifiManager.setTimeout and a ESP.restart.

wifiManager.setTimeout(120);
wifiManager.setConfigPortalTimeout(120);

if(!wifiManager.autoConnect("APSetup WIFI", "password")) {
Serial.println("failed to connect 1st time after BOOT and hit timeout");
delay(100);
ESP.restart();

I also added the following in Setup:

WiFi.persistent(true);
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);
<!-- gh-comment-id:718250780 --> @ddweber456 commented on GitHub (Oct 28, 2020): I have had the same issue with a power outages. Router takes 30 - 40 sec to connect to the web, mean time the ESP wifiManager.autoConnect is already in AP mode waiting for SSID/Password. I fixed the problem with wifiManager.setTimeout and a ESP.restart. wifiManager.setTimeout(120); wifiManager.setConfigPortalTimeout(120); if(!wifiManager.autoConnect("APSetup WIFI", "password")) { Serial.println("failed to connect 1st time after BOOT and hit timeout"); delay(100); ESP.restart(); I also added the following in Setup: WiFi.persistent(true); WiFi.setAutoConnect(true); WiFi.setAutoReconnect(true);
Author
Owner

@tablatronix commented on GitHub (Oct 29, 2020):

Yeah I would just add a little loop and deal with it in your own code.

<!-- gh-comment-id:718307778 --> @tablatronix commented on GitHub (Oct 29, 2020): Yeah I would just add a little loop and deal with it in your own code.
Author
Owner

@tablatronix commented on GitHub (Oct 29, 2020):

if you wifiManager.setTimeout(120); then it should keep checking before starting the cp, but maybe there is something else going on. There are documented issues of connects failing first time, I would reproduce and get some logs to see why the 120S is not enough

<!-- gh-comment-id:718308408 --> @tablatronix commented on GitHub (Oct 29, 2020): if you wifiManager.setTimeout(120); then it should keep checking before starting the cp, but maybe there is something else going on. There are documented issues of connects failing first time, I would reproduce and get some logs to see why the 120S is not enough
Author
Owner

@tarontop commented on GitHub (Mar 16, 2021):

我曾经遇到过停电的问题。路由器需要30到40秒才能连接到网络,这意味着ESP wifiManager.autoConnect已处于AP模式,正在等待SSID /密码。

我解决了wifiManager.setTimeout和ESP.restart的问题。

wifiManager.setTimeout(120);
wifiManager.setConfigPortalTimeout(120);

if(!wifiManager.autoConnect(“ APSetup WIFI”,“ password”)){
Serial.println(“启动后第一次连接失败,命中超时”);
延迟(100);
ESP.restart();

我还在安装程序中添加了以下内容:

WiFi.persistent(true);
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);

The problem I encountered is the same as you. I used non-blocking. How can I reconnect to Wi-Fi without restarting ESP8266?

<!-- gh-comment-id:800066403 --> @tarontop commented on GitHub (Mar 16, 2021): > 我曾经遇到过停电的问题。路由器需要30到40秒才能连接到网络,这意味着ESP wifiManager.autoConnect已处于AP模式,正在等待SSID /密码。 > > 我解决了wifiManager.setTimeout和ESP.restart的问题。 > > wifiManager.setTimeout(120); > wifiManager.setConfigPortalTimeout(120); > > if(!wifiManager.autoConnect(“ APSetup WIFI”,“ password”)){ > Serial.println(“启动后第一次连接失败,命中超时”); > 延迟(100); > ESP.restart(); > > 我还在安装程序中添加了以下内容: > > ``` > WiFi.persistent(true); > WiFi.setAutoConnect(true); > WiFi.setAutoReconnect(true); > ``` The problem I encountered is the same as you. I used non-blocking. How can I reconnect to Wi-Fi without restarting ESP8266?
Author
Owner

@tablatronix commented on GitHub (Mar 16, 2021):

esp8266 is supposed to autoreconnect

<!-- gh-comment-id:800252191 --> @tablatronix commented on GitHub (Mar 16, 2021): esp8266 is supposed to autoreconnect
Author
Owner

@tarontop commented on GitHub (Mar 17, 2021):

esp8266应该自动重新连接

When I test, it will reconnect, but this is only when the ESP8266 is not powered off, only the router is powered off, and then the router ESP8266 will reconnect, if the ESP8266 and the router are both powered off, they will not reconnect , Because it takes 2 minutes for my router to start up, ESP8266 automatically enters the AP mode.

<!-- gh-comment-id:800945380 --> @tarontop commented on GitHub (Mar 17, 2021): > esp8266应该自动重新连接 When I test, it will reconnect, but this is only when the ESP8266 is not powered off, only the router is powered off, and then the router ESP8266 will reconnect, if the ESP8266 and the router are both powered off, they will not reconnect , Because it takes 2 minutes for my router to start up, ESP8266 automatically enters the AP mode.
Author
Owner

@tablatronix commented on GitHub (Mar 17, 2021):

wm.setConfigportalTimeout()

There are about 5 solutions to this discussed all over these issues

<!-- gh-comment-id:801299813 --> @tablatronix commented on GitHub (Mar 17, 2021): wm.setConfigportalTimeout() There are about 5 solutions to this discussed all over these issues
Author
Owner

@tarontop commented on GitHub (Jul 9, 2021):

wm.setConfigportalTimeout()

在这些问题上讨论了大约 5 个解决方案

For non-blocking, wm.setConfigportalTimeout() does not seem to work

<!-- gh-comment-id:877071422 --> @tarontop commented on GitHub (Jul 9, 2021): > wm.setConfigportalTimeout() > > 在这些问题上讨论了大约 5 个解决方案 For non-blocking, `wm.setConfigportalTimeout()` does not seem to work
Author
Owner

@tablatronix commented on GitHub (Jul 10, 2021):

Hmm have log output?

<!-- gh-comment-id:877531262 --> @tablatronix commented on GitHub (Jul 10, 2021): Hmm have log output?
Author
Owner

@tarontop commented on GitHub (Jul 10, 2021):

image

<!-- gh-comment-id:877582605 --> @tarontop commented on GitHub (Jul 10, 2021): ![image](https://user-images.githubusercontent.com/37043404/125154879-e34cd100-e18e-11eb-982a-2f007681a7ca.png)
Author
Owner

@tarontop commented on GitHub (Jul 10, 2021):

After 30 seconds it was still in AP mode, it did not close the captive portal (I used non-blocking)

<!-- gh-comment-id:877582891 --> @tarontop commented on GitHub (Jul 10, 2021): After 30 seconds it was still in AP mode, it did not close the captive portal (I used non-blocking)
Author
Owner

@tarontop commented on GitHub (Aug 20, 2021):

Hello, will the poor signal quality cause the device to restart? I have a device whose signal is not very good, and the device will restart from time to time

<!-- gh-comment-id:902428645 --> @tarontop commented on GitHub (Aug 20, 2021): Hello, will the poor signal quality cause the device to restart? I have a device whose signal is not very good, and the device will restart from time to time
Author
Owner

@tablatronix commented on GitHub (Aug 21, 2021):

That sounds like bad power, unless you have some code to do a restart no

<!-- gh-comment-id:903039988 --> @tablatronix commented on GitHub (Aug 21, 2021): That sounds like bad power, unless you have some code to do a restart no
Author
Owner

@tablatronix commented on GitHub (Aug 21, 2021):

@tarontop can you make a new issue for timeout not woking in non blocking, it seems to be a valid bug. If not I will try to sometime soon.

<!-- gh-comment-id:903040133 --> @tablatronix commented on GitHub (Aug 21, 2021): @tarontop can you make a new issue for timeout not woking in non blocking, it seems to be a valid bug. If not I will try to sometime soon.
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#958
No description provided.