Pages

Using Node-Red with OpenRemote for basic home automation

I’ve recently been using a new tool that describes itself as “A visual tool for wiring the Internet of Things“. More details are available at their website http://nodered.org/. The idea is that ‘things’ can be brought together in a visual manner without any programming, although a little coding in JavaScript is needed in places. I’ve also been looking at OpenRemote to provide a tablet based front-end for home automation and monitoring. For this project I selected the D-I-Y approach using the open source version available from http://www.openremote.org/. MQTT is also being used as a message passing mechanism. The broker I use is the most excellent Mosquitto. This post described a basic setup for using Node-Red and OpenRemote to produce a simple tablet based control and monitor application. Full details of Node-Red, OpenRemote and Mosquitto are available on their websites and should be referred to for installation and configuration of these packages.

The components

I have a number of components that need to be linked together, these are on the hardware side:

  • 2 Channel wireless relay board, based on Ciseco SRF module
  • Linux server to run the stuff on
  • Ciseco XRF with FTDI interface connected to Linux server
  • Nexus 7 tablet

And on the software side:

  • Node-Red
  • OpenRemote controller and Android app
  • Mosquitto MQTT broker

Communications

Wireless communications is being done by the Ciseco SRF modules using the LLAP protocol described in detail here.

The relay boar id controlled by sending the command aRLRELAYAON- to turn on relay A on board with an ID of RL. The relay board will respond to indicate the state of the relay.

Within the server, the messaging broker will have topics that the various parts of the Node-Red application can subscribe and publish to. For example, to send new LLAP message, I would publish to a topic llap/send. Received messages from sensor nodes would publish to their own topics, for example llap/T4/TMPA or llap/T4/BATT which would be for the sensor node T4 where the temperature and battery voltage are published to.

One final part of the communications is to provide a basic HTTP interface to control the relays and provide feedback to the OpenRemote application.

Building the Node-Red application

Interfaces to wireless devices

The first part is to receive LLAP messages from the module plugged into the Linux server, this uses the Serial input node then builds up the complete 12 character message. The LLAP message is then split up by the second function before it is finally published to a MQTT topic. A number of debug nodes are included for debugging purposes and can be turned on and off at runtime to display messages being passed between the nodes. A number of values are also stored in the Node-Red global context variables for retrieval later.

NR-llap-receive

Code for the LLAP Parse function:

// name: Parse LLAP message
// outputs: 1
// anything stored in context is kept available for next time we get called
context.buff = context.buff || "";
context.count = context.count || 0;

// wait for an a and reset the buffer and counter
if (msg.payload == "a") { context.count = 0; context.buff = ""; } // this does mean the a must NOT appear elsewhere in the message

// add payload to the buffer and increase the count
context.buff = context.buff + msg.payload;
context.count += 1;
// when we reach 11 then output the whole message, assumes length is the a plus 11 - may need to tweak...
if (context.count == 12) {
msg.topic = context.buff.slice(1,3); // return the first two chars as the topic
msg.payload = context.buff.slice(3); // return rest of the buffer as the payload
return msg;
}
return null;

Code for LLAP to MQTT function:

// Topic is part set, now see if TMPA, LVAL or BATT msg

var msgType = msg.payload.substr(0,4);
if( msgType == "TMPA" || msgType == "LVAL" || msgType == "BATT" ) {
var newTopic = "llap/" + msg.topic + "/" + msgType;
var value = msg.payload.substr( 4, 5);
// Strip any - chars
value = value.replace(/-/g,"");
var newMsg = { topic: newTopic, payload: value };

if(msgType == "TMPA" && msg.topic == "T4" )
context.global.tempt4 = value;
else if( msgType == "TMPA" && msg.topic == "T2" )
context.global.tempt2 = value;

return newMsg;
}
return null;

The second part is to subscribe to the topic llap/send and pass the message out through the Serial out node and onto the wireless devices that would be listening for it.

NR-llap-send

The code for the LLAP format function:

var newMsg = { payload: "a" + msg.payload };
return newMsg;

I have additional nodes that take received LLAP messages, parse them and store the values in the global context for later retrieval.

Interface to Openremote

Next I provided a way to read the previously stored values in the Node-Red global context, this takes a URL of the form http://ip_address:1880/sensor?name=varname where varname is the name of one of the variables. The value is returned without any other formatting or markup. The values I’ve setup this way include temperature measurements, electricity usage and relay states.

readsensor

The code for the Get Reading function:

var name = msg.req.query.name;
var reading = context.global[name];
var newMsg = { payload: reading };

// Send http response
msg.res.send(200, reading );

return newMsg

In order to control the relay board I’ve setup another node to receive http requests of the form http://ip_address:1880/relaycontrol?relay=a&action=on. This will post a message to the MQTT Topic llap/send that will in turn be picked up by the nodes previously described and turn on the relay. Other options are to select either relay a or b and to turn them on, off or tog (toggle).

RelayControl

Code for the Relay To MQTT function:

var deviceId = "RL";
var cmd = "RELAY";
var id = msg.req.query.relay.toUpperCase();
var origAction = msg.req.query.action;
var action = msg.req.query.action.toUpperCase();
if(action == "ON") action="ON-";
var llap = deviceId + cmd + id + action;
var newMsg = { payload: llap };

// Send http response
msg.res.send(200, origAction );

return newMsg;

Building the OpenRemote Application

The OpenRemote Android or iPad application connects to the local controller to retrieve the details needed to draw the panels on the screen that perform the control actions defined using the OpenRemote designer. To start with, OpenRemote needs a number of components defining. These are the building blocks that provide the functionality for a particular device. In the example presented here, I have associted them with a device called Node-Red which is my linux server with Node-Red running on it. The components are:

  • Commands – Perform communication with a device using a variety of different protocols and methods and polling intervals
  • Sensors – Define asensor that uses a command to read a value, range of values or custom data
  • Switches – Select the commands to turn the device on, off and determine its state
  • Slider – Select a command to send a range of values depending on slider position

In the example below, I’m setting the http command to access the relay control node that was previously defined using Node-Red.If this was a slider command then parameters could be passed in the URL using parameter notation ${param}.

or-1

Once the commands, sensors and switches have been defined, you can start on the panel layout by adding labels, buttons and images to create your panel. Multiple panels can be created and accessed via a series of tabs or sliding from left to right. An background image can be used to create different effects and styles.

or-3

When using switches, you need to select both an ON image and an OFF image to show the switch or button state. The community version of the designer doesn’t come with any images so you need to provide your own. I used a couple of images found on the net but for my final design I’m looking to employ a graphic designer to work on new images and the user interface. But for now its not pretty but is functional. The switches are defined as shown in the image below, they need the name, sensor selected from the drop down of available sensors and the images to use for the on/off states.

or-2

Adding labels that have a sensor associated with them will draw the received value where the label is positioned. You need to remember to set a polling interval in the command setup otherwise your readings wont be updated.

This is only a small selection of what OpenRemote can do. I’ve not even explored the many protocols and devices it can control or the macros and rules that can help automate tasks. I’ll leave this for another time.

Testing the application

When the panels have been designed and you are happy with it then you need to sync the design with your controller. This can be done in an online or offline mode. Online is the most convenient method as you just need to select the controller url of the form http://server_ip:8080/controller then enter your OpenRemote designer username and password and click the Sync with Online Designer button.

The Android app is available in Google Play, however this is an old version, following the links on download page will get you the latest version. If you use your tablet and have the option to install software from non-marketplace sites then you should be able to download and install directly.
If all is well then you should see your panel appear on the tablet. The buttons should be selectable as shown in the demo video below.
If the switches fail to operate correctly then it could be down to the sensor part not working correctly as it appears the state of the switch needs to be correct for it to change the image and command used.

Quick Demo video

Listen for the Exterminate as its triggered by the relay board when the second round button is pressed.

Have fun if you decide to try this yourself.

Bookmark this post: bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

Comments are closed.