aquifer/net/wifi.py

50 lines
972 B
Python
Raw Permalink Normal View History

2023-02-07 08:49:20 -05:00
import network
import rp2
import time
from . import logging
from .config import secrets
class WifiConnectionError(RuntimeError):
pass
def connect():
while True:
try:
return connectToWifi()
except WifiConnectionError as e:
logging.error(e.value)
time.sleep(180)
def connectToWifi():
rp2.country('US')
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.config(pm=0xa11140)
wlan.connect(secrets['ssid'], secrets['password'])
wait_for_connection(wlan)
logging.info('connected')
logging.info(f'ip = {wlan.ifconfig()[0]}')
return wlan
def wait_for_connection(wlan):
maxWait = 10
while maxWait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
maxWait -= 1
logging.info('waiting for connection...')
time.sleep(1)
if wlan.status() != 3:
raise WifiConnectionError('network connection failed')