User Tools

Site Tools


Installing MQTT

MQTT stands for “Message Queue Telemetry Transport” and is an in 1999 finally created/authored very simple protocol to transport data between several clients in a TCP/IP network in all formats (like, text, numbers, hex, Base64, etc.) by using only a minimum of data to save expensive network bandwidth and keep battery drain as low as possible.

MQTT is today used a lot for M2M communication in the IoT. Each client connects to a broker, which is acting as a server, and can then post (publish) data towards a specific topic. All other clients can subscribe to such a topic or also more topics or wildcard-named topics to receive information. The broker usually runs on port 1883.

For further details see

MQTT-Broker which equals the server

Simply install 'mosquitto' as a perfectly running server aka broker. It supports the most used protocols including user/passord ans TLS encryption if needed:

sudo apt-get update
sudo apt-get install mosquitto

The simple initial startup is usually sufficient for first tests in a local environment like the private home network.


sudo apt-get update
sudo apt-get install mosquitto-clients python-mosquitto
sudo pip install paho-mqtt


A topic specifies a filterset, to specify which data to subscribe to when reading and which value when sending. When sending the topic has to be exact as it can be seen in the examples below, but when reading from it, the description can be chosen so just select a set of data. The characters '#' is used for wildcard-topics between slashes and '+' is used for sections. So the following subscription wants to grab all out-door values ('aussen') from all devices of my home ('zuhause'). Remind, topics are case sensitive.

With useful filterings when asigning to topics and a well thought structure in the environment, data can be grabbed afterwards in useful filtered groups.

If everything shall be monitored, there is only a subscribtion to topic '#' neccessary.

MQTT-Client in Python

Simple python script sending “Hello World” to a broker with topic “test/example”:
#!/usr/bin/env python
   Send data as an MQTT-Client via python and 
   being used to be integrated in other scripts
   Andreas Link, 11.2016
import paho.mqtt.client as mqttcl
MQTTdata="Hello World"
client = mqttcl.Client('Python-TestClient1')
client.publish('test/example', MQTTdata)

MQTT-Client via command line

Reading a simple temp-value from a file and send it as the topic “house/livingroom/temperature” via command line:

mosquitto_pub -h -t 'house/livingroom/temperature' -f /tmp/temperatur.txt

MQTT-Broker Watchdog

As I had some issues with the web socket part of the mosquitto broker (currently also in v1.4.11)…

kernel: [531318.740882] traps: mosquitto[12696] general protection ip:7feccd7a8a0c sp:7fff92339300 error:0 in[7feccd7a0000+1f000]

… which finally killed the broker's functionality over all ports/interfaces, I decided to create a short bash watchdog script, which is running every minute via cron testing whether the broker is still alive and if not, simply restart it:

/usr/bin/timeout --preserve-status -s 9 10 mosquitto_pub -h -t 'system/watchdog' -m "$(date)" > /dev/null 2>&1
#Save last process status in var STATUS
if [ $STATUS -gt 0 ]; then
    if [ $STATUS -eq 137 ]; then
        echo "ERROR: Watchdog timeout reached, mosquitto broker seems to be down or damaged."
        echo "ERROR: An unexpected error occurred with Code $STATUS"
    #Anyway, restart mosquitto asap:
    echo "Timestamp: $(date)"
    echo "Restarting mosquitto..."
    /etc/init.d/mosquitto restart
    echo "...done"
#if [ $STATUS -eq 0 ]; then
#    echo "$(date) -> Broker is still alive, everything is fine. :-)"
setupmqtt.txt · Last modified: 08.04.2017 22:23 by andreas