Introduction: Install OpenHAB on Raspberry Pi

Install latest version of openHAB on Raspberry Pi and get the demo running. For many makers, the internet of things is all about home automation. openHAB is the leading open-source home automation hub.

I struggled with openHAB's installation instructions. Home Automation for Geeks' instructions helped me get an old version running (1.7). However, I wanted to install the latest stable version.

The instructions were verified with 1.8.3, but should work for any new stable version.

In general, raspbian and debian variants use packages enabling simple installation with apt-get. However, java is operating system independent and uses zip files for installation. openHAB has apt-get install, but it didn't work for me.

Notes:

  • Replace text enclosed in spades with an actual value ♣replace-this♣
  • Remove spaces around the colon (https : //)
  • Setup Raspberry Pi (any model) running raspbian or DietPi
  • I use a MacBook but any computer can be used. I think the only change if you are using a PC, is to use putty to login and open a terminal window.
  • My openHab Raspberry Pi is using ♣ip-address♣ = 192.168.1.100

Step 1: Step-by-Step Install

Step-by-step instructions follow, or go to the next step and use an unattended install to do the steps below.

Open terminal window and ssh into Raspberry Pi

$ ssh pi@♣ip-address♣

Always update and upgrade

$ cd ~/.
$ sudo apt-get update
$ sudo apt-get upgrade

Install java and eclipse

Check if java and eclipse are installed. If installed, then you should see /usr/bin/java or /usr/bin/eclipse. If not installed, install them

$ which java
$ sudo apt-get install oracle-java8-jdk -y 
$ which eclipse
$ sudo apt-get install eclipse -y

Install Mosquito

Remove the spaces around the colons.

$ wget http : //repo.mosquitto.org/debian/mosquitto-repo.gpg.key 
$ sudo apt-key add mosquitto-repo.gpg.key
$ rm mosquitto-repo.gpg.key
$ cd /etc/apt/sources.list.d/
$ sudo wget http : //repo.mosquitto.org/debian/mosquitto-jessie.list
$ sudo apt-get update
$ sudo apt-get install mosquitto mosquitto-clients

Test Mosquito

$ mosquitto_sub -d -t hello/world

Open a second terminal and ssh in

$ ssh pi@♣ip-address♣
$ mosquitto_pub -d -t hello/world -m "Hello from terminal window 2!"

The message from window 2 should appear in the first window.

Logout and close second terminal window.

CTRL-c in the first terminal window

Make directories

$ ssh pi@♣ip-address♣
$ sudo mkdir /opt
$ sudo mkdir /opt/openhab
$ sudo chmod -R ugo+rw /opt/openhab

Get links to latest version of openHAB

On MacBook (or PC):

  • open browser
  • open text file

In browser, go to openHAB downloads page and copy the following links to text file:

  • Runtime core
  • Addons
  • Demo setup

To copy a link on a MacBook, hold CTRL and click on link, then select Copy Link

Paste each link into text file. You will see something like this:

Download

CTRL-Click on Download and Edit Link. Select the link, copy it and paste it to text file. You will see something like this:

https : //bintray.com/artifact/download/openhab/bin/distribution-1.8.3-runtime.zip

Remember to remove spaces around the colon.

As of 27MAY2016, the current openHab links for the stable release 1.8.3 are:

♣runtime-core♣ = ♣https : //bintray.com/artifact/download/openhab/bin/distribution-1.8.3-runtime.zip♣

♣addons♣ = ♣https : //bintray.com/artifact/download/openhab/bin/distribution-1.8.3-addons.zip♣

♣demo-setup♣ = ♣https : //bintray.com/artifact/download/openhab/bin/distribution-1.8.3-demo.zip♣

Download latest version of openHAB

Run the following commands using links and names from above

$ cd /opt/openhab
$ sudo wget ♣runtime-core♣
$ ls
♣runtime-core-zip-file♣
$ sudo unzip ♣runtime-core-zip-file♣
$ sudo rm ♣runtime-core-zip-file♣

$ cd /opt/openhab/addons
$ sudo wget ♣addons♣
$ ls
♣addons-zip-file♣
$ sudo unzip ♣addons-zip-file♣
$ sudo rm ♣addons-zip-file♣

$ cd ..
$ sudo wget ♣demo-setup♣
$ ls
♣demo-setup-zip-file♣
$ sudo unzip ♣demo-setup-zip-file♣
$ sudo rm ♣demo-setup-zip-file♣
$ sudo chmod +x start.sh

Start openHAB running

$ cd /opt/openhab
$ sudo ./start.sh

openHAB takes about 5 minutes to start

In the browser, open a new tab and go to (remove spaces around colon):

http : //♣ip-address♣:8080/openhab.app?sitemap=demo

And it should work!

However, openHAB should really start and run on boot.

Stop openHAB

CTRL-z to stop openHAB. I haven't figured out how to recover from stopping openHAB. The only way to get openHAB to work after stopping is to reboot.

Add username and password

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

and add the following line, replacing names in spades with real values. Even though I am using diet-pi, I created a pi username.

user=password,user,role
♣username♣=♣password♣

Add MQTT binding

$ cd /opt/openhab/configurations
$ sudo cp openhab_default.cfg openhab.cfg
$ sudo nano openhab.cfg

CTRL-w to search for mqtt and uncomment the lines and edit to look like:

mqtt:broker.url=tcp://localhost:1883 
mqtt:broker.clientId=openhab

Use localhost and not your ♣ip-address♣. DHCP might change the ip-address and localhost will keep up with these changes.

CTRL-o, ENTER, CTRL-x to write and exit nano editor

Start openHAB on boot

Unless running an old version of raspbian, don't use init.d. Diet-Pi and raspbian use systemd, which is is a linux init system to bootstrap user space and manage processes. To check if systemd is being used, enter the command:

$ ps -p 1 -o comm=
systemd

Make system directory if it doesn't exist and create a start-up file

$ sudo mkdir /usr/lib/systemd/system 
$ sudo nano /usr/lib/systemd/system/openhab.service

And add

[Unit]
Description=openHAB Home Automation Bus
Documentation=http://www.openhab.org
Wants=network-online.target
After=network-online.target

[Service]
Type=simple 
GuessMainPID=yes
User=pi
ExecStart=/opt/openhab/start.sh
ExecStop=kill -SIGINT $MAINPID
Restart=on-failure
WorkingDirectory=/opt/openhab

[Install]
WantedBy=multi-user.target

To save and exit: CTRL-o, ENTER, CTRL-x

Change owner recursively on openhab.

$ sudo chown -R pi:pi /opt/openhab

Reload systemd to make the daemon aware of the new configuration.

$ sudo systemctl --system daemon-reload
$ cd /usr/lib/systemd/system
$ sudo systemctl enable openhab.service
$ sudo systemctl start openhab.service 
$ sudo reboot

Fix any issues in the logs. To see the log, enter the command:

$ sudo journalctl -f -u openhab.service

CTRL-c to stop

$ sudo reboot

openHAB takes about 5 minutes to start

In the browser, open a new tab and go to

http : //♣ip-address♣:8080/openhab.app?sitemap=demo

And it works!

Step 2: Unattended Install of OpenHAB Demo

Make openHAB Demo Installation very easy by automating the previous step using a bash unattended install script. The script assumes a raspberry pi running raspbian or dietpi, which was setup using one of the embedded links.

I finished the script on github and

  • tested the source on dietpi
  • tested unattended install script on dietpi
    • ran one time
    • ran multiple times, with CTRL-c interrupts
  • tested unattended install script on raspbian

The script assumes the username = pi, and the password = raspberry, and the script is trying to install openHAB 1.8.3. If these aren't correct, then edit the file accordingly.

Open terminal window on a MacBook or PC and run the following commands:

$ ssh pi@♣ip-address♣
$ sudo wget "https://raw.githubusercontent.com/dumbo25/install-openhab-demo/master/openhab_uai.sh"
$ sudo chmod o+x openhab_uai.sh
$ sudo bash openhab_uai.sh
$ sudo reboot

The script takes about ten minutes to run onraspbian.

Be sure to reboot the Raspberry Pi and wait 5 minutes until openhab starts

And it should work!

Once it works, be sure to change the hostname and password.

Internet of Things Contest 2016

Participated in the
Internet of Things Contest 2016