Sunday, November 8, 2015

openHAB and Colorific

After my initial adventures in exploring openHAB, I wanted something I can control. The only thing around at the time was my Colorific RGB bulb I was playing around with in the Jamrific project. Even though openHAB doesn't support this bulb directly, I can still make it work since it is so flexible. One of the available bindings is the EXEC which can call a system command or shell script. Perfect!






First thing was to update the Python script to control the bulb so that it will feedback to openHAB. As before, you will need Bluez for BT 4.0 support. You can use the same directions I initially used for Jamrific from Adafruit. This could work for other BT RGB bulbs as well but I haven't tried it yet. There are some WiFi ones I would like to play with too.

Once the basic communication is working with the bulb we can add it to openHAB. First copy the Python code to the openHAB configuration folder or you could use your home folder. In Linux, that would be /usr/share/openhab/configuration. The code can be found on github.com

https://github.com/FriedCircuits/Home_Automation/tree/master/openHAB/Colorific


Then edit or create a new items file and add the following filing in your MAC of your bulb.

Items
 
  //Colorific RGB Bulb
  Color colorific "Color" (colorize) {exec=">[OFF:sudo /usr/bin/python /usr/share/openhab/configuration/colorific.py B4:99:4C:64:80:3E 0 0 0] >[ON:sudo /usr/bin/python /usr/share/configuration/colorific.py B4:99:4C:64:80:3E 255 255 255]"}
 
 


This gives the basic ON/OFF functionality. Of course we want to control color! That has to be done via a rule. You could add a simple Switch to the sitemap to make sure you can at least talk to the bulb before you try and add color control.

I created the following rule that gets the RGB values and sends them to the bulb. I need to work on retires after a failure. Since there is no feedback the system doesn't know if it was actually set other the message returned by the BT Gatt tool. The retry logic will be in the Python code.

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

rule "Set Colorific Bulb Color"
when
        Item colorific changed
then
        val hsbValue = colorific.state as HSBType
        val brightness = hsbValue.brightness.intValue
        val redValue   = ((((hsbValue.red.intValue * 255) / 100) * brightness) / 100).toString
        val greenValue = ((((hsbValue.green.intValue * 255) / 100) * brightness) / 100).toString
        val blueValue  = ((((hsbValue.blue.intValue *255) / 100) * brightness) / 100).toString
        logWarn("Red", redValue)
        logWarn("Green", greenValue)
        logWarn("Blue", blueValue)

        var String cmd = 'sudo /usr/bin/python /usr/share/openhab/configuration/colorific.py B4:99:4C:64:80:3E ' + redValue + ' ' + greenValue + ' ' + blue + blueValue

        executeCommandLine(cmd)
end
 
 

I left the logWarn() calls for debugging. You can check the openhab.log in /var/log/openhab to make sure the rule is being called.

Finally, we need the sitemap to control the light. Originally I had both the simple ON/OFF and color picker. Now that it is working all I need is the color picker. Having both helps for testing.

Here is that part of my sitemap.

Sitemap
 
 Frame label="Livingroom" icon="sofa" {
                Colorpicker item=colorific label="Colorific" icon="slider"
}
 
 

That is pretty much it. Now you could write some rules to have the bulb respond to events such as weather or notifications. I think it would be neat to have it set to some rainfall, although it's a scarcity here in California!



Here are some links I used while working on this that you may find useful.

http://www.element14.com/community/groups/arduino/blog/2014/12/23/christmas-tree-internet-of-holiday-lights--control-from-openhab
http://www.rapidtables.com/convert/color/rgb-to-hsv.htm




No comments:

Post a Comment