From 5565c45e71b00cd5660e84281b0091cd8c7e25b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20=C5=9EEKER?= Date: Wed, 22 Jan 2020 09:22:20 +0300 Subject: [PATCH 1/2] Update client.py changed topic, added print function for received messages and changed parameter name obj to userdata --- Data API/MQTT/Python/client.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Data API/MQTT/Python/client.py b/Data API/MQTT/Python/client.py index 30e8552..8d00c5b 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,7 +24,7 @@ # Set Parameters hostname = 'mqtt.cloud.rtloc.com' -topic = 'rtls/kart/posxyz' # Replace with own topic +topic = 'rtls/kart/status' # Replace with own topic username = 'demo:demo@rtloc.com' # Replace with own username password = '12345' # Replace with own password port = 1883 @@ -38,14 +38,17 @@ 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 + print("Received message '" + str(msg.payload) + "' on topic '" + + msg.topic + "' with QoS " + str(msg.qos)) decoder.decode(msg) -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() @@ -63,11 +66,11 @@ def on_log(client, obj, level, string): mqttc.connect(hostname, port) # Subscribe (QoS level = 0) -mqttc.subscribe(topic, 0) +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)) From c26234fc8542655cbb1ff55ef9bd862fb28c3dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20=C5=9EEKER?= Date: Sat, 25 Jan 2020 15:33:55 +0300 Subject: [PATCH 2/2] Update client.py Added subscribing different topics and only when the topic is posxyz decode the message, added keepalive period --- Data API/MQTT/Python/client.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Data API/MQTT/Python/client.py b/Data API/MQTT/Python/client.py index 8d00c5b..def00c8 100644 --- a/Data API/MQTT/Python/client.py +++ b/Data API/MQTT/Python/client.py @@ -24,10 +24,11 @@ # Set Parameters hostname = 'mqtt.cloud.rtloc.com' -topic = 'rtls/kart/status' # 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() @@ -42,8 +43,11 @@ def on_message(client, userdata, msg): #msg.topic, msg.qos, msg.payload print("Received message '" + str(msg.payload) + "' on topic '" + msg.topic + "' with QoS " + str(msg.qos)) - decoder.decode(msg) - + # only when the topic is posxyz, decode the message + if msg.topic == 'rtls/kart/posxyz': + decoder.decode(msg) + else: + pass def on_subscribe(client, userdata, mid, granted_qos): print(" >> Subscribed: " + str(mid) + " " + str(granted_qos)) @@ -63,10 +67,12 @@ def on_log(client, userdata, 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, qos=0) +for topic in topics: + mqttc.subscribe(topic, qos=0) # Loop (exit when an error occurs) rc = 0