If a device communicates via MQTT with akenza, an MQTT connector has to be set up and assigned to a data flow.
After the creation of the MQTT device connector, a secret is generated which needs to be provided in the topic structure of the uplink request.
MQTT username: the device connector id
MQTT password: the device connector secret
MQTT topic: a topic with the following structure /up/<deviceConnectorSecret>/id/<deviceID>
uplink.pyimport paho.mqtt.publish as publish​topic ="<copy from Akenza Device Api configuration>"host = "mqtt.akenza.io"mqtt_username = "<copy from Akenza Device Api configuration>"mqtt_password = "<copy from Akenza Device Api configuration>"payload = '{"foo":"bar"}'​publish.single(topic, payload, hostname=host, port=1883, auth={'username':mqtt_username, 'password':mqtt_password})
pip3 install paho-mqttpython3 uplink.py
Subscribing to downlinks is possible by providing the following info:
MQTT username: the device connector id
MQTT password: the device connector secret
MQTT topic: a topic with the following structure /down/<deviceConnectorSecret>/id/<deviceID>
downlink.pyimport paho.mqtt.client as mqttfrom paho.mqtt.client import error_string​host = "mqtt.akenza.io"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 brokerprint("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))if rc != 0:print("Unexpected disconnection. " + error_string(rc))​client = mqtt.Client("mqtt-test-client")client.username_pw_set(mqtt_username, mqtt_password)client.on_message = on_messageclient.on_connect = on_connectclient.on_disconnect = on_disconnectclient.connect(host, 1883)client.subscribe(topic) # Subscribe to the topic, receive any messages published on it​client.loop_forever() # Start networking daemon
pip3 install paho-mqttpython3 downlink.py
The downlink topic added in akenza will be appended to the topic subscribed to e.g. when providing "myTopic" the resulting downlink topic will look as follows: /down/{0}/id/{1}/myTopic.
akenza allows sending data using transport layer security (TLS). For this the port 8883 has to be used.
import paho.mqtt.publish as publish​topic ="<copy from Akenza Device Api configuration>"host = "mqtt.akenza.io"mqtt_username = "<copy from Akenza Device Api configuration>"mqtt_password = "<copy from Akenza Device Api configuration>"payload = '{"foo":"bar"}'​tls_config = {"cert_reqs": ssl.CERT_REQUIRED,"tls_version": ssl.PROTOCOL_TLSv1_2}publish.single(topic, payload, hostname=host, port=8883, tls = tls_config, auth={'username':mqtt_username, 'password':mqtt_password})
Refer to the paho-mqtt documentation for more information.