Appunti su Node-RED

In questa pagina sto raccogliendo tutti i miei appunti e link interessanti su Node-RED

Link alle sessioni Node-red su cloud per le prove durante le lezioni

MQTT broker di servizio

Show the system log on a terminal session

sudo journalctl -f -u node-red -o cat

To generate the messages inside NodeRED:

node.log("Something happened");
node.warn("Something happened you should know about");
node.error("Oh no, something bad happened");

Variables scope

// initialise the counter to 0 if it doesn't exist already
var count = context.get('count')||0;
count += 1;
// store the value back
context.set('count',count);
// make it part of the outgoing msg object
msg.count = count;

Scope types:

  • context Static data at node level
  • flow Static data at flow level
  • global Static data at global level

Ref: https://nodered.org/docs/writing-functions

Change the node status

node.status({fill:"red",shape:"ring",text:"disconnected"});
node.status({fill:"green",shape:"dot",text:"connected"});
node.status({text:"Just text status"});
node.status({});   // to clear the status

Installing

Roadrunner

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo npm install -g --unsafe-perm node-red

Rasbian Stretch Lite 2018-03-13

Type at command line prompt:

bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)

Enable the systemd service by typing:

sudo systemctl enable node-red.service

Then start it:

sudo systemctl start node-red.service

Get the access to the NodeRED IDE:

Enabling NodeRED projects

Install git by typing:

sudo apt-get update
sudo apt-get install git

Enable the project feature editing the file setting.js:

sudo nano .node-red/settings.js

Add these lines inside the module.exports session:

module.exports = {

    editorTheme: {
       projects: {
           enabled: true
       }
    },

...

Restart NodeRED:

sudo systemctl restart node-red.service

Check for any errors at NodeRED startup:

sudo journalctl -f -u node-red -o cat

Systemd

Salvare il seguente contenuto in: /lib/systemd/system/node-red.service:

[Unit]
Description=Node-RED 
After=syslog.target network.target

[Service]
ExecStart=/usr/bin/node-red 
WorkingDirectory=/home/tanzilli/
User=tanzilli
Group=tanzilli
Nice=10
SyslogIdentifier=Node-RED
StandardOutput=syslog
Restart=on-failure
# Node-RED need a SIGINT to be notified to stop
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

Poi dare i comandi:

sudo systemctl daemon-reload
sudo systemctl enable node-red
sudo systemctl start node-red

Security

Install node-red-admin

sudo npm install -g node-red-admin

Create a password hash by typing:

node-red-admin hash-pw

Add these lines in .node-red/settings.js inside the module.exports session:

module.exports = {
    adminAuth: {
        type: "credentials",
        users: [{
            username: "admin",
            password: "$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN.",
            permissions: "*"
        }]
    },

Restart NodeRED:

sudo systemctl restart nodered.service

Check for any errors at NodeRED startup:

sudo journalctl -f -u node-red -o cat

Links

Sergio Tanzilli (Autore - Progettista hw & sw - Imprenditore - Musicista dilettante, classe 1964)
Fondatore di Area SX srl, Acme Systems srl e del TanzoLab, si occupa dal 1974 di elettronica digitale, dal 1978 di microprocessori, dal 1992 di microcontroller, dal 1995 di Linux e pubblicazioni su Web, dal 2005 di progettazione, produzione e commercializzazione di schede Linux Embedded.
http://www.tanzilli.com - http://www.acmesystems.it - https://github.com/tanzilli - sergio@tanzilli.com