Tuesday, 20 June 2017

HOME AUTOMATION: Google Home Assistant + Say a simple phrase with a number + openHAB

I've spent a bit of time integrating IR blasters in to my openHAB setup to gain control of my TV and AC. I was able to get simple functions working with the basic "Say a simple phrase" but this wasn't cutting it for something like controlling the volume of the TV so I've written a script that executes a command a given number of times based on the "Say a simple phrase with a number" action in IFTTT. This way I can say "Turn up the TV volume by 10".

 #!/bin/sh  
 # execute the command multiple times based on command line argument  
 for i in $(seq 1 $1); do  
  python /home/openhabian/addons/BlackBeanControl/BlackBeanControl.py -c LG_TV_V$  
 done  

I put my scripts in the /etc/openhab2/scripts folder.
You will need to create a thing/item combination that uses the Exec binding as I've done here.
To test the command is working as expected use the command:

 bash /etc/openhab2/scripts/tv_lounge_volume_down.sh 5  

If everything went well you should see your command executed 5 times. Pair this with my instructions on connecting openHAB with the Google Home Assistant and you should be set.

For reference this is what my applet looks like at the command end.


HOME AUTOMATION: Voice control with Google Home + IFTTT + myopenHAB

Recently I purchased a Google Home Assistant and it's been a lot of fun to play with the native features like shopping lists, reminds and Chromecast control I was really excited to dig in to getting the Home Assistant tied in to my openHAB setup. While the HA does not native support openHAB, it does support IFTTT and now openHAB also has added support for IFTTT through myopenHAB.
I followed the steps below to get my setup working.


Install the "openHAB Cloud Connector" Add-on

I've already covered this in a previous post, make sure myopenHAB is set up correctly and ensure you have exposed the items you want to connect with your HA.

Note: I've seen it take up to 36 hours for settings updated from the cloud connect plugin to appear in the IFTTT actions menu


IFTTT Integration

Register for IFTTT and create a custom applet by selecting the "My Applets" link

IFTTT home page



Create a new applet and click on the blue "+ this" button. This was confusing to me at first because I don't think it looks much like a link/button.

Select "New Applet"

Click the "+ this" button


Filter the available connections by typing "google" and select the "Google Assistant" service.
There are a number of options to choose from with the Google Assistant service, the simplest applet would use the "Say a simple phrase" function.



It's important to think about the various ways you might want to say the command you wish to execute. I've had to change a number of my applets because what I thought was natural when making the applet didn't roll off the tongue when I was sitting on the couch.


Next thing to do is to set the "that" part of IFTTT by clicking the "+ that" button and filtering the services for openHAB. Once the openHAB service is selected there is only one action, so no place to go wrong here.



Now select the item that was exposed through the openhab Cloud Connector Add-on. Once again, if your items aren't showing up you may need to give it more time to be updated. I've seen it take up t 36 hours.


That's it! Finish off the applet creation, give it a few seconds and test it out.

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"]  

Friday, 2 June 2017

HOME AUTOMATION: openHAB and myopenhab - Accessing your openHAB installation from the web

I've been thinking about the best way to access my openHAB installation from outside my network for a little while, but I've always been worried about the safety of exposing my network to the world so I never did it. Luckily though the folks at openHAB have created a free way to access your openHAB installation via the web without having to deal with port forwarding, firewalls and VPNs, This service is called myopenHAB.

My main reasons for enabling this feature is for integration with cloud services like IFTTT, which I am using for integration with the Google Home Assistant. I'll share more on that topic in another post.


Plugin Installation

Log in to your local openHAB installation and navigate to the PaperUI interfact, From here navigate to the Add-ons tab and search for "openHAB Cloud Connector" in the MISC page of add-ons.


Installation takes while so be patient. It took around 20 minutes on my Raspberry Pi 3.
While we are waiting for the add-on to install we will get our myopenHAB account setup and ready for action.

myopenHAB Account Setup

Navigate to the myopenHAB website and then make your way to the registration page. Create your account and be sure to verify your account once created. You will be sent a verification email to the registered email address, follow the links to confirm your account. If you don't do this you may end up stuck with your device only ever showing "Offline" even if you have everything configured correctly.
Once you have setup you account check back in to your openHAB add-on installation. Once it is complete we need to grab a couple of auto-generated strings to link our openHAB add-on installation to the cloud service. We need to grab the UUID and Secret that were generated by the openHAB Cloud add-in and add them to our account:


Use the following commands to view your UUID and Secret from the command line:
 nano /var/lib/openhab2/uuid  
 nano /var/lib/openhab2/openhabcloud/secret  

Once these settings have been added to myopenHAB if everything was done right you should see your device come online. If not, there are a few things that could have gone wrong that I'll cover in the troubleshooting section below.


Add-on Configuration

With the add-on installed the last thing we need to do is enable remote access of our device and exposing items to 3rd party services like IFTTT
openHAB Cloud add-on configuration


Add the items you would like to be exposed to external services like IFTTT here by selecting the items from the drop down box and save your configuration.

If it has all worked you should now be able to navigate to the myopenHAB page, login and then follow the links to your openHAB dashboard.

myopenHAB logged in


Troubleshooting

I ran in to a few issues when connecting my openHAB installation to the myopenHAB cloud service:

  1. I had missed a character off the secret. 
  2. I was running an old version of oracle java. I fixed this by simply doing an apt-get update then apt-get upgrade followed by a reboot. There is also another issue with open-jdk that is described in this post

HOME AUTOMATION: openHAB exec2 binding

Since I started with openHAB I've been annoyed that it's not super simple to execute a script with a button press. There is an openHAB2 binding called the "Exec" binding that allows us to do exactly this, but the use of it is a mess in my opinion. The official documentation doesn't really give many good examples of how it can be used and trying to find a good example in the forums is hard because there is a openHAB1 and openHAB2 version of the binding that are used differently.

The first useful bit of information I came across was github repo XML definition of the Thing type.
This gives an explanation of what the various parameters and what they do.

Another bit of info that may be useful is how the different elements required for the Exec binding to work tie together:
    One Thing tied to a command we want to run
          Multiple Items for each Thing (input, output, exit, run, lastexecution)
              A sitemap that displays any Items we want to interact with

Armed with this info lets take a look at a few examples of using the Exec binding:


Example 1

This example executes a python command when the defined button changes states. It does not resend the command if the button is in the OFF state and it is turned OFF again.

Thing:

 Thing exec:command:TV_GF_Vol_Up "TV" [ command="python /home/openhabian/addons/BlackBeanControl/BlackBeanControl.py -c LG_TV_Vol_Up -d Lounge", timeout=15, interval=0, autorun=true ]  
 Thing exec:command:TV_GF_Vol_Down "TV" [ command="python /home/openhabian/addons/BlackBeanControl/BlackBeanControl.py -c LG_TV_Vol_Down -d Lounge", timeout=15, interval=0, autorun=true ]  

Item:

 String TV_GF_OnOff "TV" { channel="exec:command:TV_GF_OnOff:input" }  
 String TV_GF_OnOff_Output "TV" { channel="exec:command:TV_GF_OnOff:output" }  
 Switch TV_GF_OnOff_Running "TV" { channel="exec:command:TV_GF_OnOff:run" }  
 Number TV_GF_OnOff_ExitValue "TV" { channel="exec:command:TV_GF_OnOff:exit" }  
 DateTime TV_GF_OnOff_LastExec "TV" { channel="exec:command:TV_GF_OnOff:lastexecution" }  

 String TV_GF_Vol_Up "TV" { channel="exec:command:TV_GF_Vol_Up:input"}  
 String TV_GF_Vol_Up_Output "TV" { channel="exec:command:TV_GF_Vol_Up:output"}  
 Switch TV_GF_Vol_Up_Running "TV" { channel="exec:command:TV_GF_Vol_Up:run"}  
 Number TV_GF_Vol_Up_ExitValue "TV" { channel="exec:command:TV_GF_Vol_Up:exit" }  
 DateTime TV_GF_Vol_Up_LastExec "TV" { channel="exec:command:TV_GF_Vol_Up:lastexecution" }  

 String TV_GF_Vol_Down "TV" { channel="exec:command:TV_GF_Vol_Down:input"}  
 String TV_GF_Vol_Down_Output "TV" { channel="exec:command:TV_GF_Vol_Down:output"}  
 Switch TV_GF_Vol_Down_Running "TV" { channel="exec:command:TV_GF_Vol_Down:run"}  
 Number TV_GF_Vol_Down_ExitValue "TV" { channel="exec:command:TV_GF_Vol_Down:exit" }  
 DateTime TV_GF_Vol_Down_LastExec "TV" { channel="exec:command:TV_GF_Vol_Down:lastexecution" }  

Sitemap:

 Switch item=TV_GF_Vol_Up label="Volume Up" mappings=[OFF="+", ON="+"]  
 Switch item=TV_GF_Vol_Down label="Volume Down" mappings=[OFF="-", ON="-"]  



Example 2

In this example we will create a single button that runs a python script each time the button is pressed.

Things:

Not needed as we will be executing the commands from within the rules we create.

Items:

This example creates a virtual item that is not tied to a Thing.
 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  

Sitemaps:

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


I've also found the following forum examples useful in developing my Exec scripts and functions.
Online example 1Online example 2

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:



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 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   

Saturday, 8 April 2017

ROBOTICS: Android App for Bluetooth Remote Control Robots

A while back I was tinkering with the MIT App Inventor project and wrote a little android app that would connect to a bluetooth device and send ASCII commands via a bluetooth serial connection. It turned out to be a really handy way to control my robots over bluetooth so I shared it and made a short youtube video showing how to use it. It's my most successful youtube video to date and I still get people asking for the codes.

Since then I taught myself to code for Android and rewrote the app with plans of releasing it on the app store, but never found the motivation to take it all the way. It's been a couple of years since I put any time in to active development of the app, so in the spirit of sharing the codes I've decided to upload my project to Github for anyone who is interested. To make it easier for use I've also included a compiled apk.


The App

Basically, the app connects to a bluetooth device and creates a serial connection and streams ASCII characters to and from the connected device. Typically the way I use this by assigning specific ASCII characters to buttons, then when coding my android project I interpret the ASCII characters and react based on the command.
Vertical Layout

Landscape Layout

Pair with Bluetooth device

Preferences and Help menu


The app also allows you to change the button labels as well as the characters being sent for each button.

The app requires a minimum of Android 4.0 and has been written to support a number of screen resolutions and configurations.

Features

Inverted button layout switches the position of the direction keys and action keys:





Receive commands from your robot with the Feedback Group scale scale bars. Commands need to be sent over serial in the format "SB1=XX" where XX is a number from 0 to 100.






Known Bugs

Pairing a new bluetooth device is clunky. I've had the most success in performing the pairing from within the settings menu first, then going back in to the app and connecting to the bluetooth device.