Skip to content

Deploy and configure Node-Red

How to remote deploy Node-Red in production?

This how-to explains how qbee.io can deploy Node-Red applications to a fleet of distributed remote devices in poduction. It allows headless setup and control of Node-Red on large numbers of devices. Node-Red is an extremely powerful tool and we will only give some basic insights into what and how you can use this. This will enable you to gain very secure and granular control over your distributed SCADA, HVAC or operational technology (OT) deployments. qbee.io will enable Node-Red to do its magic behind firewalls, NATs, mobile networks or other real industrial developments. This allows to securely deploy flows from the lab to the real world into production. And yes, Node-Red is stable enough to run in production (and qbee is governing it while doing so).

Tutorial how to install and configure Node-Red:

qbee.io and Node-Red work extremely well together in production. In this tutorial we will focus on the following tasks:

  • install Node-Red properly on Debian systems (such as RPI)
  • get control over Node-Red settings
  • allow system wide credential access
  • enable automatic updates of flows on remote target devices
  • allow templating of Node-Red flow properties

This is all done on a Raspberry Pi (RPI) as this is widely available. But it will work on any other embedded Linux platforn as well.

Sign up for your 30 day free trial!

The following section explains how to get a clean and updated installation of Node-Red on your Raspberry Pi (RPI). It is strongly recommended by the Node-Red organization to use the script below instead of relying on pre-delivered packages.

Creating a clean Node-Red installation manually

Installing Node-Red on the Raspberry Pi manually

The easisest and most recommended way to get a working Node-Red installation is to follow the recipe on this page. If you have an installation from before just delete your .node-red directory as user pi. Then run the command form the web page: bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

Completely headless and automated install of Node-Red on Debian systems such as Raspberry Pi

Installing Node-Red fully headless and automated

In production it is preferable to be able to install a complete Node-Red system automatically without any user interaction. qbee can do this for you and there is a detailed install Node-Red headless tutorial available on how to do this. This installs Node-Red headless on any Raspberry Pi or Debian based system. It is also relying on the script mentioned in the manual install.

Some basic information about how Node-Red works and how the settings.js, flows.json and flows_cred.json impact each other

There are three main files in a typical Node-Red installation. Usually they are in the hidden .node-red directory under the user account that runs Node-Red. The settings.js contains all relevant settings. This file is created the first time Node-Red is run as long as there is no file from before. In the standard settings.js most settings are commented out. So it makes sense to configure this according to your use case. If a standard setting.js is used and a flow is created and deployed that contains credentials two new files are created: The flows.json and flows_cred.json. This is not 100% correct as the standard settings will create a flows_YOUR-HOSTNAME.json and a flows_YOUR-HOSTNAME_cred.json. This can also be controlled through settings. The _cred.json file contains an encrypted version of the credentials (for example your MQTT user name and password). And here it can get a bit confusing: In a standard settings.js file there is no password defined. Therefore Node-Red will create its own and save it in .config.json after the first time a flow with credentials is deployed. This can cause problems if this is not getting deleted properly.

Properly define your credentials

When a credentialSecret is defined in settings.js AND Node-Red was restarted then new credentials can be defined and will be encrypted with the key from settings with the next deploy.

Why are my Node-Red credentials empty?

When no password is defined in settings.js and a flow with credentials is deployed Node-Red creates a secret key in .config.json. This key allows to decrypt flows_cred.json. But if the settings.js and the flows files are moved it is not possible to decrypt the credentials anymore. If configured correctly with a password in settings.js the .config.json should show an empty user entry at the end of the file. If there is an entry here (and you want to follow the instructions further) please delete the key. Another source of error is the ownership rights of the settings file. I seems that credentials only get decrypted if the settings.js file has the same ownership for group and user as the user running Node-Red. This means that for user pi it needs to look like this

16 -rw-r----- 1 pi pi 13444 Sep 7 15:16 settings.js

To correct this just call (the pi.pi doing the "change owner" both for user and group in one command)

chown pi.pi /home/pi/.node-red/settings.js

Creating a Node-Red settings.js file that can be distributed to all devices

The first thing you want to do is to adjust your settings file. On Linux the settings.js file is copied from the /usr/lib/node-modules/node-red/ folder to your .node-red folder in your home directory when you start node-red, and there is no previous settings.js file present. This file contains a lot of options. Here we will only look at the ones that are relevant for describing our base use case. There are very good tutorials on the internet how to tweak and adjust additional settings. The settings file itsself can also be explored in its latest version on github.

Tip

We recommend to base your settings.js file on the one that you get from your version of node-red (the path defined above), but it should be compatible across versions as well.

Warning

There is one setting in Node-Red called credentialSecret: "a-secret-key". Once you set this property, do not change it - doing so will prevent node-red from being able to decrypt your existing credentials and they will be lost. We circumvent this problem by playing out a defined settings.js with your password and the according flows_creds.json to all devices.

Let's get started. Please install Node-Red on your local development machine and start Node-Red. This can be done on any platform. Just make sure that you do not have a previously set key in .config.json (see warning above).

This will copy a settings.js file into your .node-red folder. Open this and edit the following items: Remove the trailing "//" to activate this the following. This basically removes the dependency of the device hostname out of the flow name and makes life easier.

//flowFile: 'flows.json',

Setting the secret key

The following change encrypts the credentials used in your blocks. This is very useful and this binds the settings.js together with the flows_cred.json. Please make sure you restarted Node-Red.

Do the same with the following and insert your secret key.

//credentialSecret: "a-secret-key",

Thus this line changes into

credentialSecret: "my-personal-secret-key",

Then save this file again and restart Node-Red. Now it is time to fire up Node-Red and create a basic flow.

Creating a basic Node-Red flow file with MQTT credentials

We will build a very simple example. This is a weather station that outputs the weather as a string and sends it to a debug output and to a MQTT output node. An inject node, a function node and a debug node is needed for each. This is how the flow will look:

qbee-node-red-flow1

The complete flow can be seen here and be copied into your Node-Red editor:

flows.json
[{"id":"117f088e.84eb97","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"d75ee7b0.0775c8","type":"inject","z":"117f088e.84eb97","name":"weather","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"10","crontab":"","once":true,"onceDelay":0.1,"topic":"weather","payload":"very good","payloadType":"str","x":171.5,"y":175,"wires":[["d1e24e2f.b5a2"]]},{"id":"d1e24e2f.b5a2","type":"function","z":"117f088e.84eb97","name":"weather station","func":"var payload = msg.payload;\n\nmsg.payload=\"The weather is \"+msg.payload;\n\nreturn msg;","outputs":1,"noerr":0,"x":373.5,"y":175,"wires":[["e7b731c2.2f7f2","ae91ac8a.7bf89"]]},{"id":"e7b731c2.2f7f2","type":"debug","z":"117f088e.84eb97","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":586.5,"y":174,"wires":[]},{"id":"ae91ac8a.7bf89","type":"mqtt out","z":"117f088e.84eb97","name":"","topic":"","qos":"","retain":"","broker":"bcb17249.9cb7c","x":580,"y":260,"wires":[]},{"id":"bcb17249.9cb7c","type":"mqtt-broker","z":"","name":"qbee","broker":"your-broker.com","port":"8888","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

The weather is hard-coded for now. The relevant injectors and functions look as follows:

qbee-node-red-weather

qbee-node-red-function

Please run the flow locally, add your MQTT credentials and you should see the weather message both in the debug output and your MQTT stream.

This can now be played out to a large number of devices

This flow is a static flow and this could be played out to a large number of devices by including settings.js, flows_cred.json. How this is done will be explained below. But we don't want to bore you and add another interesting step.

Adding the magic - qbee templating

qbee has a templating engine that can identify any value in any configuration file and make it accessible as a key-value pair. This is done by using Mustache annotation with double curly brackets around a variable. So the "good" weather turns into the variable {{weather}}. Please see more below.

Creating flows with templating to allow for automated adjustment of parameters

In real life Node-Red applications changes often need to be introduced between different installations. Typical examples of this are:

  • different modbus addresses
  • a different string to tag a device
  • adjust an update rate dynamically
  • point to a different MQTT server
  • change the MQTT topic

With qbee Node-Red templating you can do all this on a device or group level through the templating engine.

Let's revisit the flow and change it into qbee's templating format by using a so-called Mustache notation. This basically means that the exposed key-value variable will be made accessible by qbee. So instead of having the value "very good" we define the payload as {{weather}} introducing "weather" as key and defining the value through qbee templating. This can be done in the Node-Red editor or just in the json text file. If done in the editor please deploy to write the json.

Ignore editor warnings when defining numbers as string

If you have defined a payload as number and then you introduce a {{my-number}} string an error message is issued. This can be ignored. Just make sure that the value you insert later is a number.

qbee-node-red-weather-mustache

Instead of using the flows.json we copy and rename the new flow into

flows.tmpl

This file is used by the qbee agent to create the real flows.json on the device after the automated key-value extraction.

Now you upload the settings.js and the flows.tmpl and the according flows_cred.json to the qbee file manager.

Tip

The qbee file manager does not work with hidden directories like .node-red yet. Please copy the files to a non-hidden location and upload from there.

Now everything is prepared to play out files and configuration to real target machines. We are assuming that we run Node-Red as user pi from the /home/pi user directory.

Distributing flows with different settings to different devices by using templating

In the devices tab of qbee we create a tree structure with one or more devices to experiment. In this case we can later select a whole group, a sub-group or a single device to execute the file distribution upon. For a reference about the qbee tree concept please look here.

It is also possible to play out the same flows to all devices in production and then configure them differently for group "industrialRPI" and Systec. If a device is moved from one group to another or if new devices will be moved into a group they will automatically receive the correct Node-Red files and templating variables. For a proper Node-Red file distribution we need to play out the settings.js, the flows.tmpl (if no templating is used use flows.json) and the flows_cred.json.

qbee-node-red-targets

Now we can start defining the configuration to achieve the templating. Go into the "Configure" menu of qbee and navigate to the group or device that is supposed to receive the configuration. Select the function "file distribution" from the "system" menu. In our case we select the single device "A-devpi2", as can be seen in the selector box.

qbee-node-red-configuration

In system->file distribution please enable this for this group level and then we define the files we want to distribute. In our case all files reside in the /node-demo/ folder in the file manager.

So first we distribute

/node-demo/settings.js

to

/home/pi/.node-red/settings.js

and then we do the same with the credential file:

/node-demo/flows_cred.json

to

/home/pi/.node-red/flows_cred.json

The third file is slightly different. If you only have static content you would play out the flows.json. But since we demo templating we distribute the file with the templating variable in Mustache notation that we called flows.tmpl. In addition, we need to check the "templating" box that allows to define one or more key-value pairs:

/node-demo/flows.tmpl

to

/home/pi/.node-red/flows.json

What happens with the flows.tmpl file?

Any time there is a change in the value for the key "weather" qbee will expand the flows.tmpl into a new flows.json on the device. This will then trigger the "command to run" and restart Node-Red. From there the flow runs with the new variable.

Now we can define what the weather should be this time. We define it as "bad"

weather : bad

In order to make this all active we need to do one more thing. We need to issue a "command to run" that will be triggered each time something changes or the first time a device is moved into that group. Commands in this box can be chained using &&. There are two things to do here. First we need to change the ownership of the settings.js file to the local user "pi" and also to group pi. The command chown pi.pi file changes both user and group for "file". This is needed in order to allow Node-Red to function properly. And then we need to restart Node-Red as user "pi":

chown pi.pi /home/pi/.node-red/settings.js && sudo -u pi node-red-restart

Please save and commit these changes and wait one run interval of the qbee agent. In the log files you will see that both the flows.tmpl file was transformed into the flows.json with the appropriate value and that the settings.js and flows_cred.json files were played out. It also displays that Node-Red was restarted automatically.

Any new commit will cause qbee to restart Node-Red

Any change in configuration causes qbee to restart the service thus making configuration valid automatically.

Only saved AND committed changes will become active

Don't forget to save AND commit (button in the right hand corner) in order to make this configuration active.

qbee-node-red-deployed

Investigating the result of the remote Node-Red deployment in the MQTT stream

Did it work? Well, let's see by going to the remote device and press on the debug pane. There is a manual for remote web access here. Now there should be a weather state defined as bad. Just try to change it, save and commit and after the next agent run it will change according to whatever value you selected in the key-value form.

Ok, then let's check how it could look like for different weather settings in an MQTT client like CloudMQTT:

qbee-node-red-MQTT

Tip

At this point in time it is possible to define groups of devices running node-red and provide them with a common flow. The templating variables will get set through the qbee UI. Any change is logged in the audit trail of qbee so it is possible to see which user has set something and what it actually was.

qbee.io has an audit function that tracks any change to the system. below you see the audit entry for the

qbee-node-red-change-audit

So this tutorial has shown how to handle Node-Red and gave some insights into the pitfalls and problems associated with moving the flow, credential and setting files around between different installations and devices as often done in production environments. We also showed how qbee can help to setup Node Red headless as well as to distribute specific files to different devices. In addition, it is possible to work with templating which allows to re-use flows by exposing certain key-value variables into the qbee UI making them accessible from the tool (or through the API). Thus the templating allows to apply certain conditions to a group, a sub-group or a single device.

Some final thoughts and comments about Node-Red and HVAC setpoints

As a final remark it can be said that groups of devices can be configured with the same flow and specific properties can be changed as needed. This could be different update settings (fast on ethernet, slower for devices on mobile network), machine configurations or anything else. By moving a device into the correct group it will receive the correct settings. Any change is registered in the audit log. This records also the values. If anything changes the node-red process is restarted automatically and this is recorded in the logs as warning.

If we assume a Node-Red flow is used to control the HVAC system in a smart building context we could envision a fleet of industrial Raspberry Pi devices in different buildings controlling the HVAC or SCADA system. Now not all smart buildings and building managers will want to have the same setpoint for the climate control. Here grouping the devices and using flows.json templating with Mustache will resolve this very elegantly.

It is also possible to monitor the service and restart it automatically if it terminates through a watchdog like function.

Sign up for your free trial here!