Skip to content

Commit 8780e56

Browse files
committed
Added a maximum number of attempts for connecting to WiFi
1 parent a62c68d commit 8780e56

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

adafruit_portalbase/network.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,12 @@ def wget(self, url, filename, *, chunk_size=12000):
316316
if not content_length == os.stat(filename)[6]:
317317
raise RuntimeError
318318

319-
def connect(self):
319+
def connect(self, max_attempts=10):
320320
"""
321321
Connect to WiFi using the settings found in secrets.py
322322
"""
323323
self._wifi.neo_status(STATUS_CONNECTING)
324+
attempt = 1
324325
while not self._wifi.is_connected:
325326
# secrets dictionary must contain 'ssid' and 'password' at a minimum
326327
print("Connecting to AP", self._secrets["ssid"])
@@ -340,8 +341,11 @@ def connect(self):
340341
self._wifi.connect(self._secrets["ssid"], self._secrets["password"])
341342
self.requests = self._wifi.requests
342343
except RuntimeError as error:
344+
if max_attempts is not None and attempt >= max_attempts:
345+
raise OSError("Maximum attempts reached when trying to connect to WiFi")
343346
print("Could not connect to internet", error)
344347
print("Retrying in 3 seconds...")
348+
attempt += 1
345349
time.sleep(3)
346350
gc.collect()
347351

0 commit comments

Comments
 (0)