
Z-Wave based smart home mobile application with OpenRemote

Not so long ago, the Z-Wave module for the Raspberry Pi - RaZBerry, which turns the mini-computer into a full-fledged smart home controller, was introduced. Z-Wave network management is carried out using a web-interface using the HTTP / JavaScript API. Using JavaScript, you can create a set of functions for automation (turning on / off the light, checking the temperature, polling the motion sensor, etc.), which can then be performed by sending an HTTP request.
An OpenRemote product of the same name allows you to create mobile applications for a smart home without programming, while different technologies can be used in one application: Z-Wave, KNX, X10, ZigBee, computer control via ssh, etc.
OpenRemote is a server that executes any commands and interface designer in which you create buttons, switches, labels, etc. And you already assign commands to these elements, in our case these are HTTP requests for executing JavaScript functions on the Z-Wave server.
Next, I will point by point how to create a smart home control panel for iPhone and Android! And this is how our application will look when we finish:

There are a lot of pictures under the cut.
1. Creating a Z-Wave network using the Raspberry Pi + RaZberry controller
- Installing RaZberry software on a Raspberry Pi
- Adding Z-Wave Devices
- Checking device operation
- About the provided automation API
- Creating JS scripts for remote control
2. Installing the OpenRemote controller and mobile application
3. Creating the application design and its logic
- Design development
- Creating control commands and sensors
- Association of buttons with commands
- Mobile App Sync
4. Summary
5. Links
Creating a Z-Wave network using the Raspberry + RaZberry controller
RaZberry is a board connected to the Raspberry Pi via GPIO, which allows you to create and manage a Z-Wave network. For a better understanding read Faq . Included with the board is software that needs to be installed.
Installing RaZberry software on Raspberry
The installer only supports Debian-based distributions (Raspbian, Xbian, etc.). However, you can use the handles to install the software on OpenElec and other OSs.
Run the command to install the Z-Way software:
wget -q -O - http://razberry.z-wave.me/install | sudo bash
After installation, you must definitely restart the Raspberry Pi in order to apply the changes for the Serial port.
To get to the smart home control panel, go to the address http: // IP_OF_YOUR_RASPBERRY: 8083 . The program interface is very simple, it will not be difficult to understand it, there is documentation . The most compatible with the interface are browsers: Chrome, Safari, Firefox, other browsers: IE, Opera may not work correctly.
Adding Z-Wave Devices
Decide on the list of equipment used:
- Relays Fibaro Single Switch 3kW 2 pcs.
- Sensor for opening a door / window and temperature Fibaro Door / Window Sensor 1pc.
In the bottom menu, select Expert Mode . Using the top menu, go to the Network → Network management tab and click Enable (re) device , this will start the process of waiting for the device to be connected to the network, now on the device three times press the service button so that the controller sees and adds it.

Checking device operation
Make sure that the device is working. Go to the Device Settings tab , click on only that added device in the left column and check the Stage of interview , it should be The interview was successful and there should not be any dots and zackes between pluses.

If the interview is not completed (there are dots and signs Ø), then you can repeat it, for this, at the bottom of the screen, click Additional Actions → Force Repeat Interview (Expert mode must be turned on to see this menu).
After a successful interview, on the same tab Device Settings, you can configure some device parameters (do not forget to apply the settings withApply settings to this device at the bottom of the screen, and wake up the device if it is running on batteries):

To manage the device, go to the Device Management tab → Switches

About the provided automation API
After making sure that all devices are working correctly, we will now try to manage them remotely. There are several ways:
1. Using the HTTP / JSON API
2. Using the JavaScript API, the
HTTP / JSON API uses simple syntax to manage devices.
You can turn on the light from the browser:
http://192.168.1.113:8083/ZWaveAPI/Run/devices[6].instances[0].SwitchBinary.Set(255)
Or request a temperature:
http://192.168.1.113:8083/ZWaveAPI/Run/devices[8].instances[2].commandClasses[49].data[1].val.value
The JavaScript API allows you to write various automation scripts, for example: turn on / off the light, interrogate the sensor, get the temperature, turn off the light 2 minutes after turning it on. These scripts can work both independently (for example: at night the light turns on only 15% so as not to blind the eyes), and can be called remotely using the HTTP / JSON API .
The script to turn on the light:
SwitchOn = function(N,I) {
zway.devices[N].instances[I].SwitchBinary.Set(255);
}
Call the script to turn on the light:
http://192.168.1.113:8083/JS/Run/SwitchOn(6,0)
Unfortunately, direct access to devices using the HTTP / JSON API from OpenRemote is problematic for several reasons:
1. The [] characters must be encoded in UTF-8 in the OpenRemote Constructor
2. When polling sensors, OpenRemote expects “on” or “off”, and Z-Wave sensors can send 255 or 0.
3. For each device you have to write your own request, and using JS you can use only one function to turn on various devices, changing only the function parameter in the request - the device number.
When using the JavaScript API, all these problems disappear - several functions of the “helpers” will help transform the Z-Wave terms into concepts convenient for OpenRemote.
Read more about the syntax of the HTTP / JSON API and JavaScript API in the recentRaZberry article on Habré .
Creating JS scripts for remote control
JS scripts are located in / opt / z-way-server / automation / , create a file in which our automation functions openremote.js will be stored so that it automatically loads when Z-Way is turned on, add to the end of the main automation file main.js :
// ======================================================
executeFile(automationRoot + "/" + "tags.js");
executeFile(automationRoot + "/" + "openremote.js");
startAutomation();
/opt/z-way-server/automation/openremote.js
// Включение устройства
SwitchOn = function(N,I) {
zway.devices[N].instances[I].SwitchBinary.Set(255);
}
// Выключение устройства
SwitchOff = function(N,I) {
zway.devices[N].instances[I].SwitchBinary.Set(0);
}
// Запрос на статус датчика (сработал/не сработал)
SensorStatus = function(N,I) {
return zway.devices[N].instances[I].SensorBinary.data.level.value;
}
// Запрос на состояние устройство (включено/выключено)
SwitchStatus = function(N,I) {
return zway.devices[N].instances[I].SwitchBinary.data.level.value;
}
// Запрос температуры с округлением до целого
Temperature = function(N,I) {
return Math.round(zway.devices[N].instances[I].commandClasses[49].data[1].val.value);
}
Parameter N is the device number on the network.
Parameter I - within one device there can be physically several devices (channels), for example 2 relays or a temperature sensor, a motion sensor, an ambient light sensor. Parameter I is the channel number inside the devices. If the device contains only one physical device, then this parameter is 0.
After creating the file, you must either restart the Z-Way command:
/etc/init.d/Z-Way restart
either load the script manually by sending a request from the browser:
http://192.168.1.113:8083/JS/Run/executeFile("automation/openremote.js")
You can check the functionality of the functions from the browser.
Turn on the light:
http://192.168.1.113:8083/JS/Run/SwitchOn(6,0)
Request temperature:
http://192.168.1.113:8083/JS/Run/Temperature(8,2)
The Z-Way server log helps very well in debugging:
tail -f /var/log/z-way-server.log
If everything works, go to the next step!
Installing an OpenRemote Controller
The OpenRemote controller is a server that receives commands from a mobile or web application and then broadcasts them to another controller or server. In our case, this is a Z-Way server.

The OpenRemote website has a very detailed installation guide, which I propose to use:
The official OpenRemote installation instruction in English
I just note that OpenRemote is written in Java and we need a virtual machine version with hardware floating point support:
JAVA for ARM processors in floating point support comma
Install a mobile application for your phone:
the mobile app OpenRemote
Before you start developing applications for a better understanding see how it will work:

Creation of application design and logic of its work
All the previous steps were only preparation for the main thing - the creation of a mobile application!
Open the cloud Constructor http://designer.openremote.org . Understanding it is not difficult!
Design development
Immediately move on to design development.
1. Go to the UI Designer tab


2. Drag the buttons and images from the right pane onto the iPhone screen.

3. In the image properties (right panel) upload your pictures and use the Left, Right, Width, Height fields to place them on the screen as you need. I uploaded images of a square and a light bulb, and also added an inscription.

Creating control commands and sensors
Now you need to assign commands to the buttons, and the picture of the bulb should change depending on the state of the light (on / off).
1. Go to the tab


2. Select the newly created Raspberry device and create a new command for it, New →

This command includes device number 6. Similarly, we create commands to turn on other devices and turn off commands.
http://192.168.1.113:8083/JS/Run/SwitchOn(6,0)

3. Now you need to create a team to poll the state of light. This command will be called every 2 seconds, so if you manually turn off the light, then in our application it will immediately become noticeable. As usual, New →


4. Create a Sensor that will process the received values from this command and transmit them to the image or label. New →


Association of buttons with commands
1. The last stage of application development, the association of buttons with commands. Return to the designer of the UI Designer application


2. Associate the image with the sensor, so that when the state of the device changes, the picture in the application also changes. Select an image and set the sensor in its properties.

3. In the image properties, select which picture will be displayed if the sensor sent on and which if the sensor sent off .

Mobile App Sync
1. Our application is ready, it remains only to fill it into the phone. Go to the address of the OpenRemote controller http: // OpenRemoteServer: 8080 / controller / , and click Sync with Online Designer , thereby downloading the created application to the OpenRemote controller.

2. At the address http: // OpenRemoteServer: 8080 / webconsole / you can see our finished application.

3. Open the OpenRemote application on your mobile, set the address of the OpenRemote server, select the iPhone4 panel that we created in UI Designer .

4. Now you can test the application on a mobile, I continued development further, added a temperature sensor, a window opening sensor, and made a panel to control the XBMC media player.

Summary
I spent one day studying OpenRemote and developing the application! This tool is very easy to learn, which saves time. OpenRemote provides unlimited possibilities for home automation, combining various protocols under one wing, and RaZberry has proven to be a very successful solution for controlling Z-Wave equipment from OpenRemote.
References
Z-Way
documentation RaZberry - smart home based on Z-Wave and Raspberry Pi
How To Install OpenRemote Controller on Raspberry Pi
OpenRemote Configure Z-Wave for Razberry