diff --git a/enviro/__init__.py b/enviro/__init__.py index ae07ecf..415c1ed 100644 --- a/enviro/__init__.py +++ b/enviro/__init__.py @@ -154,89 +154,89 @@ def stop_activity_led(): print("") def reconnect_wifi(ssid, password, country): - import time - import network - import math - import rp2 - import ubinascii - - start_ms = time.ticks_ms() - - # Set country - rp2.country(country) - - # Reference: https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf - CYW43_LINK_DOWN = 0 - CYW43_LINK_JOIN = 1 - CYW43_LINK_NOIP = 2 - CYW43_LINK_UP = 3 - CYW43_LINK_FAIL = -1 - CYW43_LINK_NONET = -2 - CYW43_LINK_BADAUTH = -3 - - status_names = { - CYW43_LINK_DOWN: "Link is down", - CYW43_LINK_JOIN: "Connected to wifi", - CYW43_LINK_NOIP: "Connected to wifi, but no IP address", - CYW43_LINK_UP: "Connect to wifi with an IP address", - CYW43_LINK_FAIL: "Connection failed", - CYW43_LINK_NONET: "No matching SSID found (could be out of range, or down)", - CYW43_LINK_BADAUTH: "Authenticatation failure", - } + import time + import network + import math + import rp2 + import ubinascii + + start_ms = time.ticks_ms() + + # Set country + rp2.country(country) + + # Reference: https://datasheets.raspberrypi.com/picow/connecting-to-the-internet-with-pico-w.pdf + CYW43_LINK_DOWN = 0 + CYW43_LINK_JOIN = 1 + CYW43_LINK_NOIP = 2 + CYW43_LINK_UP = 3 + CYW43_LINK_FAIL = -1 + CYW43_LINK_NONET = -2 + CYW43_LINK_BADAUTH = -3 + + status_names = { + CYW43_LINK_DOWN: "Link is down", + CYW43_LINK_JOIN: "Connected to wifi", + CYW43_LINK_NOIP: "Connected to wifi, but no IP address", + CYW43_LINK_UP: "Connect to wifi with an IP address", + CYW43_LINK_FAIL: "Connection failed", + CYW43_LINK_NONET: "No matching SSID found (could be out of range, or down)", + CYW43_LINK_BADAUTH: "Authenticatation failure", + } - wlan = network.WLAN(network.STA_IF) + wlan = network.WLAN(network.STA_IF) - def dump_status(): - status = wlan.status() - logging.info(f"> active: {1 if wlan.active() else 0}, status: {status} ({status_names[status]})") - return status - - # Return True on expected status, exception on error status (negative) and False on timeout - def wait_status(expected_status, *, timeout=10, tick_sleep=0.5): - for i in range(math.ceil(timeout / tick_sleep)): - time.sleep(tick_sleep) - status = dump_status() - if status == expected_status: - return True - if status < 0: - raise Exception(status_names[status]) - return False + def dump_status(): + status = wlan.status() + logging.info(f"> active: {1 if wlan.active() else 0}, status: {status} ({status_names[status]})") + return status - wlan.active(True) - # Disable power saving mode if on USB power - if vbus_present: - wlan.config(pm=0xa11140) + # Return True on expected status, exception on error status (negative) and False on timeout + def wait_status(expected_status, *, timeout=10, tick_sleep=0.5): + for i in range(math.ceil(timeout / tick_sleep)): + time.sleep(tick_sleep) + status = dump_status() + if status == expected_status: + return True + if status < 0: + raise Exception(status_names[status]) + return False - # Print MAC - mac = ubinascii.hexlify(wlan.config('mac'),':').decode() - logging.info("> MAC: " + mac) - - # Disconnect when necessary - status = dump_status() - if status >= CYW43_LINK_JOIN and status <= CYW43_LINK_UP: - logging.info("> Disconnecting...") - wlan.disconnect() - try: - wait_status(CYW43_LINK_DOWN) - except Exception as x: - raise Exception(f"Failed to disconnect: {x}") - logging.info("> Ready for connection!") - - # Connect to our AP - logging.info(f"> Connecting to SSID {ssid} (password: {password})...") - wlan.connect(ssid, password) + wlan.active(True) + # Disable power saving mode if on USB power + if vbus_present: + wlan.config(pm=0xa11140) + + # Print MAC + mac = ubinascii.hexlify(wlan.config('mac'),':').decode() + logging.info("> MAC: " + mac) + + # Disconnect when necessary + status = dump_status() + if status >= CYW43_LINK_JOIN and status <= CYW43_LINK_UP: + logging.info("> Disconnecting...") + wlan.disconnect() try: - wait_status(CYW43_LINK_UP) + wait_status(CYW43_LINK_DOWN) except Exception as x: - raise Exception(f"Failed to connect to SSID {ssid} (password: {password}): {x}") - logging.info("> Connected successfully!") + raise Exception(f"Failed to disconnect: {x}") + logging.info("> Ready for connection!") - ip, subnet, gateway, dns = wlan.ifconfig() - logging.info(f"> IP: {ip}, Subnet: {subnet}, Gateway: {gateway}, DNS: {dns}") - - elapsed_ms = time.ticks_ms() - start_ms - logging.info(f"> Elapsed: {elapsed_ms}ms") - return elapsed_ms + # Connect to our AP + logging.info(f"> Connecting to SSID {ssid} (password: {password})...") + wlan.connect(ssid, password) + try: + wait_status(CYW43_LINK_UP) + except Exception as x: + raise Exception(f"Failed to connect to SSID {ssid} (password: {password}): {x}") + logging.info("> Connected successfully!") + + ip, subnet, gateway, dns = wlan.ifconfig() + logging.info(f"> IP: {ip}, Subnet: {subnet}, Gateway: {gateway}, DNS: {dns}") + + elapsed_ms = time.ticks_ms() - start_ms + logging.info(f"> Elapsed: {elapsed_ms}ms") + return elapsed_ms def connect_to_wifi(): try: