ESP32 with WiFiMulti: Connect to the Strongest Wi-Fi Network

Learn how to use the ESP32 with WiFiMulti. You may register numerous networks (SSID/password combinations) using it. The ESP32 will connect to the Wi-Fi network that has the strongest signal strength (RSSI). If the connection fails, it will attempt to connect to the next network in the list.

WiFiMulti is useful in your ESP32 IoT projects if your board can connect to more than one Wi-Fi network. Implementing this functionality in your projects is quite simple and dramatically improves them.

ESP32 with WiFiMulti

With just a few lines of code, you can quickly integrate WiFiMulti into your ESP32 projects. In your Arduino IDE, you may find an example. When you have an ESP32 board selected (Tools > Board), proceed to File > Examples > WiFi > WifiMulti.

lilygo Official Store

Here are the essential steps to using WiFiMulti with the ESP32:

Include Libraries

First, you need to include both the 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 a List of Networks

Then, in the setup(), use the wifiMulti object's addAp() method to add a network. The network SSID and password are sent as arguments to the addAP() method. At least one network should be added.

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, using the run() method, connect to Wi-Fi. You may also print a message if the Wi-Fi connection is lost.

keyestudio Official Store

if( != WL_CONNECTED) {
  Serial.println("WiFi not connected!");

If you run this snippet in the loop() section, the ESP32 will automatically try to connect to the next strongest network in the list if it is detached from a Wi-Fi network.

ESP32 with WiFiMulti Example

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

  • Scans for accessible wi-fi networks and prints their RSSI (to ensure that the ESP32 is connecting to the strongest network on the list);
  • Connects to the strongest available Wi-Fi network from a list of available networks;
  • It will automatically connect to the next strongest network on the list in case it loses connection with the first one.

You may test this by copying the following code into your Arduino IDE: It is based on the WiFiScan and WiFiMulti examples from the ESP32 Arduino core examples.

 *  Based on the following examples:
 *  WiFi > WiFiMulti:
 *  WiFi > WiFiScan:
 *  Complete project details at our blog:

#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(){
  // 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.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(" (");
      Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");

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

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

Don't forget to add a list of networks on the following lines. You can add additional networks by multiplying those lines.

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 wish to test this project but only have access to one network, you may use your smartphone to establish a hotspot and add the hotspot name and password to the list of possible networks. I tested it on my iPhone, and it worked well (you may need to remove spaces and special characters from the hotspot name).

ESP32 with WiFiMulti Demonstration

You can upload your code to your ESP32 after adding a list of networks to it.

TSCINBUNY Official Store

To restart the board, open the Serial Monitor at 115200 baud and press the ESP32 RST button.

It will first provide a list of nearby networks and their RSSI. In my case, I have access to the first and third networks. In my case, the ESP32 connects to the strongest network on the list (an RSSI closer to zero indicates a stronger signal).

WiFiMulti Example with the ESP32 Scan and Connect to Network

When I remove the iPhone hotspot, the connection is gone, 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


In this article, you learn how to use ESP32 with WiFiMulti to add a list of networks to which the ESP32 may connect. It will connect to the network that has the strongest signal strength (RSSI). If it loses contact with that network, it will try to connect to the next network on the list.

If you like ESP32, you may also like:

We hope you find this tutorial useful. Thanks for reading.

Oh hi there It’s nice to meet you.

Sign up to receive awesome content in your inbox, every month.

We don’t spam! Read our privacy policy for more info.

Leave a Reply

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

Developing IoT Projects with ESP32

Automate your home or business with inexpensive Wi-Fi devices