Skip to content

Commit

Permalink
ezwifi: minor improvements and connect function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Nov 27, 2024
1 parent 35b1d98 commit a0df519
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions modules/py_frozen/ezwifi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import network
import uasyncio
import asyncio
from micropython import const


Expand Down Expand Up @@ -69,14 +69,14 @@ async def connect(self, timeout=60, retries=10):
self._log(f"Connecting to {self._ssid} (Attempt {retry + 1})")
try:
self._if.connect(self._ssid, self._password)
result = await uasyncio.wait_for(self._wait_for_connection(), timeout)
if result:
return
if await asyncio.wait_for(self._wait_for_connection(), timeout):
return True

except uasyncio.TimeoutError:
except asyncio.TimeoutError:
self._log("Attempt failed...", LogLevel.WARNING)

self._callback("failed")
return False

async def _wait_for_connection(self):
while not self._if.isconnected():
Expand All @@ -86,7 +86,7 @@ async def _wait_for_connection(self):
self._log(f"Connection failed with: {self._statuses[status]}", LogLevel.ERROR)
self._last_error = status
return False
await uasyncio.sleep_ms(1000)
await asyncio.sleep_ms(1000)
self._log(f"Connected! IP: {self.ipv4()}")
self._callback("connected")
return True
Expand All @@ -107,3 +107,7 @@ def _secrets(self):
return WIFI_SSID, WIFI_PASSWORD
except ImportError:
raise ImportError("secrets.py: missing or invalid!")


def connect(**kwargs):
return asyncio.get_event_loop().run_until_complete(EzWiFi(**kwargs).connect(retries=kwargs.get("retries", 10)))

0 comments on commit a0df519

Please sign in to comment.