OpenHAB on RaspberryPI

RaspberryPi (Courtesy: Wikipedia)
RaspberryPi (Courtesy: Wikipedia)

In my earlier post, Starting off with OpenHABI listed down the steps involved in building a OpenHAB distribution. Since OpenHAB runs on Java, it can run anywhere if built once.  Also in the post, OpenHAB – Working with the GUI, we added a new device to the OpenHAB system, the “Second Floor Corridor Light” and watched it enumerated on the brower GUI. We will treat this device as a LED on RaspberryPi’s GPIO and control it through the OpenHAB GUI.

OpenHAB demo on RaspberryPi

  1. Ensure that Java is installed and JAVA_HOME is setup properly on RaspberryPi (else follow this)
  2. Copy all the 3 distributions of OpenHAB (distribution-1.6.0-SNAPSHOT-runtime.zip, distribution-1.6.0-SNAPSHOT-addons.zip and distribution-1.6.0-SNAPSHOT-demo.zip) from your build machine to Raspberry Pi and unzip them into your local directory as described here. Please note that the sequence of steps is important here.
  3. Now, run start.sh from your $OPENHAB_HOME. Please note that since RaspberryPi is still an under-powered device and it will take a few minutes for OpenHAB to completely initialize. On my device it takes anywhere between 3-4 mins.
  4. Once all the waiting is done, point your browser to the demo URL provided by OpenHAB. If RaspberryPi has an IP address of 192.168.0.104, your new OpenHAB demo URL will be http://192.168.0.104:8080/openhab.app?sitemap=demo#_Home

You should now be seeing the new “Second Floor Corridor Light” device from your RaspberryPi and toggle it on the browser.

Binding OpenHAB device to RaspberryPi GPIO

OpenHAB has a concept of bindings that allow easy addition and control of different types of automation devices. For example, if I need to support NXP light device, I will need to bind it. Technically this translates to, a abstraction code that will allow me to easily add, configure, monitor and generally communicate to this device. For more on binding refer to the OpenHAB website here. I would suggest you read through this link once and then get your hands dirty.

So, for the job at hand, we still need to get to the “Second Floor Corridor Light”. If I need to bind a GPIO present on RaspberryPI to the OpenHAB server running locally, I will need to use a GPIO Binding (for more on this see here). The examples in this link are quite straight forward.

GPIO on RaspberryPI

RaspberryPI provides a series of GPIO pin-outs that can be configured as input as well as outputs (more on RaspberryPI GPIO here) For now, we will choose to use GPIO 17 that can be configured as an output to drive an LED. Please note that

the LEDs already present on the board can be driven too (well with a hack), but they are for special purposes only. We will not be using these. GPIO 17 can be found on physical pin number 11 on the board breakout. Now connect this pin 11 to positive end of an LED on a breadboard with a 1K register in series to ground (physical pin number 25).Your connection should now look like the one in this picture.

GPIO 17 Connection
GPIO 17 Connection

Now to test the LED, we will use the “gpio” kernel device driver that is provided with the Raspberry distribution (more on this here) For a quick LED toggle, follow the following steps,

  1. cd /sys/class/gpio
  2. echo 17 > export
  3. echo out > gpio17/direction
  4. echo 1 > gpio17/value
  5. echo 0 > gpio17/value
  6. echo 17 > unexport

After step 4, you should be able to see the LED glow. Please note that step 6 is an essential step to ensure that any other application can use GPIO 17.

GPIO Binding

In the earlier post, we made changes to the OpenHAB configuration to add a new “Second Floor Corridor Light” device. The changes to the demo.items file looked like this,

Switch Light_SF_Corridor_Ceiling “Corridor” (SF_Corridor, Lights)

Now at the end of the above line, add the device binding to GPIO 17. The change should look like this,

Switch Light_SF_Corridor_Ceiling “Corridor” (SF_Corridor, Lights) { gpio=”pin:17″ }

Now start the OpenHAB server and wait for the URL to come up. Once you are able to view the URL, you will be able to toggle the “Second Floor Corridor Light” as shown in the video.

As you can see, binding makes configuring OpenHAB a lot easier and maintainable. However, this was an example of local binding where the device to be controlled was directly connected. However, in reality, devices in home automation are spread across different places in your home. This necessities binding to work over the access network (like WLAN) as well. The next step in this project is to head towards defining my own binding for my custom device.

 Update:

OpenHAB doesn’t cleanup well in case you exit from the console. Due to this, the exported GPIOs will not be unexported. Hence the next time you start OpenHAB, it will find the GPIO busy. For now, the workaround is to ensure that the GPIOs are unexported before you start OpenHAB. More on this issue in this thread here, GPIO Binding problem.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s