OpenHAB2 and MQTT
OpenHAB 2 Installation
We are going to use the apt-get package manager to install the latest OpenHAB 2 snapshot on our Debian Linux server. For more installation options - different Linux versions, stable builds, etc. - check out the OpenHAB Download Section.
- We will start by adding the needed repository key:
wget -qO - 'https://bintray.com/user/downloadSubjectPublicKey?username=openhab' | sudo apt-key add -
- Add the HTTPS transport for APT:
sudo apt-get install apt-transport-https
- Add the repository:
echo 'deb https://openhab.jfrog.io/openhab/openhab-linuxpkg unstable main' | sudo tee /etc/apt/sources.list.d/openhab2.list
- Update the package lists and install the openHAB distribution package:
sudo apt-get update && sudo apt-get install openhab2
- Execute the following statements to configure openHAB to start automatically using
systemd
:
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable openhab2.service
- You can start openhab2 by executing:
sudo /bin/systemctl start openhab2.service
- Open the OpenHAB default port in your Firewall:
sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
- Navigate with a web browser to
http://<ip-address>:8080
and select the Standard Package:
If the OpenHAB user interface does not load, you are probably missing Java on your Linux server. You can check the OpenHAB service status by typing:
systemctl status openhab2
How to install Java with Apt on Debian 9
In this guide, you will install various versions of the Java Runtime Environment (JRE) and the Java Developer Kit (JDK) using apt
.
Installing the Default JRE/JDK
The easiest option for installing Java is to use the version packaged with Debian. By default, Debian 9 includes Open JDK, which is an open-source variant of the JRE and JDK.
This package will install OpenJDK version 1.8, which is compatible with Java 8. Java 8 is the current Long Term Support version and is still widely supported, though public maintenance ends in January 2019.
- To install this version, first update the package index:
sudo apt update
- Next, check if Java is already installed:
java -version
If Java is not currently installed, you'll see the following output:
- Execute the following command to install OpenJDK:
sudo apt install default-jre
This command will install the Java Runtime Environment (JRE). This will allow you to run almost all Java software.
Verify the installation with java -version
:
- You may need the Java Development Kit (JDK) in addition to the JRE in order to compile and run some specific Java-based software. To install the JDK, execute the following command, which will also install the JRE:
sudo apt install default-jdk
Verify that the JDK is installed by checking the version of javac
, the Java compiler:
javac -version
You can now navigate with a web browser to http://<ip-address>:8080
and select the Standard Package of OpenHAB2:
We highly recommend that you read the Concepts Introduction. It introduces a number of important ideas that will help you as you install and begin to configure openHAB for the first time.
Once you have openHAB up and running, the Configuration Guide contains everything you need to know to get your openHAB installation talking to different devices around your home. For instance, you can use Sitemaps to control how the status of these devices are displayed on different openHAB User Interfaces, and you can begin to experiment with Rules in order to get the most out of your installation. There are many other ways of interacting with your openHAB smart home: the most popular are presented in the Interfaces and Ecosystem section below.
OpenHAB Configuration though Paper UI
The Paper UI is a new interface that helps setting up and configuring your openHAB instance. It does not (yet) cover all aspects, so you still need to resort to textual configuration files.
Add-on management: Easily install or uninstall openHAB add-ons:
MQTT Binding
Go to the Add-Ons Menu and click on the Bindings Tab. We are going to use the MQTT protocol to communicate with our INSTAR IP camera from OpenHAB. To be able to connect to the Mosquitto MQTT Server we need to install the MQTT Binding - make sure that you choose the Version 2 (at the moment of writing this is the 2.4.0 version).
Now switch to your OpenHAB Inbox:
Click on the + icon to add the MQTT Binding:
Find the MQTT Binding inside the list and select it:
Click on Add Manually:
And choose the MQTT Broker:
With the newest Firmware for your INSTAR Full HD camera, you can use the camera as MQTT Broker. For older cameras models please install a MQTT broker like Mosquito first. Add your brokers IP address and the broker port - by default this is 1883. Note that we are first not going to use TLS encryption. We will add it once we are able to establish the connection:
Your INSTAR Full HD cameras MQTT default login is the Administrator login you added to your camera (if you are using Mosquitto). Then save your setting by clicking on the blue confirmation button:
Back in the Things Menu click on + again - this time to add a Thing that we can use for an PaperUI button later on:
Click on MQTT Binding:
And Add Manually:
This time we need to add a Generic MQTT Thing:
I am going to create a button that triggers the alarm on my INSTAR Full HD camera and name it here accordingly. As Bridge you need to select the MQTT broker you just created. Click on the blue button to confirm:
Back in the Thing Menu click to edit the Thing you just created:
Click on the blue + button to add a Channel to the Thing:
I am going to use an On/Off Switch which is not ideal for the function - the Alarm-Trigger command does not have an On/Off state. Unfortunately OpenHab does not offer a simple button. The MQTT topic to trigger the alert is alarm/pushalarm
and as message payload we need to add {"val":"1"}
. Click on Save to confirm:
By clicking on the blue icon in front of the created channel we are now able to link it to a switch in the OpenHAB PaperUI:
Click on Create new item...:
Click on Link:
You can now click on Control to open the PaperUI:
Our Alarm Trigger now appears in the Paper UI and clicking the switch will send the MQTT message to our MQTT server that is going to notify our camera to trigger an alert. Note the MQTT.fx program at the bottom of the frame can be used to verify that the command was actually send - but the installation is optional:
Instead of using MQTT.fx you can also just check your cameras log book that should now show an Audio Alarm (the manual alarm trigger uses the audio alarm to trigger all your programmed Alarm Actions):
All right that's it - the Binding was successfully added and we are now able to control all of our camera functions with the MQTT interface.
OpenHAB Cloud Connector
You can use the OpenHAB Cloud to access your Smarthome over the internet. First we need to install openHAB Cloud Connector that can be found in the Add-ons Menu in the Miscellaneous section:
Now you need to create a free account on the OpenHAB Cloud Service. Once your account is set up and you are signed in, go to the Account Page. Here you need to add your OpenHAB UUID and OpenHAB Secret. Those can be found on your OpenHAB installation under the following paths (make sure that you install the Cloud Connector first for them to be generated):
cat /var/lib/openhab2/uuid
cat /var/lib/openhab2/openhabcloud/secret
Add your UUID and Secret on your Account Page and update your configuration:
Now back to the Paper UI go to Configuration and Services:
You can find the Cloud Connector in the IO Section. Click to configure it:
And make sure that the Remote Access is active and the Base URL is set to https://myopenhab.org/
:
Now go back to your terminal and reboot your Linux system that is running OpenHab with sudo reboot
. Once OpenHAB is back up refresh the Cloud Page - you should see that your status is now set to ONLINE:
When you now switch to the dashboard URL you will be able to see your regular OpenHAB interface. And this URL will work from anywhere where you have internet access: