Post 2 of 2 on the topic of Arduino and openHAB for home automation and wireless sensors. Click here to see the first post for the general background information, and also for the actual Arduino sketches that implements this setup. This second post gives more detail regarding MQTT binding between my wireless Arduino sensor and OpenHAB. Read up on how to install Mosquitto here, and the basics of the MQTT protocol here.
Goal: We want the Arduino Gateways (both the RFM Gateway and the Ethernet Gateway) to just work. We don’t want to have to constantly change the sketches on the gateway Arduinos just because we add a new sensor node or add a new device on an existing sensor node. We also don’t want to double manage things. I hope to explain how this double management problem occurs and how to minimize it.
I’ll reuse the communication diagram from the previous post, and reiterate that the “Arduino Gateway” is actually composed of two Arduinos (RFM gateway and Ethernet gateway).
MQTT Message = <topic name> & <data>
topic name = “Topic_Garage_Temperature”
data = 75.2
A MQTT message is composed of two parts: the topic name, and the data itself. With MQTT, there does not seem to be any data types like int and float. On the Ethernet Arduino, you take the floats and ints you get from the sensors, convert them to strings, and send the string off to the MQTT library to be magically sent via ethernet to Mosquitto. Here is a very simplified setup. Click to enlarge.

An overly simplified (but totally workable) way to take wireless data (a float), and put it out on ethernet MQTT to OpenHAB
But this method might be too simple. Using this setup, every time you add a field node, you’ll have to reprogram the two gateway Arduinos in order to account for the new topic name. It’s a pain to have the gateway Arduino sketches identify and convert each field node’s transmission into a unique topic name. You’re managing topic names in both gateway Arduinos and also in OpenHAB. Too many things to go wrong.
Using the RFM69 library, each Arduino is assigned unique Node ID. The Node ID doesn’t have to be an integer, but let’s stick with integers for now.
#define NODEID 40 //unique for each node on same network
You’re responsible for making sure your Node ID’s are unique, so keep a spread sheet of which Arduinos are using which Node ID’s. I’m taking advantage of this unique node ID in order to form parts of the topic name. Hopefully this picture explains a thousand words.

To avoid having to make changes to the gateway Arduino sketches every time a sensor node needs to be added, we’ll automate the formation of unique topic names by taking advantage of the uniqueness of NodeID in the RFM network. I hope this picture makes sense.
Packing Data
Since each field node knows its own node ID (because you assigned it), we’ll embed the unique Node ID into the data structure we defined for wireless transmission. The field Arduino also knows how many sensor devices are connected to it, so we might as well also embed an integer to capture this arbitrarily assigned Device ID. And because we have the freedom to define the wireless transmission structure, we can capture more than one piece of data per transmission, and determine what each data type means on a field node to field node basis. So there’s Var1 (unsigned long int) that can be the millisecond counter, Var2 (float) that can hold the temperature data or whatever, and Var3 (also a float) that can be used eventually for battery power of the field node.
Each time the Arduino Ethernet Gateway receives new data, it actually sends several MQTT messages, one for each of the variables we’re interested in. You’ll notice that not all the data from the wireless transmission struct is actually being sent to MQTT. Only Var2 and Var3 (both floats) are sent to MQTT. Partly because I got lazy. I initially thought I’d care about the millisecond count, but it turned out not to be the case.
The RSSI variable is the wireless transmission strength. See this image. Only the Arduino RFM Gateway knows this information, which is why it injects a new data type, int, to capture the RSSI.
Here’s an example of how I keep track of all the ID’s and topic names.
Spreadsheet for assigning Node ID’s, Device ID’s, and Variable ID’s. Topic names are needed for setting up each item in OpenHAB

Nice to read your posts! I’m in the making of doing something pretty similar with Arduino Nano (serial attached) and writing a gateway software to communicate with a MQTT bus (RabbitMQ) and end up in openHAB.
Will definitely follow your blog!
Best regards
Hi Patrik,
I’d be interested in how things go with using RabbitMQ. With Mosquitto, my Arduino Uno (clone) with the Wiznet 5100 shield would frequently get disconnected from the MQTT server. Not sure what the problem was. When I have data to send to MQTT, I do a connected() check, and reconnect if needed. This solved the problem of unreliable connection, but I haven’t figured out if the issue is my cheap Ethernet shield or something else.
I’d be interested in how you make yours work.
Eric
Hi, interesting Blog you have. I am trying to use Raspberry Pi as well for MQTT broker and OpenHab, as I am building my new house with SmartHome capability. However, it will be fully hard wired, not using RF communication.In order to get more juice, I was thinking to use those Mini PC box. Also, have you thought of using MQTT-SN for communication for RF ? I believe MQTT-SN is quite suitable for this.
Hi Patrick
Are you looking in sending data from openhab to switch on lights via mqtt?
Regards
Francois
Very glad to discover your two posts as they are very similar to what I’m aiming to do. Over the last few weeks I have been making sketches (the artistic kind in notebooks rather than the arduino kind… although those as well!) and thinking about how I can leverage the ridiculously cheap sensors available from HK and Taiwanese sellers on eBay.
I’m not quite as far on as you, I started with a set of RF 433 wall light switch dimmers then bought an RFXcom 433 transceiver, and only recently placed orders for more: arduino, RF transmitters and receivers (separate modules running on 433 for testing), and sensors (temp, humidity, ultrasonic – although I plan to use these for control panel hand sensing), PIRs, and even an alcohol gas sensor (mad-hat idea about installing one of these by the front door with an ultrasonic and a display to notify dinner guests of their drunkenness as they leave the house…) – anyway so far I have only received and played with the arduino, PIR, and rotary controllers.
My plan was slightly different to yours in that my field nodes would be RF 433 enabled and would send signals directly to OpenHAB with the rfxcom binding. However this is obviously quite limiting (in terms of the messages supported by the binding) and so I though perhaps I would wore CAT5 cables to the field nodes… that pushes the price of a field node up considerably though given the need for ethernet shields on field nodes.
Another snag was the fact I am hell-bent on controlling Squeezebox room players with a physical rotary controller and circular led bar graph wired into arduinos installed in wall switch back boxes – and I simply can’t figure out in my head how to integrate that idea into the masterplan.
Given these constraints / challenges I’m coming round to your way of thinking by having a double arduino gateway, one with sensors attached and the other with ethernet sending OpenHAB (served elsewhere) MQTT messages.
In fact just as I was thinking about how the heck I would do this without the need to constantly re-program both sides of the gateway every time I added a new node (it makes for fun project work but not a happy girlfriend), I stumbled upon your posts on this subject.
Looking forward to incorporating your ideas into my project now!
My slight concern is that we are having a new build being done and I reeeally need to figure out what wires to put where. Obviously CAT6 everywhere but how- 1wire standard of a long bus from component to component or field-node style of hub and spoke layout… Unfortunately these decisions need making before I have time to settle on my design
Thanks though – I will follow your progress!
I hate the Arduino ethernet shields. They’re very inefficient, and generate lots of heat. I’ve had to put heat sinks on mine. Plus, I really like the option of doing battery powered sensor nodes, so that definitely rules out ethernet shields.
Your idea about controlling squeezeboxes with a wall-mounted controller…it seems pretty doable. I haven’t tried the squeezebox bindings on OpenHAB, but the Arduino side is very doable. My garage door uses an ultrasonic sensor…which is pretty much like a rotary dial in terms of sending a float back to openhab, and then reacting to the value of that float. Heck, I can see a battery powered Arduino used for the Squeezebox controller, so the controller could be placed on a coffee table. It would require a interrupt button for that – push this button and turn the knob…or maybe use a PIR sensor such that most of it is covered up and use the PIR to activate the interrupt and read the analog pot…so using the PIR sensor as a “wake up, a person is operating the pot” signal.
Thank you for your post, it was exactly what I needed to get started.We are going to be building a new house and my idea is to use an Arduino (or 2) in each room to act as a master control/feedback to openHAB. Considering an ethernet shield/Pro Mini can be had for $6, wired field nodes are fairly inexpensive. For control, I found this little example that would make field nodes more versitile:
http://m2mio.tumblr.com/post/30048662088/a-simple-example-arduino-mqtt-m2m-io
Again, thank you for the post, your ideas on naming and organizing are invaluable.
Eric,
Do you have any projects that sends commands to leds or relays? Myself and another guy are using this uber project to learn Arduino and OpenHAB. Thank you for your attention to detail. We are gaining quite a bit of knowledge based on what you have shared.
Respectfully,
Joshua
Joshua did you find any solution about how to control a relay from openhab? i’m asking everybody everywhere but no answer till now….
any suggestion will be kindly appreciated!
Not yet. I’m about to try out two things:
– Esp-12 Serial WIFI ESP8266 Module + Full IO Port Leads Adapter Plate http://bit.ly/1MwBmxC
– Here’s a board with an upgraded RFM Gateway Arduino – RFM69 based sensors and MQTT gateway http://bit.ly/1MwBq0i
For outputs, I’ve been using direct MQTT subscribes from an Arduino w/ network connection. I don’t have the gateway developed for RFM69 outputs. Someone on the forum did write a version of a RFM gateway with outputs. You can give that a try.
Otherwise, the other promising solution is to use the $4 ESP8266 chip. I haven’t played with it enough to know how to use it yet. There’s an Espduino project that uses the ESP8266 for wifi connection and can do MQTT subscribes, but I haven’t been able to load the firmware.
electronichamsters i read your article but i didn’t understand how to connect everything together. could you please tell me about the integration of mqtt, arduino and openhab? how did you do it? i want to try a mqtt communication between my arduino with one dht11 and openhab but i don’t understand how!
only for test i’m using my laptop to install openhab server. where and how to install mqtt ? what broker do you use? i cant use mosquitto on my laptop ‘couse of some missing dll … about the arduino, what libraries should i use or i need some special code?
thanks in advance,
Awsome stuff electronichamsters! I am using your project as a guide for mine. The only difference is that I managed to have my Nodes talk directly to the RaspberryPi via the nRF24L01’s. So no need for gateways or direct wiring (I2C).
Nightparrot please blog about that!