One essential task when using the ESP32 is ensuring it connects to the strongest available Wi-Fi network. This is crucial for maintaining optimal performance and reliability, especially in environments with multiple access points (APs) and fluctuating signal strengths.
There are two methods to achieve this:
- The Basic RSSI Approach
- Using the WiFiMulti Library (Recommended)
In this article, we’ll discuss each method in more detail to understand how they work and how you can implement them to ensure your ESP32 always connects to the strongest Wi-Fi network available.
Let’s get started!
Method 1 – The Basic RSSI Approach
In this approach, the ESP32 scans for all available Wi-Fi networks within range. For each network found, the ESP32 determines the Received Signal Strength Indicator (RSSI). This is a measure of how strong the signal is. The RSSI is a negative value; the closer to 0, the stronger the signal. The ESP32 compares the RSSI values of all networks, selecting the one with the strongest signal. Once identified, the ESP32 then attempts to connect to this chosen network.
Below is the code snippet to implement this method.
But before you upload it to your ESP32, be sure to replace the placeholder SSIDs and passwords with your actual Wi-Fi credentials.
// Replace with your network credentials
const char* ssidList[] = {"Network_1", "Network_2", "Network_3"};
const char* passwordList[] = {"password1", "password2", "password3"};After making these changes, go ahead and upload the code.
#include "WiFi.h"
// Replace with your network credentials
const char* ssidList[] = {"Network_1", "Network_2", "Network_3"};
const char* passwordList[] = {"password1", "password2", "password3"};
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Scanning for Wi-Fi networks...");
int n = WiFi.scanNetworks();
if (n == 0) {
Serial.println("No networks found.");
return;
}
int maxRSSI = -1000; // Initialize with a very low RSSI value
int bestNetworkIndex = -1;
for (int i = 0; i < n; ++i) {
int rssi = WiFi.RSSI(i);
Serial.printf("%d: %s, RSSI: %d\n", i + 1, WiFi.SSID(i).c_str(), rssi);
for (int j = 0; j < sizeof(ssidList)/sizeof(ssidList[0]); ++j) {
if (WiFi.SSID(i) == ssidList[j] && rssi > maxRSSI) {
maxRSSI = rssi;
bestNetworkIndex = j;
}
}
}
if (bestNetworkIndex == -1) {
Serial.println("No known networks found.");
} else {
Serial.printf("Connecting to the strongest network: %s\n", ssidList[bestNetworkIndex]);
WiFi.begin(ssidList[bestNetworkIndex], passwordList[bestNetworkIndex]);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected!");
Serial.printf("IP Address: %s\n", WiFi.localIP().toString().c_str());
}
}
void loop() {
// Nothing to do here
}After uploading, open the Serial Monitor at a baud rate of 115200 and press the EN button.
You’ll see a list of nearby networks and their corresponding RSSI values displayed in the Serial Monitor. The ESP32 will automatically select and connect to the network with the strongest signal, as indicated by the highest (least negative) RSSI value on the list.

Code Explanation
The code starts by including the WiFi.h library, which is essential for Wi-Fi functionality on the ESP32. Next, it defines two arrays: ssidList and passwordList. These arrays store the names (SSIDs) and passwords of the Wi-Fi networks you want your ESP32 to connect to. Be sure to replace the placeholder values with your actual network credentials.
#include "WiFi.h"
// Replace with your network credentials
const char* ssidList[] = {"Network_1", "Network_2", "Network_3"};
const char* passwordList[] = {"password1", "password2", "password3"};The setup() function is where the main action happens. It initializes serial communication, allowing you to see messages on the Serial Monitor, and sets the ESP32 to Station (STA) mode, indicating it will act as a Wi-Fi client connecting to an existing network. The code then disconnects from any previous connection for a clean start.
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.disconnect();The ESP32 then begins scanning for available Wi-Fi networks within range. If no networks are found, it simply prints a message and stops. If networks are found, the code enters a loop to find the strongest one. It measures the RSSI (signal strength) of each network and compares it with a variable called maxRSSI, which starts with a very low value. The code keeps track of the network with the highest RSSI among your specified networks.
int n = WiFi.scanNetworks();
if (n == 0) {
Serial.println("No networks found.");
return;
}
int maxRSSI = -1000; // Initialize with a very low RSSI value
int bestNetworkIndex = -1;
for (int i = 0; i < n; ++i) {
int rssi = WiFi.RSSI(i);
Serial.printf("%d: %s, RSSI: %d\n", i + 1, WiFi.SSID(i).c_str(), rssi);
for (int j = 0; j < sizeof(ssidList)/sizeof(ssidList[0]); ++j) {
if (WiFi.SSID(i) == ssidList[j] && rssi > maxRSSI) {
maxRSSI = rssi;
bestNetworkIndex = j;
}
}
}After scanning, if none of your preferred networks are found, the code prints a message. Otherwise, it prints the name of the strongest network and attempts to connect using the corresponding credentials from the arrays. The ESP32 continues trying to connect, displaying dots to indicate the process, until it successfully connects or times out. Once connected, it prints a “Connected!” message along with its assigned IP address.
if (bestNetworkIndex == -1) {
Serial.println("No known networks found.");
} else {
Serial.printf("Connecting to the strongest network: %s\n", ssidList[bestNetworkIndex]);
WiFi.begin(ssidList[bestNetworkIndex], passwordList[bestNetworkIndex]);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected!");
Serial.printf("IP Address: %s\n", WiFi.localIP().toString().c_str());
}The loop() function is empty in this case since the primary task of connecting to Wi-Fi is completed in the setup() function.
void loop() {
// Nothing to do here
}Method 2 – Using the WiFiMulti Library (Recommended)
In the second approach we use the WiFiMulti library. This library simplifies the management of multiple Wi-Fi networks. You add the credentials (SSIDs and passwords) of your preferred networks to the WiFiMulti object. When you call wifiMulti.run(), it automatically scans for available networks and attempts to connect to the strongest one from your list. If the connection fails, it seamlessly tries the next best network, ensuring more reliable connectivity.
Below is the code snippet to implement this method.
Remember to replace the placeholder SSIDs and passwords with your actual network credentials before uploading the code to your ESP32.
#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_1", "password_1");
wifiMulti.addAP("ssid_2", "password_2");
wifiMulti.addAP("ssid_3", "password_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);
}Once uploaded, open the Serial Monitor at a baud rate of 115200 and press the EN button.
You’ll see a list of nearby networks and their corresponding RSSI values displayed in the Serial Monitor. The ESP32 will automatically select and connect to the network with the strongest signal.
But the real magic of WiFiMulti lies in its ability to adapt. If the connection to the strongest network is lost for any reason, the library seamlessly switches to the next strongest network on your list, ensuring your ESP32 stays connected even in fluctuating environments.

Code Explanation
The code begins by including the WiFi.h and WiFiMulti.h libraries, which are essential for managing Wi-Fi connections on the ESP32. It then creates a WiFiMulti object, which will be used to store and manage multiple Wi-Fi network credentials.
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti wifiMulti;The setup() function sets the ESP32 to Station (STA) mode and adds a list of Wi-Fi networks (SSID and password pairs) that the ESP32 should try to connect to using wifiMulti.addAP() function. Afterward, it scans for available Wi-Fi networks in the vicinity. If it finds none, it prints a message indicating so. However, if it finds networks, it displays their details, including their names, signal strengths (RSSI), and security types.
WiFi.mode(WIFI_STA);
// Add list of wifi networks
wifiMulti.addAP("ssid_1", "password_1");
wifiMulti.addAP("ssid_2", "password_2");
wifiMulti.addAP("ssid_3", "password_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);
}
}Following the scan, the ESP32, using the wifiMulti.run() function, automatically scans for available networks and attempts to connect to the strongest one from your list. If the connection is successful, it prints a confirmation message along with the IP address assigned to the ESP32 by the network.
if(wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}The loop() function is where the continuous monitoring and reconnection logic resides. It checks if the ESP32 is still connected to the strongest network. If so, it displays the currently connected network’s name and signal strength. If the connection is lost, it automatically attempts to connect to the next strongest network on your list. This process repeats continuously, ensuring that your ESP32 always tries to maintain the best possible Wi-Fi connection.
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);
}Which One Should You Choose?
In summary, the basic RSSI approach is a good starting point. However, if you need a more robust and flexible solution that can automatically connect to the strongest network among multiple options, choose the WiFiMulti library.
