Skip to content

Commit ad4d704

Browse files
committed
Try multiple wifi networks
1 parent 17a8f45 commit ad4d704

File tree

1 file changed

+47
-32
lines changed

1 file changed

+47
-32
lines changed

adafruit_portalbase/network.py

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,13 @@ def __init__(
9898
]
9999

100100
if secrets_data is not None:
101-
self._secrets = secrets_data
101+
self._secrets_entries = secrets_data
102102
else:
103-
self._secrets = secrets
103+
self._secrets_entries = secrets
104+
105+
if not isinstance(self._secrets_entries, (list, tuple)):
106+
self._secrets_entries = [self._secrets_entries]
107+
self._secrets = self._secrets_entries[0]
104108

105109
self.requests = None
106110

@@ -325,36 +329,47 @@ def connect(self, max_attempts=10):
325329
failing or use None to disable. Defaults to 10.
326330
327331
"""
328-
self._wifi.neo_status(STATUS_CONNECTING)
329-
attempt = 1
330-
while not self._wifi.is_connected:
331-
# secrets dictionary must contain 'ssid' and 'password' at a minimum
332-
print("Connecting to AP", self._secrets["ssid"])
333-
if (
334-
self._secrets["ssid"] == "CHANGE ME"
335-
or self._secrets["password"] == "CHANGE ME"
336-
):
337-
change_me = "\n" + "*" * 45
338-
change_me += "\nPlease update the 'secrets.py' file on your\n"
339-
change_me += "CIRCUITPY drive to include your local WiFi\n"
340-
change_me += "access point SSID name in 'ssid' and SSID\n"
341-
change_me += "password in 'password'. Then save to reload!\n"
342-
change_me += "*" * 45
343-
raise OSError(change_me)
344-
self._wifi.neo_status(STATUS_NO_CONNECTION) # red = not connected
345-
try:
346-
self._wifi.connect(self._secrets["ssid"], self._secrets["password"])
347-
self.requests = self._wifi.requests
348-
except ConnectionError as error:
349-
if max_attempts is not None and attempt >= max_attempts:
350-
raise OSError(
351-
"Maximum number of attempts reached when trying to connect to WiFi"
352-
) from error
353-
print("Could not connect to internet", error)
354-
print("Retrying in 3 seconds...")
355-
attempt += 1
356-
time.sleep(3)
357-
gc.collect()
332+
for secret_entry in self._secrets_entries:
333+
334+
self._secrets = secret_entry
335+
336+
self._wifi.neo_status(STATUS_CONNECTING)
337+
attempt = 1
338+
339+
while not self._wifi.is_connected:
340+
# secrets dictionary must contain 'ssid' and 'password' at a minimum
341+
print("Connecting to AP", self._secrets["ssid"])
342+
if (
343+
self._secrets["ssid"] == "CHANGE ME"
344+
or self._secrets["password"] == "CHANGE ME"
345+
):
346+
change_me = "\n" + "*" * 45
347+
change_me += "\nPlease update the 'secrets.py' file on your\n"
348+
change_me += "CIRCUITPY drive to include your local WiFi\n"
349+
change_me += "access point SSID name in 'ssid' and SSID\n"
350+
change_me += "password in 'password'. Then save to reload!\n"
351+
change_me += "*" * 45
352+
raise OSError(change_me)
353+
self._wifi.neo_status(STATUS_NO_CONNECTION) # red = not connected
354+
try:
355+
self._wifi.connect(self._secrets["ssid"], self._secrets["password"])
356+
self.requests = self._wifi.requests
357+
break
358+
except ConnectionError as error:
359+
if max_attempts is not None and attempt >= max_attempts:
360+
break
361+
print("Could not connect to internet", error)
362+
print("Retrying in 3 seconds...")
363+
attempt += 1
364+
time.sleep(3)
365+
gc.collect()
366+
367+
if self._wifi.is_connected:
368+
return
369+
370+
raise OSError(
371+
"Maximum number of attempts reached when trying to connect to WiFi"
372+
)
358373

359374
def _get_io_client(self):
360375
if self._io_client is not None:

0 commit comments

Comments
 (0)