MQTT Controller


On this page

List Of Improvements

The MQTT controller from the introduction project works fine as an introduction, but you'll soon find yourself looking for ways to extend it. On this page you'll find example code with the following improvements:

  • Example of using different types of sensors besides the DHT sensor, and only send data when there are significant changes
  • Example of an action node, letting you control lights, shades, etc
  • Example of how to handle multiple nodes and multiple MQTT channels
  • Better file names :-)

Controller changes

One big change compared to the introduction project is that this controller can monitor MQTT channels for instructions to send to your Arduino(s). To be able to do that, you need to first install the libmosquitto development packages on your Pi:

sudo apt-get install libmosquitto0-dev libmosquittopp0-dev

Then take a look at the code below. The make file to compile it can be downloaded from here. There are comments thoughout the code to explain what's going on. In this version there are 2 example sensor nodes and 2 example action nodes. Remove or add nodes as needed, and update the channels to fit your setup.

As a reminder, this is how you compile and run it (execute this from the folder where you put both new files. I have them in a folder named "sensors" under my home folder):

sudo ./MQTTController

And if you had the old hareceiver start on boot, this is the file to modify so it starts MQTTController instead:

sudo nano /etc/rc.local

Arduino - Sensor Node

MQTTController works best with an updated version of the Arduino code that can handle multiple sensor types.

Wiring instructions and the .ino program can be found here.

Arduino - Action Node

With MQTTController you can not only receive data from your Arduino(s), you can also send instructions to them.

An example that turns an LED on or off can be found here.

Adding It All In OpenHAB

For the default.items file I added the 5 sensors from the first node like this:

Number	Node01Temperature	"Temperature [%.1f F]"	<temperature>	(GF_Living)		{ mqtt="<[mymosquitto:home/bedroom/temperature:state:default]" }
Number	Node01Humidity	"Humidity [%.1f %%]"	<bath>	(GF_Living)		{ mqtt="<[mymosquitto:home/bedroom/humidity:state:default]" }
Number	Node01Light	"Light [%d %%]"	<sun>	(GF_Living)		{ mqtt="<[mymosquitto:home/bedroom/light:state:default]" }
Number	Node01Motion	"Motion [MAP(]"	<shield>	(GF_Living)		{ mqtt="<[mymosquitto:home/bedroom/motion:state:default]" }
Number	Node01Door	"Door [MAP(]"	<frontdoor>	(GF_Living)		{ mqtt="<[mymosquitto:home/bedroom/door:state:default]" }

The parts with MAP( and MAP( might be new to you. These files tell OpenHAB how to map the 0/1 values reported by the sensor to text values so it looks nicer for us humans. You create them under /opt/OpenHAB/configurations/transform (two different files):

0=No Motion
1=Motion Detected


Finally you can show the new items in default.sitemap like this:

Frame label="Sensor Node 1"  {
	Text item=Node01Temperature
	Text item=Node01Humidity
	Text item=Node01Light
	Text item=Node01Motion
	Text item=Node01Door

Sensor node view in OpenHAB
The final result in OpenHAB. I had the sensornode on top of my computer - I don't live in a sauna. TODO: figure out why the frontdoor icon isn't showing.

For the action node, you can add a switch to the items file like this:

Switch LEDSwitch { mqtt=">[mymosquitto:home/bedroom/light:command:ON:1],>[mymosquitto:home/bedroom/light:command:OFF:0]"}

In the sitemap file, you add a switch like this:

Switch item=LEDSwitch

Go Back

Back to the guides