Close

Holiday Lights – Control from openHAB

After setting everything up in last post, I started working on the actual configuration of openHAB and control of the Arduino Yun via MQTT.

This is the result so far …

openHAB

To be able to control the colors and animations of my digital Christmas tree, I opted for openHAB. I discovered openHAB during the Forget Me Not Design Challenge and like it very much. I find it easy to work with and very versatile (home automation, pet feeding, controlling Christmas lights).

In my previous post, I covered the installation and basic configuration. Now, I will document how I’ve created following interface to control my lights:

Screen Shot 2014-12-23 at 22.16.52 Screen Shot 2014-12-23 at 22.16.45 Screen Shot 2014-12-23 at 22.17.01

Items

The openHAB documentation on MQTT was sufficient to get me started. It describes how to define items to subscribe to or publish MQTT messages.

At this stage, I’m using openHAB to publish values selected in the web interface such as color or animation.

Here are some example items (the others are just duplicates of this with other names and topics):

You’ll notice that I’ve separated the MQTT binding from the color picker. This is because I want to process the color picker’s value before sending it over MQTT.
The processing of the color picker’s value is done with a rule and then assigned to another item which then triggers the binding.

Rules

The color picker’s default value format was not really usable in my case, so I needed to convert it to something I could use.
Taking the state of the color picker, I extracted the R, G, B and brightness values and put everything in one big string.

The processed data is then sent to the correct variable, triggering the MQTT binding.

Sitemap

Finally, there is the sitemap, in charge of the layout of the web interface. This configuration defines how items are ordered in the GUI.
The configuration is very basic and categorises everything per LED strip. You’ve seen the result in one of the screenshots above.

Retain

One more thing … Because some of my functions (animations) take longer to execute, I might miss certain MQTT messages. Also, if power is lost, no state is kept in the Arduino to restore colors and animations.

To remedy this, I enabled the option to “retain” messages. This will ensure the MQTT broker will keep the messages even after being sent, so that new or late subscribers are sent the messages again instead of having to wait for an update.

This is change in the “openhab.cfg” file:

Arduino

Benjamin Cabé did a great job to help us forward with his post on MQTT with the Paho client.

Still, I decided to give another library a try, not because I’m stubborn (perhaps a little bit ) but because I believe in multiple solutions to a certain problem.
This is why I used the following library: Arduino Client for MQTT « knolleary

The library is easy to use. The following bit of code reflects the general use:

The hardest part (for me) was converting the received messages in a format I could use to compare/parse/etc …
As you can see in the code above, the payload and topic are not strings.

For example, the MQTT messages sent for color codes, are formatted in RGB format with values ranging from 0, 0, 0 to 255, 255, 255.

To parse these, I did the following:

I repeated the “if” statement for every topic and am now able to receive all the messages required to apply color and animation of both strips.

Troubleshooting

During testing of this project, there are two “tools” I’m using quite often: MQTT Lens and Arduino Console.

MQTT Lens

MQTT Lens is an MQTT client capable of subscribing to and publishing MQTT messages.

Screen Shot 2014-12-23 at 21.04.04

Using this tool, I was able to verify the following:

  • MQTT messages are being sent properly
  • the content of the messages is correct
  • the content is published for the correct topic

Arduino Console

The Arduino Yun makes it possible to upload sketches wirelessly, so it must be able to debug wirelessly as well.

I initially tried to use “Serial.println()” statements for debugging, but that only works when connected to the Yun via USB.
After some digging around, I found the command to be used is very similar: “Console.println()”.

In short, you can use the console as follows:

When placed at relevant places in your code, the output becomes very useful:

Screen Shot 2014-12-23 at 21.44.16

As you can see in the screenshot above, I’m using the console to verify the incoming MQTT messages on both topic and content.
This is very useful to know if the received content is parsed properly before being passed to the next function.

Demo

Finally, here’s a short video demonstrating the control of both analog and digital LED strips connected to the Arduino Yun via openHAB and MQTT.

Happy Holidays

If you’ve come this far reading this blog post (or if you skipped ahead ), I’d like to wish you and your family happy holidays! May they be fun and festive.

I’ll be back with more blogs for you to read in 2015! But in the mean time, enjoy this painting my 4yo daughter made:

kerstman

© Frederick Vandenbosch, 2014-2019. Unauthorised use and/or duplication of this material without express and written permission from this blog’s author and owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Frederick Vandenbosch with appropriate and specific direction to the original content.