Try Multiple WIFI Connections
Posted: Tue Sep 08, 2020 8:33 pm
I would like to keep trying a list of WIFI connections until the ESP32 connects to one. Here is what I have but I keep getting the no AP found error. If I just use one of them it connects fine. I know there is a better way. Any help is appreciated.
Code: Select all
import esp32
from machine import Pin, ADC, reset
SSID1 = "firstAP"
PASSWORD1 = "letmein"
SSID2 = "secondAp"
PASSWORD2 = "letmein"
SSID3 = "thirdAP"
PASSWORD3 = "letmein"
SSID4 = "fourthAP"
PASSWORD4 = "letmein"
def do_connect():
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect(SSID1, PASSWORD1)
while not sta_if.isconnected():
pass
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect(SSID2, PASSWORD2)
while not sta_if.isconnected():
pass
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect(SSID3, PASSWORD3)
while not sta_if.isconnected():
pass
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect(SSID4, PASSWORD4)
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
do_connect()