39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
|
# 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():
|
||
|
print("start: ", start_time)
|
||
|
print("time:", time.time())
|
||
|
print("condition: ", start_time - time.time() > timeout)
|
||
|
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")
|