In an earlier post, I showed how I installed the open source openHAB home automation system (see Installing openHAB Home Automation on Raspberry Pi). In this post, I'll show you how to control a local LED on the Raspberry Pi with openHAB and how to control any GPIO pin on the Raspberry Pi by remote.

Controlling the LED with openHAB

Of course the LED is just a good demonstration for doing other things with GPIO, not only to write GPIO values (solenoid, LED), but as well to read in values (e.g. from a push button).

Items, Sitemaps, Rules, Persistence, Transform

In the previous post Installing openHAB Home Automation on Raspberry Pi, I used a demo openHAB environment to verify things are working properly. But, the real fun I plan on having with openHAB is to build up my own home automation system. For this I need to know a bit about the entities in openHAB:

Items are things I can use and interact with in my system: a device, sensor, or an information event. It does not have to be a physical device, e.g. it could be a web source, a weather forecast, or a stock price item. Items are stored in an inventory file inside the /opt/openhab/configurations/items.

Sitemaps are used to arrange items so they can be displayed (i.e. in web browser or smartphone/tablet app). It defines the layout of the items to be shown. Sitemaps allows me to group and organize items too.

Rules are the heart of any automation system. They define schedules or tell what has to happen (i.e. if a sensor reaches a certain value). Rules can define time-triggered events as well (i.e. to turn off lights after midnight automatically if no person is present in a room).

Persistence is an advanced topic—with persistence, data values can be stored and shown (i.e. over time). Things like outdoor temperature can be measured. We can define what is stored, in which format, and how frequently.

Transform contains mappings for data to labels. For example, I can define "comfort" if the room temperaturure is between 20°C and 22°C with a humidity between 70% and 80%.

The Sitemap and Items are essential for openHAB and are covered in this article. The other items are something for future posts.

In this article, I’m going to use a sitemap with an item to control an LED, attached to a GPIO pin on the Raspberry Pi.

Items File

Create a new empty home.items file in /opt/openhab/configurations/items:

New empty home.items file

Sitemap File

Create a new sitemap file home.sitemap in /opt/openhab/configurations/sitemaps:

New home sitemap

With the following skeleton:

sitemap home label="Home"
{
}

openHAB will automatically detect that new sitemap, and I can inspect it in my browser (replace 192.168.0.108 with your IP address):

http://192.168.0.108:8080/openhab.app?sitemap=home

New Sitemap in browser

But, as the sitemap is empty, it does not show any items.

Adding LED Switch

So, let’s add a simple LED that is able to turn on/off on the Raspberry. For this, I add this to the home.items file:

I have attached an LED with the cathode to GND (pin 9) and with the anode to GPIO 4 (pin 7). Between the cathode and GND, I used a 150 Ohm current limiting resistor (any other value somewhat between 100–600 Ohm should do it as well):

LED Wiring

One thing is still missing: the openHAB bindings. Bindings are kind of device drivers used by openHAB to send and receive messages. To have the LED with GPIO working, I need to add the following two to the /opt/openhab/addons folder:

org.openhab.io.gpio*

org.openhab.binding.gpio*

Binding Files for GPIO

Now, I can control the LED on the Raspberry Pi from the Raspberry itself, or any web browser connected to it (e.g. from a smart phone or tablet):

Controlling the LED with openHAB

Summary

openHAB uses a "sitemap" to represent the controls to the user. The controls are stored as "items". One very simple item is the GPIO item which can be used as a switch to turn things on/off. The "bindings" act as device drivers and are used to make the connection between the items and the hardware. That way it is very easy to build up an open source and versatile home automation system. With this I have a way to control GPIO pins of the Raspberry Pi. Of course it does not stop here... I could use pins as inputs (e.g. with push buttons) and attach external devices on a bus, define rules and other advanced automation tasks, etc. But these will be the subjects of further posts.