Skip to content

Commit fe58cff

Browse files
authored
Merge pull request #75 from arduino/add_async_wifi
ucloud: Add Async WiFi connection function.
2 parents c12ffd6 + b85d111 commit fe58cff

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/arduino_iot_cloud/__init__.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,44 @@ class Television(ArduinoCloudObject):
165165
INPUT_XBOX = 60
166166

167167
def __init__(self, name, **kwargs):
168-
super().__init__(name, keys={"swi", "vol", "mut", "pbc", "inp", "cha"}, **kwargs)
168+
super().__init__(
169+
name, keys={"swi", "vol", "mut", "pbc", "inp", "cha"}, **kwargs
170+
)
171+
172+
173+
def async_wifi_connection(client=None, connecting=[False]):
174+
import time
175+
import network
176+
import logging
177+
178+
try:
179+
from secrets import WIFI_SSID
180+
from secrets import WIFI_PASS
181+
except Exception:
182+
raise (
183+
Exception("Network is not configured. Set SSID and passwords in secrets.py")
184+
)
185+
186+
wlan = network.WLAN(network.STA_IF)
187+
188+
if wlan.isconnected():
189+
if connecting[0]:
190+
connecting[0] = False
191+
logging.info(f"WiFi connected {wlan.ifconfig()}")
192+
if client is not None:
193+
client.update_systime()
194+
elif connecting[0]:
195+
logging.info("WiFi is down. Trying to reconnect.")
196+
else:
197+
wlan.active(True)
198+
wlan.connect(WIFI_SSID, WIFI_PASS)
199+
connecting[0] = True
200+
logging.info("WiFi is down. Trying to reconnect.")
201+
202+
# Running in sync mode, block until WiFi is connected.
203+
if client is None:
204+
while not wlan.isconnected():
205+
logging.info("Trying to connect to WiFi.")
206+
time.sleep(1.0)
207+
connecting[0] = False
208+
logging.info(f"WiFi Connected {wlan.ifconfig()}")

src/arduino_iot_cloud/ucloud.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ def __init__(
180180
self.last_ping = timestamp()
181181
self.senmlpack = SenmlPack("", self.senml_generic_callback)
182182
self.started = False
183+
self.ntp_server = ntp_server
184+
self.ntp_timeout = ntp_timeout
183185

184186
if "pin" in ssl_params:
185187
try:
@@ -200,7 +202,7 @@ def __init__(
200202
self.device_topic = b"/a/d/" + device_id + b"/e/i"
201203

202204
# Update RTC from NTP server on MicroPython.
203-
self.update_systime(ntp_server, ntp_timeout)
205+
self.update_systime()
204206

205207
# If no server/port were passed in args, set the default server/port
206208
# based on authentication type.
@@ -234,11 +236,11 @@ def get(self, key, default=None):
234236
return self[key]
235237
return default
236238

237-
def update_systime(self, server, timeout):
239+
def update_systime(self, server=None, timeout=None):
238240
try:
239241
import ntptime
240-
ntptime.host = server
241-
ntptime.timeout = timeout
242+
ntptime.host = self.ntp_server if server is None else server
243+
ntptime.timeout = self.ntp_timeout if timeout is None else timeout
242244
ntptime.settime()
243245
logging.info("RTC time set from NTP.")
244246
except ImportError:

0 commit comments

Comments
 (0)