Skip to content

Commit

Permalink
Badger 2040W: Handle WiFi STAT_CONNECT_FAIL.
Browse files Browse the repository at this point in the history
Expose some more NetworkManager config to the BadgerOS connect() method.
  • Loading branch information
Gadgetoid committed Mar 27, 2024
1 parent 1d145cc commit 31144f6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
5 changes: 4 additions & 1 deletion firmware/PIMORONI_BADGER2040W/lib/badger2040.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,13 @@ def connect(self, **args):
import gc

status_handler = args.get("status_handler", self.status_handler)
error_handler = args.get("error_handler", None)
client_timeout = args.get("timeout", 60)
retries = args.get("retries", 2)

if WIFI_CONFIG.COUNTRY == "":
raise RuntimeError("You must populate WIFI_CONFIG.py for networking.")

network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler)
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, client_timeout=client_timeout, status_handler=status_handler, error_handler=error_handler, retries=retries)
uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK))
gc.collect()
31 changes: 20 additions & 11 deletions firmware/PIMORONI_BADGER2040W/lib/network_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
class NetworkManager:
_ifname = ("Client", "Access Point")

def __init__(self, country="GB", client_timeout=60, access_point_timeout=5, status_handler=None, error_handler=None):
def __init__(self, country="GB", client_timeout=60, access_point_timeout=5, status_handler=None, error_handler=None, retries=2):
rp2.country(country)
self._ap_if = network.WLAN(network.AP_IF)
self._sta_if = network.WLAN(network.STA_IF)

self._retries = retries
self._mode = network.STA_IF
self._client_timeout = client_timeout
self._access_point_timeout = access_point_timeout
Expand Down Expand Up @@ -53,7 +54,12 @@ def disconnect(self):
async def wait(self, mode):
while not self.isconnected():
self._handle_status(mode, None)
if mode == network.STA_IF and self._sta_if.status() == network.STAT_CONNECT_FAIL:
self._handle_status(mode, False)
return False
await uasyncio.sleep_ms(1000)
self._handle_status(mode, True)
return True

def _handle_status(self, mode, status):
if callable(self._status_handler):
Expand All @@ -75,16 +81,20 @@ async def client(self, ssid, psk):

self._sta_if.active(True)
self._sta_if.config(pm=0xa11140)
self._sta_if.connect(ssid, psk)

try:
await uasyncio.wait_for(self.wait(network.STA_IF), self._client_timeout)
self._handle_status(network.STA_IF, True)
for _ in range(self._retries):
try:
self._sta_if.connect(ssid, psk)
result = await uasyncio.wait_for(self.wait(network.STA_IF), self._client_timeout)
if result:
return

except uasyncio.TimeoutError:
self._sta_if.active(False)
self._handle_status(network.STA_IF, False)
self._handle_error(network.STA_IF, "WIFI Client Failed")
except uasyncio.TimeoutError:
pass

self._sta_if.active(False)
self._handle_status(network.STA_IF, False)
self._handle_error(network.STA_IF, "WIFI Client Failed")

async def access_point(self):
if self._ap_if.isconnected():
Expand All @@ -99,8 +109,7 @@ async def access_point(self):
self._ap_if.active(True)

try:
await uasyncio.wait_for(self.wait(network.AP_IF), self._access_point_timeout)
self._handle_status(network.AP_IF, True)
_ = await uasyncio.wait_for(self.wait(network.AP_IF), self._access_point_timeout)

except uasyncio.TimeoutError:
self._sta_if.active(False)
Expand Down

0 comments on commit 31144f6

Please sign in to comment.