Improve connect_wifi function for better error handling and connection logic
This commit is contained in:
@@ -2,36 +2,65 @@ import network
|
|||||||
import time
|
import time
|
||||||
from secrets import secrets
|
from secrets import secrets
|
||||||
|
|
||||||
RECONNECT_COOLDOWN_MS = 60000 # 60 seconds
|
def connect_wifi(led=None):
|
||||||
|
"""Connect to WiFi using credentials from secrets.py"""
|
||||||
def connect_wifi(led=None, timeout=10):
|
try:
|
||||||
"""
|
wlan = network.WLAN(network.STA_IF)
|
||||||
Connect to WiFi using secrets['ssid'] / secrets['password'].
|
|
||||||
If `led` (machine.Pin) is provided, pulse it once on successful connect.
|
|
||||||
Returns the WLAN object or None on failure.
|
|
||||||
"""
|
|
||||||
wifi = network.WLAN(network.STA_IF)
|
|
||||||
wifi.active(True)
|
|
||||||
|
|
||||||
# print("Connecting to WiFi...", end="")
|
|
||||||
wifi.connect(secrets['ssid'], secrets['password'])
|
|
||||||
|
|
||||||
# Wait for connection with timeout
|
|
||||||
max_wait = timeout
|
|
||||||
while max_wait > 0:
|
|
||||||
if wifi.status() < 0 or wifi.status() >= 3:
|
|
||||||
break
|
|
||||||
max_wait -= 1
|
|
||||||
# print(".", end="")
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
if wifi.isconnected():
|
# Deactivate first if already active (fixes EPERM error)
|
||||||
# print("\nConnected! Network config:", wifi.ifconfig())
|
if wlan.active():
|
||||||
if led:
|
wlan.active(False)
|
||||||
led.on()
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
led.off()
|
|
||||||
return wifi
|
wlan.active(True)
|
||||||
else:
|
time.sleep(1) # Give it time to initialize
|
||||||
# print("\nConnection failed!")
|
|
||||||
return None
|
except OSError as e:
|
||||||
|
print(f"WiFi activation error: {e}")
|
||||||
|
print("Attempting reset...")
|
||||||
|
try:
|
||||||
|
# Force deinit and reinit
|
||||||
|
wlan.deinit()
|
||||||
|
time.sleep(2)
|
||||||
|
wlan = network.WLAN(network.STA_IF)
|
||||||
|
wlan.active(True)
|
||||||
|
time.sleep(1)
|
||||||
|
except Exception as e2:
|
||||||
|
print(f"WiFi reset failed: {e2}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
if not wlan.isconnected():
|
||||||
|
print('Connecting to WiFi...')
|
||||||
|
try:
|
||||||
|
wlan.connect(secrets['ssid'], secrets['password'])
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Connection attempt failed: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Wait for connection with timeout
|
||||||
|
max_wait = 20
|
||||||
|
while max_wait > 0:
|
||||||
|
if wlan.isconnected():
|
||||||
|
break
|
||||||
|
if led:
|
||||||
|
led.toggle()
|
||||||
|
time.sleep(0.5)
|
||||||
|
max_wait -= 1
|
||||||
|
print('.', end='')
|
||||||
|
|
||||||
|
print()
|
||||||
|
|
||||||
|
if not wlan.isconnected():
|
||||||
|
print('WiFi connection failed!')
|
||||||
|
if led:
|
||||||
|
led.off()
|
||||||
|
return None
|
||||||
|
|
||||||
|
if led:
|
||||||
|
# Single pulse on successful connection
|
||||||
|
led.on()
|
||||||
|
time.sleep(0.5)
|
||||||
|
led.off()
|
||||||
|
|
||||||
|
print('Connected to WiFi')
|
||||||
|
return wlan
|
||||||
Reference in New Issue
Block a user