diff --git a/Data API/MQTT/Python/client.py b/Data API/MQTT/Python/client.py index 30e8552..def00c8 100644 --- a/Data API/MQTT/Python/client.py +++ b/Data API/MQTT/Python/client.py @@ -1,6 +1,6 @@ ''' * Copyright (c) 2018 - 2019 - RTLOC - * + * * This file is part of RTLOC API tools. * * RTLOC API tools is free software: you can redistribute it and/or modify @@ -24,10 +24,11 @@ # Set Parameters hostname = 'mqtt.cloud.rtloc.com' -topic = 'rtls/kart/posxyz' # Replace with own topic +topics = ('rtls/kart/status', 'rtls/kart/anchors', 'rtls/kart/posxyz') # Replace with own topic username = 'demo:demo@rtloc.com' # Replace with own username password = '12345' # Replace with own password port = 1883 +mqtt_keepalive = 50 decoder = Decoder() @@ -38,14 +39,20 @@ def on_connect(client, userdata, flags, rc): else: print(" >> Connection failed (rc= " + str(rc) + ")") -def on_message(client, obj, msg): +def on_message(client, userdata, msg): #msg.topic, msg.qos, msg.payload - decoder.decode(msg) + print("Received message '" + str(msg.payload) + "' on topic '" + + msg.topic + "' with QoS " + str(msg.qos)) + # only when the topic is posxyz, decode the message + if msg.topic == 'rtls/kart/posxyz': + decoder.decode(msg) + else: + pass -def on_subscribe(client, obj, mid, granted_qos): +def on_subscribe(client, userdata, mid, granted_qos): print(" >> Subscribed: " + str(mid) + " " + str(granted_qos)) -def on_log(client, obj, level, string): +def on_log(client, userdata, level, string): print(string) mqttc = mqtt.Client() @@ -60,14 +67,16 @@ def on_log(client, obj, level, string): # Connect mqttc.username_pw_set(username, password) -mqttc.connect(hostname, port) +mqttc.connect(hostname, port, mqtt_keepalive) +# Subscribe all topics # Subscribe (QoS level = 0) -mqttc.subscribe(topic, 0) +for topic in topics: + mqttc.subscribe(topic, qos=0) # Loop (exit when an error occurs) rc = 0 while rc == 0: rc = mqttc.loop() -print(" >> error - rc: " + str(rc)) \ No newline at end of file +print(" >> error - rc: " + str(rc))