WiFiManager: if wifi is initially not available, but becomes available, autoConnect() still fails, even if we try again

Basic Infos

Hardware

WiFimanager Branch/Release: 0.16.0

Esp8266

Hardware: Wemos D1 mini

Description

After a power failure, when power returns, my ESP devices boot up and attempt to join the network before the wifi router has started up. This means that after a power failure, all the ESP devices have to be restarted to get them on the network.

My expectation was that if the wifi network is absent, and the AP is started, that WifiManager would keep looking for the network, and connect to it if it appeared during the AP session. OK it does not do that, perhaps for technical reasons.

OK, so I thought what I will do is attempt a second call to autoConnect() if the first one times out. (I set the timeout to 180 seconds.) To test the code I ran a wifi router that was powered off initially. During the first call to autoConnect() I powered the wifi router on. The network was thus present for the second call. This also fails and the second call to autoConnect() prints a debug message that the credentials are not present. This is not true.

In case the credentials were being somehow ‘hidden’ by the first call, I ensured that the wifiManager object went out of scope and I created a second WifiManager object for the second call to autoConnect() so there could be no caching issues. Still does not work! (Code is below)

If I reset the ESP, leaving the router powered on WifiManager connected with the stored credentials on the first call.

It appears that WifiManager cannot connect to a wifi network that was not present when the ESP started.

I expect the second call after a 3 minute delay to work just like an initial call.

What can one do to make an ESP join the network if the network comes online after the ESP starts?

Settings in IDE

Module: Wemos D1

Code

{
	  WiFiManager wifiManager;	

	  Serial.print(F("Trying wifi manager with AP:"));
	  Serial.println(APName);
	  wifiManager.setConfigPortalTimeout(timeout);  
	//  wifiManager.setDebugOutput(false);
	  wifiManager.autoConnect(APName);
		// wifi manager wait here until they connect or timeout expires
	 
	}	
	if (WiFi.status() != WL_CONNECTED) {
		// try once more with a new wifi manager
		Serial.println(F("Trying wifi manager again"));
		WiFiManager wifiManager2;
		wifiManager2.setConfigPortalTimeout(10);  // seconds
	//  wifiManager.setDebugOutput(false);
		wifiManager2.autoConnect(APName);
	} // not connected

Ouput

And here is the debug output from the serial monitor:

19:18:35.473 -> Trying wifi manager with AP:RemoteSign
19:18:35.473 -> *WM: 
19:18:35.473 -> *WM: AutoConnect
19:18:35.473 -> *WM: Connecting as wifi client...
19:18:35.507 -> *WM: Status:
19:18:35.507 -> *WM: 0
19:18:35.507 -> *WM: Using last saved values, should be faster

Switched router on at this stage

19:18:42.219 -> *WM: Connection result: 
19:18:42.219 -> *WM: 1
19:18:42.254 -> *WM: 
19:18:42.254 -> *WM: Configuring access point... 
19:18:42.254 -> *WM: RemoteSign
19:18:42.792 -> *WM: AP IP address: 
19:18:42.792 -> *WM: 192.168.4.1
19:18:42.792 -> *WM: HTTP server started
19:21:42.243 -> *WM: freeing allocated params!
19:21:42.243 -> Trying wifi manager again
19:21:42.243 -> *WM: 
19:21:42.243 -> *WM: AutoConnect
19:21:42.243 -> *WM: Connecting as wifi client...
19:21:42.243 -> *WM: Status:
19:21:42.243 -> *WM: 0

19:21:42.243 -> *WM: No saved credentials


19:21:42.243 -> *WM: Connection result: 
19:21:42.243 -> *WM: 0
19:21:42.243 -> *WM: 
19:21:42.243 -> *WM: Configuring access point... 
19:21:42.243 -> *WM: RemoteSign
19:21:42.773 -> *WM: AP IP address: 
19:21:42.773 -> *WM: 192.168.4.1
19:21:42.773 -> *WM: HTTP server started
19:21:52.264 -> *WM: freeing allocated params!

I then reset the ESP module and it connect the first try, proving that the credentials are in fact present.

19:22:32.659 -> Trying wifi manager with AP:RemoteSign
19:22:32.659 -> *WM: 
19:22:32.659 -> *WM: AutoConnect
19:22:32.659 -> *WM: Connecting as wifi client...
19:22:32.659 -> *WM: Status:
19:22:32.659 -> *WM: 0
19:22:32.659 -> *WM: Using last saved values, should be faster
19:22:40.163 -> *WM: Connection result: 
19:22:40.163 -> *WM: 3
19:22:40.163 -> *WM: IP Address:
19:22:40.163 -> *WM: 192.168.2.100
19:22:40.163 -> *WM: freeing allocated params!

About this issue

Most upvoted comments

I have this same problem reported. At first I thought there was a problem with my code, because the ESP sometimes reconnected, and sometimes it didn’t.

What I noticed, and was able to reproduce on the bench, is that if the Internet does not come back when the ESP is running the setup, it thinks that the internet does not exist, it generates an AP and is in an infinite loop waiting for connections on the AP. After a simple reset on ESP, everything is back to normal operation!

Has anyone managed to resolve this issue?