In the age of the Internet of Things, more and more devices are connected. This is also suitable for your own Raspberry Pi home automation. However, each device uses a different transmission protocol, which can make controlling them quite uncomfortable. Fortunately, there is a remedy for this: The Smart Home System OpenHAB offers many existing packages, with which all devices from well-known manufacturers can be controlled using one interface. And even for components that are not yet supported by default, we can simply write our own plugins.
The main advantage of this is that we can monitor and control all connected devices via a single control panel, be it inside or outside.
In order to be able to use OpenHAB and thus take the first step for our Raspberry Pi home automation, we begin with the installation, setup and configuration of OpenHAB in this tutorial before we connect further modules in the following tutorial parts.
Required Hardware Parts for Raspberry Pi Home Automation
As already said, pretty much all devices and sensors can be used with OpenHAB, including many that we have already used in previous tutorials. In addition, the following components are essential:
- Raspberry Pi* (model 3B preferred)
- micro SD card* (minimum 16GB, recommended 32GB)
- Power plug* (micro USB)
- possibly a LAN cable*
- optimal: Raspberry Pi housing*
To give you an outlook on the possibilities and further tutorials, here are just a few more devices, protocols and services that are supported:
- Amazon Echo*
- Amazon Dash Buttons*
- Bluetooth
- IFTTT
- KODI Media Center
- Philips Hue Light*
- Sonos HiFi systems*
- Belkin WeMo Switches*
- and much more
The protocols of all new Internet of Things devices (or which are advertised for use in smart home applications) are supported by OpenHAB and more and more are being added.
Install OpenHAB on the Raspberry Pi
There are basically two ways to install OpenHAB. The first (somewhat more complicated) method would be to extend an existing Raspbian system with all packages. The other, more convenient solution is to take a Raspbian system with preinstalled packages, which we also do in this tutorial.
First, we load the latest image on the project page, which has the name “openhabianpi-raspbian -….. xy”. Save the file.
最新的镜像:https://github.com/openhab/openhabian/releases
If your micro SD card is not yet in the card reader of your PC/Mac, insert it. If your SD card is not yet formatted, I recommend the SD Formatter tool. Damit kannst du einfach deine (micro) SD Karten mit zwei Klicks löschen und anschließend weiter verwenden.
SD Formatter工具:https://www.sdcard.org/downloads/formatter/
We now need a tool that can write “.xy” files to an SD card. For this I use Etcher, which is available for all common operating systems. Download and install it. Then select the currently loaded file and press “Flash!”.
Etcher:https://www.balena.io/etcher
The process takes a few minutes. In the meantime, you can already connect an Ethernet cable to the Pi. If you cannot establish an Ethernet connection, you can also enter the WiFi data right away.
After the image has been written to the SD card, open the directory in File Explorer. There is a file called “openhabian.conf” which has to be opened with a text editor. Below are two commented lines (with hash/hash tag). Remove the hashtags at the beginning and enter the name and password of your WiFi network and then save the file:
#wifi_ssid="My Wi-Fi SSID"
#wifi_psk="password123"
Now insert the micro SD card into the Pi and connect the power. You should not disconnect the power supply for the next 45 to 60 minutes, as all necessary updates and configuration files will be loaded! Only after all points are finished (installation steps are displayed, so you can restart if necessary.
If everything went well and your Raspberry Pi is connected to the network, you can now open the page in a browser: http://openhabianpi:8080
The setup menu (step by step instructions below in the article) is displayed.
Update OpenHAB via SSH access
You can either enter the commands directly using the keyboard on the Pi, or via SSH. Choose your SSH client (like Putty) and enter the following hostname:
openhabianpi
The default username is openhabian and the password is also openhabian. You could also change this in the configuration file beforehand.
If the update process is not yet finished, this will be displayed. Otherwise, or as soon as this process is finished, the output will look like this:
Here we call the following:
sudo openhabian-config
If you have changed the password, enter the current one as confirmation, otherwise the default password: openhabian
First we select “01 | Update” and then “02 | Update System”. Under “30 | System Settings” you can change further settings such as the host name, the time zone (the default is already Berlin), passwords, ports and your WiFi network.
With “Optional Components” you can add some more packages like Node-RED and many more be installed, which includes you can easily create your own IoT applications without having to program much. If necessary, I can go into this in another tutorial.
Raspberry Pi Home Automation: OpenHAB User Interface
Of course, we also want a user interface or a graphical user interface for our Raspberry Pi home automation, from which we can control our later components – from smartphones, tablets, touch screens or PCs. Therefore, we are setting this up now.
First, we open http://openhabianpi:8080/start/index on a browser in the same network:
http://openhabianpi:8080/start/index:
Here we choose “Standard”, which pre-installs some themes. Later, more can be added manually.
After a few moments, we can now determine the appearance of the user interface. HABPanel, Paper UI and Basic UI. You can change the selection later. For example, I like the Paper UI.
On the left side, you will find the menu. That selection can be made here. Since we have not installed anything yet, the control menu is also empty. Under Configuration we can make settings and install new packages for our Smart Home under Add-ons.
Install OpenHAB add-ons on the Raspberry Pi
In order to be able to use certain modules such as Phillips Hue Light or Z-Wave, the necessary files must first be installed. In this example, we start with network binding, which checks whether a device is currently in the local network. This can be used to trigger certain actions as soon as you are away, for example.
We switch to “Add-Ons” on the left and then to the “Bindings” tab. Here we are looking for the package to be installed, in our case “Network Binding”. After clicking on “Install” the process may take a few minutes.
As soon as everything has been installed, we navigate back to the “Inbox” in the left bar. A click on the big plus (+) shows the installed bindings, including our network binding. If we select this, the devices are searched for in our network.
If you are unsure which internal IP address your smartphone has, you can either look in your router (192.168.1.1) or directly in the settings of your device.
With a click on the blue tick, we can add devices and name them accordingly. Other devices can also be ignored or deleted from the list (ignoring them only temporarily does not show them, deleting them completely makes them disappear until you search again).
We now switch to Configuration> Things. Select the device you just added. Here you will find the two items “Online” and “Time”. We select these two checkboxes:
By clicking on the pin button above, certain settings (IP address etc.) can be changed. Other devices have different items that can be activated as needed.
If we now switch under “Control” in the left panel, we see the activated device. Below that are the activated items. In our case, this is simply a boolean value or a number, but could also be temperature values and the like include.
In further tutorials, we will install and configure other things.
Create Your Own OpenHAB Things and Items – Display CPU Temperature
Finally, I would like to show how easy it is to create your own modules. However, this step is optional and can be skipped if necessary.
OpenHAB knows two types of objects: Things and Items. Put simply, a thing is displayed as a tile in the control panel and can have several items (values, buttons, etc.). In a small example we will display the CPU temperature:
In this example, we will create a thing with two items (temperature and time when it was last queried).
Since we will execute shell commands for this, an add-on must first be installed for this. Search under Add-Ons -> Bindings for Exec Binding (Doku) and install it.
Then connect to the Raspberry Pi via SSH. First we create a script that reads out the CPU temperature. This could also be a Python script, Java or C ++ program.
sudo nano /etc/openhab2/scripts/cpu_temp.sh
It gets the following content:
#! /bin/bash
INPUT=$(/bin/cat /sys/class/thermal/thermal_zone0/temp)
TEMP=$(echo "scale=3; $INPUT / 1000" | bc)
echo $TEMP
Save it with CTRL + O and exit the Nano Editor with CTRL + X.
We now grant the necessary rights:
chmod +x /etc/openhab2/scripts/cpu_temp.sh
As a test, you could already run it, which should show the temperature: bash/etc/openhab2/scripts/cpu_temp.sh
It continues with the creation of our things with the following content:
sudo nano /etc/openhab2/things/cpu_temp.things
Thing exec:command:cpu_temp "CPU Temperatur" [command="bash /etc/openhab2/scripts/cpu_temp.sh", transform="REGEX((.*?))"]
After saving and closing the editor we create the items:
sudo nano /etc/openhab2/items/cpu_temp.items
Here we insert the following two items:
String CPUTEMPRaw "Temperature [%.1f °C]" (All) {channel="exec:command:cpu_temp:output"}
DateTime CPUTEMPLastExecution "Executed[%1$tH:%1$tM Uhr]" {channel="exec:command:cpu_temp:lastexecution"}
Further information on the details of the two (Things and
https://www.openhab.org/docs/configuration/things.html
https://www.openhab.org/docs/configuration/items.html
导航回控制面板后,你将看到上面显示的视图。
在您的网络外部访问面板
您还可以使用内部网络名称(http://openhabianpi:8080)从智能手机或平板电脑访问控制面板。此外,还有适用于Android设备和iOS iPhone或iPad的应用程序。
然而,您可能还希望在外出时(例如,在移动中或在工作场所)访问您的智能家居控制面板,但如果不采取预防措施,这是不可能的。为此,您需要做到以下几点:
- 一方面,如果您没有静态IP地址,则需要一个使用动态域名系统(Dynamic Domain Name System,简称dDNS)服务的账户,如NoIP或DynDNS。
- 此外,您需要在路由器的“端口转发”设置中为您的Raspberry Pi(Raspberry Pi)的内部IP地址激活端口(对我们来说为8080)。
- 另外,当全局IP地址更改时,必须通知dDNS服务。您的路由器可能提供此选项(如Fritz Box等路由器有相关设置),或者您可以在Raspberry Pi上执行此步骤。有关此步骤的教程可在此处找到。
但请注意,控制面板未受密码保护,因此您很容易面临安全风险。因此,除非您已采取适当的预防措施并确保自己的操作无误,否则我不建议将面板设置为可从外部访问。
原文链接: https://tutorials-raspberrypi.com/raspberry-pi-home-automation-install-configure-openhab/