MQTT is the communication protocol that the Open Source IOT community uses. This guide will help you learn everything you need to know on MQTT, how to use it in your own project and how to integrate it with OpenHAB. Combining an home automation server software like OpenHAB and this MQTT communication protocol is a superb idea to start integrating your own Smart Home project into your Home Automation system.
What is MQTT
It is a communication protocol developed by IBM and Eurotech in 1999 as a lightweight, bandwidth-efficient and uses little power.
MQTT stands for Message Queue Telemetry Transport and is a protocol that focus on Machine to Machine (M2M) communication. That being said, you migth wonder why is it useful in a Smart Home concept?
In our case, MQTT is primarily used in IoT devices to communicate with other devices or with the central home automation brain.
Why use MQTT in a Smart Home System
Chances are that your smart home system will be composed of many components that communicate together.
Also, if you are looking to build your own IoT devices, you want to use simple, small and cost effective micro controllers, such as Arduino or ESP8266 that aren’t so powerful but do the job well. These microcontrollers are the perfect tools to use MQTT:
- Very low bandwidth usage;
- Very low energy consomption; and
- Multi-platform availability and usage.
Additionally, it’s a communication protocol that is easy to understand and to use operationally in your home automation environment, you don’t need a master in computer science to understand MQTT and implement it in your projects.
MQTT protocol architecture
All communication protocols uses some sort of architecture.
In our case, MQTT uses what is called a Star topology with a central node, called a Broker, and clients which connects to it.
The Broker runs the show, it is in charge of sending and receiving messages to and from the connected clients. Since it’s the central nodes, it’s used by all clients to communicate among themselves. As such, when you implement MQTT in your home automation eco system, it will be composed of 2 main “elements”:
- MQTT Broker: Allows the different clients to publish and read message to and from the numerous topics.
- Sensors and Actuators: MQTT Clients that either subscribe to a topic to publish message (sensors) or subscribe to a topic to read “commands” to execute some actions (actuators).
MQTT message distribution.
MQTT uses topics to distribute information, be it commands or data and statuses. Also, every clients that subscribe to that topic will receive the information other client post to it.
Here is an example.
Your house has 2 floors and the living room is on the first floor. Let’s say that in your living room, you have a light sensor in your window sending a signal when the light level reach a certain threshold. As such, the sensor publishes a message to the Home/FirstFloor/LivingRoom/LightSensor with a On message. And, this message is read by all clients that subscribes to this topic. As such, you could use that topic to open up the blinds in the living room!
Indeed, the motorized blind actuator would subscribe to the same topic and when it sees that On message, would open the living room blinds automatically.
Also, MQTT is built on a hierarchical concept.
This means, that all subscribers listening on topics above will see the message. As such, let’s say that you have a dashboard listing all data on the first floor, can get all the data required only by subscribing to Home/FirstFloor. And, this means that you are able to collect all the data you need from a higher level.
Also, you are able to restrict the information required by an actuator to only what it needs to perform its duty correctly while lowering the bandwidth.
MQTT Broker
When it comes to your MQTT enabled devices, like any Arduino, Raspberry Pi or ESP8266 based solutions, the first step is to care of the central messenger: The MQTT Broker. Developers around the globe have created their own MQTT brokers but one of the most popular one is called Mosquito.
Depending on your system configuration, the installation will be different:
For Windows.
For Unix.
If you are using OpenHabian, a version of Debian that includes Openhab for a Raspberry Pi, installing Mosquitto is extremely easy. If you have not installed Openhab using OpenHabian it is never too late.
Firstly, you only need to SSH into your OpenHab Raspberry Pi , get into the OpenHab Configuration and select the Optional Components.
sudo openhabian-config