ESP32 WIFI Client Mode - Multiple Wirelss APs

At times, it may be necessary to move your ESP32 device between different locations, such as using your home WiFi at home, office WiFi at work, or mobile WiFi while on the move. It would be convenient to register multiple access points on the ESP32 development board so that it can automatically choose and connect to the available AP.

In this article, we will guide you on how to achieve this. The ESP32 will intelligently select the AP with the strongest signal among the available options to establish a connection.

It is called WiFi Multi Mode.

The main method is wifiMulti.run(). Essentially, this helper method performs three tasks:

  1. Conducts a WiFi scan to identify available access points (APs).
  2. Selects the best AP based on signal strength and the lists you provided.
  3. Attempts to connect to the chosen AP within a default timeout of 5 seconds.

By utilizing this method, you no longer need to manually implement a while loop or handle the connection process yourself, as typically done in basic WiFi mode. This method simplifies the process by handling all the necessary steps on your behalf.

wifiMulti.addAP(SSID, PWD) Add a Wireless AP
wifiMulti.run() Connect to the best AP. Default timeout is 5 seconds.
wifiMulti.run(3000) Change default connection timeout to 3 seconds

 

One drawback of the mentioned method is that it requires scanning all available WiFi networks, which results in longer execution time and increased power consumption. If you are utilizing an ESP32 device powered by a battery, it is highly recommended to use the basic WiFi mode with a fixed IP address to optimize power efficiency.

 

#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
void setup()
{
Serial.begin(115200);
Serial.println("Lonely Binary ESP32 Multi Wifi Example");
/*
Add your home, office APs here, as many access points as you like
ESP32 will use the available access point with the strognest signal.
*/
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");
/*
The function wifiMulti.run() executes a WiFi scan initially,
aiming to connect to the access point (AP) with the strongest
signal from the list of APs provided using wifiMulti.addAP() methods.
If the ESP32 fails to establish a connection with the chosen AP
within the default timeout period of 5 seconds,
it will abandon the attempt and return an error message.
*/
Serial.println("Connecting Wifi...");
if (wifiMulti.run() == WL_CONNECTED)
{
Serial.println("");
Serial.println("WiFi connected");
Serial.print("ESP32's IP Address : ");
Serial.println(WiFi.localIP());
Serial.print("ESP32's Mac Adress : ");
Serial.println(WiFi.macAddress());
Serial.print("ESP32's Subnet Mask: ");
Serial.println(WiFi.subnetMask());
Serial.print("ESP32's Gateway IP: ");
Serial.println(WiFi.gatewayIP());
Serial.print("ESP32's DNS: ");
Serial.println(WiFi.dnsIP());
Serial.print("Wireless SSID : ");
Serial.println(WiFi.SSID());
Serial.print("Wireless AP's Mac Address : ");
Serial.println(WiFi.BSSIDstr());
Serial.print("Wireless Channel : ");
Serial.println(WiFi.channel());
Serial.print("Wireless Signal Strength : ");
Serial.println(WiFi.RSSI());
}
}
void loop()
{
/*
If wifi is disconnected, reconnect again,
if all wifis are not available, print message on serial monitor
*/
if (wifiMulti.run() != WL_CONNECTED)
{
Serial.println("WiFi not connected!");
delay(1000);
}
}
view raw wifi_multi.ino hosted with ❤ by GitHub

RELATED ARTICLES

Leave a comment

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

Please note, comments must be approved before they are published