From 307b467ac1181dd5dd2e6532c84bc57725d5e2a7 Mon Sep 17 00:00:00 2001 From: Daniel Svitan Date: Sun, 1 Jun 2025 21:19:13 +0200 Subject: [PATCH] :construction: Adds checking and reconnecting when wlan is down --- peripheral/main.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/peripheral/main.py b/peripheral/main.py index c2383f2..fc65736 100644 --- a/peripheral/main.py +++ b/peripheral/main.py @@ -17,6 +17,7 @@ class App: opened: bool = False previously_opened: bool = False + wlan: network.WLAN led = Pin(15, Pin.OUT) trigger = Pin(2, Pin.OUT) @@ -55,18 +56,18 @@ class App: def connect(self): print("Connecting to Wi-Fi...") - wlan = network.WLAN(network.STA_IF) + self.wlan = network.WLAN(network.STA_IF) - wlan.active(False) + self.wlan.active(False) utime.sleep_ms(250) - wlan.active(True) + self.wlan.active(True) utime.sleep_ms(250) - wlan.connect(self.ssid, self.password) + self.wlan.connect(self.ssid, self.password) utime.sleep_ms(100) retry_count = 0 - while not wlan.isconnected(): + while not self.wlan.isconnected(): if retry_count >= MAX_CONNECTION_RETRIES: print("Max connection retries reached") exit(1) @@ -80,12 +81,12 @@ class App: if retry_count % 10 == 0: print("Attempting to restart connection...") - wlan.connect(self.ssid, self.password) + self.wlan.connect(self.ssid, self.password) for _ in range(10): self.led.toggle() utime.sleep_ms(50) - print(f"Connected with IP {wlan.ifconfig()[0]}") + print(f"Connected with IP {self.wlan.ifconfig()[0]}") self.update_server() def health_check_server(self): @@ -139,6 +140,9 @@ class App: i = 0 while True: try: + if not self.wlan.isconnected(): + self.connect() + distance = self.measure_distance() self.opened = distance >= THRESHOLD_DISTANCE