With the first introduction, we now have Openhab and demo house installed, so let's take a look at how Openhab works.
How does Openhab work?
Next you will see a subdirectory of the Sitemap and items in the shared folder configured by Openhab.
Items are libraries that store every control device, sensor, or information element that you want to use in your system. It is not necessarily a physical device, you can define a network source such as weather or stock. Each item can be named or specified in multiple groups and connected to a specific bindings.
When you open Openhab Mobile or Web application, Sitemaps only cares about the interface you will see. You can control how the buttons are placed and how the information is rendered. You can define the highest-level groups for each of your rooms and click on the device list for each room. Or you might prefer a type of device as a group.
Rules are a place where home automation plays a role, where you can preset the schedule or conditions for opening your device. For example, in the evening 10 o'clock open the bedroom red lights, or, if the temperature is less than 0 or someone in the room when a heater is opened. You can find a script folder that provides similar functional rules.
Persistence engine is an important issue, but this article does not mention. By default, Openhab only shows the status of things, and if you want to keep track of the data over time, you need to set a data source for continuous values. In this you can set how often data points are measured once, or delete old data points, and you also need to tell it what database to use, such as MySQL or simple logging to a file.
You can also change the data value into a text label. For example, the Humidex.scale file defines the range of humidity values and how to convert to text: 29-38 "some discomfort".
In Openhab, the sitemap and items files are essential, and the rest is optional. You can have multiple sitemaps and item, so you can control the demo content, or try a new home control interface.
Next, we'll add some common smart home kits, starting with a new sitemap. Each one will introduce some core concepts, such as how to install bindings, and define the items.
First create a new (blank) Home.items file and create a new Home.sitemap file in the relevant directory. Open Home.sitemap Paste the following code. This is just as a basic skeleton and we'll add bits after that.
Sitemap home label="My home"{}
You should see a notification that Openhab has identified a new sitemap and items file.
Enable debug mode
When you are trying to get Openhab to work correctly, enabling debug mode is handy for your work, and its verbose debug log lists all the useful things. To enable this mode, open the Openhab Configuration shared folder and edit it in Logback.xml. In line 40, change debug to info. After that you need to reboot.
<logger name="org.openhab" level="INFO"/>
Once you track the logs you will see a lot of information.
Add Philips Hue
This article uses Philips Hue as a demonstration. Hue bulb requires you to install a binding--think of him as a device driver. There are currently about bindings for Openhab 1, which is why Openhab is a powerful system that can be combined with all the different control systems into one unified interface.
Bindings must be downloaded first, the simplest method on the Raspberry Pi is to use apt-get and then give control to the Openhab user.
sudo apt-get install openhab-addon-binding--hr Openhab:openhab/usr/share/openhab
Next, you need to tell Openhab to load the binding and configure all the required variables. go to the configuration folder, copy a copy of Openhab-default.cfg, and name it openhab.cfg. Open the file, search for hue, and replace the entire section with the following code. The only thing you need to change is the IP value of your bridge, and if you don't know, try searching the tool online. The value of secret is not important, it is just a openhab used to identify the bridge user name.
################################ HUE Binding #########################################
# IP of the Hue bridgehue:ip=192.168. 1.216 Hue:secret=Makeuseofdotcomhue:refresh=10000
Save and exit. If you're tracking a day piece, you'll see a message that lets you wait for pairing, but if you forget or miss the countdown, you just have to reset the Raspberry Pi, and when the hue binding starts you get another 100 seconds timer. Make sure that you have successfully paired before continuing.
Next, open the Home.items file and we'll add some hue bulbs. The following is an example definition:
" Bedroom Hue " "1"}
· Color specifies what kind of control we have on this project. RGB-Tone bulbs are "color" because we have full-color control over them. The other lights may just be a switch. Second is the project code: I choose Bedroom_hue, anything can be, but you have to remember to make a sitemap later. The code must not have a space. • The tag is between the quotation marks. In this case it is simple, but for example temperature or a value, you need to add some special code that tells it how to display the value or what kind of conversion to use. The label is for the interface, and it can have spaces. • The name of the icon between the angle brackets. You willfind all the available icons in the WebApps/images shared by Openhab. In fact, the entire series of tonal icons represent different brightness or on/off. Just specify the name of the base icon – if this is a toggle, Openhab will automatically find/ close the icon. This is optional. • In parentheses, we tell it which is part of the group, in this case mostly just the bedroom group. • Finally, most importantly, we bind the item appropriately to any variable that needs it. In this case, the hue bulb is bound to a number of 1. You can find this number by opening the official Tone app and then look at the Lights tab. Each bulb has a unique number.
Four bulbs are added here, which is the complete home.items:
Group bedroomgroup officegroup kaigroup living_roomgroup cinemagroup Secret
Group Lights
/*Lights*/Color Bedroom_hue"Bedroom Hue""1"}color Office_hue"Office Hue""2"}color Secret_hue"Secret Hue""3"}color Kai_hue"Kai ' s Hue""4"}
/* Lights */text is just a comment, it doesn't have any features except to help us scan the file. Now we have the added device. But opening http://raspberrypi.local:8080/?sitemap=home is a blank interface because we haven't created the interface element in the sitemap yet. Let's start now.
The code used to describe it is different, but now we create a new "frame" and add some group controls as well as 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" } }
A group is a tool for quick testing, and in reality you want more control over how items are displayed. Now, that's enough. Save and load your home sitemap in the browser. You should see this.
We have put them in a light group, click on all lighting to see.
Note that the office Hue item shows a different icon – because the office light is already open at this time, Openhab will intelligently adjust the icon to "on" version. However, it does not reflect the color, but if you install the mobile app, it will reflect the current color.
If you find something extra, don't worry. You know, although you can only load one sitemap at a time, all items are displayed on one page from all the. Item files. So if you have left the code entry file, there may be some additional entries in your group. It is recommended that you back up your code content and move it out of the folder to avoid duplicate errors.
Use My.openhab IFTTT remote access
Now you need to access the Openhab system on the same local network, but what if you want to control your device and check the sensor outside of your Wi-Fi range? Then we need to set up remote access – The simplest way is to use the My.openhab Web service, which bypasses port forwarding and router configuration. The My.openhab service also has a IFTTT channel that allows you to remotely control and automate.
Install the binding first. If you don't know the exact name of a particular installation package, you can find it through Apt-cache.
sudo apt-get install openhab-addon-io--hr Openhab:openhab/usr/share/openhab
Before registering with the My.openhab website, you need to create a key and find your uuid--uniquely identifying your installation. Then check Openhab Home share, WebApps, static, you should be able to find a UUID file that contains your unique ID. Now that the Raspberry Pi you are using is using an older version of Java, the secret key is not created correctly.
Java-version
Take a look at your Raspbian. if it is not version 1.7 or later, then your version is not available to be updated quickly. Oddly, the latest version of Raspbian installed Oracle Java 8, but it was not set to the default.
sudo update-Alternatives -config java
Select Show Jdk-8-oracle, and then restart Openhab. Oracle Java 8 is faster than the default OPENJDK!
Now you need to find a secret file in the Webapps/static folder. Open Secret and UUID, and prepare to copy and paste.
Now go to create a My.openhab account, use the content, and come back – you'll also need to confirm your email first. First, we need to set the default persistence engine for Myopenhab (we need to build some basic things to "export" our data to online services and make IFTTT visible). To do this, open openhab.cfg, find the variable persistence:default= and change to Persistence:default=myopenhab. Save.
Finally, create a new file in the Configurations/persistence folder named Myopenhab.persist, and then copy and paste the contents below.
Strategies { default = Everychange}items { *: Strategy = Everychange}
You don't need to understand this now, but remember to "save each item's state every time you change it."
Go to the Openhab channel (you need to verify and allow it to access your Myopenhab account) to connect IFTTT. Also note that unless your item has changed at least once, the list of items on the IFTTT will not appear, so if it does not appear, switch the switch and reload.
Congratulations, you can now access your Openhab system through IFTTT!
Using the Bluetooth status sensor with rest
On any other platform, there are ready-made Bluetooth bindings. However, because the Java file needs to be recompiled into an arm architecture, the off-the-shelf Bluetooth bindings are not implemented on the Raspberry Pi, and must be added to the binding before rebuilding the binding. This is very complex and not yet achievable. But there's a simpler solution: tweak the previous Python script and pass it directly to the Openhab restful interface.
The restful interface means that you can use its built-in Web server to interact with the system, just by invoking the URL and passing or fetching the data, you can access the URL on your own server to see a simple example: http://raspberrypi.local : 8080/rest/items--outputs all the list of project encodings you have defined. This is powerful because it takes full advantage of the full potential of the Openhab, and also allows you to write custom interfaces, and passes the sensor state without requiring a specific binding. We use this to present the presence of a particular Bluetooth device.
First add a new switch entry to your Home.items file. We name it "Jamesinoffice" and then make a switch on it, which is not a simple switch contact so that you can manually control the phone when it freezes.
" James in Office " (Office)
Note that I do not have an icon defined or associated with a specific binding. It's just a normal switch.
Next, plug in a compatible USB Bluetooth adapter and install some basic tools that you can interact with.
sudo apt-get install BlueZ Python-bluez python-pipsudo pip install Requestshcitool Dev
The last command is to display your Bluetooth adapter. If not listed, try another adapter. The next step is to find the Bluetooth address of your device.
wget https://pybluez.googlecode.com/svn/trunk/examples/simple/inquiry.pypython inquiry.py
Make sure your phone has the Bluetooth Settings page turned on (get it into pairing/public mode), and it's clear that Bluetooth is activated. You need to find out the hexadecimal hardware addresses listed.
Create a new Python script in your Raspberry Pi user home directory and paste the code in.
Here are some of the things you need to edit, first your specific device address:
result = Bluetooth.lookup_name (039; 7F: +:£ º1b', timeout=5)
And this, replace the jamesinoffice with your defined switch code.
R = Requests.put ("http://localhost:8080/rest/items/JamesInOffice/state", Data=payload)
Finally, tell the script to run when it starts.
sudo nano/etc/rc.local
Scroll down to the bottom and add the following line before exiting:
Python/home/pi/detect.py &
The and symbol means "run in the background". If you add it to a group, click the group. It takes about 10 seconds to update, but if your phone can detect it, you'll see the default light bulb icon turned on or off. If you do not see this situation, check the log, you may have used the wrong item name.
Openhab mobile App
While you can use the mobile device's web interface, Openhab has iOS and Android versions of native apps that look more beautiful than the default browser interface. In the settings, enter the local address as the internal IP, and don't forget the port number. But if you have not registered Myopenhab, you can only access your system from your local Wi-Fi.
Play the Raspberry Pi: A Primer for Openhab (ii)