Introduction: OpenHAB on Raspberry Pi

About: I am a life long biz owner. I've been fortunate enough to have a successful business and a fun life.

NOTE: Updated 6/4/16 This instructable is outdated. It can certainly be of assistance but please also checkout Jeff's instructable below. Two little babies can suck the free time right out of you.
https://www.instructables.com/id/Install-OpenHAB-on-Raspberry-Pi/

This instrucable covers how to download and install openHAB on your Raspberry Pi through the command line. I also go one step further and show you how to configure your openHAB to load at boot.

As many of you may know openHAB is a server system design to integrate the myriad of available home automation gadgets in the market and control them with one single interface. You can do away with all of the apps that come with each individual gadget and have one central control.

I understand that many people haven't taken the plunge to use openHAB simply because they think it is too complex. I put this instructable together so others can see that there is really nothing to it. I myself had a few issue when I was installing openHAB so I thought others might find this helpful.

Step 1: Downloading and Installing OpenHAB on Pi

Its always good practice to be certain you are working with the latest updated software packages so lets begin with this:

sudo apt-get updatesudo apt-get upgrade

Certain apps may require you to update the firmware but typically its not a requirement. I suggest to do it anyway.

sudo rpi-update

You can use any kind of way that you would like to copy files to your Pi. I choose to do download them. I also like to download them directly to the folder that I will be extracting them from. I then do some housekeeping and remove the unnecessary zip file. First lets make the folders we need.

sudo mkdir /opt/openhab


You will then want to download the latest release to that folder. You can do that by going to the openHab downloads page and right clicking on the runtime core download and copying the link address to get the latest version. You can then replace the url below with the one you copied.

cd /opt/openhab

wget https://github.com/openhab/openhab/releases/download/v1.5.1/distribution-1.5.1-runtime.zip


Ok, lets install this b*tch. The zip files that you downloaded should be extracted to the correct location already. If your not already there go to the openhab folder and extract the runtime zip file there.

cd /opt/openhabsudo

unzip distribution-1.5.1-runtime.zip


Lets now remove the zip file which will no longer be needed.

rm distribution-1.5.1-runtime.zip


Great, the runtime is installed and the zip files erased, but in order for openHAB to work you will need to add bindings. Upon extrating the runtime zip a "addons" folder was created. All bindings belong in this folder. Go to the addons folder and extract the addons zip. First lets download them to the appropriate folder.

cd addons/

wget https://github.com/openhab/openhab/releases/download/v1.5.1/distribution-1.5.1-addons.zip


This contains all the bindings available for openHAB. As stated in the openHAB wiki, Bindings are optional packages that can be used to extend functionality of openHAB. By help of bindings openHAB users can e.g. access Asterisk communications software or connect to the KNX Home Automation Bus.

Lets just install all of them and later we can sift through and decide which one we keep. You simply remove the .jar file from the addons folder.
sudo unzip distribution-1.5.1-addons.zip
Remove or erase the zip file.
rm distribution-1.5.1-addons.zip

Next lets go back one folder to the openhab folder and make a copy of the "openhab_default.cfg" file. You will call the copy "openhab.cfg". In the event that you have to update your openhab the default file will be updated as well. By making a copy of this file, openhab will use this file for configurations and also not update it. Since you will be making your own custom configurations, it is important that those get written in the "openhab.cfg" file for safe keeping.

cd ..
sudo cp configurations/openhab_default.cfg configurations/openhab.cfg


In order to understand how this crazy thing works I recommend deploying the demo app. Its a good way to get your foot on the ground and make some sense of this.

cd /opt/openhab

wget https://github.com/openhab/openhab/releases/download/v1.5.1/distribution-1.5.1-demo-configuration.zip


Go to the openhab folder and extract the demo zip files there.
cd /opt/openhab

sudo unzip distribution-1.5.1-demo-configuration.zip


remove the garbage files

rm distribution-1.5.1-demo-configuration.zip 

By default the "start.sh" script is not an executable file. Do the following in order to make it executable:

sudo chmod +x start.sh

sudo ./start.sh



And the fun begins! You should have a fully loaded demo that you can use to familiarize yourself with openHAB. Just go to your telephone or computer and place the following url in your favorite browser. Be sure to replace the ip address with the Ip address of your Pi.

http://192.168.X.XXX:8080/openhab.app?sitemap=demo

Step 2: Configuring OpenHAB to Start Automatically at Boot

In the event of a power surge, outage or if you decide to restart your Pi, your OpenHAB program will need to be re-booted. That is a big pain the rear end so we will want to configure it to boot at start.

First off you will want to create a new file in the /etc/init.d folder called "openhab".

You will want to add a few lines of code to your openhab file by using the nano editor or your favorite editor of choice.
sudo nano /etc/init.d/openhab

Add this code:
#! /bin/sh<br>### 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/binDESC="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 # be sure you are adopting the user to your local OH user RUN_AS=pi# 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 :

You can also go here to see the readable version or you can copy and paste the code from there.

You may need to change the RUN_AS=pi to your current user.

Lastly you will want to make this an executable file.
sudo chmod a+x /etc/init.d/openhab

And for it to automatically boot at the start of Pi
sudo update-rc.d openhab defaults


If you get an error that looks like this:
insserv: Script openhab is broken: incomplete LSB comment. insserv: missing `Provides:' entry: please add. insserv: missing `Required-Start:' entry: please add even if empty. insserv: missing `Required-Stop:' entry: please add even if empty. insserv: missing `Default-Start:' entry: please add even if empty. insserv: missing `Default-Stop:' entry: please add even if empty. insserv: Default-Start undefined, assuming empty start runlevel(s) for script `openhab' insserv: Default-Stop undefined, assuming empty stop runlevel(s) for script `openhab'


Then you have an indentation problem. Sometimes upon pasting into your editor the text gets placed into an easy to read format which places indentations into some of the initial lines of code. Any information that is written for update-rc.d must be in first column. And there should not be any tabulation or space before the "#". If there are any remove them or you will generate that error.

Go back to your file and double check.
sudo nano /etc/init.d/openhab


The first few columns should look like this (DO NOT COPY AND PASTE, CONTAINS ASCII CHARACTER SPACING) just use it as a visual example:

#! /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


Notice how there are no spaces or indents before each # sign.

Complete, your Raspberry Pi will now boot openHAB at start.

Step 3: Extras (Admin Console)

FYI
There is a cool Admin Console for openHAB called HABmin. Its great for performing administrative tasks as well as configuring various procedures. Google it and decide whether or not its right for you. Its quite easy to install. If you are interested I have created an instructable on how to install it here.

http://instructables.com/id/openHAB-Admin-Console-...