Node Red - johnosbb/Automation GitHub Wiki

Guides

FAQ

  • Where is the node-red installation directory on the PI headless Server? :/home/ubuntu/.node-red
  • Where can I find the cofor the individual nodes? : /home/ubuntu/.node-red/node_modules
  • How to restart nodered: sudo systemctl restart nodered.service

Displaying Static Content

  • Add a location for static content to your settings file (settings.js) in ~/./node-red/ For example
httpStatic: '/home/ubuntu/.node-red/static',

An image called test.jpg placed in the static directory can then be displayed with

<img src="/test.jpg">

Using HTTP Request

HTTP Request with JSON body

Integrating with Dropbox

Configuration node

DropboxB DataConfiguration

Request an access token on https://www.dropbox.com/developers/apps/info/

Make sure you set the required permissions before generating the access token otherwise you will get an error like:

 {"error_summary": "missing_scope/.", "error": {".tag": "missing_scope", "required_scope": "files.content.read"}}

Dropbox Setting Permissions

Token Expiry

Sending a file to Dropbox

Dropbox Setting Permissions

Integrating with MongoDB

Based on the use of node-red-node-mongodb

Setting up the database configuration

MongoDB DataConfiguration

Configuring an in node to retrieve some data

MongoDB Configuration

Configuring an out node to insert some data

HTTP Request with JSON body

HTTP Request with JSON body

Storing images on MongoDB

Parsing Messages

Given a message like:

tele/SENSORS_FRONT/STATE : msg : Object
object
topic: "tele/SENSORS_FRONT/STATE"
payload: object
Time: "2021-09-16T18:51:08"
Uptime: "0T10:19:09"
UptimeSec: 37149
Heap: 27
SleepMode: "Dynamic"
Sleep: 50
LoadAvg: 19
MqttCount: 1
POWER1: "OFF"
POWER2: "OFF"
Wifi: object
qos: 0
retain: false
_msgid: "b8c0580330e8a880"


We can parse it with a switch node as follows

Switch Node

Or a Function Node:

msg.payload=msg.payload.POWER1;
node.warn(msg.payload);
return msg;

Run this script to update rasbian or hassbian image:

bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)
node-red-start
Then point your browser to localhost:1880
To start nodered as a service use: sudo systemctl enable nodered.service
Integrating with HomeAssistant

Open nodered in a browser
Got to the menu selector on the top right
select mange pallette
select the install tab
select and install the 'node-red-contrib-home-assistant' package
Restart nodered service with - sudo systemctl restart nodered.service
See Here (HASS & Node-Red - Integrating Them Together)

Dash Board

Url 192.168.1.xxx:1880/ui

Embedding an Image into a Node Red DashBoard

Using Template Nodes

Starting on Boot

Node Red as a Service on PI

# This script work on any system using systemd as the init process.
# It works on Debian/Raspbian Jessie.
# If you have Debian/Rapbian Wheezy and want to use this script with systemd
# follow the information here : https://wiki.debian.org/systemd

# To easily download, install and set at startup:
# wget -O /tmp/download https://gist.github.com/Belphemur/3f6d3bf211b0e8a18d93/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/systemd/system/ && sudo systemctl --reload-daemon && sudo systemctl enable Node-RED

# To consult the log : journalctl -u Node-RED
[Unit]
Description=Node-RED is a tool for wiring together hardware devices, APIs and online services in new and interesting ways.
After=syslog.target network.target
Documentation=http://nodered.org/

[Service]
Environment="NODE_OPTIONS=--max-old-space-size=128"
Environment="NODE_RED_OPTIONS=-v"
#Full Path to Node.js
ExecStart=/usr/local/bin/node-red-pi --max-old-space-size=256  $NODE_OPTIONS red.js $NODE_RED_OPTIONS
WorkingDirectory=/home/ubuntu/node-red/
# User/Group that launches node-RED (it's advised to create a new user for Node-RED)
# You can do : sudo useradd node-red
# then change the User=root by User=node-red
User=ubuntu
Group=ubuntu
Nice=10
SyslogIdentifier=Node-RED
StandardOutput=syslog
# Make Node-RED restart if it fails
Restart=on-failure
# Node-RED need a SIGINT to be notified to stop
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target
  • In /etc/systemd/system create a sym link
  • sudo ln -s /lib/systemd/system/nodered.service nodered.service
  • sudo systemctl enable Node-Red.service
  • sudo systemctl start Node-Red.service
  • journalctl -u nodered.service -b (Shows Node Red logs when running as a service)

Custom Nodes

Interfacing with RestDB.io

Getting Data

Switch Node

Details for the function node

var apikey = "7a................................";
msg.method = "GET";
msg.headers = { "x-apikey" : apikey };
msg.url = "https://xxxxxx-xxx.restdb.io/rest/Events";
return msg;

Adding New Data

Switch Node

Details for the function node

var apikey = "7a3.....................................e";
var date = new Date();
var data = { "date" : date, "type" : "motion", "details" : "Test Sensor Activated"};
msg.method = "POST";
msg.headers = { "x-apikey" : apikey , 'content-type': 'application/json' };
msg.url = "https://xxxxxx-xxxx.restdb.io/rest/events";
msg.payload = JSON.stringify(data);
return msg;