56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
import network
|
|
import webrepl
|
|
import uasyncio as asyncio
|
|
import time
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
webrepl.start()
|
|
|
|
sta_if = network.WLAN(network.STA_IF)
|
|
sta_if.active(False)
|
|
time.sleep(0.1)
|
|
sta_if.active(True)
|
|
time.sleep(0.1)
|
|
sta_if.config(dhcp_hostname="wecker")
|
|
|
|
#print("sta_if.config")
|
|
|
|
|
|
async def connect():
|
|
connectingFor = 0
|
|
while True:
|
|
if sta_if.status() != network.STAT_GOT_IP:
|
|
if connectingFor > 5:
|
|
sta_if.disconnect()
|
|
connectingFor = 0
|
|
else:
|
|
connectingFor += 1
|
|
try:
|
|
availableNetworkSSIDs = [s[0].decode() for s in sta_if.scan()]
|
|
if len(availableNetworkSSIDs) > 0:
|
|
print("availableNetworkSSIDs:\n", availableNetworkSSIDs)
|
|
# load wifi credentials from file
|
|
with open("wifi-credentials", "r") as f:
|
|
while sta_if.status() != network.STAT_GOT_IP:
|
|
ssid = f.readline()
|
|
password = f.readline()
|
|
if not ssid or not password:
|
|
break
|
|
ssid = ssid.replace("\n", "")
|
|
password = password.replace("\n", "")
|
|
if ssid in availableNetworkSSIDs:
|
|
print("Connecting to", ssid)
|
|
sta_if.connect(ssid, password)
|
|
break
|
|
except Exception as e:
|
|
print(e)
|
|
pass
|
|
finally:
|
|
await asyncio.sleep(60*2)
|
|
else:
|
|
connectingFor = 0
|
|
await asyncio.sleep(60*15)
|
|
|
|
loop.create_task(connect())
|