Esp32

Reconnect ESP32 to WiFi Network After Lost Connection

ESP32 is a WiFi based microcontroller board. ESP32 comes with a pre-installed WiFi module that provides it with wireless communication capabilities. ESP32 can be operated in three modes of WiFi station, access, or both. Sometimes ESP32 WiFi gets disconnected when ESP32 is in station mode so we have to adjust it or reconnect it. Read this guide to learn how to reconnect ESP32 to WiFi network:

How to Reconnect ESP32 with WiFi Network

Three different functions can be used to reconnect WiFi after a lost connection. Following are the three ways:

  1. WiFi.reconnect() function
  2. ESP.restart()
  3. ESP32 WiFi Events

1: Reconnect WiFi Using WiFi.reconnect() function

To reconnect ESP32 with WiFi we can use the following function that checks for the available WiFi networks. Once WiFi is disconnected the ESP32 board will try to connect again with it.

WiFi.reconnect()

Or alternatively we can also first disconnect the connected network using WiFi.disconnect() and then again begin the WiFi connection using the command WiFi.begin(ssid, password).

2: Reconnect to WiFi Network After Lost Connection using WiFi.restart() Function

Another way of connecting ESP32 with lost WiFi is restarting the ESP32 once the connection is lost. Now we will write a code which will check for connection status every 30 sec and if the connection is lost it will restart the connection and try to connect again to ESP32 with the lost network.

Open Arduino IDE and run the code.

#include <WiFi.h> /*WIFI library included*/

const char* ssid = "ABC";   /*SSID of network to connect*/
const char* password = "Password123";  /*password for SSID*/
unsigned long previousMillis = 0;
unsigned long interval = 30000;
void initWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());
}
void setup() {
  Serial.begin(115200);
  initWiFi();
  Serial.print("RSSI (WiFi strength): ");
  Serial.println(WiFi.RSSI());
}
void loop() {
  unsigned long currentMillis = millis();
  /*if condition to check wifi reconnection*/
  if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >=interval)) {
    Serial.print(millis());
    Serial.println("Reconnecting to WiFi...");
    WiFi.disconnect();
    WiFi.reconnect();
    previousMillis = currentMillis;
  }
}

Here in above code first write the SSID and password for the network one wants to connect. Next define the millis and total time after which ESP32 will check the connection status. Remember to define previous millis at starting because every time code run ESP32 will take millis as a reference for time to calculate 30 sec time intervals.

Code will check for the available network if the network is available and correct credentials are written inside the code ESP32 will automatically get connected to it.

Output on Serial Monitor

Following output will appear on the serial monitor. Here we can see the IP address and strength of the WiFi network ESP32 is connected to.

Note: RSSI measures in dBm and its value is negative.

3: How to Reconnect ESP32 WiFi Using WiFi Events

ESP32 because of its WiFi library has multiple WiFi events which are blessing in disguise. ESP32 has some specific events which automatically run if a certain event happens, after that it will automatically call a certain function.

Following are some important functions that helps ESP32 o automatically reestablished the lost connection:

  • ARDUINO_EVENT_WIFI_STA_CONNECTED: ESP32 in station mode connected with router
  • ARDUINO_EVENT_WIFI_STA_DISCONNECTED: ESP32 get disconnected from router

Code for WiFi Events

Below is the code for three different events of WiFi connection in ESP32. Open Arduino IDE, compile and upload the given code.

#include <WiFi.h> /*WIFI library included*/

const char* ssid = "ABC";   /*SSID of network to connect*/
const char* password = "Password123";  /*password for SSID*/

void Wifi_connected(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("ESP32 WIFI Connected to Access Point");
}

void Get_IPAddress(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("WIFI Connected!");
  Serial.println("IP address of Connected WIFI: ");
  Serial.println(WiFi.localIP());
}

void Wifi_disconnected(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("Disconnected from WIFI");
  Serial.print("Connection Lost Reason: ");
  Serial.println(info.disconnected.reason);
  Serial.println("Reconnecting...");
  WiFi.begin(ssid, password);
}

void setup(){
  Serial.begin(115200);
  WiFi.disconnect(true);
  delay(1000);

  WiFi.onEvent(Wifi_connected,SYSTEM_EVENT_STA_CONNECTED);
  WiFi.onEvent(Get_IPAddress, SYSTEM_EVENT_STA_GOT_IP);
  WiFi.onEvent(Wifi_disconnected, SYSTEM_EVENT_STA_DISCONNECTED);
  WiFi.begin(ssid, password);
  Serial.println("Waiting for WIFI network...");
}

void loop(){
  delay(1000);
}

In above code three different events are defined:

  • SYSTEM_EVENT_STA_CONNECTED: When ESP32 connected to WiFi
  • SYSTEM_EVENT_STA_GOT_IP: When ESP32 gets the IP address of the connected network
  • SYSTEM_EVENT_STA_DISCONNECTED: When ESP32 got disconnected from a network

When ESP32 gets connected to a network WiFiStationConnected() function will be called. It will simply print that ESP32 is connected to a network successfully. However, we can also modify it to light an LED or something else when ESP32 gets connected to a network.

Secondly when ESP32 gets the IP address of a network WiFiGotIP() function will run. This function like the previous one will print the IP address.

And the last event which we describe is when it will run the function WiFiStationDisconnected(), this function will print the message that WiFi is disconnected and tries to reconnect ESP32 with the WiFi network.

Output on Serial Monitor

Following output will appear on the serial monitor: it displays the IP address and a message that WiFi is connected. Similarly, if the WiFi is disconnected it will automatically check for WiFi connection every 30 sec and try to reconnect ESP32 with the network.

We have successfully sorted out the issue of ESP32 WiFi lost connection using three different methods.

Conclusion

ESP32 WiFi sometimes gets disconnected so to reconnect three different methods can be used. First is to simply rebegin the connection using WiFi.begin(ssid, password) command. The second method includes a constant check for ESP32 internet connection and automatically reconnects once its connection is lost. The last method which we covered would run certain functions when a specific WiFi event occurs such as connecting or disconnecting.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.