Controlling lights with openHAB, Raspberry Pi and 433Mhz remote switches

In this post we will turn lamps – connected to a remote switch – on and off via openHAB.

Used components

The following components have been used to realize the light control:

  • Raspberry Pi (35 Euro) with openHAB installed
  • Elro Wireless Home Control AB440S/3C (3 Remote controlled power switches, 15 Euro)
  • 433Mhz RF Transmitter Module (2 Euro)
  • Some jumper cables & wire as antenna

Hardware setup

First, the RF Transmitter Module needs to be installed on the Raspberry Pi. Check your RF Module and Raspberry Pi for the correct connections (I will not go into detail here as a lot of tutorials are already available online).

The RF Transmitter module has three pins: Ground, VCC and Data. In my case, I needed to connect Data with GPIO 17:

Raspberry Pi with 433 MHz Transmitter

I also soldered an antenna to the transmitter which tremendously increased the range.

Software installation/configuration

First, we need to install wiringPi (library to access the GPIO pins of the Raspberry Pi) as it is a prerequisite for rcswitch-pi (library to talk to the RF Transmitter):

sudo mkdir /opt/rc-switch
cd /opt/rc-switch
sudo git clone git://git.drogon.net/wiringPi
cd wiringPi
sudo ./build

Now we install rcswitch-pi:

cd /opt/rc-switch
sudo git clone https://github.com/r10r/rcswitch-pi.git
cd rcswitch-pi
sudo make

Afterwards, we should be able to trigger the remote switch via

sudo ./send 11111 3 1

where 11111 is the System-Code and 3 is the unit-code according to our DIP-switch settings on the remote power switch to be controlled. The last parameter is 1 (ON) or 0 (OFF).

ELRO Remote Switch DIP Settings

OpenHAB configuration

exec binding

First, if not done already, we need to copy the exec binding (included in the openHAB addons package) to our openHAB/addons directory. This binding enables us to later call the command line (and therefore rc-switch) from openHAB.

Switch scripts

Now we can create two shell scripts in folder /opt/rc-switch/ to be called from openHAB: switchOn.sh and switchOff.sh:

#!/bin/bash
for run in {1..5}
do
/opt/rc-switch/rcswitch-pi/send 11111 $1 1
done

And also make them executable:

sudo chmod +x switchOn.sh switchOff.sh

The unit-code is passed as the first parameter ($1) to the script. The system-code (11111) is hard-coded. If we’d have more than one system-code, we need to pass it as a parameter, too.

On the one hand, these two scripts simplify calls from openHAB (as we will see soon). On the other hand, it increases the chance for successfully transmitting the command by sending it multiple (5) times. As there is no message to acknowlegde retrieval of the command by the remote switch and messages sometimes get lost/distorted, this is probably the easiest way to make sure the switch command gets through eventually.

Allow execution of the switch scripts as root

If we run openHAB as user ‘openhab’ (i.e. not as root), we need to make sure user ‘openhab’ is allowed to run the switch scripts as root without the need to enter a password, because wiringPi needs to be run as root.

One way to achieve this is to run sudo visudo and allow user openhab to run scripts on path /opt/rc-switch/switch* (which includes switchOn.sh and switchOff.sh) as root without entering a password:

# User privilege specification
root    ALL=(ALL:ALL) ALL
openhab ALL=NOPASSWD: /opt/rc-switch/switch*

Be careful when using visudo as it is easy to mess up the complete system!

Add openHAB item

Finally it is time to add a switch to openHAB to turn our lights on and off! With all the preparation we have done above, this is quite simple now:

Switch Lamp_LV_Standing "Standing Lamp" {exec=">[OFF:sudo /opt/rc-switch/switchOff.sh 1] >[ON:sudo /opt/rc-switch/switchOn.sh 1]"}

This will add an item to openHAB to turn our remote switch with unit-code 1 on and off. After adding the item to our sitemap, the switch should appear in openHAB:

openHAB Light Items

Disadvantages

Even though this solution is a great way to add smart lamps to one’s home, it has two disadvantages:

  1. One-way communication: The communication is only from the RF Transmitter / Raspberry Pi to the power switch. We neither get feedback if the light on/off signal really reached the power switch nor it is possible to check the current state of the remote switch. – No security (can be controlled from the outside / from neighbours)
  2. No Security: The communication channel is not secure, which means anybody who is in transmission range and knows/guesses the system and unit code of the power switches can switch our lights on and off. This might be acceptable for LED lamps, but I wouldn’t connect devices like an electricity heater.

Summary

All in all, if one can live with these disadvantages and the manual configuration, this is probably the cheapest solution for smart lamps and a great way to get started with a smart home!

Here is a video with the smart lamps in action, controlled via HABDroid (Android mobile client for openHAB):