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

17 thoughts on “Holiday Lights – Control from openHAB”

  1. Hi, excellent job! i am actually using the esp8266 modules to make wifi iot devices cheaper, do you have any idea of how to approach this same project but not using arduino Yun? Thanks. Javier

  2. Hi, I hopen for information how do you control/connect the LED stripes to the Arduino. Do these LED stripes have a special interface?

  3. I am doing something like this now, but just gave up on openHAB for the time being. What version of openHAB are you using. I was trying 1.8.0 and it did not work with MQTT, but a good friend has 1.7.1 and that worked for MQTT. Then he tried 1.8.0 and it did not work. So I am wondering what you are using.

  4. I mean while sending data to client hacker may modify our data rt so,is there any encryption technique we can use.

    1. According to the FAQ on mqtt.org:

      Does MQTT support security?
      You can pass a user name and password with an MQTT packet in V3.1 of the protocol. Encryption across the network can be handled with SSL, independently of the MQTT protocol itself (it is worth noting that SSL is not the lightest of protocols, and does add significant network overhead). Additional security can be added by an application encrypting data that it sends and receives, but this is not something built-in to the protocol, in order to keep it simple and lightweight.

      I haven’t tried it myself, but it does seem to be possible.

  5. okay ,Thank you.Have you herd about TLS provided by openhab.It provides security if you know anything on this please share.

  6. Hoi Frederick,

    als ik het goed heb bestaat de yun niet meer.
    Ik was geïnteresseerd om hetzelfde te doen, en wil openhab met de arduino laten praten via een netwerk kabel (dus geen draadloos)
    Mara als ik het goed heb bestaat de yun niet meer. Weet jij of er een andere arduino is die een netwerk kabel (out of the box ) ondersteund?

  7. Hoi Frederik,
    bedankt voor het snelle antwoord.
    ik heb me blijkbaar laten leiden door een website die ze niet meer verkochten maar nu gezien dat er andere plaatsen zijn …
    Andere vraag. Weet jij hoe lang de verbinding kabels tussen de neopixels en de arduino mogen zijn?
    Moeten die op dezelfde plaats staan, of kan je die verbinding verlengen en zoja enig idee hoe lang?
    (ik ben aan het kijken of ik de arduino’s kan plaatsen bij mijn openhab server.)

  8. Nu ik mijn openhab opgezet heb en ook mijn arduino, eindelijk bezig met dit stukje.

    Ik werk in openhab 2 en dat is een pak cryptischer als het komt op documentatie.
    Als ik het echter goed begrijp van jouw blogpost, dan gebruik jij een publieke MQTT server en niet een mqtt server op openhab. is dat correct?

  9. I have a smal update to your rule:

    var String redValue = String::format( “%03d”, ((((hsbValue.red.intValue * 255) / 100) * brightness) / 100))
    var String greenValue = String::format( “%03d”, ((((hsbValue.green.intValue * 255) / 100) * brightness) / 100))
    var String blueValue =String::format( “%03d”, ((((hsbValue.blue.intValue *255) / 100) * brightness) / 100))
    var String color = redValue + “,” + greenValue + “,” + blueValue
    sendCommand( TreeLightColor, color )

    This way, your colors keep working when a value is selected that is smaller then hundred.
    By doing this, the arduino side is a little easier: now you can hard code the positions, instead of looking for a “,”
    And that makes it also easier to work with an RGBW led strip. (no need to figure out what is the last “,”

Leave a Reply

Your email address will not be published. Required fields are marked *