MISC-TN-007: Using Node-RED on SBCSPG to develop zero code applications

From DAVE Developer's Wiki
Jump to: navigation, search
Info Box
SBCSPG-1.jpg Applies to SBCSPG
Cloud-computing-banner.jpg Applies to IoT
Warning-icon.png This technical note was validated against specific versions of hardware and software. It may not work with other versions. Warning-icon.png


History[edit | edit source]

Version Date Notes
1.0.0 October 2019 First public release

Introduction[edit | edit source]

In a typical Industrial IoT (IIoT) scenario, the edge devices like the SBCSBP gateway are installed and used by personnel who have limited or no programming skills at all. Nevertheless, these operators are required to implement simple automation processes with edge devices. To address this situation, no-code visual development tools can be used. Node-RED is an example of such tools.

According to the official home page of the project, it is defined as:


... a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single-click.


This Technical Note (TN) shows how Node-RED can be installed and used on the SBCSBP gateway.

The procedure was tested on a SBCSPG gateway running armbian GNU/Linux distribution. For more details about this configuration, please refer to this TN.

Installing Node-RED[edit | edit source]

Installing Node-RED is quite straightforward. Essentially, you need to follow the procedure described here.

First, make sure that the build-essential package is installed:

root@sbcspg:~# apt-get install build-essential                                                                                                                 
Reading package lists... Done                                                                                                                                  
Building dependency tree                                                                                                                                       
Reading state information... Done                                                                                                                              
build-essential is already the newest version (12.3).                                                                                                          
0 upgraded, 0 newly installed, 0 to remove and 16 not upgraded.


Then, invoke the script update-nodejs-and-nodered:

armbian@sbcspg:~$ bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
q
 
This script will remove versions of Node.js prior to version 7.x, and Node-RED and
if necessary replace them with Node.js 10.x LTS (dubnium) and the latest Node-RED from Npm.
 
It also moves any Node-RED nodes that are globally installed into your user
~/.node-red/node_modules directory, and adds them to your package.json, so that
you can manage them with the palette manager.
 
It also tries to run 'npm rebuild' to refresh any extra nodes you have installed
that may have a native binary component. While this normally works ok, you need
to check that it succeeds for your combination of installed nodes.
 
To do all this it runs commands as root - please satisfy yourself that this will
not damage your Pi, or otherwise compromise your configuration.
If in doubt please backup your SD card first.
 
Are you really sure you want to do this ? [y/N] ? y

Would you like to install the Pi-specific nodes ? [y/N] ? N

Running Node-RED update for user armbian at /home/armbian on debian


We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for armbian: 

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

  Stop Node-RED                       \u2714
  Remove old version of Node-RED      \u2714
  Remove old version of Node.js       \u2714
  Install Node.js LTS                 \u2714   Node v10.16.3   Npm 6.12.0
  Clean npm cache                     \u2714
  Install Node-RED core               \u2714   1.0.1 
  Move global nodes to local          -
  Install extra Pi nodes              -
  Npm rebuild existing nodes          -
  Add shortcut commands               \u2714
  Update systemd script               \u2714
                                      

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  Mon Oct 14 10:56:44 CEST 2019  -  Finished  Mon Oct 14 11:15:09 CEST 2019


After a while, Node-RED is ready to be started:

armbian@sbcspg:~$ node-red-start 

Start Node-RED
 
Once Node-RED has started, point a browser at http://192.168.0.81:1880
On Pi Node-RED works better with the Firefox or Chrome browser
 
Use   node-red-stop                          to stop Node-RED
Use   node-red-start                         to start Node-RED again
Use   node-red-log                           to view the recent log output
Use   sudo systemctl enable nodered.service  to autostart Node-RED at every boot
Use   sudo systemctl disable nodered.service to disable autostart on boot
 
To find more nodes and example flows - go to http://flows.nodered.org
 
Starting as a systemd service.
Started Node-RED graphical event wiring tool.
14 Oct 11:27:55 - [info]
Welcome to Node-RED
===================
14 Oct 11:27:55 - [info] Node-RED version: v1.0.1
14 Oct 11:27:55 - [info] Node.js  version: v10.16.3
14 Oct 11:27:55 - [info] Linux 4.1.15-rialto-1.1.3 arm LE
14 Oct 11:27:59 - [info] Loading palette nodes
14 Oct 11:28:10 - [info] Settings file  : /home/armbian/.node-red/settings.js
14 Oct 11:28:10 - [info] Context store  : 'default' [module=memory]
14 Oct 11:28:10 - [info] User directory : /home/armbian/.node-red
14 Oct 11:28:10 - [warn] Projects disabled : editorTheme.projects.enabled=false
14 Oct 11:28:10 - [info] Flows file     : /home/armbian/.node-red/flows_sbcspg.json
14 Oct 11:28:10 - [info] Creating new flow file
14 Oct 11:28:10 - [warn]
---------------------------------------------------------------------
Your flow credentials file is encrypted using a system-generated key.
If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.
You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.
---------------------------------------------------------------------
14 Oct 11:28:10 - [info] Starting flows
14 Oct 11:28:10 - [info] Started flows
14 Oct 11:28:10 - [info] Server now running at http://127.0.0.1:1880/

Implementing a simple application (flow)[edit | edit source]

Many tutorials are available on the Internet showing how to develop, test, and deploy Node-RED based applications. Here, a simple example is shown. It consists of one flow that performs the following operations every 5 seconds (please note that the execution of the flow is triggered by a 5-second periodic timer):

  • logging in the local debugger window a message containing the timestamp associated with the timer
  • saving the timestamp on a local file
  • sending a message to a remote MQTT server.


Before creating this simple flow, the connection with the remote MQTT server can be tested (test.mosquitto.org):

root@sbcspg:~# apt install mosquitto-clients
...
armbian@sbcspg:~$ mosquitto_sub -h test.mosquitto.org -t "#" -v
...


The test flow looks like this:

caption


The following pictures show how the individual nodes were configured:

Debug node configuration
MQTT out node configuration
File node configuration
Timer node configuration


Once deployed, to verify that the flow is working correctly, it is possible to read the local file where the timestamps are stored ...

armbian@sbcspg:~$ tail -f nr-flow1-test 
1571145963457
1571145968461
1571145973478
...

and receive the test messages from the remote broker, by subscribing to the test topic:

armbian@sbcspg:~$ mosquitto_sub -h test.mosquitto.org -t "dave-am-test/#" -v
dave-am-test 1571146144165
dave-am-test 1571146149185
dave-am-test 1571146154204
...