marshaller/net/wifi.py

52 lines
1.0 KiB
Python

import network
import rp2
import time
from . import logging
from .config import config
from .config import secrets
class WifiConnectionError(RuntimeError):
pass
def connect():
while True:
try:
return connect_to_wifi()
except WifiConnectionError as e:
logging.error(e.value)
time.sleep(180)
def connect_to_wifi():
rp2.country('US')
network.hostname(config['hostname'])
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')