diff --git a/arduino_iot_cloud/ucloud.py b/arduino_iot_cloud/ucloud.py index 0b22a43..8081941 100644 --- a/arduino_iot_cloud/ucloud.py +++ b/arduino_iot_cloud/ucloud.py @@ -42,6 +42,12 @@ class InvalidStateError(Exception): pass +# Server/port for basic auth. +_DEFAULT_UP_SERVER = ("mqtts-up.iot.oniudra.cc", 8884) + +# Default server/port for key/cert auth. +_DEFAULT_SA_SERVER = ("mqtts-sa.iot.oniudra.cc", 8883) + def timestamp(): return int(time.time()) @@ -171,8 +177,8 @@ def __init__( username=None, password=None, ssl_params={}, - server="mqtts-sa.iot.oniudra.cc", - port=8883, + server=None, + port=None, keepalive=10 ): self.tasks = {} @@ -183,15 +189,28 @@ def __init__( self.last_ping = timestamp() self.device_topic = b"/a/d/" + device_id + b"/e/i" self.senmlpack = SenmlPack("", self.senml_generic_callback) + + # MicroPython does not support secure elements yet, and key/cert + # must be loaded from DER files and passed as binary blobs. if "keyfile" in ssl_params and "der" in ssl_params["keyfile"]: - # MicroPython does not support secure elements yet, and key/cert - # must be loaded from DER files and passed as binary blobs. with open(ssl_params.pop("keyfile"), "rb") as f: ssl_params["key"] = f.read() with open(ssl_params.pop("certfile"), "rb") as f: ssl_params["cert"] = f.read() - self.mqtt = MQTTClient(device_id, server, port, ssl_params, username, password, keepalive, self.mqtt_callback) - # Note: the following internal objects are initialized by the cloud. + + # If no server/port were passed in args, set the default server/port + # based on authentication type. + if server is None: + server = _DEFAULT_SA_SERVER[0] if password is None else _DEFAULT_UP_SERVER[0] + if port is None: + port = _DEFAULT_SA_SERVER[1] if password is None else _DEFAULT_UP_SERVER[1] + + # Create MQTT client. + self.mqtt = MQTTClient( + device_id, server, port, ssl_params, username, password, keepalive, self.mqtt_callback + ) + + # Add internal objects initialized by the cloud. for name in ["thing_id", "tz_offset", "tz_dst_until"]: self.register(name, value=None) diff --git a/examples/micropython.py b/examples/micropython.py index 4a0dbb6..9ac4592 100644 --- a/examples/micropython.py +++ b/examples/micropython.py @@ -36,10 +36,7 @@ def on_clight_changed(client, clight): async def main(): # Create a client to connect to the Arduino IoT cloud. For MicroPython, the key and cert files must be stored # in DER format on the filesystem. Alternatively, a username and a password can be used for authentication: - # client = AIOTClient( - # device_id=b"DEVICE_ID", - # username=b"DEVICE_ID", password=b"SECRET_KEY", server="mqtts-up.iot.oniudra.cc", port=8884 - # ) + # client = AIOTClient(device_id=b"DEVICE_ID", username=b"DEVICE_ID", password=b"SECRET_KEY") client = AIOTClient(device_id=DEVICE_ID, ssl_params={"keyfile": KEY_PATH, "certfile": CERT_PATH}) # Register cloud objects. Note these objects must be created first in the dashboard.