ESP32 – Connect to best WiFi from multiple options

WiFiMulti in Action

Generally, we specify one SSID and one password for the ESP32 WiFi connection. However, what if you have credentials available for multiple WiFi sources around ESP32 and wish to connect to the best network available? ESP32 Arduino has a built-in library specifically for this purpose: WiFiMulti.

Here are the steps required to add multiple SSID and password options to your sketch:

Step 1: Import WiFiMulti along with WiFi

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

Step 2: Create an object of type WiFiMulti

WiFiMulti wifiMulti;

Step 3: Add the multiple SSIDs and passwords using the .addAP() function (in the setup)

wifiMulti.addAP("SSID1", "password1");
wifiMulti.addAP("SSID2", "password2");
wifiMulti.addAP("SSID3", "password3");

Step 4: Connect to the WiFi using the .run() function.

It returns the status of the WiFi connection. If connected, you can print the SSID it is connected to.

if( == WL_CONNECTED) {

The .run() function first checks if the WiFi is already connected to one of the options provided by you. If yes, it returns. Otherwise, it tries to connect to that SSID from the list which has the best RSSI. You can try experimenting with two networks, keeping the ESP32 closer to one and then the other. The ESP32 will connect to different networks in both cases.

An example sketch is provided with the ESP32 Arduino Core. It can be found on GitHub here.

That’s it. You now have multiple options for WiFi connection and your ESP32 can connect to another available network if one network goes down. This makes the overall system more robust.

For more tutorials on ESP32, check out Also, you may find this course on ESP32 on Udemy to be quite helpful. Do check it out.

Leave a comment

Your email address will not be published. Required fields are marked *