OpenHAB is a mature open source home automation platform that runs on a variety of hardware and is protocol agnostic, which means it can connect to almost any home automation hardware on the market today. If you’re frustrated by the number of vendor-specific apps you need to run just to control your lighting, I have great news for you: OpenHAB is the solution you’ve been looking for, the most flexible smart home. hub you ever find.

This guide is available for download in PDF format. Download Getting Started with OpenHAB Home Automation on Raspberry Pi now . Feel free to copy and share this with your friends and family.

Unfortunately, that’s pretty much what you can get from consumer-friendly — but as always, that’s where it comes in: we’ll show you how to get started with the ultimate smart home system that doesn’t have to be bought with money (because OpenHAB is 100% free — just supply the equipment).

The first part of this guide focused on how to set up OpenHAB with Raspberry Pi 2, but the rest of the tutorials and tips can be applied wherever OpenHAB is installed.

This guide covers three introductory topics and one more advanced one.

  • Installing and running OpenHAB on the Pi and setting up a demo house configuration to test the functionality of the main systems.
  • How to add bindings and profiles for devices. I will work with Philips Hue.
  • Enable remote access and connect to IFTTT.
  • Adding a DIY presence sensor using Bluetooth and an introduction to the REST interface.
  • Setting up the OpenHAB mobile application.

What you need

At the very least, you’ll need a Raspberry Pi (preferably v2) and an Ethernet adapter or wireless adapter (preferably Ethernet, this guide won’t include Wi-Fi adapter instructions.). Everything else is optional. Please note that OpenHAB will work on the original Raspberry Pi as well, but there is a known issue with slower processing and Z-Wave devices. If you don’t need Z-Wave, you can safely ignore this warning and move on to a Raspberry Pi model B or B+ because everything else works fine. You can always upgrade to the latest Pi if and when you add Z-Wave.

Photo of a Raspberry Pi 2 used as an OpenHAB controller
This little thing could be the best smart home you’ve ever had!

At the time of writing, the latest stable version of OpenHAB is version 1.71; version 1.8 is expected soon and everything in this guide should still be up to date, although some bindings may have more features. Version 2 is also currently available as a very early alpha preview, but adopts a fundamentally different architecture compared to the OpenHAB 1 series: this guide is not compatible with version 2.

I highly recommend that you follow this guide slowly and methodically. Don’t try to jump into the deep end and add everything at once. Yes, this is a long guide — OpenHAB is a complex system that often needs tweaking to suit your needs, and the best way to ensure success is to work slowly and do one piece at a time.

The good news is that once it’s up and running, it’s incredibly useful and incredibly useful.

Installing OpenHAB

There is no preconfigured image for OpenHAB, so installation is done the old-fashioned way via the command line. I suggest you work without RPi support — the overhead of managing a graphical interface that you will rarely use is not worth it.

Start with the latest (full) Raspbian SD image (not the «lite» version, this does not include the Java Virtual Machine). Plug in the network cable, then boot up and SSH into it. Run:

sudo raspi-config 

Expand the file system; and from the advanced menu, change the memory division to 16. When you’re done, restart, and as a good practice, run a full refresh

 sudo apt-get update sudo apt-get upgrade 

The easiest way to install the OpenHAB runtime is via apt-get but first we need to add a secure key and a new repository:

 wget -qO - '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 sudo update-rc.d openhab defaults 

Curiously, everything was set to be owned by «root». We need to fix it with the following commands.

 sudo chown -hR openhab:openhab /etc/openhab sudo chown -hR openhab:openhab /usr/share/openhab 

Next, we’ll install Samba and share the configuration and user folders — this will make it easier to install add-ons and modify the sitemap remotely.

 sudo apt-get install samba samba-common-bin sudo nano /etc/samba/smb.conf 

Change the workgroup name if necessary, but otherwise enable WINS support:

 wins support = yes 

(you need to uncomment the line and change «no» to «yes»)

then add the following to the share definitions section (scroll to the very end of the long file):

 [OpenHAB Home] comment= OpenHAB Home path=/usr/share/openhab browseable=Yes writeable=Yes only guest=no create mask=0777 directory mask=0777 public=no [OpenHAB Config] comment= OpenHAB Site Config path=/etc/openhab browseable=Yes writeable=Yes only guest=no create mask=0777 directory mask=0777 public=no и [OpenHAB Home] comment= OpenHAB Home path=/usr/share/openhab browseable=Yes writeable=Yes only guest=no create mask=0777 directory mask=0777 public=no [OpenHAB Config] comment= OpenHAB Site Config path=/etc/openhab browseable=Yes writeable=Yes only guest=no create mask=0777 directory mask=0777 public=no 

I also commented out the «Printers» section. I did two promotions since the config files are actually kept separate from the add-ons.

Save and exit. Finally, we need to set the Samba password for the openhab user:

 sudo smbpasswd -a openhab 

I would suggest «openhab» as the password just for ease of use, but it doesn’t really matter.

Thanks to reader David L. — it looks like the Samba restart method has changed in the latest Raspian. Here are the updated instructions:

 sudo update-rc.d smbd enable sudo update-rc.d nmbd enable sudo service smbd restart 

After restarting Samba (older installations use restart samba service sudo ), check access to the shared drive. It may not be auto-detected on a Mac; but you can use Finder -> go -> Connect to Server and address

 smb://openhab@raspberrypi.local 

Log in with the openhab username and password of your choice, then open both of your folders to take a look around. You can even open http://raspberrypi.local:8080/ in your web browser, but you will run into an error because we haven’t created a sitemap yet. This is fine.

error on first launch of openhab

Now is a good time to learn the command to bind the OpenHAB log so you can keep an eye on the errors.

 tail -f /var/log/openhab/openhab.log 

Go ahead and always open it in a separate SSH window as you continue through the tutorial.

Set up a demo house

Before we get into the intricacies of config files, adding devices and bindings, etc.; Let’s test that everything works by adding some demo content. You will find «Demo Setup» in the download section of OpenHAB.org.

Once you have unzipped it, there are two folders: additions and configuration .

openhab demo content

Using network resources copy configuration in OpenHAB Config shared folder and overwrite the existing folder. Copy additions to another general resource OpenHAB overwriting the existing folders again. If you are not prompted to overwrite something, you are doing it wrong. If you notice the debug log file, you should see a burst of activity as it notices new bindings and a buzz in action. Open raspberrypi.local:8080/openhab.app? Sitemap = demo to see a demo.

Screenshot of the web interface of the OpenHAB demo house

It’s a bit simplistic at the moment, but OpenHAB’s openness means we can install a great new theme later, or a completely alternative interface. For now, we just needed to know that everything was working. Please note that what we are looking at is called a map site (has nothing to do with the sitemap). The sitemap describes the user interface — not the actual devices on your network or sensors — only the interface for viewing them. Every part of it is fully customizable. To see how it was created, open the file sitemaps / demo.sitemap in general OpenHAB Config folder.

sitemap demo file

It’s pretty tricky, but for the most part, you’ll be copying code snippets from examples elsewhere to create your own interface. Here’s a technical overview of all the possible sitemap elements, but for now, it’s enough just to think about what kind of interface you want to create and what information you want to display.

While you’re there, open up items/demo.items too. Again, it looks scary, but here you create controls and define sensors for tracking.

So how does OpenHAB work?

Now that you’ve taken a quick look at the sitemap and items folder, let’s break down exactly what these files are and the other core OpenHAB components that come together to create a complete smart home. You will find subdirectories for each of them in the OpenHAB Config shared folder.

Items is an inventory of each control device, sensor, or information item that you want to use in your system. It also doesn’t have to be a physical device — you can define a web source like the weather or stock prices. Each element can be given a name, assigned to multiple groups (or none), and associated with a particular binding. (Beginner tip: Capitalization is important when it comes to anchors. I spent a lot of time trying to figure out why my «Hue» bulbs weren’t working; it was because they were supposed to be «hue» instead.)

Sitemaps only apply to the interface you will see when you open the OpenHAB mobile or web application. You can control exactly how you want the buttons to be positioned and the information to be presented. You can define top-level groups for each room in your house; by clicking on each of them, you will see a list of all the devices in that room. Or you might prefer to show groups for each type of device: a button for lights, another for electrical outlets. There may be some devices you use so often that you just need a toggle for them right on your home screen.

Rules This is where the home automation aspect comes into play, where you can define a schedule or conditions for an action to take place. Simple events like turning the bedroom lights on at 10pm a warm red; or more complex logic, such as turning on the heater if the temperature is below 0 and someone is present in this room. You will also find a folder scenarios which offers functionality similar to rules, but at a more complex level of programmable logic.

permanence is a complex topic that we won’t cover in this tutorial, but persistence defines the data you want to keep. By default, OpenHAB will only show the current state of something; if you want to track this value over time, you need to set up a persistence definition for this data source. In this you will specify things like how often a data point should be measured, or when to discard old data points — you will also need to specify what type of persistence mechanism to use, such as MySQL or simple file writing. ,

transformation contains mappings of data values ​​to labels. For example file humidex scale defines the range of moisture index values ​​and how they should be displayed in English: 29-38 is «some discomfort».

Map site and files elements required to run OpenHAB; the rest are optional. You can have multiple sitemaps and elements so you can save the demo content and come back to it at any time, or try out a new layout for your home control interface. Don’t worry if this all seems overwhelming, we’ll break it down into manageable parts and I promise you will have the confidence to build your own OpenHAB installation by the end of this guide.

Next, we’ll walk you through adding some common smart home kit, starting from scratch in a new sitemap. Each of these will introduce some basic concepts such as setting bindings and element definitions, so I highly recommend that you read these instructions, even if you don’t have those specific devices .

Start by creating a new (empty) file home.items and a new file home.sitemap in the respective directories. Open home.sitemap and paste the following code. This just acts as a basic framework that we will add bits to later.

 sitemap home label="My Home" { } 

You should see a notification that OpenHAB has identified a new sitemap and item file.

openhab new sitemap and items

Enable debug mode

While you’re still trying to get OpenHAB to work correctly, it can be helpful to include a more verbose debug log that lists everything, not just the important stuff. To enable this mode, open the OpenHAB Config shared folder and edit logback.xml file . On line 40, change the following line to read DEBUG instead of INFO. You will need to reboot after changing this.

   

This is a global change, so you’ll get a lot more information once you include the log file.

Adding Philips Hue

I’m going to start with Philips Hue. Like most things you will want to interact with in OpenHAB, Hue lamps require you to install binding Think of bindings as a device driver. At the time of this writing, there were about 160 bindings available for OpenHAB 1, which is why OpenHAB is such a powerful system — it can interact with anything, bringing all these disparate control systems together into a single unified interface. Here is a demo and a quick overview of the steps.

Bindings need to be loaded first, and the easiest way to do this on the Pi is to use apt-get and then force ownership of the openhab user.

 sudo apt-get install openhab-addon-binding-hue sudo chown -hR openhab:openhab /usr/share/openhab 

You then have to tell OpenHAB to load this binding and set up any necessary variables. Go to config folder and make a copy openhab-default.cfg by calling her openhab.cfg . Open it, find HUE and replace the entire section with the following code. The only thing you need to change is your bridge’s IP value — if you don’t already know that, try an online discovery tool. The secret value doesn’t really matter, it’s just a kind of username that OpenHAB will use to identify itself on the bridge.

Advice : to include a line, just remove the # from the beginning. By default, the line specifying the bridge’s IP address is disabled (or technically «commented out»). Also, if you’re trying an alternative configuration, it might be helpful to just copy the existing line and put a # at the beginning to mark it as a comment so you can easily come back if something goes wrong.

 ################################ HUE Binding ######################################### 
 # IP of the Hue bridge hue:ip=192.168.1.216 hue:secret=dotcom hue:refresh=10000 

openhab config

Save and exit. As with any third party Hue app, you need to approve OpenHAB on the Hue bridge by pressing the button on the front — you only need to do this once. You will see a message about waiting for pairing if you’re setting up a log file, but if you forgot or missed the countdown, just reset the Pi — you’ll get a 100 second timer from the moment the Hue binding is initiated. Please make sure you have successfully paired before proceeding.

Then open the file home.items to which we will add some Hue bulbs. Here is an example of an element definition:

 Color Bedroom_Hue "Bedroom Hue"  (Bedroom) {hue="1"} 
  • Word » Color» specifies how much control we have over this element. RGB Hue lamps have «Color» as we have full control over them. Other lights may just be a switch.
  • Next comes the element codename: I chose Bedroom_Hue but literally everything is fine — just something descriptive that feels natural to you because you’ll need to remember that later when creating the sitemap. The codename must not contain spaces.
  • There is a mark between the quotes. In our case, this is simple, but for some elements, such as temperature or something that reports a value, you will add special code that tells how to display this value or with what transformations. The label is for an interface and may contain spaces.
  • Between the angle brackets is the name of the icon. All available icons can be found in the shared resource OpenHAB in webapps/images directory . There are actually a whole range of tint icons that represent different brightness or on/off. Just provide the icon’s base name — OpenHAB will know to automatically look for different on/off icons if it’s a toggleable item. It’s not obligatory.
  • In parentheses, we say which groups to refer to, in this case, just the group bedrooms .
  • Finally, and very importantly, we bind the element with the appropriate binding with any required variables. In this case, the binding shade and the number of bulbs is 1. You can find the number by opening the official tint app and looking at the lights tab. Each light bulb has a unique number.

I added a total of four bulbs, as well as a simple group declaration, which we’ll cover later. Here is my complete home.items at the moment:

 Group Bedroom Group Office Group Kai Group Living_Room Group Cinema Group Secret 
 Group Lights 
 /* Lights */ Color Bedroom_Hue "Bedroom Hue"  (Bedroom,Lights) {hue="1"} Color Office_Hue "Office Hue"  (Office, Lights) {hue="2"} Color Secret_Hue "Secret Hue"  (Secret, Lights) {hue="3"} Color Kai_Hue "Kai's Hue"  (Kai, Lights) {hue="4"} 

Text /*Lights*/ is just a comment, it has no function other than to help us scan the file later when it gets bigger. We now have devices added, but opening http://raspberrypi.local:8080/? Sitemap = home results in an empty interface — of course, because we haven’t created any interface elements in the sitemap yet. Let’s start very simply now. Open home.sitemap .

The code used to describe the interface is different from elements, but for now we will create a new «frame» and add a couple of group controls along with some icons.

 sitemap home label="My Home" { Frame { Group item=Lights label="All lighting" icon="hue" Group item=Bedroom label="Bedroom" icon="bedroom" Group item=Office label="Office" icon="desk" } } 

Groups are a useful tool for quick testing, but you really need more control over how elements are displayed. For now, this will be enough. Save and reload your home sitemap in your browser. You should see this (or any groups you have added).

openhab first sitemap

Click » All sources of light to see all of the Hue lights since we’ve defined them as belonging to this general light group.

openhab lights group hue demo

Note that the Office Hue item is shown with a different icon — this is because my office light is already on and OpenHAB knows this when it talks to the Hue bridge and was smart enough to customize the icon for the «on» version of the file. Unfortunately it doesn’t reflect the color, but if you have the mobile app installed, it will display the current color.

If you see more items than you thought, you have defined or received errors about multiple definitions, be aware that while you can only load one sitemap at a time per page, all sitemaps will extract items from all .item files so if you left a demo element file there, it’s possible that additional elements will appear in your groups. At this point, I would suggest backing up the content of the demo items and moving it out of the folder to avoid duplication errors.

Remote access and IFTTT with My.OpenHAB

Right now, you need to be on the same local network to access your OpenHAB system, but what if you want to monitor your devices and check sensors when they are out of range of your Wi-Fi? To do this, we need to set up remote access — and we will do it in a simple way, using the My.OpenHAB web service, which bypasses the need to fiddle with port forwarding and router settings. As a bonus, My.OpenHAB also has an IFTTT channel giving you endless possibilities for remote control and automation.

First: set up a binding. Tip: If you don’t know the exact name of a particular installation package, try looking it up with apt-cache.

apt-cache search

 sudo apt-get install openhab-addon-io-myopenhab sudo chown -hR openhab:openhab /usr/share/openhab 

Before you can register on the My.OpenHAB website, you need to generate a secret key and find your UUID, which uniquely identifies your installation. Check under OpenHAB Home share -> webapps -> static, and you should find the UUID file containing your unique id. It was at this point that I discovered that my Pi was using an older version of Java that was not generating the secret key correctly. Type of

 java -version 

Check. If it doesn’t say 1.7 or higher, you have the wrong version. Oddly enough, the latest version of Raspbian comes with Oracle Java 8 installed, but not by default.

  альтернативы обновления sudo --config java 

update java version

Select the option that indicates jdk-8-oracle then restart OpenHAB. Bonus: Oracle Java 8 is faster than standard OpenJDK!

Now you should also find the secret file in folder webapps/static . Open secret and uid and get ready to copy paste.

and mystery

Now proceed to create a My.OpenHAB account using these details and then come back — you also need to verify your email before anything will work. There are a couple more steps to this. First, we need to set the default persistence mechanism to myopenhab (persistence is something for further guidance, but regardless we need to set up something basic to «export» our data to the online service and make it visible to IFTTT) , To do this, open the openhab.cfg file and look for the variable that says persistence: default= and change it to persistence: default = myopenhab . Save.

Finally, create a new file in the folder configuration / persistence with name myopenhab.persist and paste the following rule.

 Strategies { default = everyChange } Items { * : strategy = everyChange } 

You don’t need to understand it yet, but know that it says «preserve each element’s state as it changes».

my open items

To connect to IFTTT, go to the OpenHAB channel — you will need to authenticate and give it access to your MyOpenHAB account. Also note that as long as your items haven’t changed at least once, they won’t be visible in the item list on IFTTT, so if it’s not visible, toggle something on and off, then reload. Congratulations, you now have full IFTTT access to everything on your OpenHAB system!

openhab ifttt channel

Bluetooth presence sensor using REST

Some time ago, I showed you how to make an automatic office door lock using Bluetooth. scanning to detect user presence — I wanted to bring something similar to OpenHAB.

On any platform other than the Raspberry Pi, this will be easy thanks to out-of-the-box Bluetooth tethering; unfortunately it doesn’t work on the Pi due to a critical Java file that will need to be recompiled for the ARM architecture, added to the binding and then rebuilt the binding. Suffice it to say that I tried this and it was terribly difficult and didn’t work. However, there is a much simpler solution that also serves as a good introduction to OpenHAB’s explicit extensibility: we simply adapt our previous Python script to report directly to the OpenHAB RESTful interface.

Also: A RESTful interface means you can interact with the system using the built-in web server by simply calling URLs and passing or fetching data. You can visit this URL to see a simple example of this on your own server: http://raspberrypi.local:8080/rest/items — which outputs an encoded list of all your defined items. This is an incredibly powerful tool, as it unlocks the full potential of OpenHAB and allows you to create your own interfaces; or vice versa to report the state of the sensors without having a specific binding. We will use this capability to report the presence of a specific Bluetooth device without resorting to Bluetooth tethering.

Start by adding a new element switch to your file home.items . I named mine «JamesInOffice» and made it a toggle rather than a simple on/off contact so I could manually control my presence in the event my phone died.

 Switch JamesInOffice "James in Office" (Office) 

Note that I haven’t defined an icon or attached a specific anchor. It’s just a general switch.

Then insert a compatible Bluetooth USB dongle and install a few basic tools to interact with it.

 sudo apt-get install bluez python-bluez python-pip sudo pip install requests hcitool dev 

The last command should show your bluetooth adapter. If nothing is listed, try a different adapter, yours is not compatible with Linux. The next step is to find the Bluetooth hardware address of your device.

 wget https://pybluez.googlecode.com/svn/trunk/examples/simple/inquiry.py python inquiry.py 

Make sure your phone is open on the Bluetooth settings page (which puts it in pairing/public mode) and obviously Bluetooth is activated. You should find the hexadecimal hardware address in the list.

Bluetooth identified

From your Pi user’s home directory, create a new Python script and paste in this code.
There are a few things you need to edit, starting with your specific device address:

 result = bluetooth.lookup_name('78:7F:70:38:51:1B', timeout=5) 

As is the this line, which is in two places (yes, it could probably be structured better). Change JamesInOffice to the codename of the radio button you defined.

 r = requests.put("http://localhost:8080/rest/items/JamesInOffice/state",data=payload) 

The last step is to tell this script to run at boot time.

 sudo nano /etc/rc.local 

Scroll down to the bottom and before exit 0 add the following lines:

 python /home/pi/detect.py & 

The & sign means «do this in the background». Run the script if you haven’t already and open your OpenHAB interface. If you added it to a group, click on that group. The update takes about 10 seconds, but you will see the default light bulb icon turn on or off depending on whether your phone is detected or not. Check the log file, if nothing happens, you may have used the wrong element name.

OpenHAB mobile application

While you can of course use the web interface from a mobile device, OpenHAB has built-in apps for iOS and Android — and they look like much better than the default browser interface. On the settings screen, enter the local URL as the internal IP you have been using so far, including the port number. For remote URL enter https://my.openhab.org as well as the username (email address) and password that you entered during registration. If you haven’t signed up for MyOpenHAB yet, just leave the authentication and remote URL fields blank, but you will only be accessing your system from your local Wi-Fi.

openhab mobile

Moving Forward and Getting Help

The amount of customization and useful features you can add to your OpenHAB controller is truly epic. Along with an extensive list of supported devices with bindings, you can use the RESTful interface, HTTP and IFTTT extensions to read or control literally any type of IoT device, and then some (try some of our creative lighting ideas). Yes, it’s an absolute pain to install, but no commercial system can come close to the power of a custom OpenHAB system.

However, the trip was not at all easy for me, which is why I wrote this guide to make the process easier for you. And if you find the OpenHAB system overwhelmed, there are other options when it comes to Raspberry home automation — like using a Raspberry Pi to automate your garage door to automate your garage door. for example.

Stay tuned in for an extended guide that covers Z-Wave and other cool tricks you can tweak.

If you need help with a particular part of this guide, please ask in the comments. If you need help with other must-haves or some optional topics we haven’t covered yet, the official OpenHAB forums are the place to be.

Похожие записи