Skip to content

ucloud: Add Async WiFi connection function. #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/arduino_iot_cloud/__init__.py
Original file line number Diff line number Diff line change
@@ -165,4 +165,44 @@ class Television(ArduinoCloudObject):
INPUT_XBOX = 60

def __init__(self, name, **kwargs):
super().__init__(name, keys={"swi", "vol", "mut", "pbc", "inp", "cha"}, **kwargs)
super().__init__(
name, keys={"swi", "vol", "mut", "pbc", "inp", "cha"}, **kwargs
)


def async_wifi_connection(client=None, connecting=[False]):
import time
import network
import logging

try:
from secrets import WIFI_SSID
from secrets import WIFI_PASS
except Exception:
raise (
Exception("Network is not configured. Set SSID and passwords in secrets.py")
)

wlan = network.WLAN(network.STA_IF)

if wlan.isconnected():
if connecting[0]:
connecting[0] = False
logging.info(f"WiFi connected {wlan.ifconfig()}")
if client is not None:
client.update_systime()
elif connecting[0]:
logging.info("WiFi is down. Trying to reconnect.")
else:
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASS)
connecting[0] = True
logging.info("WiFi is down. Trying to reconnect.")

# Running in sync mode, block until WiFi is connected.
if client is None:
while not wlan.isconnected():
logging.info("Trying to connect to WiFi.")
time.sleep(1.0)
connecting[0] = False
logging.info(f"WiFi Connected {wlan.ifconfig()}")
10 changes: 6 additions & 4 deletions src/arduino_iot_cloud/ucloud.py
Original file line number Diff line number Diff line change
@@ -180,6 +180,8 @@ def __init__(
self.last_ping = timestamp()
self.senmlpack = SenmlPack("", self.senml_generic_callback)
self.started = False
self.ntp_server = ntp_server
self.ntp_timeout = ntp_timeout

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

# Update RTC from NTP server on MicroPython.
self.update_systime(ntp_server, ntp_timeout)
self.update_systime()

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

def update_systime(self, server, timeout):
def update_systime(self, server=None, timeout=None):
try:
import ntptime
ntptime.host = server
ntptime.timeout = timeout
ntptime.host = self.ntp_server if server is None else server
ntptime.timeout = self.ntp_timeout if timeout is None else timeout
ntptime.settime()
logging.info("RTC time set from NTP.")
except ImportError: