Last active
June 17, 2023 22:40
-
-
Save lonelybinary/4cdb958196db5d49c164ae14cdc3a7b2 to your computer and use it in GitHub Desktop.
Arduino ESP32 WIFI - Multiple Wireless Networks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment