Connect to the Strongest WiFi Network in ESP32 Using WiFiMulti Function
ESP32 with WiFiMulti Function
ESP32 can connect to multiple networks if we have access to more than one network, we just need to define their SSID and password inside the code. It will search for the available network and connect to the strongest Wi-Fi network we define in code.
Note: Remember it will connect to one network at a time if the connection to that network is lost then ESP32 will connect to other available WiFi networks.
We can open WiFiMulti example in Arduino IDE:
Go to: File > Examples > WiFi > WifiMulti
How to Add Multiple WiFi Networks in ESP32
To add multiple WiFi networks in the ESP32 board. We will use the WifiMulti function with addAP(). The addAP() function can accept multiple SSIDs and passwords as an argument. To add multiple networks their SSID and passwords should be defined separately. Following is the syntax to add more than one network using WifiMulti function with addAP():
How to Connect to Strongest WiFi Network in ESP32
To connect ESP32 with the strongest available network we will combine the ESP32 WiFi scan and WiFi multi example. We will write a code in Arduino IDE that will do the following steps:
- Scan for the available networks
- Print their RSSI (Signal Strength) on serial monitor. So, we can check the strongest available network
- Automatically connects to the strongest network
- If it loses connection, it will automatically connect to the next available strongest network
To test the code, we will be using a Smartphone hotspot and a WiFi network. Always assign a simple name to the mobile hotspot network to avoid any error.
Now upload the code given in ESP32 board using Arduino IDE.
Open Arduino IDE and upload code in ESP32. Remember to select COM port.
Code started by defining WiFi libraries for ESP32, then we created a WiFiMulti object. Next, in the setup part we added two networks. One is a WiFi network that is password encrypted so we have to provide a password while the second network which is a smartphone hotspot is open, so we don’t need any password just typed the network SSID.
Next, using wifiMulti.run() command ESP32 will connect to the available strongest network. Once the WiFi is connected code will print the connected network SSID, IP address and RSSI.
After uploading code to ESP32 first it will scan for the available network then it will connect to the strongest available network. Here we can see RSSI value for each network a lower RSSI means stronger is the network.
We can see that ESP32 is connected to the phone network because it has an RSSI value of -62 and the other network whose SSID and password is defined is Team SAM whose value is -73. Here the mobile hotspot network has an RSSI value closer to zero means a stronger connection.
Now disconnect the Smartphone hotspot. ESP32 will connect itself to other stronger available networks. As in output we can see ESP32 is now connected to the next strongest available network. Network to which it is connected is Team SAM having RSSI value 0f -65.
ESP32 can connect to the strongest network available in its circle. We just need to define multiple networks SSID and password inside code. Using the WiFiMulti function ESP32 will scan for available networks and when connection is lost it will connect to other available networks inside ESP32 range.