Skip to content

Commit bc69bfa

Browse files
authored
Merge pull request #65 from tekktrik/dev/multiple-wifi
Try multiple wifi networks
2 parents 2bc1b86 + 7166e56 commit bc69bfa

File tree

1 file changed

+54
-30
lines changed

1 file changed

+54
-30
lines changed

adafruit_portalbase/network.py

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ def __init__(
102102
else:
103103
self._secrets = secrets
104104

105+
if "networks" in self._secrets:
106+
if isinstance(self._secrets["networks"], (list, tuple)):
107+
self._secrets_network = self._secrets["networks"]
108+
else:
109+
raise TypeError(
110+
"'networks' must be a list/tuple of dicts of 'ssid' and 'password'"
111+
)
112+
else:
113+
self._secrets_network = [
114+
{
115+
"ssid": self._secrets["ssid"],
116+
"password": self._secrets["password"],
117+
}
118+
]
119+
105120
self.requests = None
106121

107122
try:
@@ -325,36 +340,45 @@ def connect(self, max_attempts=10):
325340
failing or use None to disable. Defaults to 10.
326341
327342
"""
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()
343+
for secret_entry in self._secrets_network:
344+
345+
self._wifi.neo_status(STATUS_CONNECTING)
346+
attempt = 1
347+
348+
while not self._wifi.is_connected:
349+
# secrets dictionary must contain 'ssid' and 'password' at a minimum
350+
print("Connecting to AP", secret_entry["ssid"])
351+
if (
352+
secret_entry["ssid"] == "CHANGE ME"
353+
or secret_entry["password"] == "CHANGE ME"
354+
):
355+
change_me = "\n" + "*" * 45
356+
change_me += "\nPlease update the 'secrets.py' file on your\n"
357+
change_me += "CIRCUITPY drive to include your local WiFi\n"
358+
change_me += "access point SSID name in 'ssid' and SSID\n"
359+
change_me += "password in 'password'. Then save to reload!\n"
360+
change_me += "*" * 45
361+
raise OSError(change_me)
362+
self._wifi.neo_status(STATUS_NO_CONNECTION) # red = not connected
363+
try:
364+
self._wifi.connect(secret_entry["ssid"], secret_entry["password"])
365+
self.requests = self._wifi.requests
366+
break
367+
except (RuntimeError, ConnectionError) as error:
368+
if max_attempts is not None and attempt >= max_attempts:
369+
break
370+
print("Could not connect to internet", error)
371+
print("Retrying in 3 seconds...")
372+
attempt += 1
373+
time.sleep(3)
374+
gc.collect()
375+
376+
if self._wifi.is_connected:
377+
return
378+
379+
raise OSError(
380+
"Maximum number of attempts reached when trying to connect to WiFi"
381+
)
358382

359383
def _get_io_client(self):
360384
if self._io_client is not None:

0 commit comments

Comments
 (0)