The Internet of Things (IoT), those words will either fill you with hope or leave you in fear! But the IoT is here and many devices now communicate their information using a myriad of protocols. Some of these protocols are proprietary, closed source or “heavyweight” but what if we needed a lightweight protocol to send messages over a low bandwidth network? Well MQTT is the protocol you need!
Message Queue Telemetry Transport (MQTT) is a protocol designed for limited bandwidth, remote connections where sensor devices can be left in the field, gathering data and reporting that data to another device over a network.
MQTT works as a publisher > broker > subscriber model. The publisher is the sensor device, somewhere in the world gathering data, this data is then published using a “topic” via a broker who handles the connections made on the network. The subscriber is listening to the same topic as the publisher and will see any data published using that topic. A classic analogy is YouTube – content produces published content – which subscribers choose to subscribe to. YouTube merely acts as a broker enabling the publisher to send content to the subscribers.
What can MQTT be used for?
MQTT is a flexible protocol, from sending sensor data, to triggering remote devices to come alive from a low power sleep mode, MQTT is an easy to use protocol that can be used with many devices. Common uses are for remote sensors sending data back to base for further analysis. Home automation systems that monitor and regulate the temperature, lighting and security of our home. MQTT is a swiss army knife in IoT, as it works with many different platforms using a common protocol.
What platforms can it be used with?
MQTT is at home on many platforms, but for devices such as the Raspberry Pi, there are software packages available in the repositories for Linux and Python (these also work with Linux machines).
MQTT is even part of the Node-RED application, enabling projects to be programmed with greater ease using connected nodes.
MQTT can also be used with devices such as the Onion Omega, a small lean Linux device that is a cross between a Raspberry Pi and an Arduino. We used an Onion Omega 2 as a MQTT broker for a test project and it coped with a significant amount of traffic. There are even MQTT clients for Arduino, of course, you will need an Ethernet / WiFi shield to make use of them unless you are using an ESP8266 board.
How can I use MQTT?
There are MQTT clients for most platforms and here are a few suggestions.
The most basic tool is called Mosquitto, and it is a terminal application that can be used publish, subscribe and create a broker on any Linux machine.
To install open the terminal and using your package manager search for
To create a broker on a machine, we simply need to start the Mosquitto service and it will run in the background.
Then on a machine that we wish to subscribe from
mosquitto_sub -t Data -h BROKER IP ADDRESS
And then we can publish a message on the same topic as the subscriber.
mosquitto_pub -t Data -m "HELLO WORLD" -h BROKER IP ADDRESS
The message is then sent to the broker, and the subscriber then receives the message less than a second later!
Two MQTT clients for Android are MQTT Client (https://play.google.com/store/apps/details?id=in.dc297.mqttclpro)
Both of these clients can be used to subscribe to topics and publish to the MQTT network.
There is a Python library for MQTT called paho-mqtt and to install using the pip3 package manager, open a terminal and type.
$ sudo pip3 install paho-mqtt
For an example of how to use a sensor device to publish data over an MQTT network, take a look at this demo (https://github.com/lesp/HIVE-MQTT-Demo) that was created for school children to post temperature data over a network from one Raspberry Pi to another.
For other platforms such as Windows and Mac, please take a look at https://www.hivemq.com/blog/seven-best-mqtt-client-tools which is a great resource for MQTT projects.
Is MQTT secure?
By default MQTT is insecure and messages are sent with no authentication, but you can set up a username and password to ensure that only authorised users can publish and subscribe to your MQTT network.
MQTT is a great platform for IoT as it provides a simple yet powerful base from which we can create projects. It crosses different programming languages, operating systems and can be used with many different network. The inclusion of a Python library enables makers using the Raspberry Pi to use MQTT in their builds and create projects that are triggered and react to sensor input, control motors and other methods of creative input / output. MQTT could just be the ideal way in which citizen science projects communicate and share data across the world.
- Easy to use
- Lots of configuration options
- Publisher/broker/subscriber model takes some getting used to
- Python library while easy to use, requires a decent level of Python knowledge
- MQTT is insecure by default so will need user configuration to make it secure