Skip to content

Added a maximum number of attempts for connecting to WiFi #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion adafruit_portalbase/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,16 @@ 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

: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
while not self._wifi.is_connected:
# secrets dictionary must contain 'ssid' and 'password' at a minimum
print("Connecting to AP", self._secrets["ssid"])
Expand All @@ -340,8 +345,13 @@ 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 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
time.sleep(3)
gc.collect()

Expand Down