You can run this snippet on the loophole() area and if the ESP32 obtains separated from a Wi-Fi network, it will automatically try to link to the following toughest network on the list.
Note: if you want to evaluate this job, yet at the minute, you only have access to one network, you can create a hotspot with your smart device and add the hotspot name and password to the checklist of readily available networks. If it loses connection with that network, it will immediately attempt to link to the next network on the checklist.
ESP32 with WiFiMulti
Then, you need to produce a WiFiMulti item:
Then, in the configuration(), use the addAp() approach on the wifiMulti object to add a network.
Include Libraries
You can run this snippet on the loophole() section and also if the ESP32 obtains separated from a Wi-Fi network, it will instantly try to connect to the next strongest network on the list.
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti Object
WiFiMulti wifiMulti;
Add List of Networks
Don’t fail to remember to include a listing of networks on the adhering to 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");
Connect to Wi-Fi
Note: if you desire to check this project, yet at the moment, you just have accessibility to one network, you can produce a hotspot with your smartphone and add the hotspot name as well as password to the checklist of readily available networks. If it sheds link with that network, it will immediately try to connect to the following network on the list.
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 and 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);
}
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).
If I remove the iPhone hotspot, the connection will be lost and it will connect to the next strongest 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.