shiney_light_things/boot.py

36 lines
1.1 KiB
Python
Raw Normal View History

2024-07-17 22:34:14 +02:00
# This file is executed on every boot (including wake-boot from deepsleep)
import uos, machine, time, network, gc, webrepl
# start webinterface and do a garbage collection (bc of reasons)
webrepl.start()
gc.collect()
# get wifi credentials
from wifi_config import NETWORKS
def connect_to_wifi(ssid, password, timeout = 15):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
print(f"connecting to network \"{ssid}\" ...")
wlan.connect(ssid, password)
start_time = time.time()
while not wlan.isconnected():
if time.time() - start_time > timeout:
print(f"failed to connect to {ssid} within {timeout} seconds")
return False
time.sleep(1)
print(f"connection to {ssid} established")
print("network config: ", wlan.ifconfig())
return True
# try to connect to networks
for wifi in NETWORKS.values():
ssid = wifi["ssid"]
password = wifi["password"]
if connect_to_wifi(ssid, password):
break
# no connection established
if not network.WLAN(network.STA_IF).isconnected():
print("failed to connect to any wifi network")