Tag Archives: Imperihome and Node-Red

Putting it all together in Imperihome

This blog entry is about using the Imperihome APP with Node-Red to control IOT. This is looking like the best APP out there.. Read on..

Please note: 14/03/2016 Update thanks to vastly improved understanding of Imperihome – part of this is  thanks to Peter Oakes and his homework (check him out on YouTube). See here for a new video on the setup… more up to date than this blog entry. https://www.youtube.com/watch?v=1p3OwICqQbc

I’m writing this for three reasons:

1. To clarify how Node-Red and Imperihome can work together to allow remote access to IOT

2. To form a base from which to expand

3. To get the conversation away from the original Imperihome article (previous blog) which is now WAY overcrowded.

So here is a basic setup for an RGB LED with colour, saturation and intensity control. Output is to debug but you could of course send that off to any RGB light or to MQTT (which is what I will do). Here is the API information for various devices..

Here is the APP itself in the Google Play Store.

The basic setup for using the Imperihome API (documented here – suggest printing this out it you are interested) is as follows:

You need to define:

  1. The system
  2. The rooms
  3. The devices

Rooms are merely a mechanism for separating stuff up into pages – use them as you will.

You set this up on Node-Red – then merely create a new SYSTEM using IMPERIHOME STANDARD SYSTEM in the App. If you don’t have password setting on your Node-Red pages then you don’t need a username and password. That simple. The local URL  in the APP will be http://192.168.0.x:1880/test1 – if you want to use this outside of the home at the very minimal you want username and pass and preferably setup https (the two security items being outside of the scope of this article).

Imperihome[6]

Let’s go through this step by step – the cream things are standard http input and output nodes. The latter need no setup at all, the input nodes need and address – I’ve called my base address “test1” – don’t worry you don’t have to create any test pages – you need do nothing more than set up this tab in Node-Red and setup the APP as I’ve described above.

The URLS for the first 3 http nodes are self explanatory – the last on (rest actions) is as follows:

/test1/devices/:deviceID/action/:actionName/:actionParameter

So the first 3 are incoming requests from the APP (by default every 5 seconds and at startup) – I change my default (in settings for the APP SYSTEM) to 2 seconds.

The system request and room requests are just setup – in this case I have one “room”  - I can have as many as I like and these are essentially tabs in the APP.

The orange units are functions you write yourself. The first function is merely setup.

msg.payload = { "id": "ISS:Test:01", "apiversion": 1};
return msg;

The third one – the rooms is also simple:

msg.payload = {"rooms": [{ "id": "roomID1", "name": "Test Room" }]};
return msg;

If you want more rooms you simply add them as such:

msg.payload = {"rooms": [{ "id": "roomID1", "name": "Test Room" },[{ "id": "roomID2", "name": "Other Room" }]};
return msg;

Ok, this takes us to the devices function. This simultaneously tells the APP what devices you have (you don’t need to set anything up on the APP at all other than your node-red page address the name of which (test1) is arbitrary.

var rgbState = global.get('rgbState') || 0;
var rgbLevel = global.get('rgbLevel') || 0;
var rgbColour = global.get('rgbColour') || "AAAAAAAA";

msg.payload = {"devices": [
{ "id": "dev01", "name": "RGB Light", "type": "DevRGBLight", "room": "roomID1", "params": [{"key": "dimmable", "value": "1"}, {"key": "whitechannel", "value": "0"}, {"key": "Status", "value": rgbState}, {"key": "Level", "value": rgbLevel}, {"key": "color", "value": rgbColour}]}
]};
return msg;

If you look at the Imperihome API instructions you’ll see all about parameters and types.

So – I pull in three global variables (which used to be of the form context.global.rgbstate but which are now accessed with .get and .put commands – looks odd if you’re not used to them but it’s simple enough.. the “!! 0” simply means return 0 or “AAAAAAAA”  if the variable has yet to be initialised.

Incidentally AAAAAAAA for a colour – yes that caught me out – it is HEX and the first 2 digits are irrelevant unless you are using WRGB LEDS.  I’m using RGB (the W is for WHITE).

So this sets up the RGB light device in the APP and prefills values – but also it is called regularly to update the APP display – such as the sliders and RGB control. rgbSTATE incidentally is for the APP RGB on-off button – and will be 1 or 0. Level will be 0-100 and the rgbColour will be any WRGB value (first 2 digits ignore).

What we are saying in that mess above is that this is an RGB light control in the main room, it is dimmable, we’re not using WHITE and here are the settings.

All that remains to do now is take action when the actual APP controls are changed… and that means storing updates to the global variables.

That is this bit extracted from above.

Imperihome[8]

So we have the http input and I described that URL above – and a simple switch – ie. which device are we talking about? I only have one device here…

Imperihome switch

See “dev01” – that is the name of the one and only device – really one should give the devices more meaningful names but that’ll do here.

There is one output from this switch node – there would be more if you added more (the +rule).

The output goes to a simple standard function (top)

msg.payload = { "success": true, "errormsg": "ok" };
return msg;

That then goes back to the APP via the http node. This is common to all devices…

So here it gets more interesting. You are taking in commands from the APP and doing something – firstly storing the results in global variables – but then taking RGB and brightness data and creating an output to go off to your RGB light – in this case just a debug output.

This is a bodge – does not take into account proper colour adjustment at various brightnesses – and right now people (just got your email Chris) are firing ideas at me so this version will go in the bin – but it will do for the purpose of experimenting.

 

function colorLuminance(hex, lum) {
    // Validate hex string
    hex = String(hex).replace(/[^0-9a-f]/gi, "");
    if (hex.length < 6) {
        hex = hex.replace(/(.)/g, '$1$1');
    }
    lum = lum || 0;
    // Convert to decimal and change luminosity
    var rgb = "#",
        c;
    for (var i = 0; i < 3; ++i) {
        c = parseInt(hex.substr(i * 2, 2), 16);
        c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
        rgb += ("00" + c).substr(c.length);
    }
    return rgb;
}

switch (msg.req.params.actionName){
case "setStatus": global.set('rgbState', msg.req.params.actionParameter);  break; 
case "setColor":  global.set('rgbColour', msg.req.params.actionParameter);  break;
case "setLevel":  global.set('rgbLevel', msg.req.params.actionParameter);  break;
default: break;
}
if (global.get('rgbState') == '0' || global.get('rgbLevel') == '0') {
    msg.payload = 'RGB = 0,0,0';
} else {

    hexColor = colorLuminance(global.get('rgbColour').slice(2), -1+(parseInt(global.get('rgbLevel'))*0.01));

    msg.payload = 'RGB = ' + parseInt(hexColor.substr(1,2), 16) + ',' + parseInt(hexColor.substr(3,2), 16) + ',' + parseInt(hexColor.substr(5,2), 16);
    }
return msg;

 

The function above merely takes RGB and brightness and produces an RGB value adjusted by brightness.

The main code has a switch to check which command came in – to adjust status (1,0), level (brightness 0-100) and RGB value. These are merely stored in the global variables.

All that remains to do is output something suitable to the debug – it is crude but it works.

 rgb

 

So whenever you make a change to the on-off state of the control in the APP, or change colour, brilliance or saturation – you will see a new RGB value come out of the debug.

When first setup this works. I then go into the APP and adjust the refresh time to every 2 seconds and something strange happens… the level control “flickers”. It did this in my more complex set up and now does it in this simplified and “clean” version.

https://bitbucket.org/snippets/scargill/prn7L

Here are APP screenshots – ignore my username and pass.

Imperihome[10]

Imperihome[12]

 

Imperihome[14]

 

All very simple – and can be expanded on with multiple rooms, IP cameras and all SORTS of widgets – a BARGAIN at £4 for the APP.

Update 09/03/2016

It turns out that the brilliance slider thing was a bug – fixed. Meanwhile here’s a picture of my current setup with a few controls – note that the http responses are all tied together where the response is identical – makes for a much simpler layout.

imperihome

Facebooktwittergoogle_pluspinterestlinkedin