Skip to content

Commit 743dace

Browse files
authored
Merge pull request #17 from bcmi-labs/default_servers
ucloud: Set default server/port based on auth type.
2 parents 4c817cc + 1c2a942 commit 743dace

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

arduino_iot_cloud/ucloud.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
class InvalidStateError(Exception):
4343
pass
4444

45+
# Server/port for basic auth.
46+
_DEFAULT_UP_SERVER = ("mqtts-up.iot.oniudra.cc", 8884)
47+
48+
# Default server/port for key/cert auth.
49+
_DEFAULT_SA_SERVER = ("mqtts-sa.iot.oniudra.cc", 8883)
50+
4551

4652
def timestamp():
4753
return int(time.time())
@@ -171,8 +177,8 @@ def __init__(
171177
username=None,
172178
password=None,
173179
ssl_params={},
174-
server="mqtts-sa.iot.oniudra.cc",
175-
port=8883,
180+
server=None,
181+
port=None,
176182
keepalive=10
177183
):
178184
self.tasks = {}
@@ -183,15 +189,28 @@ def __init__(
183189
self.last_ping = timestamp()
184190
self.device_topic = b"/a/d/" + device_id + b"/e/i"
185191
self.senmlpack = SenmlPack("", self.senml_generic_callback)
192+
193+
# MicroPython does not support secure elements yet, and key/cert
194+
# must be loaded from DER files and passed as binary blobs.
186195
if "keyfile" in ssl_params and "der" in ssl_params["keyfile"]:
187-
# MicroPython does not support secure elements yet, and key/cert
188-
# must be loaded from DER files and passed as binary blobs.
189196
with open(ssl_params.pop("keyfile"), "rb") as f:
190197
ssl_params["key"] = f.read()
191198
with open(ssl_params.pop("certfile"), "rb") as f:
192199
ssl_params["cert"] = f.read()
193-
self.mqtt = MQTTClient(device_id, server, port, ssl_params, username, password, keepalive, self.mqtt_callback)
194-
# Note: the following internal objects are initialized by the cloud.
200+
201+
# If no server/port were passed in args, set the default server/port
202+
# based on authentication type.
203+
if server is None:
204+
server = _DEFAULT_SA_SERVER[0] if password is None else _DEFAULT_UP_SERVER[0]
205+
if port is None:
206+
port = _DEFAULT_SA_SERVER[1] if password is None else _DEFAULT_UP_SERVER[1]
207+
208+
# Create MQTT client.
209+
self.mqtt = MQTTClient(
210+
device_id, server, port, ssl_params, username, password, keepalive, self.mqtt_callback
211+
)
212+
213+
# Add internal objects initialized by the cloud.
195214
for name in ["thing_id", "tz_offset", "tz_dst_until"]:
196215
self.register(name, value=None)
197216

examples/micropython.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ def on_clight_changed(client, clight):
3636
async def main():
3737
# Create a client to connect to the Arduino IoT cloud. For MicroPython, the key and cert files must be stored
3838
# in DER format on the filesystem. Alternatively, a username and a password can be used for authentication:
39-
# client = AIOTClient(
40-
# device_id=b"DEVICE_ID",
41-
# username=b"DEVICE_ID", password=b"SECRET_KEY", server="mqtts-up.iot.oniudra.cc", port=8884
42-
# )
39+
# client = AIOTClient(device_id=b"DEVICE_ID", username=b"DEVICE_ID", password=b"SECRET_KEY")
4340
client = AIOTClient(device_id=DEVICE_ID, ssl_params={"keyfile": KEY_PATH, "certfile": CERT_PATH})
4441

4542
# Register cloud objects. Note these objects must be created first in the dashboard.

0 commit comments

Comments
 (0)