akenza.io
Search…
MQTT
This page describes MQTT Connectivity in more detail
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>

Sending data with MQTT

uplink.py
1
import paho.mqtt.publish as publish
2
3
topic ="<copy from Akenza Device Api configuration>"
4
host = "mqtt.akenza.io"
5
mqtt_username = "<copy from Akenza Device Api configuration>"
6
mqtt_password = "<copy from Akenza Device Api configuration>"
7
payload = '{"foo":"bar"}'
8
9
publish.single(topic, payload, hostname=host, port=1883, auth={'username':mqtt_username, 'password':mqtt_password})
Copied!
1
pip3 install paho-mqtt
2
python3 uplink.py
Copied!
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.py
1
import paho.mqtt.client as mqtt
2
from paho.mqtt.client import error_string
3
4
host = "mqtt.akenza.io"
5
mqtt_username = "<copy from Akenza Device Api configuration>"
6
mqtt_password = "<copy from Akenza Device Api configuration>"
7
device_id= "<copy from Akenza Device Api configuration>"
8
topic = "/down/{0}/id/{1}/#".format(mqtt_password, device_id)
9
10
def on_connect(client, userdata, flags, rc): # The callback for when the client connects to the broker
11
print("Connected with result code {0}".format(error_string(rc)))
12
13
def on_message(client, userdata, message):
14
print("Received message '" + str(message.payload) + "' on topic '"
15
+ message.topic + "' with QoS " + str(message.qos))
16
17
def on_disconnect(client, userdata, rc):
18
print("Unexpected disconnection. " + error_string(rc))
19
if rc != 0:
20
print("Unexpected disconnection. " + error_string(rc))
21
22
client = mqtt.Client("mqtt-test-client")
23
client.username_pw_set(mqtt_username, mqtt_password)
24
client.on_message = on_message
25
client.on_connect = on_connect
26
client.on_disconnect = on_disconnect
27
client.connect(host, 1883)
28
client.subscribe(topic) # Subscribe to the topic, receive any messages published on it
29
30
client.loop_forever() # Start networking daemon
Copied!
1
pip3 install paho-mqtt
2
python3 downlink.py
Copied!
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.

Using TLS

akenza allows sending data using transport layer security (TLS). For this the port 8883 has to be used.
1
import paho.mqtt.publish as publish
2
3
topic ="<copy from Akenza Device Api configuration>"
4
host = "mqtt.akenza.io"
5
mqtt_username = "<copy from Akenza Device Api configuration>"
6
mqtt_password = "<copy from Akenza Device Api configuration>"
7
payload = '{"foo":"bar"}'
8
9
tls_config = {
10
"cert_reqs": ssl.CERT_REQUIRED,
11
"tls_version": ssl.PROTOCOL_TLSv1_2
12
}
13
publish.single(topic, payload, hostname=host, port=8883, tls = tls_config, auth={'username':mqtt_username, 'password':mqtt_password})
Copied!
Refer to the paho-mqtt documentation for more information.
Why am I getting a certificate expired error?
Akenza is using an ISRG Root X1 root certificate provided by Let's Encrypt to sign certificates. If your device is older, it might not contain an up to date root certificate. Aquire the root certificate from here and update your trust store accordingly.