OpenHAB is an ‘open source’ Home Automation system which allows to connect and control a variety of ‘smart devices’ from different vendors. OpenHab is a Java-application which can run on different hardware such as the popular Raspberry Pi. The concept of ‘Bindings’ facilitates the usage of different protocols.
One of the Bindings allows us to install an MQTT client. This way we can connect a local OpenHAB installation as a gateway to AllThingsTalk maker and aggregate sensor data from different connected OpenHaB devices.
We will integrate with openhab to send over the state of a Zwave flood sensor and control a Zwave smart plug from within AllThingsTalk Maker
Step 1: Install OpenHabAs a first step we will install OpenHaB on a raspberry Pi. this link has everything you need to accomplish this task. To make use of the Zwave protocol, you will need a Zwave controller. I have tested successfully the following Zwave USB controllers:
- aeotec z-stick gen5
- Z-wave static controller from SIGMA (ACC-UZB2-E)
Further you will need one or more Zwave devices.
Step 2: Creating your gateway in AllthingsTalk MakerThe first step to connect your OpenHab devices is to setup a gateway in AllThingsTalk maker that represents your local OpenHab environment.
Login* AllThingsTalk Maker and create a gateway.
- With your (play)ground select Gateways in the menu
- Click on + New Gateway
If you don’t have an account yet, you can create a free account, for non commercial use on https://maker.allthingstalk.com.
When the gateway is created, click on settings -> authentication. For the gateway you have created you will find the Gateway ID and Gateway Token. We will need these values in the next step.
Now that you have setup a gateway in AllThingsTalk Maker, we can install and configure an MQTT client on your local OpenHab environment. Using the gateway token from the gateway you have created in the previous step, we can set the scope in which the MQTT client can operate.
Login using an SSH session on your Raspberry PI OpenHab installation (default username/pwd = openhabian/openhabian)
cd /etc/openhab2/services
sudo nano mqtt.cfg
- Add the following lines:
ATTALK.url=tcp://api.allthingstalk.io:1883
ATTALK.user=”enter your gateway token”
ATTALK.pwd=XXX
Replace “enter your gateway token” with your gateway token issued in the previous step.
Note : the password can be chosen random, as it si not needed to make the connection, but we provide it so the mqtt client can function properly.
4. Create a digital representation representing your Zwave devices.As a next step we will create 2 devices and appropriate assets in AllThingsTalk maker under the gateway we have created in step 2 which represent your Zwave devices in OpenHab.
- Under your gateway click on ‘+New Device’
- Enter the name for your device (e.g. Flood Sensor)
- Click on the device you just have created.
- Within the device, click on ‘create asset’
- Create a sensor asset called ‘flood_alarm’ of type boolean
- Create a second device called ‘wallplug’
- Click on the device you just have created.
- Within the device, click on ‘create asset’
- Create an actuator asset called ‘switch’ of type boolean
After setting up the MQTT communication between the MQTT client and the AllThingsTalk MQTT broker, we can setup the MQTT topics for each of the devices we want to connect.
As a first example we will connect the Flood Sensor (we assume the Zwave devices have been added to OpenHab).
Login using an SSH session on your Raspberry PI OpenHab installation (default username/pwd = openhabian/openhabian):
cd /etc/openhab2/items
sudo nano mqtt.items
Add the following line in the newly created file:
Switch zwave_device_2ba39949_node5_alarm_flood {mqtt=">[ATTALK:device/3Q79l9QOaG4E4elUDZzuPJYF/asset/flood_alarm/state:state:ON:0|true],>[ATTALK:device/3Q79l9QOaG4E4elUDZzuPJYF/asset/flood_alarm/state:state:OFF:0|false]"}
Where:
- Zwave_device_2ba39949_node5_alarm_flood = is the channel (asset) we want to connect to of the zwave device
- ATTALK:device/3Q79l9QOaG4E4elUDZzuPJYF/asset/flood_alarm/state = the mqtt topic we want to publish to.
- state:ON:0|true = translation of the data ON -> 0|true (CSV data format)
After updating the file, you can view the alarm status in AllThingsTalk maker.
From the Flood sensor device under your gateway, you can create a pinboard. On the Pinboard you will see the status of the flood_alarm sensor.
As a second example we will connect a Fibaro wall plug. The Fibaro wall plug is a z-wave device which can be plugged into a mains outlet and control (switch on/off) appliances.
- Login using an SSH session on your Raspberry PI OpenHab installation (default username/pwd = openhabian/openhabian)
cd /etc/openhab2/items
sudo nano mqtt.items
- Append the following line in mqtt.items file:
Switch zwave_device_c323c5e1_node2_switch_binary {mqtt="<[ATTALK:device/9pQeUTnM4dioBPPlGJuI6PRW/asset/switch/command:command:ON:.*true.*],<[ATTALK:device/9pQeUTnM4dioBPPlGJuI6PRW/asset/switch/command:command:OFF:.*false.*]"}
Where:
- zwave_device_c323c5e1_node2_switch_binary = is the channel (asset) we want to connect to of the zwave device
- ATTALK:device/9pQeUTnM4dioBPPlGJuI6PRW/asset/switch/command = the mqtt topic we want to listen to.
- ON:.*true.* = translation of the data ON -> .*true.* (JSON data format)
After updating the file, you can control the Zwave WallPlug from within AllThingsTalk Maker.
From the WallPug device under your gateway, you can create a pinboard. When you control the switch on the Pinboard you can turn on/off the physical switch.
Comments