List of previous posts

 

Preface

 

My project for the Internet of Holoday lights was based around our living room which was not very livable. It was a mess and with the upcoming holidays I wanted to set the living room in such a way that it would be suitable for entertaining guests. Additionally I wanted to accomplish the above mentioned in such a way that the holiday lighting becomes part of the living room and I don't need to remove it after the holidays. Hence the concept of Dynamic Living-room Lighting.

 

In the previous posts, I have review the YUN and the infineon shield and have presented an overview of the project system. I also setup the place for the lighting with some homemade arts and crafts.

 

In this post, I will explain the control of the RGB LED strips attached to the arduino YUN and connected to an instance of OpenHAB. This instance of OpenHAB is running a raspberry Pi and an MQTT broker.

 

Installing OpenHAB and Mosquittoe

 

For the Forget Me Not Challenge, I used OpenHAB as the center piece and details can be found at

 

The "Forget_What ?" project - Index

 

I did various tutorials on installing and using OpenHAB and I suggest going through them if interested. Once you have installed it, you can install addons for such as MQTT etc. Additionally, I installed Mosquitto MQTT broker but you can use iot.eclipse.org:1883 . My reason for installing a local MQTT broker was the ability to work in the absence of an internet connection.

 

1.png

Configuring the YUN for MQTT

 

Benjamine Cabe did an excellent post at Controlling Holiday Lights using MQTT and the Arduino Yún and the code is available on github. For my experiment, I changed the subscribed topic according to my requirement. The rest of the code is more or less the same. The code can be downloaded at https://github.com/kartben/internet-of-holiday-lights

 

Once you have setup the YUN with the code and into your network, you can open up a serial monitor in the Arduino IDE by pressing Ctrl+Shift+M

 

You will also need a program to test MQTT off the bat. I choose MQTT Lens which is a Google Chrome Addon. Point it towards the MQTT Broker and you should be done.

 

1.png

The screenshot above shows how to control the YUN with the code supplied by Benjamine Cabe as is. Just send RGB values and you are good. If you send 000000 then the LEDs switch off and if you send ffffff then the LEDs turn on full brightness hence the white color.

 

Now you should be able to see the results almost instantaneously and if you don't try and see if the YUN is connected to the internet or not. The serial monitor can help you debug that.

 

Making OpenHAB talk to the YUN

 

It took me a little while to get this up and I am not completely happy with it, but its a goog start for the lazy man. The task is to format the MQTT messages just like Cabe Sir's application reads so we can use the YUN Code directly. Its actually quite easy.

 

The first thing we need to do is create an Item in the .Items file. This just creates a place to house the data for OpenHAB. I created a simple item as follows.

 

Color      Hue                      "IP Hues"                                      <colorwheel>      (All)
String            HueMqtt                  (All)            { mqtt=">[eclipse1:ip-led:command:*:default]"}







 

Note that I have a color wheel object that can store the RGB values and a string object that will store the string to be passed to the YUN. The '{' indicates that a binding has been employed which is mqtt. > specifies that the direction is outwards. eclipse1 is my name for the MQTT connection configuration stored in the openha.cfg file  and ip-led is the topic. You can change this to match your topic such as /IOTLights/LEDs1 etc. Please note these are case sensitive. Next it will send whatever (*) command this object gets. You can specifify conversions here using XLS but I choose to do all that in the rules segment.

 

The second thing we need to do is make it a part of the UI. You can see there is a segment in the above screenshot for the IOT Holiday Lights and is labeled YUN. To make that, we use the following code.

 

sitemap demo label= "Main Menu"
{


// All my other stuff//
// IOT Holiday Lights Frame
Frame label= "IOT Holiday Lights"{
            Text label= "YUN" icon= "yun "{
                  Frame label= "Yun Control"{
                        Colorpicker item= Hue icon="slider"
                  }
            }
      }
}







 

I downloaded the icon from the net and put it in the OpenHAB/webapps/images/yun.png . The resulting sub frame is shown below.

 

1.png

 

1.png

So now we have a color wheel. Brilliant!

 

The Third step is creating rules to connect the Color Wheel to the MQTT topic via the String. This is where all the fun stuff happens and for my instance my code is given below.

 

 

import org.openhab.core.library.types.*


// Stuff for the IOT Holiday Lights
var HSBType hsbValue
var int Brightness
var String redValue
var String greenValue
var String blueValue


// ********************* IOT Holiday Lights ****************************
rule "YUN Lights MQTT"
when
      Item Hue changed
then
      hsbValue = Hue.state as HSBType


      val Red1 = hsbValue.red.intValue
      val Green1 = hsbValue.green.intValue
      val Blue1 = hsbValue.blue.intValue

      redValue = Red1. toString(16 )
      greenValue = Green1. toString(16 )
      blueValue = Blue1. toString(16 )

      val Tmp = redValue + greenValue + blueValue

      sendCommand(HueMqtt, Tmp)
end







 

The rule can be named anything and I created some variables globally in case I want to use the values somewhere else as well. When the item Hue changes, we extract the color values after converting the object's state to HSBType. The color values are extracted as integers and I use local variables for some experiments later. Lastly, we convert them to strings but of base 16 since Cabe Sir's code reads hex values only. Club them together and send them across. Thats it!

 

It works as is and I tested it out with the same YUN code.

 

1.jpg

 

More to DO

 

At this point I am not very happy with the way messages are sent on MQTT. I am used to writing more lower level protocols but here I need to extend the use of MQTT to send elaborate commands to the YUN which will in turn control some other things. At the time of this writing, I have already made changes to the above code and only documentation remains. This post will complement kartben '  post and will use OpenHAB to control it out of the err.. github.

 

Comment if this helps you.

 

Cheers,

IP