Skip to content

Setting up OpenHAB on rpi (and BBB)

Carlos Sanchez edited this page Aug 21, 2015 · 6 revisions

So here are some steps I took to install and configure openHAB on my Raspberry pi with a ninja crust (Steps could be applied just as easily to those with a Beagleboard too!)

Install some prereqs##

We need pip to install some python pre-reqs

sudo apt-get install python-pip
sudo pip install pyserial
sudo pip install paho-mqtt

We need Java (You can installed the old, v7 if you want - just replace 8 with 7)

sudo apt-get install oracle-java8-jdk

or in the BBB

sudo apt-get install openjdk-7-jre

We will also need a mqtt broker (and client)

sudo apt-get install mosquitto

or

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list

sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients

Stop and disable ninja software##

service ninjablock stop OR service ninjablocks stop
echo manual | sudo tee /etc/init/wifisetup.override
echo manual | sudo tee /etc/init/ninjablock.override

Install openHAB##

The easy way

curl 'https://bintray.com/user/downloadSubjectPublicKey?username=openhab' | sudo apt-key add -
echo "deb http://dl.bintray.com/openhab/apt-repo stable main" | sudo tee /etc/apt/sources.list.d/openhab.list
sudo apt-get update
sudo apt-get install openhab-runtime openhab-addon-binding-mqtt openhab-addon-io-myopenhab

or

sudo mkdir /opt/openhab/
cd /opt/openhab/

I do this because its easiest - if your worried about security, you can tighten it up later

sudo chown -hR pi:pi /opt/openhab/

Download the required files (links are from openhab website, you can check yourself openhab.org/download)

wget https://bintray.com/artifact/download/openhab/bin/distribution-1.7.0-addons.zip
wget https://bintray.com/artifact/download/openhab/bin/distribution-1.7.0-runtime.zip

Unzip

unzip distribution-1.7.0-runtime.zip

cleanup

rm distribution-1.7.0-runtime.zip

install addons

mv distribution-1.7.0-addons.zip addons/
cd addons/
unzip distribution-1.7.0-addons.zip
rm distribution-1.7.0-addons.zip

Configure openHAB with ninja stuff#

First thing you will probably want to do is to make openHAB start at startup (or be able to daemonise it)

https://github.com/openhab/openhab/wiki/Samples-Tricks#how-to-configure-openhab-to-start-automatically-on-linux

Next, lets configure the default config. The default config has alot of stuff in there, most of the stuff you wont need, unless you are going to configure those services later. For now we will only configure what we need for the ninja stuff. I'll leave the rest for you to figure out yourself :D

cd /opt/openhab/configurations
nano openhab.cfg

You will want the config file to look something like this. Change items where required (if you want to know what they are, you can read the full default config at /opt/openhab/configurations/openhab_default.cfg)

#default stuff    
folder:items=10,items
folder:sitemaps=10,sitemap
folder:rules=10,rules
folder:scripts=10,script
folder:persistence=10,persist
#mqtt stuff. In all my examples, broker name is mosquitto - this is customisable.
# if you need to add in authentication here, you can do that also - see default config for required lines
mqtt:mosquitto.url=tcp://127.0.0.1:1883

Save this. Now we will create some items - this is explained in the home page of this wiki but I will repeat here for good luck

Now for watts clever actuators you will need to get the on/off rf commands. To do this, go to https://api.ninja.is/rest/v0/device. This will list the devices and their saved on/off commands. Now just search the text for the name of your device. In this example, my device in the ninja cloud was called "Big Lamp". I searched for this and found something like (with lots more around it of course):

"category":"","shortName":"Big Lamp on","data":"110110101101101011011010","type":"actuator"},"EwCcb"
{"category":"rf","shortName":"Big lamp off","data":"110110101101101011010010","type":"actuator"},"qS5le":

But this shows the on/off rf commands you will need in the "data": section

So lets create them so openHAB can talk to them

cd /opt/openhab/configurations/items
nano default.items

The group parts are used so you can group things together when creating the sitemap and it groups them in the UI. Once you get your head aroun it, it will make sense. As described already, an actuator needs 4 mqtt actions so you can capture an even if used by the remote too, so everything stays in sync

Group All
Group mainFloor (All)
Group Lights "Lights" (mainFloor)
Group frontroom "Front Room" (mainFloor)
Switch B_Lamp   "Big Lamp"     (frontroom)     {mqtt="<[mosquitto:ninjaCape/input/11:state:ON:110110101101101011011110],
                            <[mosquitto:ninjaCape/input/11:state:OFF:110110101101101011010110],
                            >[mosquitto:ninjaCape/output/11:command:OFF:110110101101101011010110],
                            >[mosquitto:ninjaCape/output/11:command:ON:110110101101101011011110]"
                            }
Switch Motion_Sensor    "Room Sensor"   (Persist_change) {mqtt="<[mosquitto:ninjaCape/input/11:state:ON:100000000101101001011001]"}

This is a very basic example but refer to the home page of the wiki for all the other types

Now we can create a sitemap. this is a config file to tell the UI how to present your items

cd /opt/openhab/configurations/sitemaps
nano default.sitemap

Your sitemap will look like this (if following the example)

sitemap default label="Main Menu"
{
    Frame label="Lights" {
            Switch item=B_Lamp
            Switch item=Motion_Sensor
}
}

Starting services

Now lets start up the ninjacape-mqtt-bridge and start up openhab

Download the python script and make it executable

curl --create-dirs -o /opt/openhab/ninjaCapeSerialMQTTBridge.py https://raw.githubusercontent.com/perrin7/ninjacape-mqtt-bridge/master/ninjaCapeSerialMQTTBridge.py
chmod +x ninjaCapeSerialMQTTBridge.py
sudo curl -o /etc/init/mqttbridge.conf https://raw.githubusercontent.com/perrin7/ninjacape-mqtt-bridge/master/mqttbridge.conf
service mqttbridge start
service openhab start

Wait for openhab to fully initialise (It takes about 3 minutes on the raspberry pi to fully initialise everything) Once its ready open a browser and go to http://ninja.local:8080

this will open up openHAB's browser UI and you should be able to interact with your 433mhz devices on your ninjablock!

Troubleshooting

Running out of memory

If the system runs out of memory you may find that OpenHab is killed by the kernel when running out of memory. If you get a line like

ninjablock kernel: [1894103.106658] Killed process 21933 (java) total-vm:393992kB, anon-rss:151156kB, file-rss:164kB

in /var/log/kern.log then you are running out of memory

Change the JAVA_ARGS line in /etc/default/openhab to

JAVA_ARGS="-Xmx125M -XX:MaxPermSize=50M -XX:+HeapDumpOnOutOfMemoryError"

and restart openhab sudo service openhab restart

If openhab still gets killed, you may create a swap file

sudo dd if=/dev/zero of=/mnt/swap.1 bs=1M count=512
sudo chmod 600 /mnt/swap.1
sudo mkswap /mnt/swap.1
sudo swapon /mnt/swap.1
sudo echo "/mnt/swap.1	/mnt/swap.1	swap	defaults	0	0" >> /etc/fstab