🚧 Adds checking and reconnecting when wlan is down
All checks were successful
Gitea Build Action / build (push) Successful in 24s

This commit is contained in:
Daniel Svitan 2025-06-01 21:19:13 +02:00
parent 995bf90efb
commit 307b467ac1

View File

@ -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