throttle connects a simpler way than claude suggested. and also sending discord message if connected and reconnects.
This commit is contained in:
parent
328302d8ee
commit
9d12db931b
23
main.py
23
main.py
@ -3,6 +3,8 @@ import time
|
|||||||
import network
|
import network
|
||||||
import urequests as requests
|
import urequests as requests
|
||||||
from secrets import secrets
|
from secrets import secrets
|
||||||
|
from scripts.discord_webhook import send_discord_message
|
||||||
|
|
||||||
|
|
||||||
# Initialize pins (LED light onboard)
|
# Initialize pins (LED light onboard)
|
||||||
led = Pin("LED", Pin.OUT)
|
led = Pin("LED", Pin.OUT)
|
||||||
@ -39,6 +41,16 @@ def connect_wifi():
|
|||||||
# Connect to WiFi
|
# Connect to WiFi
|
||||||
wifi = connect_wifi()
|
wifi = connect_wifi()
|
||||||
|
|
||||||
|
# Send startup message if connected
|
||||||
|
if wifi and wifi.isconnected():
|
||||||
|
send_discord_message("Pico W online and connected ✅")
|
||||||
|
|
||||||
|
|
||||||
|
# Throttle reconnect attempts
|
||||||
|
RECONNECT_COOLDOWN_MS = 60000 # 60 seconds
|
||||||
|
last_attempt_ms = time.ticks_ms()
|
||||||
|
|
||||||
|
|
||||||
# Connection monitoring loop
|
# Connection monitoring loop
|
||||||
while True:
|
while True:
|
||||||
if not wifi or not wifi.isconnected():
|
if not wifi or not wifi.isconnected():
|
||||||
@ -47,8 +59,15 @@ while True:
|
|||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
led.off()
|
led.off()
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
# Try to reconnect
|
|
||||||
wifi = connect_wifi()
|
# Only try to reconnect after cooldown
|
||||||
|
if time.ticks_diff(time.ticks_ms(), last_attempt_ms) >= RECONNECT_COOLDOWN_MS:
|
||||||
|
last_attempt_ms = time.ticks_ms()
|
||||||
|
wifi = connect_wifi()
|
||||||
|
# Notify only when connection is restored
|
||||||
|
if wifi and wifi.isconnected():
|
||||||
|
send_discord_message("WiFi connection restored 🔄")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Slow blink when connected
|
# Slow blink when connected
|
||||||
led.on()
|
led.on()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user