Introduction: Part 1 OpenHAB and Zwave - Getting Started

***Update 11-13 1930CDT***
I've switch to the Aeon Labs Aeotec Z-Wave Z-Stick, Gen5 Z-Wave Plus - ZW090 due to problems with the one listed below. The issue is that I experience a loss of connection with all my nodes about once a week. I would restart the Pi to resolve the problem. I've posted the issue on the openHAB forums and the consensus is there are known problems with the original stick. I'll let you know the result after a few weeks of testing with the new one.

***Update 11-08-15 1930CDT***

I found that cut and paste through Firefox transferred html code from my Evernote draft. I've switched to IE and fixed the errors.

******* Introduction *******

In this tutorial, I'm showing you how to install and setup openHAB on a Raspberry Pi. I've batted around with the idea of installing openHAB on a small Intel Compute Stick with Windows because I cannot get openHAB to run sound through the USB card on a Pi. I already have the Compute Stick and it works great on another application at my office. Based on what I have read, one can easily move the openHAB to another machine easily. I'll include the instruction for the installation and setup of a USB card with hopes that someone knows how to get it working with openHAB. openHAB has audio bindings that are useful. However, the factory audio port is noisy. USB audio would be much better.

******* Quick Background *******

I prefer openHAB because of it is open source and does not require payments or to log into a web server. The last thing I want is to trust an Internet of Things (IoT) provider with my house. My solution was to setup a VPN server on my PfSense box through OpenVPN. This allows any phone or PC with an OpenVPN client to access the LAN, thus openHAB. My optimal home automation setup is configured so that I have to provide minimal input to make things happen. I don't want to issue voice commands, push a button, or open an app. I still have to do these actions sometimes. However, setting rules (if this - then that) cut down on my input.

******* Minimum Equipment *******

(all links refer to Amazon for simplicity. You may be able to find better pricing at other locations.)

******* A round of thanks *******

  • Eric Tsai - It was Eric's tutorial that inspired me to setup my own openHAB
  • John Wills - I hired John through eLance to help me get started with openHAB
  • The wonderful openHAB Community - This is a collection of many enthusiast eager to help with your specific application.
  • I pulled many of these steps from other posts. I referenced the ones I remember and my apologies to those I missed.

******* Notes *******

  • ALL CAPS text usually is intented for you to change the text to your preference.
  • I find that limiting the openHAB add-ons to the the ones currently in use increases reliability. (more later)This is a basic installation and setup. More detail and cooler automation later.
  • I also use Mosquitto for end nodes. I'll put together another tutorial for this sometime in the future.
  • Be consistent with your battery operated items. I have 3 diffident door/window contacts and 3 different batteries which is a pain in the ass. Your items will go through batteries. So I recommend purchasing nodes with standard AA and AAA batteries so you can recharge them.

Step 1: Raspbian Setup

******* raspbian initial setup ******* (assuming all user as pi)

Language set to US English

Expand file system

(optional) Advanced -> Rename Host

sudo apt-get update
sudo apt-get upgrade

******* raspbian optional NTP update *******

sudo apt-get install ntp 
cd .. 
sudo nano etc/ntp.conf

Update the ntp.conf file with the following.

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: 
# server 0.debian.pool.ntp.org iburst
# server 1.debian.pool.ntp.org iburst
# server 2.debian.pool.ntp.org iburst
# server 3.debian.pool.ntp.org iburst
server 0.us.pool.ntp.org
server 1.us.pool.ntp.org
server 2.us.pool.ntp.org
server 3.us.pool.ntp.org

******* raspbian optional to rename host*********

sudo nano /etc/hosts

update the file with your changes

127.0.1.1 YOURPINAME 

was raspberrypi (default)

sudo nano /etc/hostname

update the file with your changes

YOURPINAME
sudo /etc/init.d/hostname.sh

******* raspbian optional static address*********

(I've found this is requires accuracy and not very reliable. I recommend assigning your Pi a static address through your home Router instead.)

sudo nano /etc/network/interfaces

update the file with your changes

auto lo
iface lo inet loopback
auto eth0
allow-hotplug eth0
iface eth0 inet dhcp
auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
#address XXX.XXX.XXX.XXX
#netmask XXX.XXX.XXX.XXX
#gateway XXX.XXX.XXX.XXX
#network XXX.XXX.XXX.XXX
#broadcast XXX.XXX.XXX.XXX
wpa-ssid YOURSSID
wpa-psk YOURSSIDPASSWORD
sudo service networking restart

*******raspbian optional wifi access*********

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

update the file with your changes

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
        ssid="YOURSSID"
        psk="YOURSSIDPASSWORD"
        proto=RSN
        key_mgmt=WPA-PSK
        pairwise=TKIP
        auth_alg=OPEN
}
sudo reboot

Step 2: OpenHAB Setup

******* openHAB install *******

Details on these steps available here.

sudo mkdir /opt/openhab
cd /opt/openhab
<p>sudo wget <a>  https://bintray.com/artifact/download/openhab/bin...>>

(check current version)

sudo unzip distribution-1.7.1-runtime.zip
sudo rm distribution-1.7.1-runtime.zip
cd addons/

sudo wget https://bintray.com/artifact/download/openhab/bin...

sudo unzip distribution-1.7.1-addons.zip

(these are the add-on I was referring to. You can remove unused add-ons later)

sudo rm distribution-1.7.1-addons.zip
cd ..
sudo cp configurations/openhab_default.cfg configurations/openhab.cfg

Let's set openHAB to auto run based on this post. The default user in this code is "Ben". However, I've never setup users and it doesn't matter in our installation.

cd /opt/openhab
sudo nano /etc/init.d/openhab 

(add this code by pasting into the file.)

#! /bin/sh
### BEGIN INIT INFO # Provides: openhab # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: OpenHAB Daemon ### END INIT INFO # Author: Thomas Brettinger # Do NOT "set -e" # PATH should only include /usr/* if it runs after the mountnfs.sh script PATH=/sbin:/usr/sbin:/bin:/usr/bin DESC="Open Home Automation Bus Daemon" NAME=openhab DAEMON=/usr/bin/java PIDFILE=/var/run/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME ECLIPSEHOME="/opt/openhab"; HTTPPORT=8080 HTTPSPORT=8443 TELNETPORT=5555 #RUN_AS=ben # get path to equinox jar inside $eclipsehome folder cp=$(find $ECLIPSEHOME/server -name "org.eclipse.equinox.launcher_*.jar" | sort | tail -1); DAEMON_ARGS="-Dosgi.clean=true -Declipse.ignoreApp=true -Dosgi.noShutdown=true -Djetty.port=$HTTPPORT -Djetty.port.ssl=$HTTPSPORT -Djetty.home=$ECLIPSEHOME -Dlogback.configurationFile=$ECLIPSEHOME/configurations/logback.xml -Dfelix.fileinstall.dir=$ECLIPSEHOME/addons -Djava.library.path=$ECLIPSEHOME/lib -Djava.security.auth.login.config=$ECLIPSEHOME/etc/login.conf -Dorg.quartz.properties=$ECLIPSEHOME/etc/quartz.properties -Djava.awt.headless=true -jar $cp -console ${TELNETPORT}" # Exit if the package is not installed [ -x "$DAEMON" ] || exit 0 # Read configuration variable file if it is present [ -r /etc/default/$NAME ] && . /etc/default/$NAME # Load the VERBOSE setting and other rcS variables . /lib/init/vars.sh # Define LSB log_* functions. # Depend on lsb-base (>= 3.2-14) to ensure that this file is present # and status_of_proc is working. . /lib/lsb/init-functions # # Function that starts the daemon/service # do_start() { # Return # 0 if daemon has been started # 1 if daemon was already running # 2 if daemon could not be started start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE --chuid $RUN_AS --chdir $ECLIPSEHOME --exec $DAEMON --test > /dev/null \ || return 1 start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE --chuid $RUN_AS --chdir $ECLIPSEHOME --exec $DAEMON -- $DAEMON_ARGS \ || return 2 # Add code here, if necessary, that waits for the process to be ready # to handle requests from services started subsequently which depend # on this one. As a last resort, sleep for some time. return 0 } # # Function that stops the daemon/service # do_stop() { # Return # 0 if daemon has been stopped # 1 if daemon was already stopped # 2 if daemon could not be stopped # other if a failure occurred start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME RETVAL="$?" [ "$RETVAL" = 2 ] && return 2 # Wait for children to finish too if this is a daemon that forks # and if the daemon is only ever run from this initscript. # If the above conditions are not satisfied then add some other code # that waits for the process to drop all resources that could be # needed by services started subsequently. A last resort is to # sleep for some time. start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON [ "$?" = 2 ] && return 2 # Many daemons don't delete their pidfiles when they exit. rm -f $PIDFILE return "$RETVAL" } # # Function that sends a SIGHUP to the daemon/service # do_reload() { # # If the daemon can reload its configuration without # restarting (for example, when it is sent a SIGHUP), # then implement that here. # do_stop sleep 1 do_start return 0 } case "$1" in start) log_daemon_msg "Starting $DESC" do_start case "$?" in 0|1) log_end_msg 0 ;; 2) log_end_msg 1 ;; esac ;; stop) log_daemon_msg "Stopping $DESC" do_stop case "$?" in 0|1) log_end_msg 0 ;; 2) log_end_msg 1 ;; esac ;; status) status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $? ;; #reload|force-reload) # # If do_reload() is not implemented then leave this commented out # and leave 'force-reload' as an alias for 'restart'. # #log_daemon_msg "Reloading $DESC" "$NAME" #do_reload #log_end_msg $? #;; restart|force-reload) # # If the "reload" option is implemented then remove the # 'force-reload' alias # log_daemon_msg "Restarting $DESC" do_stop case "$?" in 0|1) do_start case "$?" in 0) log_end_msg 0 ;; 1) log_end_msg 1 ;; # Old process is still running *) log_end_msg 1 ;; # Failed to start esac ;; *) # Failed to stop log_end_msg 1 ;; esac ;; *) #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2 echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 exit 3 ;; esac :

sudo chmod a+x /etc/init.d/openhab
sudo update-rc.d openhab defaults
sudo nano etc/rc.local

(add this code by pasting into the file. Some lines may already exist, so paste what is missing)

# By default this script does nothing.
# By default this script does nothing. sudo /opt/openhab/start.sh fbset -xres 16 -yres 16 -vres 16 -depth 8 /opt/vc/bin/tvservice -o # Print the IP address _IP=$(hostname -I) || true if [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP" fi exit 0

*******openHAB optional demo install start*******

sudo wget https://github.com/openhab/openhab/releases/download/v1.6.2/distribution-1.6.2-demo-configuration.zip

cd /opt/openhab
sudo unzip distribution-1.6.2-demo-configuration.zip 
sudo  rm distribution-1.6.2-demo-configuration.zip 
sudo chmod +x start.sh
sudo ./start.sh

Go check it out.

http://192.168.X.XXX:8080/openhab.app?sitemap=demo... (use the IP address of your Pi)

Step 3: Binding and Your First Switch

******* openHAB optional Samba Install *******

(For easy access to the configuration files. CATION - you'll have root rights to delete/modify)

sudo apt-get install samba samba-common-bin
sudo nano /etc/samba/smb.conf
# Change this to the worgroup/NT-domain name your Samba server will part of
workgroup = YOURWORKGROUP

# WINS Support - Tells the NMBD component of Samba to enable its WINS Server
wins support = yes

#add to the last line
[OpenHAB]
comment = OpenHAB
path = /opt/openhab
browseable = Yes
writeable = Yes
only guest = no
create mask = 0777
directory mask = 0777
public = no
force user = root
sudo smbpasswd -a pi

(set password to whatever you want)

You should be able to access the openHAB directory from you PC (via LAN) now.

*******openHAB designer *********

download and follow the directions for installation.

https://bintray.com/artifact/download/openhab/bin/...

Access the openHAB configurations folder through your network folder via samba.

[See Image]

*******openHAB zwave binding*********

Go here for more information on zwave networks http://www.vesternet.com/resources/technology-ind...

sudo nano /opt/openhab/configurations/openhab.cfg

Look for the section titled Z-Wave Binding.

There are only 2 properties you need to activate (by removing the # at the front).

  • zwave:port is the port of your Z-Wave Controller. If you have a raZberry, it's /dev/ttyAMA0. A USB stick would be /dev/ttyUSB0.
  • zwave:healtime is the time for the daily network heal. You can probably leave this at 2 (2AM).
cd /opt/openhab
sudo wget 
sudo unzip habmin.zip 
sudo rm habmin.zip

Add your z wave devices to the network, then associate as needed.

http://192.168.2.100:8080/habmin/ (your ip)

[See Image]

Sample items

Dimmer test_dimmer2 "Dimmer 2 [%d %%]" (zwave) { zwave="11" }
Contact Door_Switch "Door sensor [%s]" (zwave) { zwave="10:command=switch_binary,respond_to_basic=true" } Contact Door_Alarm "Door Tamper [%s]" (zwave) { zwave="10:command=alarm" }
Number Door_Battery "Door battery [%d %%]" (zwave) { zwave="10:command=battery" }

*******openHAB setting up your first site*********

go to the openHAB Wiki and read up.

Summary:

    Items - Is a list of each node in your home automated network. Start here when adding something to you network.

    Persistence - Allows you to log inputs. Rules - Mostly a (if this - then that) to really automate you home.

    Scripts - I don't use these

    Sitemaps - This is your homepage for the web access to openHAB. You can have more than one.

    Transform - This will allow you to change text supplied from each node to your preference.

    Lets setup a zwave dimmer (files referenced below are attached to this post)

    • confirm that zwave binding is enabled in the openhab.cfg file. We did this earlier.
    • confirm that the zwave add-on is installed. We did the eariler.
      • \\YOURPI\OpenHAB\addons\org.openhab.binding.zwave-1.7.1
    • associate your new node with the zstick. Instructions here.
    • get the node information from the openHAB administration interface (habmin). This was installed earlier.
      • http://XXX.XXX.XXX.XXX:8080/habmin/ (on your pi)
      • You may need to assign an "associated group" and "configure parameters" based on what your node is. Dimmers usually don't require this.
      • get the node number. 11 in my case
        [see image]
      • code the node in your item list
        • \OpenHAB\configurations\items\HAB.items (I'm naming this set HAB. You'll see as we progress.)
        • add the following line to openhab
    Dimmer light_living_dimmer "Living Rm Dimmer [%d %%]" <slider> (all,zwave,lights) { zwave= "11" }
    • Dimmer - Is the node type
    • light_living_dimmer - Is the name of the node. I name all items by type - location - specificity
    • "Living Rm Dimmer [%d %%]" - Is the name and value that appears in the GUI (browser or application)
    • slider - is the icon. In this case, a slide allows you to adjust the brightness via the GUI
    • (all,zwave,lights) - These are groups. They help with displaying multiple items (via groups) on your sitemap. They also help to apply a rule to multiple items at once.
      • These groups must also be listed in openhab HAB.items under the Dimmer line
    Group all
    Group zwave
    Group lights
    • { zwave= "11" } - Tells openHAB that it is interfacing with zwave node 11.
    • add the item to your sitemap
      • \OpenHAB\configurations\sitemaps\HAB.sitemap
      • add the following lines to the sitemap
    sitemap HAB  label="My openHAB" 
    { Frame label= "Switches" icon= "light_switch" { Group item= lights label="Lights" icon="light_switch" 
    	} 
    } 
    • In this case all items in the group "lights" will show up in this frame. You may also add items individually.
    • Let's setup a simple cron (time) based rules
      • \OpenHAB\configurations\rules\HAB.rules
      • I use http://www.cronmaker.com/ to pull the cron times
      • Go ahead and add some libraries along with the rules.
    import org.openhab.core.library.types.* 
    import org.openhab.core.persistence.* 
    import org.openhab.model.script.actions.* 
    import java.lang.Math 
    import org.openhab.core.library.types.DecimalType
    rule "Trigger Turn Light up to 75% at 1900"
    	when
    		Time cron "0 0 19 1/1 * ? *"
    	then
    		sendCommand(light_living_dimmer , 75)
    	}
    end
    rule "Trigger Turn Light off at 2200"
    	when
    		Time cron "0 0 22 1/1 * ? *"
    	then
    		sendCommand(light_living_dimmer , 0)
    	}
    end

    That's it for now. Have fun!

    Step 4: Raspbian Optional USB Audio

    I can't get openHAB to run audio through the USB. Post a solution in the comments if you have it working.

    Credit to this post

    Syba USB 2.0 External Virtual 7.1 Surround Sound Card Adapter

    I can get the Pi to work with the USB audio just fine. This will not work for openHAB as is. Maybe a java issue?

    Figure out your chipset

    Start by having your Raspi turned off/shutdown (perform a clean shutdown!) and then plugging in your USB audio card. Then boot the Pi as normal.

    Once you log in, type dmesg | grep cm109 to look at the boot messages. You should either see some lines about cm109 if you have a CM109 chipset

    Image 1

    or if nothing comes up, try dmesg | grep Headphone you will see the C-Media USB Headphone Set driver. This means its a CM-headphone

    Image 2

    or if nothing comes up, try dmesg | grep C-Media you will see some C-Media notes but no mention of the cm109 driver. This means its a CM108

    Image 3

    CM-Headphone Type

    Type dmesg to look at the boot messages. You should see a bunch of lines that talk about C-Media USB Headphone Set

    Image 4

    And if you type in lsusb you should see a reference to C-Media Electronics Audio Adapter but no mention of CM108 and the VID/PID is 0x0d8c:0x00c

    Image 5

    Nothing special needs to be done! Hurray! Continue on to the Updating ALSA Config section

    Updating alsa options

    Backup first

    sudo cp /etc/modprobe.d/alsa-base.conf /etc/modprobe.d/alsa-base-1.conf

    We'll edit the audio system configuration file with

    sudo nano /etc/modprobe.d/alsa-base.conf

    For all but the most recent Raspbian releases, look for the line that reads:

    #options snd-usb-audio index=0

    Change this to:

    options snd-usb-audio index=0

    Note we removed the # at the start of the line.

    For the latest Raspbian versions, look for this line instead:

    options snd-usb-audio index=-2

    There's no # to remove in this case, just change the index from -2 to 0:

    options snd-usb-audio index=0
    Image 6

    That's it! Now reboot with sudo reboot and log in again, you can test with speaker-test by runnig

    speaker-test -c2 -D hw:0,0

    Which will play white noise through the left and right 'speakers' on the audio card. Once you've got something coming out, try to play an audio file with aplay (for WAV files, not MP3)

    aplay /usr/share/scratch/Media/Sounds/Animal/Bird.wav 
    aplay /usr/share/sounds/alsa/Front_Center.wav

    If you want to play a stream of music, you can try

    sudo apt-get install mpg123 
    mpg123 <a href="http://voxsc1.somafm.com:8882"> http://voxsc1.somafm.com:8882</a>

    If you want to play MP3's on command, check out this tutorial which covers how to set that up

    Headphone vs Audio card

    Don't forget, you still have the built in headphone jack on the Pi, called card 1 now (not the default card 0)

    Image 7

    If you want to aplay through that jack again, specify card 1 with -D hw:1,0 instead of -D hw:0,0

    speaker-test -c2 -D hw:1,0

    Step 5: