Menu Close

MQTT basics for a Smart Home with OpenHAB

MQTT Basic for a smart home with openhab

This post may contain affiliate links.  If you make a purchase I may get a commission at no cost to you.  Please see the Disclaimer for additional information.

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
 
Openhab Mosquitto MQTT broker

Secondly, in the Optional Components Menu simply select Mosquitto and confirm the installation. Thirdly, provide a password for the MQTT Broker. Finaly, once Openhabian shows you the completion message, you simply restart the Raspberry Pi for the changes to take effect.

 

Connect your OpenHab to MQTT

Now that you have completed the installation of your MQTT Broker, it is now time to connect your Smart home brain, in this case OpenHab, to it.

In this case, connecting to Mosquitto is quite easy. You will use the Paper UI to add the MQTT Binding and to creae the Mosquito MQTT broker connection.

Open Paper UI

OpenHab Paper UI being one of the easiest way to add and configure bindings, it would be a shame not to use it. To connect o Paper UI :

http://[server Name]:8080/paperui/index.html

Where [server Name] is the local name of your server or the IP Address

Install the MQTT Binding

Once the Paper UI is loaded, navigate to the Addon page.

OpenHAB MQTT Binding Add-on

From the list of bindings, select the MQTT Binding.

OpenHAB MQTT Binding installation

MQTT Binding configuration

The next step is to add the MQTT broker connection such that openHAB is able to communicate with it. To add the broker connection, navigate to the Things configuration page using the menu.

OpenHAB Configuration things
OpenHAB MQTT Broker thing

Now, simply Add a MQTT Broker Thing from the MQTT Bingding and enter the IP Address or Local Name of the server where your Mosquitto Broker is located.

OpenHAB MQTT Broker thing

Voilà! You have now installed an MQTT broker and your OpenHAB connects to it!

 

Testing MQTT

Now that you have completed the installation of Mosquitto MQTT broker and integrate OpenHab with it, it is time to get your hands dirty and it’s time to test it.

Adding a MQTT Switch

Let’s start with something easy, let’s create a switch that will publish a command to an channel.

There are two ways to accomplish that, the GUI way or the text file way. For the purpose of this exercise, let’s use Paper UI .
The first step is to navigate to the Items menu, to add and configure a new Switch Item.
Don’t forget to set the Name, label and Category, if you want, to some values that you will understand and that will help you locate the correct switch.

OpenHAB MQTT Switch Item
OpenHAB MQTT Switch Item Configuration

Once the item is created, you need to add a MQTT Thing to OpenHAB by simply navigating to the Configuration – Things and adding a Generic MQTT thing.

OpenHAB MQTT Switch Thing

While adding the MQTT Thing, you need to configure it by giving it a name, an ID and a MQTT Bridge, which is the MQTT Broker thing you have created earlier.

OpenHAB MQTT Switch Thing Configuration
OpenHAB MQTT Switch Thing Channel

As the next step, you need to add the Channel as a Switch and at the same time you need to set up the MQTT Topic that the Thing will be using for status and command.

OpenHAB MQTT Switch Thing Channel Configuration

Once the MQTT Thing is created and configured, you need to link it to the Switch you have created before.

OpenHAB MQTT Switch Thing Item Linking

Good Job! You’ve just created your first MQTT Switch in OpenHAB.

OpenHAB MQTT Switch

 

Adding the reading switch

The next step is to create a MQTT switch that will subscribe to the command channel and that will listen to grab the first switch status.

Follow the same steps previously used to create a second switch. However, in the Topic section of the Channel, only use the State Topic and set it has the Command Topic of the first switch.

The real test

Now that everything is setup, you only need to flick the switch to see the results!

The second switch changes status as you turn the first one ON or OFF.

Also, if you have an MQTT Tool, you can use it to monitor the Command Topic.

MQTT Tool

 

MQTT troubleshooting

If your test isn’t working properly, do not despair. Here are some investigation ideas you might want to explore

  • Check your Mosquitto MQTT broker address;
  • Check the channel to witch you are publishing and reading from;
  • Get yourself a MQTT communication testing tool.
 

Final Thoughts

You are now all conquer the MQTT world! Knowing how MQTT works opens your possibility to a whole new level of DIY home automation! What will be your next step?

Don’t forget to keep the dream Alive

Enjoyed this post?

Follow us on Pinterest for additional awesome ideas!​

Some posts on this blog contain affiliate links.
At no additional charge to you, I receive a small commission whenever a product is purchased through these links. The Dreaming Dad is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com.