ESP32 WiFiMulti: Connect to the Strongest Wi-Fi Network (from a list of networks)

Learn how to use WiFiMulti with the ESP32. It allows you to register multiple networks (SSID/password combinations). The ESP32 will connect to the Wi-Fi network with the strongest signal (RSSI). If the connection is lost, it will connect to the next network on the list.

ESP32 WiFiMulti Connect to the Strongest Wi-Fi Network from a list of networks

Using WiFiMulti in your ESP32 IoT projects is useful if your board can have access to more than one Wi-Fi network. Implementing this feature in your projects is very simple and improves your projects significantly.

ESP32 with WiFiMulti

You can easily add WiFiMulti to your ESP32 projects with just a few lines of code. You can find an example in your Arduino IDE. With an ESP32 board select (Tools > Board), go to File > Examples > WiFi > WifiMulti.

Here are the essential steps to use WiFiMulti with the ESP32.

Include Libraries

First, you need to include both WiFi.h and WiFiMulti.h libraries.

#include <WiFi.h>
#include <WiFiMulti.h>

WiFiMulti Object

Then, you need to create a WiFiMulti object:

WiFiMulti wifiMulti;

Add List of Networks

Then, in the setup(), use the addAp() method on the wifiMulti object to add a network. The addAP() method accepts as arguments the network SSID and password. You should add at least one network.

wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1");
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");

Connect to Wi-Fi

Finally, connect to Wi-Fi using the run() method. You can also print a message in case the Wi-Fi is disconnected.

if(wifiMulti.run() != WL_CONNECTED) {
  Serial.println("WiFi not connected!");
  delay(1000);
}

You can run this snippet on the loop() section and if the ESP32 gets disconnected from a Wi-Fi network, it will automatically try to connect to the next strongest network on the list.

ESP32 with WiFiMulti Example

For you to understand how WiFiMulti works with the ESP32, we created a simple example that does the following:

  • scans for available wi-fi networks and prints their RSSI (so that you can check that the ESP32 is actually connecting to the strongest network on the list);
  • connects to the strongest wi-fi network from a list of provided networks;
  • in case it loses connection with the network, it will automatically connect to the next strongest network on the list.

To test this, you can copy the following code to your Arduino IDE. It is based on the WiFiScan and WiFiMulti examples provided in the Arduino core examples for the ESP32.

/*
 *  Based on the following examples:
 *  WiFi > WiFiMulti: https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WiFiMulti/WiFiMulti.ino
 *  WiFi > WiFiScan: https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WiFiScan/WiFiScan.ino
 *  Complete project details at our blog: https://RandomNerdTutorials.com/
 *  
 */

#include <WiFi.h>
#include <WiFiMulti.h>

WiFiMulti wifiMulti;

// WiFi connect timeout per AP. Increase when connecting takes longer.
const uint32_t connectTimeoutMs = 10000;

void setup(){
  Serial.begin(115200);
  delay(10);
  WiFi.mode(WIFI_STA);
  
  // Add list of wifi networks
  wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1");
  wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
  wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");

  // WiFi.scanNetworks will return the number of networks found
  int n = WiFi.scanNetworks();
  Serial.println("scan done");
  if (n == 0) {
      Serial.println("no networks found");
  } 
  else {
    Serial.print(n);
    Serial.println(" networks found");
    for (int i = 0; i < n; ++i) {
      // Print SSID and RSSI for each network found
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.print(WiFi.SSID(i));
      Serial.print(" (");
      Serial.print(WiFi.RSSI(i));
      Serial.print(")");
      Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
      delay(10);
    }
  }

  // Connect to Wi-Fi using wifiMulti (connects to the SSID with strongest connection)
  Serial.println("Connecting Wifi...");
  if(wifiMulti.run() == WL_CONNECTED) {
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }
}

void loop(){
  //if the connection to the stongest hotstop is lost, it will connect to the next network on the list
  if (wifiMulti.run(connectTimeoutMs) == WL_CONNECTED) {
    Serial.print("WiFi connected: ");
    Serial.print(WiFi.SSID());
    Serial.print(" ");
    Serial.println(WiFi.RSSI());
  }
  else {
    Serial.println("WiFi not connected!");
  }
  delay(1000);
}

View raw code

Don’t forget to add a list of networks on the following lines. You can multiply those lines to add more networks.

wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1");
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");

Note: if you want to test this project, but at the moment, you only have access to one network, you can create a hotspot with your smartphone and add the hotspot name and password to the list of available networks. I tested this with my iPhone and it worked perfectly (you may need to remove spaces and special characters from the hotspot name).

ESP32 with WiFiMulti Demonstration

After adding a list of networks to your code, you can upload it to your ESP32.

Open the Serial Monitor at a baud rate of 115200 and press the ESP32 RST button to restart the board.

First, it will show a list of nearby networks and corresponding RSSI. In my case, I have access to the first and third networks. In my case, the ESP32 connects to the iPhone network which is the strongest on the list (an RSSI closer to zero means a stronger signal).

WiFiMulti Example with the ESP32 Scan and Connect to Network

If I remove the iPhone hotspot, the connection will be lost and it will connect to the next strongest network on the list.

WiFiMulti Example with the ESP32 Connect to the Next Network on the List

Wrapping Up

In this tutorial, you learned how to use WiFiMulti with the ESP32 to add a list of networks that the ESP32 can connect to. It will connect to the network with the strongest signal (RSSI). If it loses connection with that network, it will automatically try to connect to the next network on the list.

We hope you find this tutorial useful. We have other tutorials related to Wi-Fi functions with the ESP32 that you may find useful:

Learn more about the ESP32 with our resources:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

17 thoughts on “ESP32 WiFiMulti: Connect to the Strongest Wi-Fi Network (from a list of networks)”

  1. Excellent! Thanks for posting this. In my set-up, I have some 3rd-party devices that don’t play nicely with meshed WiFi infrastructure, so I have to use different SSIDs on my access points. On some ESP32 devices that I developed myself, I had tried to write code to fail-over between access points, but my efforts didn’t give me reliable and resilient behaviour. I’m looking forward to substituting this library into my projects when I revisit them. After all, let’s not re-invent wheels!

    Reply
    • Hello Gerallt,

      I hope you are doing well. I have the same issue as you, have you tried the WiFiMulti library?
      If yes, have you achieve a reliable connection and what is your thoughts about the library?

      Thanks.

      Reply
      • Hello, Ibrahim!
        I’m afraid that I still haven’t picked-up that project and tried-out the library. Thanks for reminding me about it — it’s now on my new-year to-do list. I’m embarrassed to say that the circuits are still on breadboards, but have been in use all of this time — on a fixed access point. When I deploy them properly, selection of the best AP will matter. It will be good to get these things deployed, finally!
        Sorry I couldn’t give you a definitive answer at this time.
        Regards,
        Gerallt.

        Reply
  2. You don’t mention so I assume this does not work with ESP8266 ? I know thw wifi libraries for that are different. ( i just have some deviecs that would benefit from this feature but they have ESP8266 installed, )

    Reply
  3. Hi Hamish
    I have used this for ESP9266
    and worked perfectly.
    #include <ESP8266WiFi.h>
    #include <ESP8266WiFiMulti.h>

    Reply
  4. This is excellent! And, it works with WiFiClientSecure.
    WiFi.mode(WIFI_STA); is necessary.

    I took an old sketch and just replaced the WiFi connection routine and it works perfectly. I didn’t really expect it to work with the secure connection but it does. One thing I did notice, however, is that the WiFi.mode(WIFI_STA); is necessary for stable operation. My original sketch didn’t use this as I assumed a default. I really should know by now that such assumptions should never be made.

    Reply
  5. Excellent!. Thanks for sharing!
    I’m doing kind of similar work but saving my SSID/Pass sets, in a dynamic file on the ESP and then manually checking the power of each SSID!!

    Reply
  6. Do you need to call the “run” method, in “loop” to get reconnected or will it happen during normal network activity?

    Reply
  7. In my experience, this does not necessarily pick the strongest signal. I have three in a cluster of greenhouses and two pick the strongest signal (-56) and the third picks the second (-67). It is handy in that I can move devices between my home and the nursery without reprogramming them.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.