Reconnect to WiFi more quickly

Cool Javelin
Posts: 24
Joined: Wed Oct 20, 2021 3:58 am

Reconnect to WiFi more quickly

Postby Cool Javelin » Thu Dec 04, 2025 1:19 am

Hello:

Is there a way I can store WiFi information in flash memory such that I can reconnect to my router upon a reboot more quickly?

After I successfully connect to my WiFi, then press reset or download new code, (ESP8266 in this case) sometimes it takes a couple of minutes to reconnect.

It prints "connecting to <my WiFi>" then prints some periods, then says "Failed to connect."
I sit in a forever loop here until it connects which sooner or later it does.

Sometimes it takes 10 or 15 times.

I have read about "WiFi.setAutoReconnect(true);" and "WiFi.persistent(true);" but these things say they should be set AFTER the WiFi is connected to the router.

I have tried these things to no avail.

Thanks, Mark.

lichurbagan
Posts: 59
Joined: Thu Nov 13, 2025 3:20 pm

Re: Reconnect to WiFi more quickly

Postby lichurbagan » Thu Dec 04, 2025 3:29 pm

Use the ESP8266’s built-in WiFi storage. Don’t pass credentials every boot. Set:

Code: Select all

WiFi.persistent(true);
WiFi.setAutoReconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin();  // no arguments
This uses saved credentials. It reconnects much faster. Passing SSID and password forces a full scan. That causes long delays. A static IP helps too. It skips DHCP. Avoid forever loops that block tasks. Let auto-reconnect run in the background. This setup usually reconnects in under one second.

Cool Javelin
Posts: 24
Joined: Wed Oct 20, 2021 3:58 am

Re: Reconnect to WiFi more quickly

Postby Cool Javelin » Sat Dec 06, 2025 10:50 pm

Thank you lichurbagan:

This seems to work better.

Now I need to call WiFi.begin(ssid,passwd) or WiFi.begin() depending if this is a first time call, or subsequent call.

I will do more research on this, I think I'd like to use the later, then if that fails, use the former. But since it is now working, it will go a little lower on the priority list.

Mark.

Cool Javelin
Posts: 24
Joined: Wed Oct 20, 2021 3:58 am

Re: Reconnect to WiFi more quickly

Postby Cool Javelin » Sat Dec 06, 2025 10:52 pm

I did a revamp of my "connect to wifi" routines, I think this may be the best of both worlds.

Code: Select all

/*********************************************************************
**********************************************************************
	MyWiFi.h
	WiFi constants, variables, and procedures
**********************************************************************
**********************************************************************/

#ifndef _MyWiFi_H
#define _MyWiFi_H

#define RECONNECT_TO_WIFI true
#define ANEW_CONNECT_TO_WIFI false

#define NB_TRYWIFI 10

// provide text for the WiFi mode
const char *str_mode[]= { "WIFI_OFF", "WIFI_STA", "WIFI_AP", "WIFI_AP_STA" };

const char *str_status[]= {
  "WL_IDLE_STATUS",
  "WL_NO_SSID_AVAIL",
  "WL_SCAN_COMPLETED",
  "WL_CONNECTED",
  "WL_CONNECT_FAILED",
  "WL_CONNECTION_LOST",
  "WL_DISCONNECTED"
};

void ShowWifiStatus(void) {
	Serial.print  ("Connected to: ");			// show some info to the user
	Serial.print  (WiFi.SSID());
	Serial.print  (", as: ");
	Serial.print  (NODENAME);
	Serial.print  (". Mode: ");
	Serial.print  (str_mode[WiFi.getMode()]);
	Serial.print  (", IP address: ");
	Serial.print  (WiFi.localIP());
	Serial.print  (", MAC: ");
	Serial.println(WiFi.macAddress());
}

bool WifiGotConnected(int _try) {
	while ((WiFi.status() != WL_CONNECTED) && (_try < NB_TRYWIFI)) {	// sit here till we are connected
		Serial.print(".");						// let the user know we are working on it
		delay(500);							// wait 1/2 second
		_try++;								// and bump try count
		if ( _try >= NB_TRYWIFI ) {					// did we exceed our wait time?
			Serial.println("Didn't connect to WiFi this time.");
			return(false);
		}
	}
	return(true);						// we got connected
}

bool StartWifi(bool re_conn, const char *ssid, const char * password, const char *our_name) {
	int _try = 0;						// the connection attempt retry count
	Serial.print("Connecting to ");				// tell user we are connecting
	Serial.print  (ssid);					// to this router
	Serial.print(" - ");					// tell user we are connecting
	// use in case of mode problem (should be redundant)
	// switch to Station mode (also likely unneeded)
	if (WiFi.getMode() != WIFI_STA) {
	    WiFi.disconnect();
	}
	WiFi.hostname(our_name);				// tell the router who we are
	WiFi.persistent(true);					// reconnect if possabile
	WiFi.setAutoReconnect(true);				// auto reconnect if disconnected
	WiFi.mode(WIFI_STA);					// not an access point

// if we want to re connect
	if (re_conn) {
		Serial.println("Trying to REconnect.");
		WiFi.begin();					// no arguments, connect to last router

// now wait for a connection
		if (WifiGotConnected(_try)) {
			Serial.println("REconnection Successful");
			ShowWifiStatus();
			return(true);
		}
	}

// If we want a new connection or we didn't reconnect, supply credentials
	Serial.println("Trying to connect anew.");
	WiFi.begin(ssid, password);

// now wait for a connection
	if (WifiGotConnected(_try)) {
		Serial.println("ANEW connection Successful");
		ShowWifiStatus();
		return(true);
	}

	// we did not connect or REconnect...
	Serial.println("Cannot connect to WiFi network.");
	return(false);

}

#endif

Who is online

Users browsing this forum: PerplexityBot, Qwantbot and 4 guests