From 8780e56a352664caeb55bf326e495fcc744e4314 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 14 Jul 2021 14:29:45 -0700 Subject: [PATCH 1/3] Added a maximum number of attempts for connecting to WiFi --- adafruit_portalbase/network.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/adafruit_portalbase/network.py b/adafruit_portalbase/network.py index 9c4f7ec..64a8f03 100644 --- a/adafruit_portalbase/network.py +++ b/adafruit_portalbase/network.py @@ -316,11 +316,12 @@ def wget(self, url, filename, *, chunk_size=12000): if not content_length == os.stat(filename)[6]: raise RuntimeError - def connect(self): + def connect(self, max_attempts=10): """ Connect to WiFi using the settings found in secrets.py """ self._wifi.neo_status(STATUS_CONNECTING) + attempt = 1 while not self._wifi.is_connected: # secrets dictionary must contain 'ssid' and 'password' at a minimum print("Connecting to AP", self._secrets["ssid"]) @@ -340,8 +341,11 @@ def connect(self): self._wifi.connect(self._secrets["ssid"], self._secrets["password"]) self.requests = self._wifi.requests except RuntimeError as error: + if max_attempts is not None and attempt >= max_attempts: + raise OSError("Maximum attempts reached when trying to connect to WiFi") print("Could not connect to internet", error) print("Retrying in 3 seconds...") + attempt += 1 time.sleep(3) gc.collect() From 0396e0a357303eea8adafd60d1ca1f721829df90 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 14 Jul 2021 14:34:10 -0700 Subject: [PATCH 2/3] Added docstring for new param --- adafruit_portalbase/network.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/adafruit_portalbase/network.py b/adafruit_portalbase/network.py index 64a8f03..2c0577b 100644 --- a/adafruit_portalbase/network.py +++ b/adafruit_portalbase/network.py @@ -319,6 +319,10 @@ def wget(self, url, filename, *, chunk_size=12000): def connect(self, max_attempts=10): """ Connect to WiFi using the settings found in secrets.py + + :param max_attempts: The maximum number of of attempts to connect to WiFi before + failing or use None to disable. Defaults to 10. + """ self._wifi.neo_status(STATUS_CONNECTING) attempt = 1 @@ -342,7 +346,9 @@ def connect(self, max_attempts=10): self.requests = self._wifi.requests except RuntimeError as error: if max_attempts is not None and attempt >= max_attempts: - raise OSError("Maximum attempts reached when trying to connect to WiFi") + raise OSError( + "Maximum number of attempts reached when trying to connect to WiFi" + ) print("Could not connect to internet", error) print("Retrying in 3 seconds...") attempt += 1 From 0550f86b0f2b99b6106107b2f40521322fd6bb21 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 14 Jul 2021 14:39:44 -0700 Subject: [PATCH 3/3] Linted --- adafruit_portalbase/network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_portalbase/network.py b/adafruit_portalbase/network.py index 2c0577b..091c70f 100644 --- a/adafruit_portalbase/network.py +++ b/adafruit_portalbase/network.py @@ -348,7 +348,7 @@ def connect(self, max_attempts=10): if max_attempts is not None and attempt >= max_attempts: raise OSError( "Maximum number of attempts reached when trying to connect to WiFi" - ) + ) from error print("Could not connect to internet", error) print("Retrying in 3 seconds...") attempt += 1