Previous posts for this project:

 

 

Introduction

 

This week, I continued my adventures with openHAB and worked on controlling some remote controlled power sockets and visualising a live feed with the Pi camera.

 

As always, you can find links to previous parts of this project at the top of this page.

 

Elro CoCo

 

RF433MHz Control

 

I have some Elro CoCo (Click On, Click Off) remote controlled power sockets that work on 433MHz.

photo (1).JPG

 

I've previously made a small board for my Pi in order to control a 433MHz transmitter via an ATtiny85 over I2C.

photo 2.JPG

 

To control this, I wrote a small Python script which will send commands to the ATtiny, which in turn will send out the correct codes to control the power sockets.

 

The script looks like this:

 

#!/usr/bin/env python

import smbus
import sys

bus = smbus.SMBus(1)
address = 38

house = int(sys.argv[1])
unit = int(sys.argv[2])
on = int(sys.argv[3])

bus.write_byte(address, house)
bus.write_byte(address, unit)
bus.write_byte(address, on)






















 

The code on the ATtiny85 is also very simple:

 

#include "TinyWireS.h"                  // wrapper class for I2C slave routines
#include "RemoteSwitch.h"

#define I2C_SLAVE_ADDR  0x26            // i2c slave address (38)
KaKuSwitch kaKuSwitch(1);               // pin 1

void setup(){
  TinyWireS.begin(I2C_SLAVE_ADDR);      // init I2C Slave mode
}

void loop(){
  if (TinyWireS.available()){           // got I2C input
    char house = TinyWireS.receive();
    int unit = TinyWireS.receive();
    int on = TinyWireS.receive();

    onOff(house,unit,on);
  }
}

void onOff(char house, int unit, int on){
  kaKuSwitch.sendSignal(house,unit,on);
}

 

Setting up I2C

 

Because my little board uses I2C to communicate from Pi to ATtiny, I2C needs to be enabled before I can use it.

 

There is a great tutorial over at Adafruit on how to set up I2C on the Raspberry Pi: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c

 

I followed the instructions to enable I2C on my Pi and verified my board was detected properly:

 

pi@webserver ~ $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- 26 -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --


























 

After that, I quickly tested if everything was working as expected, by sending commands to set a socket off and on.

 

pi@webserver ~ $ sudo ./rf433.py 67 2 0
pi@webserver ~ $ sudo ./rf433.py 67 2 1


























 

 

PiRack

 

With two modules to connect to the Pi, the PiRack seemed like the obvious solution.

I plugged in both boards on the PiRack and connected it to the Pi.

 

As the EnOceanPi uses UART and my board I2C, there should be no conflicts in GPIO pins used.

 

photo 2.JPGphoto 1.JPG

 

I quickly tested the EnOcean module and my RF433 module and both still worked. Great!

 

 

Adding to OpenHAB

 

There is a general purpose binding for OpenHAB, able to execute any command. I thought I'd use this to control my wireless power sockets with my custom Python script.

 

More information on the Exec binding can be found here: https://github.com/openhab/openhab/wiki/Exec-Binding

 

Items

 

I've created four switches, each using the exec binding. The only difference, is the ID of the switch being passed to the Python script.

A Python call is specified to turn the socket ON and a second call is specified to turn it OFF.

 

//Exec Bindings
Switch Elro_socket_C1 "Exec" (exec) {exec=">[ON:/bin/sh@@-c@@/home/pi/rf433.py 67 1 1] >[OFF:/bin/sh@@-c@@/home/pi/rf433.py 67 1 0]"}
Switch Elro_socket_C2 "Exec" (exec) {exec=">[ON:/bin/sh@@-c@@/home/pi/rf433.py 67 2 1] >[OFF:/bin/sh@@-c@@/home/pi/rf433.py 67 2 0]"}
Switch Elro_socket_C3 "Exec" (exec) {exec=">[ON:/bin/sh@@-c@@/home/pi/rf433.py 67 3 1] >[OFF:/bin/sh@@-c@@/home/pi/rf433.py 67 3 0]"}
Switch Elro_socket_C4 "Exec" (exec) {exec=">[ON:/bin/sh@@-c@@/home/pi/rf433.py 67 4 1] >[OFF:/bin/sh@@-c@@/home/pi/rf433.py 67 4 0]"}


























 

Sitemap

 

The sitemap defined below will display the four switches with their proper name.

 

Frame label="Elro" {
  Switch item=Elro_socket_C1 label="Living Room - TV"
  Switch item=Elro_socket_C2 label="Living Room - Internet"
  Switch item=Elro_socket_C3 label="Office"
  Switch item=Elro_socket_C4 label="Bedroom"
}


























 

Rules

 

One of the features I wanted to have as part of this challenge, was a "master switch": one switch capable of turning everything ON or OFF.

To do this, I created a rule which will apply the state of my EnOcean switch to my Elro sockets.

 

The rule is simple:

 

rule "Master switch"
when 
  Item EnOcean_switch_00298B1A changed
then
  var state = EnOcean_switch_00298B1A.state as OnOffType

    Elro_socket_C1.send(state)
    Elro_socket_C2.send(state)
    Elro_socket_C3.send(state)
    Elro_socket_C4.send(state)
end













 

Testing with OpenHAB

 

First I tested controlling the sockets directly from the openHAB GUI, just to verify everything is set up correctly:

 

 

 

Next, I tested controlling the sockets using the EnOcean switch. To do this, I used the rule defined earlier to apply the same state as the switch to the sockets.

 

 

Note that in the videos, only a single Elro socket was configured. I later extended this to four before documenting this blog post.

 

 

Pi Camera

 

I didn't originally intend to use a camera in this project. After posting my project idea, michaelwylie suggested to have a two-way communication system with the cats.

I won't go that far (unless I have a lot of time left at the end of the project), but having the camera and being able to see the cats remotely is already a great feature.

 

Enabling the camera

 

The first steps to use the camera, are:

  • connect the camera (yes, really ...)
  • enable the camera via raspi-config

Screen Shot 2014-08-07 at 19.29.26.png

The raspi-config tool can be accessed by executing the following on the CLI:

 

pi@webserver ~ $ sudo raspi-config





 

After enabling the camera, you will be prompted to reboot the Pi.

 

 

Install motion

 

In order to have a view on my cats from within OpenHAB, I plan on using the Pi Camera in combination with the “motion” application.

 

This application makes use of the camera to generate an MJPEG stream and make it available through a simple url. Motion can be configuredto specify resolution, bitrate, etc ...

 

There was a post on raspberrypi.org a while ago where someone used the Pi, in combination with the camera and motion to create an affordable security system.

Instructions were clearly documented and easy to follow and can be found here: http://www.codeproject.com/Articles/665518/Raspberry-Pi-as-low-cost-HD-surveillance-camera

 

Using those instructions, I managed to set up motion properly. The installation and configuration were tested by accessing the live stream via http://192.168.0.205:8081/.

CaTS-Cam.png

Stream in OpenHAB


With the MJPEG stream set up, it is now very easy to add it to OpenHAB.


To do this, I edited the sitemaps and added a video and specified the encoding:

sitemap demo label="Main Menu" {
     Frame label="Camera" {
          Text item=Camera {
               Frame {
                    Video url="http://localhost:8081" encoding="mjpeg"
               }
          }  
     }
}






 

 

And there it was, my camera stream from within OpenHAB:

openhab-cam.png

 

 

I'm really enjoying how easy openHAB is and can be extended to do what you want it to do.