import paho.mqtt.client as mqtt
from paho.mqtt.client import error_string
mqtt_username = "<copy from Akenza Device Api configuration>"
mqtt_password = "<copy from Akenza Device Api configuration>"
device_id= "<copy from Akenza Device Api configuration>"
topic = "/down/{0}/id/{1}/#".format(mqtt_password, device_id)
def on_connect(client, userdata, flags, rc): # The callback for when the client connects to the broker
print("Connected with result code {0}".format(error_string(rc)))
def on_message(client, userdata, message):
print("Received message '" + str(message.payload) + "' on topic '"
+ message.topic + "' with QoS " + str(message.qos))
def on_disconnect(client, userdata, rc):
print("Unexpected disconnection. " + error_string(rc))
print("Unexpected disconnection. " + error_string(rc))
client = mqtt.Client("mqtt-test-client")
client.username_pw_set(mqtt_username, mqtt_password)
client.on_message = on_message
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.connect(host, 1883)
client.subscribe(topic) # Subscribe to the topic, receive any messages published on it
client.loop_forever() # Start networking daemon