Skip to content
Open
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
27 changes: 18 additions & 9 deletions Data API/MQTT/Python/client.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()

Expand All @@ -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()
Expand All @@ -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))
print(" >> error - rc: " + str(rc))