Showing posts with label Binding. Show all posts
Showing posts with label Binding. Show all posts

Sunday, 4 June 2017

HOME AUTOMATION: Using the Black Bean RM Mini 3 with openHAB

While there are a number of options out there for controlling infrared appliances like your TV and Air conditioner from your smartphone, none have come in at the price point of the Broadlink Black Bean RM Mini 3. I purchased 3 of the units from gearbest on flash sale for 12.89USD each with plans to use them only with the Broadlink app.

The app got old pretty quick so I started to dig in to openHAB integration. While there isn't an official openHAB binding for the devices there are a few python libraries out there for controlling the black bean. Combine those python scripts with the Exec binding and we can roll our own openHAB solution.


Requirements



Python Library Setup

Go ahead and configure the RM Mini devices per the Broadlink instructions, then set a static IP on your router for each of the devices. We need to do this because the python control script reference the device by IP address. 

Install the python-broadlink library and test the basic functions to ensure the library is working. 
 cd ~/addons/  
 git clone https://github.com/mjg59/python-broadlink.git  
 cd python-broadlink  
 python setup.py build  
 python setup.py install  

Test the installation by running python in the command line and testing a few of the functions in the readme:
 python  
 >>> import broadlink  
 >>> print broadlink.discover(timeout=5)  
 [<broadlink.rm instance at 0x7661XXXX>, <broadlink.rm instance at 0x7661XXXX>, <broadlink.rm instance at 0x7661XXXX>]  
 >>> quit()  

The above confirms that the library found 3 devices on my network.


Now that we have confirmed the python-broadlink library is working, install the blackbeancontrol library:
 cd ~/addons/  
 git clone https://github.com/davorf/BlackBeanControl  
 cd BlackBeanControl  

Test the library is working as expected by first configuring a default unit in the BlackBeanControl.ini and then attempting to learn a command. If the command argument name exists then it will be executed by the device, otherwise it will put the device in to the leaning state and wait to lean the command and save it for future use:
 python BlackBeanControl.py -c LG_TV_Vol_Down

If everything went to plan you should have seen the device go in to learning mode, learn the command, then if running the same command again you should have seen the command executed.
Next step is to pair this python script with the exec binding to enable control from openHAB.


openHAB Setup

Depending on how you would like to control your RM Mini from openHAB there are a few ways to configure your items and sitemap. I've previously written up a few examples on this topic.

Typically I want my IR devices to function like a push button remote so I use items and rules to create my buttons (as opposed to things and items with the exec binding).

Item
String TV_GF_Power "TV Power"

Rules
 rule "LG TV Power"  
 when  
     Item TV_GF_Power received update  
 then  
     executeCommandLine("python /home/openhabian/addons/BlackBeanControl/BlackBeanControl.py -c LG_TV_Power -d Lounge", 1000)  
 end  

Sitemap
Switch item=TV_GF_Power mappings=[OFF="Power"]  

Sunday, 16 April 2017

HOME AUTOMATION: Teptron Move control from OpenHAB

I've previously explored how to control the bluetooth version of Teptron Move from a linux PC (or Raspberry Pi) with the built in Bluetooth adapter. While it's interesting to be able to do this from the Linux commands line it's not particularly practical for use in a home automation system. In this post

I'll explain how to integrate those controls with openHAB for more practical use.


OpenHAB Binding Configuration

In order to run a linux script from within openHAB we need to first install the "Exec Binding".

Exec Binding Installation


There are a few examples on how to configure the exec binding but none of the examples worked for me. I ended up following this post to get the right combination of Things/Items/Sitemaps working to give the following result:


I'll go in to some more detail below on how I configured my Things/Items/Sitemap to get the units working.


There are a few peculiarities in working with the Bluetooth version of the Teptron Move with the command line functions. I've noticed the following inconsistencies:

  • Sometimes the device does not respond and the command reports an error
  • Other times the device responds and accepts the commands but does not move
  • The time between receiving the accepted response and the device moving varies from immediate to ~5 seconds

I've tried a number of workarounds for this but so far I've had the most success with simply just retrying the command multiple times, but this has the issue of pausing the unit motion when the second/third command is resent. I'm not ready to give up on this one just yet, but I'm fast running out of ideas.


Move Control Script

Create a new script in the in the openHAB scripts folder:
 sudo nano /etc/openhab/scripts/teptron_move_control.sh  

Copy the following lines in to the file and save the file:
 #!/bin/sh  
 RESPONSE=""  
   
 # execute the command multiple times  
 #for i in 1 2 3  
 for i in 1  
 do  
  RESPONSE="$(/home/openhabian/development/csrmesh/bin/csrmesh-cli move --pin 8888 --dest 43:c5:5b:04:00:06 --objid $1 --position $2)"  
  echo "Response: ${RESPONSE}"  
  sleep 1  
 done  
   

Now make the script executable with the command:
 chmod +x /etc/openhab/scripts/teptron_move_command.sh  

This script accepts two input arguments:
  1. The move unit we want to move
  2. The position to be moved to (0 to 255)
Test the command is working as expected, as an example use the command:
 /etc/openhab/scripts/teptron_move_command.sh 2 255  

This will command unit 2 to move to the fully opened position.

Things Configuration

Create a new .things file for the blinds:
 sudo nano /etc/openhab/things/blinds.things  

Add the following lines:
 Thing exec:command:Blinds_GF_Lounge_North "Blinds" [ command="bash /etc/openhab2/scripts/teptron_move_control.sh 1 %2$s", interval=0, autorun=true ]  
 Thing exec:command:Blinds_GF_Lounge_South "Blinds" [ command="bash /etc/openhab2/scripts/teptron_move_control.sh 2 %2$s", interval=0, autorun=true ]  

The important part of the command here is the  name of the Thing, in this case "Blinds_GF_Lounge_North" and "Blinds_GF_Lounge_South". These will need to match the name of the items we will create next.


Items Configuration

Next we must define items that will take the input from the sitemap and send it as a parameter to the exec binding and execute the script:

Create a new .items file:
 sudo nano /etc/openhab/items/blinds.items  

Add the following items:
 String Blinds_GF_Lounge_North_Command "Blinds" { channel="exec:command:Blinds_GF_Lounge_North:input", autoupdate="false" }  
 String Blinds_GF_Lounge_South_Command "Blinds" { channel="exec:command:Blinds_GF_Lounge_South:input", autoupdate="false" }  

Ensure the names of the defined items match the names of the things we defined previously.

Sitemap Configuration

We need to define a couple of switches with custom mapping to command the move units to the "Open" and "Close" positions:
 sudo nano /etc/openhab/sitemaps/default.sitemap  

Add the following lines where applicable:
 Switch item=Blinds_GF_Lounge_North_Command label="North Blinds" mappings=["255"="Open", "0"="Close"]  
 Switch item=Blinds_GF_Lounge_South_Command label="South Blinds" mappings=["255"="Open", "0"="Close"]  

If everything was working properly, pressing the buttons on the sitemap should command the move units to the open and closed positions.



Technically it should be possible to use a slider here to set the blind position, but I'm yet to work out how to do it.

Remember the following command is great to be able to debug any openHAB issues:
 tail -f /var/log/openhab2/openhab.log -f /var/log/openhab2/events.log   

Monday, 27 March 2017

HOME AUTOMATION: OpenHAB2 Serial Binding on Raspberry Pi

I have always wanted to enable my home automation setup with per-room presence detection. With this in mind I purchased and installed an ELK/NESS home alarm with automation capability.

I haven't been able to find any decent OpenHAB binding for the alarm, and after recently giving up on getting my M1XEP serial to network box for my alarm working I've decided to try my hand at rolling my own serial solution. Here I'll discuss how I got the serial configuration up and running.

Installation

Install the binding from the configuration GUI


I'm using a USB to Serial adapter, but other serial devices should also show up as serial devices if they have been recognised by the kernel. You can check this with the command:
 dmesg  

Or to view messages relating to the serial port configuration use the command:
 dmesg | grep tty
This should tell you what port was assigned to your USB to Serial adapter, in my case ttyUSB0

Check which users have access to the serial port with the command:
 ll /dev/ttyUSB0  

Output should look something like:
 crw-rw---- 1 root dialout 188, 0 Mar 27 10:03 /dev/ttyUSB0  

You may need to add the default user to the dialout group:
 sudo adduser openhabian dialout  


It's a good time to check the serial connection to make sure it's working as expected. You can use the GNU screen command to view the serial port from the command line:
 screen /dev/ttyUSB0 115200  

The screen command won't exit with the typical commands like CTRL-X or CRTL-Z, to exit the screen command enter 'CTRL-a' then press '\', then hit 'Y' to exit the screen.

Now that we have the binding installed and confirmed the serial port is working as expected we can continue with the OpenHAB setup

OpenHAB2 Configuration 

I created and 'alarm.items' file to store alarm related configuration info:
 Switch AlarmUpdate "Alarm Update" { serial="/dev/ttyUSB0@115200" }  
 String AlarmMessage "Alarm Message" { serial="/dev/ttyUSB0@115200" }  

and a 'alarm.rules' file with the following:
 ule "Interpret Alarm Command"  
 when  
     Item AlarmMessage received update  
 then  
     logDebug("alarm", "Alarm message updated")  
 end  
   

Check everything is working with the command:
 tail -f /var/log/openhab2/openhab.log -f /var/log/openhab2/events.log  

And you should see log messages like the following:
 2017-03-27 12:08:27.946 [ItemStateChangedEvent   ] - AlarmMessage changed from 0AZC007200C9  
 1EAS000000001111111100000000000E  
 0CAM000000007F  
  to 16XK2708212270117011006C  

Now that the serial binding is working we can move on to interpreting the data!