Skip to content

Commit

Permalink
Added wifi connect retry loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonprickett committed Aug 12, 2023
1 parent 40e4b9f commit 2f5ece8
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
AP_TEMPLATE_PATH = "ap_templates"
APP_TEMPLATE_PATH = "app_templates"
WIFI_FILE = "wifi.json"
WIFI_MAX_ATTEMPTS = 3

def machine_reset():
utime.sleep(1)
Expand Down Expand Up @@ -89,20 +90,29 @@ def app_catch_all(request):

# File was found, attempt to connect to wifi...
with open(WIFI_FILE) as f:
wifi_current_attempt = 1
wifi_credentials = json.load(f)
ip_address = connect_to_wifi(wifi_credentials["ssid"], wifi_credentials["password"])

if not is_connected_to_wifi():

while (wifi_current_attempt < WIFI_MAX_ATTEMPTS):
ip_address = connect_to_wifi(wifi_credentials["ssid"], wifi_credentials["password"])

if is_connected_to_wifi():
print(f"Connected to wifi, IP address {ip_address}")
break
else:
wifi_current_attempt += 1

if is_connected_to_wifi():
application_mode()
else:

# Bad configuration, delete the credentials file, reboot
# into setup mode to get new credentials from the user.
print("Bad wifi connection!")
print(wifi_credentials)
os.remove(WIFI_FILE)
machine_reset()

print(f"Connected to wifi, IP address {ip_address}")
application_mode()

except Exception:
# Either no wifi configuration file found, or something went wrong,
# so go into setup mode.
Expand Down

0 comments on commit 2f5ece8

Please sign in to comment.