Monday, April 24, 2017

Node Red Dashboard for Raspberry Pi

What is Node Red?




Node-RED is a programming tool for wiring together hardware devices, APIs and online services. It was developed as a visual programming tool for the Internet of Things. It also allows you to produce and publish a funky web based dashboard with one click.


Node-RED includes a browser-based editor that makes it easy to wire together flows using the selection of nodes in the side menu. Flows can be then be deployed to run in a single-click. JavaScript functions can be created within the editor to customise the messages passed between nodes. A built-in library allows you to save useful functions, templates or flows for re-use.

The light-weight runtime is built on Node.js, taking full advantage of its event-driven, non-blocking model. This makes it ideal to run on low-cost hardware such as the Raspberry Pi as well as in the cloud.

Nodes can be anything from a timer to trigger events to a Raspberry Pi GPIO output used to turn on a LED (or salt lamp in our example). With over 225,000 modules in Node's package repository, it is easy to extend the range of nodes to add new capabilities. As we will demonstrate there are packages available for the Raspberry Pi and Sense Hat. The flows created in Node-RED are stored using JSON.

Node-RED was developed by IBM and in 2016, they contributed Node-RED as an open source JS Foundation project.

The Himalayan Salt Lamp Project




My wife likes salt lamps. Salt lamps allegedly remove dust, pollen, cigarette smoke, and other contaminants from the air. How effective this is I don't know and it is really irrelevant, as I said my wife likes them! Salt is very hygroscopic, that is it absorbs water - this is the basis of the claimed health benefits, the salt also absorbs any foreign particles the water may be carrying. The water then evaporates when the lamp is switched on leaving the contaminants behind entrapped in the salt.

Salt is so hygroscopic that it readily dissolves in the water it absorbs: this property is called deliquescence. It is of course a problem if your expensive Himalayan salt lamp dissolves into a puddle of salty water, especially if it is connected to 240VAC. In our house this melting process starts at relative humidities above 70%.

The solution is to turn your lamp on if humidity gets above 70%. This seemed like a good excuse to introduce the start of our home automation hub and learn about node_RED. Turning a lamp on and off based on humidity and time (lamp goes on at 5pm and off at 10pm) is trivial using Python so we wont cover that. What we will look at is manually controlling the lamp via our node-RED dashboard and other associated data we display.

Node-RED and the Raspberry Pi




If you are running Raspbian Jessie on your Pi then you should already have node-RED installed. Before starting the node-RED server it is worth installing a few packages that you will need. Type the following at the CLI:

sudo apt-get update
sudo apt-get install npm
cd $HOME/.node-red
npm install node-red-dashboard
npm install node-red-node-snmp
npm install node-red-contrib-os

Node-RED is started by running the following command in the terminal:

node-red-start
Once started, you use a browser (either on the Pi or remotely) to build your applications and configure your dashboard. I used my Macbook Air, to do this point your browser at the ip address of your Pi:1880. If you do it on your Pi, the URL would be 127.0.0.1:1880 or localhost:1880. The associated dashboard URL is <IP Address>:1880/ui. So for example my Raspberry Pi dashboard is at http://192.168.0.18:1880/ui.

Most of the Raspberry Pi information charted in the dashboard shown above is from the node-red-contrib-os package. For example information on the SD Card is from the Drives node. You use this node to query the hard drives. Values for size, used and available are expressed in KB (1024 bytes). Value for capacity is a number between 0 and 1. Capacity*100 is also known as percentage used.

Some of the flows are shown below. The first step is to drag across a timer which you can use to poll the Drive node. Our timer sends a timestamp every minute.

Connect the timer to a Drive node and it will start pumping out messages with the size, used, available and capacity values for every drive on your target system. You can use a Debug node to see messages being sent out by any node. This is very useful in debugging your flows. On the Raspberry Pi there will be a few different file systems on your SD Card so you have to be specific about which area you want information about.


You can add a Function node to include custom JavaScript to process the messages passed between the nodes. The JavaScript used to extract the various Drive information that I use is shown below. The topic variable is used as the name for charts with multiple inputs.

var msg1,msg2,msg3;

if (msg.payload.filesystem === '/dev/root') {

    msg1 = { payload: msg.payload.used };
    msg2 = { payload: msg.payload.available };
    msg3 = { payload: msg.payload.capacity * 100 };

    msg1.topic = "used"
    msg2.topic = "available"
    msg3.topic = "capacity"

}

return [ msg1, msg2, msg3 ];

CPU Temperature




To display CPU temperature we use a different technique. On the Raspberry Pi you can display the current CPU temperature by typing:

/opt/vc/bin/vcgencmd measure_temp
You can use an Exec node to run OS commands. So connect our same timer node to an Exec node and input the command above. We then have to do a bit of processing to extract the temperature as a number. Use another function node with the following code.

msg.payload = msg.payload.replace("temp=","").replace("'C\n","");

return msg;



There are also nodes available for the Sense Hat. You need to use functions similar to those above to extract the various sensor data values.


Controlling GPIO using the Dashboard



Manual control of a GPIO is fairly straight forward. The one trick is that the Switch node outputs true/false and the Raspberry Pi GPIO out node expects a 1/0 input. So we include another Function node to mediate. The relevant code is:

msg.payload = msg.payload ? 1 : 0;

return msg;

Of course our Raspberry Pi outputs 3.3VDC which wont turn on a 240VAC lamp so we use a PowerSwitch Tail kit as an intermediary.





No comments:

Post a Comment