Difference between revisions of "Automation - Node-Red"

From Da Nerd Mage Wiki
Jump to navigation Jump to search
 
(7 intermediate revisions by the same user not shown)
Line 42: Line 42:
== Installing Node-Red ==
== Installing Node-Red ==
=== Start by installing nodejs and the javascript package manager (npm) ===
=== Start by installing nodejs and the javascript package manager (npm) ===
<span style="color:#8B0082">'''NOTE:''' The Ubunto repositories serve up an outdated version of Node.js. (v10.19.0)</span>


<span style="color:#8B0082">Node-Red 2 requires v12... (and apparently, soon to require newer than that...)</span>
<span style="color: rgb(186, 55, 42);" >'''FFFfffuuuuuu...'''</span>


[[Getting nodejs v12{{!}}Getting nodejs v12]]
nodejs & npm versions are problematic.


<span style="color:#8B0082">To determine what version you actually have once it's installed:</span>
For some reason, the versions available in all of the repositories tend to be out of date AND all attempts to update them fail in weird & wonderful ways.
* <code><span style="color:#8B0082">nodejs -v</span></code>
 
Anywho...
[[Getting up-to-date NodeJS & NPM{{!}}I have found a procedure that seems to work]]
* <code>sudo apt install nodejs -y</code>
* <code>sudo apt-get install -y build-essential</code>
* <code>sudo apt install npm -y</code>
* <code>sudo npm install npm@latest -g</code>
** (Doesn't actually seem to change anything on Debian... Updates to 8.3.0 on Ubuntu.)


=== Use npm to install Node-RED ===
=== Use npm to install Node-RED ===


* <code>sudo npm install -g node-red</code>
* <code>sudo npm install -g node-red</code>
[[File:Node-red install output (2022-05-14).png{{!}}300px{{!}}thumb{{!}}Latest WTF?]]
[[File:Node-red install output (2022-05-14).png{{!}}300px{{!}}left{{!}}thumb{{!}}Latest WTF?]]
<div style="clear: both"></div>
<div style="clear: both"></div>


Line 73: Line 67:
* <code>sudo wget <nowiki>https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start</nowiki> -O /usr/bin/node-red-start</code>
* <code>sudo wget <nowiki>https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start</nowiki> -O /usr/bin/node-red-start</code>
* <code>sudo wget <nowiki>https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop</nowiki> -O /usr/bin/node-red-stop</code>
* <code>sudo wget <nowiki>https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop</nowiki> -O /usr/bin/node-red-stop</code>
<span style="color:#4B0082">'''Note:'''</span> These 3 files are kinda rpi specific...
 
===== <span style="color:#4B0082">'''Note:'''</span> These 3 files are kinda rpi specific... =====


<span style="color:#4B0082">To run as a user other than Pi, you need to edit the nodered.service file. To edit this use sudo to edit the file <code>/lib/systemd/system/nodered.service</code> and change the lines 11, 12 and 13.</span>
<span style="color:#4B0082">To run as a user other than Pi, you need to edit the nodered.service file. To edit this use sudo to edit the file <code>/lib/systemd/system/nodered.service</code> and change the lines 11, 12 and 13.</span>


*<code>sudo vi /lib/systemd/system/nodered.service</code>
*<code>sudo vi /lib/systemd/system/nodered.service</code>
<span style="color:#4B0082">Replace the user name "pi" with a user on your actual system...</span>
<span style="color:#4B0082">Replace the user name "pi" with a user on your actual system...</span>


<span style="color:#4B0082">'''Another Note:'''</span> If you find that you've outgrown Node-Reds available environment space...
===== <span style="color: rgb(224, 62, 45);">'''Another Note:'''</span> If you find that you've outgrown Node-Reds available environment space... =====
 
 


'''i.e.:''' it starts crashing with a message like: <code>'''FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory'''</code> in the logs...
'''i.e.:''' it starts crashing with a message like: <code>'''FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory'''</code> in the logs...


You can try editing line 16 of <code>/lib/systemd/system/nodered.service</code> and change the number 256 to something larger. (within as-yet undetermined limits...)
You can try editing line 16 of <code>/lib/systemd/system/nodered.service</code> and change the number 256 to something larger. (within as-yet undetermined limits...)


==== make the two scripts executable ====
==== make the two scripts executable ====

Latest revision as of 15:39, 7 March 2023

Proven on:
Logo Debian.png
11 (bullseye)


Logo LMDE.png
4


Logo Mint.png
19.3 / 20.3


Logo Ubuntu.png
20.04.3 issues


Logo Sparky.png
5.11


Node-Red Web Site

As always...

Start with:

  • sudo apt update
  • sudo apt upgrade

Installing Node-Red

Start by installing nodejs and the javascript package manager (npm)

FFFfffuuuuuu...

nodejs & npm versions are problematic.

For some reason, the versions available in all of the repositories tend to be out of date AND all attempts to update them fail in weird & wonderful ways.

I have found a procedure that seems to work

Use npm to install Node-RED

  • sudo npm install -g node-red
Latest WTF?

Run it...

  • node-red

Adding Autostart capability using SystemD

Then download three required files to their correct locations

  • sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service -O /lib/systemd/system/nodered.service
  • sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start -O /usr/bin/node-red-start
  • sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop -O /usr/bin/node-red-stop
Note: These 3 files are kinda rpi specific...

To run as a user other than Pi, you need to edit the nodered.service file. To edit this use sudo to edit the file /lib/systemd/system/nodered.service and change the lines 11, 12 and 13.

  • sudo vi /lib/systemd/system/nodered.service

Replace the user name "pi" with a user on your actual system...

Another Note: If you find that you've outgrown Node-Reds available environment space...

i.e.: it starts crashing with a message like: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory in the logs...

You can try editing line 16 of /lib/systemd/system/nodered.service and change the number 256 to something larger. (within as-yet undetermined limits...)

make the two scripts executable

  • sudo chmod +x /usr/bin/node-red-st*

reload the systemd daemon

  • sudo systemctl daemon-reload

enable Node-RED to run automatically

  • sudo systemctl enable nodered.service

Start Node-RED

  • sudo systemctl start nodered.service

Securing Node-RED

Useful management stuff

Learn about the current service status

  • sudo systemctl status nodered.service

(Re-)Start the Node-RED background service

  • sudo systemctl restart nodered.service

Stop the Node-RED background service

  • sudo systemctl stop nodered.service

Disable the Node-RED background service

  • sudo systemctl disable nodered.service

Get the service log since the last boot

  • sudo journalctl -f -u nodered.service -b -o cat

Also

Node-RED can then be started and stopped by using the commands:

  • node-red-start

and

  • node-red-stop

Updating

Apparently, just run the install again...

  • sudo npm install -g node-red

(pretty simple...)

Debugging Node-Red issues

ECONNRESET caused by MQTT

Error: Client network socket disconnected before secure TLS connection was established
Error: read ECONNRESET
Error: read ECONNRESET
Error: Client network socket disconnected before secure TLS connection was established

Hhhmmm...

ECONNREFUSED caused by alexa-home-conf

[error] [alexa-home-conf:919d2660.bd9218] Error: connect ECONNREFUSED 34.240.81.189:8883

Strange response from the creator: https://github.com/hardillb/node-red-contrib-alexa-home-skill/issues/51

Doesn't actually explain what's going on, but it's allegedly harmless. My question is: Why in fuck does it want to connect to a non-functional "alternate endpoint"?

Graceful restarts (maybe)

This is likely not needed when running under systemd...

From the Node-Red website

You can start a script as a daemon. But first install forever

sudo npm install -g --unsafe-perm forever

Then issue command:

forever start /usr/local/lib/node_modules/node-red/red.js

Things to do with Node-RED once it's installed

WIP:  These pages will appear as I get things updated from what's on the older wiki...

Some useful Node-Red tips

Some interesting Node-Red Add-Ons

  • Machine learning package for node-red
  • node-red-contrib-boolean-logic
    • Create "Buffer" from "Invert"
  • node-red-contrib-google-home-notify
  • node-red-dashboard
  • node-red-node-ping
    • There seem to be other versions... possibly better...
  • node-red-contrib-plex

Moving Node-Red to a new server

When creating a NEW server

NOTE: If you are updating to a much newer version of Node-Red, this is not likely the best technique...

Pretty easy actually...

Build the server & install everything as normal.

Then stop the node-red service and replace ~/.npm & ~/node-red with copies from the old server.

When Renaming a server

No longer seems to be an issue with current version of Node-Red...

(i.e.: Moving or cloning a VM...)

Seems to lose the flows. :(

They're not actually lost. :)

The flow files are named with the server name.

You can select a flow file in ~/.node-red/settings.js

Uncomment the line that says: //flowfile: 'flows.json', and change flows.json to the name of your actual flow file. (in the same folder...)

Then restart node-red

sudo systemctl restart nodered.service

If you are using other services on the server (i.e.: MQTT), you may run into further weirdness like having to tell nodes where to find these services. More research still required on this...