47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
import network
|
|
import webrepl
|
|
import uasyncio as asyncio
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
webrepl.start()
|
|
sta_if = network.WLAN(network.STA_IF)
|
|
sta_if.active(True)
|
|
sta_if.config(dhcp_hostname="stepper-wecker")
|
|
|
|
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:
|
|
pass
|
|
finally:
|
|
await asyncio.sleep(2)
|
|
else:
|
|
connectingFor = 0
|
|
await asyncio.sleep(2)
|
|
|
|
loop.create_task(connect()) |