marshaller/net/wifi.py

52 lines
1.0 KiB
Python
Raw Permalink Normal View History

2023-10-23 15:43:36 -04:00
import network
import rp2
import time
from . import logging
2023-10-27 17:39:58 -04:00
from .config import config
2023-10-23 15:43:36 -04:00
from .config import secrets
class WifiConnectionError(RuntimeError):
pass
def connect():
while True:
try:
return connect_to_wifi()
2023-10-23 15:43:36 -04:00
except WifiConnectionError as e:
logging.error(e.value)
time.sleep(180)
def connect_to_wifi():
2023-10-23 15:43:36 -04:00
rp2.country('US')
2023-10-27 17:39:58 -04:00
network.hostname(config['hostname'])
2023-10-23 15:43:36 -04:00
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')