Introduction: Installing OpenHAB2 on Raspberry Pi

Please keep an eye out as we add more connect to our YouTube channels as well as here at Instructables.

John's DIY Playground at YouTube

My YouTube Channel

openHAB Community

This is an adaptation from the docs.openhab.org site as I show you how to install OpenHAB onto your Raspberry PI via the openHAB package repository. This will be a multiple part series as we (John Wills) and I (Joshua Abramson) partner on a internet of things project. The goal is to provide a guide that will produce a secure IOT implementation at your home or work.

Because of it’s low price, it’s small form factor and the low energy consumption, the Raspberry Pi is a quite popular platform for openHAB.

Prerequisites:

  • Raspberry Pi Generation 2 or newer with Raspbian installed and running with at least a 8GB SD card.
  • PuTTY- a free SSH and telnet client for Windows. (if your are using windows)

Step 1: A Little Raspbian House Cleaning

Connected via SSH, execute the Raspbian configuration menu by running sudo raspi-config. Go through the following steps:

  • Expand the file system
  • Change your password(Change the host name if you wish, e.g. “openhabpi”)
  • From the advanced menu, change the memory split for the GPU to “16”
  • Restart

As a good practice, run a full upgrade and install packages you like or need (a set of helpful packages is given as an example):

sudo apt-get update 
sudo apt-get upgrade
sudo apt-get install screen mc vim git htop

Raspbian in the latest full versions already includes the required Oracle Java 8. If you are using “Raspbian Lite” you need to install it:

sudo apt-get install oracle-java8-jdk

Step 2: Package Repository Installation

Installation through a package repository is the recommended choice on Debian/Ubuntu derivatives. Alternatively resort to the manual installation approach.

Choose between the latest Beta release or a Snapshot with all incoming contributions, rebuild on every change. As openHAB 2 is still in an evolving state, the snapshot may be the preferred choice.

The repository URLs will be stored in the file /etc/apt/sources.list.d/openhab2.list. Be careful to not have conflicting repositories in your sources list.

Decide between two options: (We are proceeding with Snapshot instead of Beta)

Snapshot Release

Add the openHAB 2 Snapshot repositories to your systems apt sources list (will overwrite your existing openhab2.list):

**Updated 11-27-16** Thanks webcompanion

echo 'deb https://openhab.ci.cloudbees.com/job/openHAB-Distribution/ws/distributions/openhab-offline/target/apt-repo/ /' | sudo tee /etc/apt/sources.list.d/openhab2.list
echo 'deb https://openhab.ci.cloudbees.com/job/openHAB-Distribution/ws/distributions/openhab-online/target/apt-repo/ /' | sudo tee --append /etc/apt/sources.list.d/openhab2.list

Additionally, you need to add the openHAB 2 Snapshots repository key to your package manager:

wget -qO - 'http://www.openhab.org/keys/public-key-snapshots.asc' | sudo apt-key add -

Note: CloudBees provides the openHAB 2 repositories through HTTPS. If your system fails at the next step, install the missing dependency:

sudo apt-get install apt-transport-https

**Updated 11-27-16** Some are reporting an error after the command above; however, it appears to work after the second attempt.

*Added 11-27-16** Scan the newly added repository and resynchronize the package index:

sudo apt-get update

Finally install openHAB 2 as either offline or online distribution. The offline distribution is full blown and comes with all add-ons, the online distribution will install additional add-ons on request from the internet. We are going with offline here.

sudo apt-get install openhab2-offline

If everything went well, start openHAB and register it to be automatically executed at system startup: Linux init systems based on sysVinit (e.g. Debian 7 / Ubuntu 14.x and earlier):

sudo /etc/init.d/openhab2 start
sudo /etc/init.d/openhab2 status
sudo update-rc.d openhab2 defaults

The first start may take up to 15 minutes, this is a good time to reward yourself with hot coffee or a freshly brewed tea!

Step 3: OpenHab First Run Test

You should be able to reach the openHAB 2 portal at http://openhab-device-ip:8080 at this point.

To get the openhab-device-ip type this command

ifconfig

You could also capture the openhab-device-ip from the IP given as you SSH into the Raspberry PI at first login of Putty. In my case the IP is 10.104.10.109, therefore I will check to see if openHAB is running by going to

http://10.104.10.109:8080 in my favorite browser.

Step 4: OpenHAB As a Service

In my case the openHAB did not setup as a service which allows openHAB to auto-start as the Raspberry Pi boots up. Here are the steps if you experience the same issue.

The following instructions are intended for a Linux init system based on systemd (e.g. Debian 8 / Ubuntu 15.x and newer). This will allow you to register openHAB as a service, so that it runs at startup and automatically restarts if openHAB crashes. The service will be running with the privileges of the user “openhab” and expects the openHAB files under /usr/share/openhab2.

Check to make sure the file /lib/systemd/system/openhab2.service exists

sudo nano ./usr/lib/systemd/system/openhab2.service

If not, create the file /lib/systemd/system/openhab2.service with the following content:

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

[Service]
Type=simple
User=openhab
Group=openhab
GuessMainPID=yes
WorkingDirectory=/usr/share/openhab2
#EnvironmentFile=/etc/default/openhab2
ExecStart=/usr/share/openhab2/start.sh server
ExecStop=/bin/kill -SIGINT $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target

Next, enable the service to be executed on system startup, start the service and retrieve status information:

# initialize the new service (execute only once)

sudo systemctl daemon-reload 
sudo systemctl enable openhab2.service

#start and retrieve status

sudo systemctl start openhab2.service
sudo systemctl status openhab2.service

The output of status after a successful execution should be similar to:

openhab2.service - The openHAB 2 Home Automation Bus Solution Loaded:
loaded (/lib/systemd/system/openhab2.service; enabled)
Active: active (running) since Thu 2016-08-14 01:16:00 GMT; 18h ago
Docs: http://docs.openhab.org

Step 5: Playing With Demo (To Get That Instant Gratifcation for All Your Hard Work)

This tutorial will stop at this step..... for now.

I'll add more as John and I progress through this project.

The demo package consists of example configuration files and samples of add-ons and UIs. In order to install the demo, you must edit the file ‘/etc/openhab2/services/addons.cfg’. Uncomment the line package= and set it to demo as shown below:

 sudo nano /etc/openhab2/services/addons.cfg
# The base installation package of this openHAB instance (default is "standard") 
# Valid options:
# - minimal : Installation only with dashboard, but no UIs or other addons
# - standard : Typical installation with all standards UIs
# - demo : A demo setup which includes UIs, a few bindings, config files etc.
#package = standard
package = demo
...

Step 6: Watch the Video.