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.





6 comments:

  1. Thanks for your great blog. The notes on how to install the enhanced Dashboard helped me. I am new to NodeRed but use similar interface (mit appinventor). The possibility of adding functions is great.
    I realize that you use a “BigTimer”. I found a reference to it in http://reefwingrobotics.blogspot.ca/2017/04/node-red-dashboard-for-raspberry-pi.html and installed it... But It does not show up if I kill the node-red process and re-start it : I have to reboot! … (is there a way to refresh nodered without rebooting?)
    But my main question is : “where can I find the “Extract …” nodes??” - thks

    ReplyDelete
    Replies
    1. Hi Marc,

      Thanks for the comment. BigTimer should come up under Scargill on the node selection list on the left. Scargill is the name of the dude who contributed the node (https://tech.scargill.net/big-timer/). You should be able to stop the node-RED server and restart it without having to reboot your Pi (using the node-red-start and node-red-stop commands).

      If you enter the following command at the Terminal:

      sudo systemctl enable nodered.service

      Node-RED will automatically start when the Raspberry Pi reboots. This is what I do.

      The "Extract" nodes are actually "split" nodes, you will find them under function, in the left hand menu. I renamed them to make it more obvious (to me) what they were doing. Apologies for the confusion!

      Delete
  2. Thank you for sharing this information. I have a rpi3Bv1.2 running jessie.

    I followed your instructions but a get a errors on step:

    "myrasp:~/.node-red $ npm install node-red-dashboard"

    npm WARN engine node-red-dashboard@2.19.4: wanted: {"node":">=8"} (current: {"node":"0.10.29","npm":"1.4.21"})
    npm WARN engine base64id@2.0.0: wanted: {"node":"^4.5.0 || >= 5.9"} (current: {"node":"0.10.29","npm":"1.4.21"})
    npm WARN engine ws@7.2.1: wanted: {"node":">=8.3.0"} (current: {"node":"0.10.29","npm":"1.4.21"})
    npm WARN engine mime@1.6.0: wanted: {"node":">=4"} (current: {"node":"0.10.29","npm":"1.4.21"})

    > utf-8-validate@5.0.2 install /home/pi/.node-red/node_modules/node-red-dashboard/node_modules/socket.io/node_modules/engine.io/node_modules/utf-8-validate
    > node-gyp-build

    make: Entering directory '/home/pi/.node-red/node_modules/node-red-dashboard/node_modules/socket.io/node_modules/engine.io/node_modules/utf-8-validate/build'
    CC(target) Release/obj.target/validation/src/validation.o
    ../src/validation.c:3:22: fatal error: node_api.h: No such file or directory
    #include
    ^
    compilation terminated.
    (...)

    Can you help ?

    ReplyDelete
    Replies
    1. I am trying to reinstall node.js and node-red following this:

      https://nodered.org/docs/getting-started/raspberrypi

      Than I will try to reinstall node-red-dashboard

      I will update as soon as possible.

      Delete
    2. So after running the script from nodered.org I get this output:

      Running Node-RED install for user pi at /home/pi on raspbian


      This can take 20-30 minutes on the slower Pi versions - please wait.

      Stop Node-RED ✔
      Remove old version of Node-RED ✔
      Remove old version of Node.js ✔
      Install Node.js LTS ✔ Node v10.19.0 Npm 6.14.1
      Clean npm cache ✔
      Install Node-RED core ✔ 1.0.3
      Move global nodes to local ✔
      Install extra Pi nodes ✔
      Npm rebuild existing nodes -
      Add shortcut commands ✔
      Update systemd script ✔


      Any errors will be logged to /var/log/nodered-install.log
      All done.
      You can now start Node-RED with the command node-red-start
      or using the icon under Menu / Programming / Node-RED
      Then point your browser to localhost:1880 or http://{your_pi_ip-address}:1880

      Started Qui Fev 27 11:41:15 -03 2020 - Finished Qui Fev 27 11:52:40 -03 2020

      I will try to follow your steps now, and update as soon as possible.

      Delete
    3. ~/.node-red $ npm install node-red-dashboard
      npm WARN node-red-project@0.0.1 No repository field.
      npm WARN node-red-project@0.0.1 No license field.

      + node-red-dashboard@2.19.4
      added 66 packages from 53 contributors and audited 669 packages in 23.439s

      3 packages are looking for funding
      run `npm fund` for details

      found 1 moderate severity vulnerability
      run `npm audit fix` to fix them, or `npm audit` for details

      pi@rasp109:~/.node-red $ npm audit fix
      npm WARN node-red-project@0.0.1 No repository field.
      npm WARN node-red-project@0.0.1 No license field.

      up to date in 5.444s

      3 packages are looking for funding
      run `npm fund` for details

      fixed 0 of 1 vulnerability in 669 scanned packages
      1 vulnerability required manual review and could not be updated

      Delete