Tuesday, 17 September 2013

Home Automation with z-wave on Raspberry pi using openhab - first steps

There are a few diversions on the road to running z-wave of RaspberryPi, so here are some notes on how I did a good working build using:


Why did I choose these?

Raspberry Pi, cos it's the obvious choice as a controller for this type as far as I'm concerned. Pleased with this choice, using a standard phone charger as a PSU, I'm seeing 2 watts at the mains side when it is actively running.

RaZberry daughter board - better answer than a USB dongle as once plugged into the pi, it fits inside a standard Pi case.

openhab software. This looked like a 'proper' ha package, with support for a large variety of hardware as well as the means to grab data from web sites etc, support for Z-Wave only just arrived in the last release (and is still a bit limited), but is pretty active. Although I had some concerns about performance / viability of a Java based solution on a comparatively lightweight computer (and particularly with the limited RAM available), it looked others had already run openhab on RPi successfully, and as I have some experience of Java on android phones I was reasonably confident any problems could be resolved (performance was an issue until I switched to using the Oracle jdk).

Here's how I build it

initial build, Z-Way setup and test

  1. Do a standard rasbian build in whatever way you prefer - see any of the getting started guides.... Just be sure the one you pick has hardware floating point support (the noobs one does).
  2. I then do everything else using nfs and ssh from my laptop, the standard build has ssh  enabled, but this will all work on the console as well. (it's easier with ssh from another PC as you can then cut and paste stuff from other windows - on the pi, I only ever use the console)
    See below for other setup I do for nfs and hdmi...
  3. install the Z-Way package to get started - see the getting started guide at zwave.me (it's at the end of the document).  This is probably the command you want:
    wget -q -O - http://razberry.z-wave.me/install | sudo bash
    restart to get the Z-Wave web interface running

    (tweaks the raspberry config to allow use of the serial interface, allow check all is OK, and also needed to bring in new devices while the razberry binding in openhab evolves a bit further)
  4. On another machine, run up a browser to your rpi port 8083 (as in the getting started info at zwave.me) - and then use the web browser to add your z-wave devices into the z-wave network...
    Record the device and its id as you add each one (you'll need this later)
  5. Under the Network tab, select Network management
  6. Click on (Re-)include device and then press the include button on the z-wave device.  Repeat to include all devices you want to start with
  7. Now go to Device control, select Switches and you should see any switches you have included, and be able to turn them on and off. Any other device types should appear under their appropriate entry in the Device control menu
  8. Finally stop Z-Way and disable the service so it doesn't run on start up anymore
    sudo service Z-Way stop
    sudo update-rc.d Z-Way disable

setting up java and openhab

  1. Install the Oracle JDK 8 as described here - below is what I did to save you digging it out. Although this says it's a developer preview, it's been going for a while now so seems to be pretty stable (and it is about 5 times faster than the default java install - without this version openhab z-wave interface takes so long to process things, operations time out and get retried leading to an ever increasing queue of never ending tasks)
  2. Go to https://jdk8.java.net/download.html , check the license agreement box and copy the url for the linux arm hardFP jdk, then on the rpi shell:
    wget --no-check-certificate <url you just copied>
  3. Now get java set up:
  4. sudo tar zxvf jdk-8-ea*.tar.gz -C /opt
    sudo update-alternatives --install "/usr/bin/java" "java" "/opt/jdk1.8.0/bin/java" 1

    (if you want to use other java versions as well go read the stackoverflow link above)
  5. Use java -version to check all is well, you should get something like:
    java version "1.8.0-ea"
    Java(TM) SE Runtime Environment (build 1.8.0-ea-b106)
    Java HotSpot(TM) Client VM (build 25.0-b48, mixed mode)
  6. Now grab openhab, you need the runtime, addons and demo configuration. While the current version is 1.3 this means:
    mkdir openhab
    cd openhab
    wget http://openhab.googlecode.com/files/openhab-runtime-1.3.0.zip
    wget http://openhab.googlecode.com/files/openhab-addons-1.3.0.zip
    wget http://openhab.googlecode.com/files/openhab-demo-configuration-1.3.0.zip

    unzip openhab-runtime-1.3.0.zip

    we only want the zwave addon from the addons zip so...
    unzip openhab-addons-1.3.0.zip org.openhab.binding.zwave-1.3.0.jar
    mv org.openhab.binding.zwave-1.3.0.jar addons/

    and finally
    unzip openhab-demo-configuration-1.3.0.zip
  7. To prepare for a first test run, we need to setup a config file to tell the zwave binding which serial port to use - create the file configurations/openhab.cfg with:
    ################################ Z-Wave  Binding ######################################
    #
    # The Z-Wave controller port. Valid values are e.g. COM1 for Windows and /dev/ttyS0 or
    # /dev/ttyUSB0 for Linux
    zwave:port=/dev/ttyAMA0

    # Z-Wave binding refresh value (optional, defaults to 10000)
    #zwave:refresh=
    # Z-Wave binding refresh interval threshold (optional, defaults to every 6 times)
    #zwave:refreshThreshold=
  8. and we need to tell java we want to use this port as well: edit openhab.start.sh and add this into the java .... command line
    -Dgnu.io.rxtx.SerialPorts=/dev/ttyAMA0
    remember to leave a space before and after this
  9. Currently the test to speech part is broken on pi (and I'm not going to use it anyway) so delete all the rules with "say" commands in the rules file:
    edit rules/demo.rules and remove all rules that include a "say"
  10. Add the z-wave devices to items in the openhab demo (this is just to check that all is working from a known baseline). This is based on the info in the openhab wiki here. This example is for simple switches. My config came up with the controller as id 1 and two switches, ids 2 & 3..
    edit openhab/configurations/items/demo.items
    append {zwave="<id>"} to a suitable device - in my case
    Switch Light_GF_Kitchen_Ceiling         "Ceiling"               (GF_Kitchen, Lights) {zwave="2"}
    Switch Light_GF_Kitchen_Table           "Table"                 (GF_Kitchen, Lights) {zwave="3"}
  11. now set the script to executable (in the openhab folder)
    chmod ug+x start.sh
    add your current user to the dialout group (assuming you're logged in as pi..):
    sudo usermod -a -G dialout pi
    then logout and in and and give it a whirl
    ./start.sh
  12. after a couple of minutes you should see...
    INFO  o.o.u.w.i.s.WebAppServlet[:99] - Started Classic UI at /openhab.app
    and then some z-wave messages
    INFO  o.o.b.z.i.p.ZWaveController[:627] - Connecting to serial port /dev/ttyAMA0
  13. If you see
    ERROR o.o.b.z.i.p.ZWaveController[:642] - null
    immediately after the connecting message, then zwave hasn't been able to connect to the serial port to talk to the RaZberry board. Probably either the port name is wrong, or permissions are wrong, or another app is using the port. Go and check that Z-Way isn't running:
    ps -ef | grep "z-way"
    should show only 1 line with your command in it. NOT the line:
    root      2805     1  4 13:19 ?        00:00:07 z-way-server
    Check device permissions:
    ls /dev/tt*
    should show lines including
    crw-rw---T 1 root dialout   x,  x mmm dd hh:mm /dev/ttyAMA0
    and the group you are in
    sudo groups pi
    should show dialout somewhere along the way
    Also check that you added the SerialPorts part to the java invocation line.
  14. You should see
    INFO  o.o.b.z.i.p.ZWaveController[:640] - Serial port is initialized
    followed by some lines about z-wave devices being detected.
    INFO  o.o.b.z.i.p.ZWaveController[:473] - Found node id = 2
    This shows things are all working well.
  15. Now on another machine go to rpi IP address port 8080 and the demo site
    (e.g. http://mypi:8080/openhab.app?sitemap=demo
    you should be able to navigate to the kitchen and turn on and switches you added zwave ids to.

Other setup not directly related to getting RaZberry or homehab running

  1. After installing the OS, use noobs to uncomment
    hdmi_safe=1
    if you are using hdmi and you get a blank (no signal) screen. Also you can uncomment
    hdmi_force_hotplug=1
    so you can boot without the monitor conected, but plug it in later
  2. use raspi-config to change password and set machine name
  3. sudo update-rc.d rpcbind enable
  4. sudo apt-get install nfs-kernel-server
  5. add
    /home/pi 192.168.1.0/24(rw,sync,no_subtree_check)
    to /etc/exports
  6. sudo service rpcbind start
  7. sudo service nfs-kernel-server start

 Notes

  1. if you are using an older or otherwise strange hdmi monitor...
  2. just good practice
  3. rpcbind is disabled in the rasbian build I got on my noobs card
  4. just adding the usual nfs server package
  5. adjust the ip address to match your local network
  6. fire up rpcbind
  7. fire up nfs-kernel-server
  8. you could reboot at this stage, just to make sure that nfs comes up OK to avoid confusion and head scratching later!

5 comments:

  1. Thanks so much for posting this. Its been a big help.
    The current Java link is http://www.oracle.com/technetwork/java/javase/downloads/jdk8-arm-downloads-2187472.html
    Followed all of the instructions.
    Had a bit of trouble with untar'ing the .gz file and then found I needed to copy the whole jdl1.8.0 folder to /usr/local and set PATH=/usr/local/jdk1.8.0/bin:$PATH as suggested by Stackexchange.
    Also had permissions problems. Openhab would run ok as root but not as user pi. Had to
    sudo chown -R pi:pi /opt/openhab/
    but then encountered:
    WARN o.o.c.p.e.PersistenceExtensions[:130] - There is no queryable persistence service registered with the name 'rrd4j'
    03:46:11.689 ERROR o.o.b.z.i.p.ZWaveController[:780] - Port /dev/ttyAMA0 does not exist
    Not sure if they are related but the Razberry daughter board definitely connected when z-way-server was running.
    Am I missing something?
    Thanks

    ReplyDelete
  2. Answering my own post - I had to put the parameter -Dgnu.io.rxtx.SerialPorts=/dev/ttyAMA0
    right after the -Dosgi.noShutdown=true parameter.
    Hope this helps some-one else who may read this.

    ReplyDelete
  3. Yeah I also heard that Z-wave is wireless communications protocol which is designed for home automation remote control applications.

    ReplyDelete
  4. Life saver!
    The ' -Dgnu.io.rxtx.SerialPorts=/dev/ttyAMA0' was my missing link :)

    ReplyDelete
  5. I had -Dgnu.io.rxtx.SerialPorts=/dev/ttyAMA0 at the end of my java line and that wasn't working. Moved it right after the -Dosgi.noShutdown=true parameter, and that fixed it.

    ReplyDelete