Node-RED

From ScottWiki
Revision as of 16:19, 15 December 2022 by Wikiadmin (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Running node-red on a Rasberry PI

After installing Rasbian-lite (or latest PI OS version) (you don't need a full desktop version) - less stuff to update.

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

The PI did not require it, but you may need to run

sudo apt install build-essential git

To put on the build-essentials and the git repository.


This script will:

  • remove the pre-packaged version of Node-RED and Node.js if they are present
  • install the current Node.js LTS release using the NodeSource. If it detects Node.js is already installed from NodeSource, it will ensure it is at least Node 8, but otherwise leave it alone
  • install the latest version of Node-RED using npm
  • optionally install a collection of useful Pi-specific nodes
  • setup Node-RED to run as a service and provide a set of commands to work with the service

Due to the limited memory of the Raspberry Pi, you will need to start Node-RED with an additional argument to tell the underlying Node.js process to free up unused memory sooner than it would otherwise. To do this, you should use the alternative node-red-pi command and pass in the max-old-space-size argument.

node-red-pi --max-old-space-size=256

Edit the systemd service file

nano /lib/systemd/system/nodered.service

Check to see if the parameter is being passed into the command line.

# systemd service file to start Node-RED

[Unit]
Description=Node-RED graphical event wiring tool
Wants=network.target
Documentation=http://nodered.org/docs/hardware/raspberrypi.html

[Service]
Type=simple
# Run as normal pi user - change to the user name you wish to run Node-RED as
User=pi
Group=pi
WorkingDirectory=/home/pi

Environment="NODE_OPTIONS=--max_old_space_size=256"
# uncomment and edit next line if you need an http proxy
#Environment="HTTP_PROXY=my.httpproxy.server.address"
# uncomment the next line for a more verbose log output
#Environment="NODE_RED_OPTIONS=-v"
# uncomment next line if you need to wait for time sync before starting
#ExecStartPre=/bin/bash -c '/bin/journalctl -b -u systemd-timesyncd | /bin/grep -q "systemd-timesyncd.* Synchronized to time server"'

ExecStart=/usr/bin/env node-red-pi $NODE_OPTIONS $NODE_RED_OPTIONS
#ExecStart=/usr/bin/env node $NODE_OPTIONS red.js $NODE_RED_OPTIONS
# Use SIGINT to stop
KillSignal=SIGINT
# Auto restart on crash
Restart=on-failure
RestartSec=20
# Tag things in the log
SyslogIdentifier=Node-RED
#StandardOutput=syslog

[Install]
WantedBy=multi-user.target

After editing the file, restart and check the service starts.

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


Setup a device for Tasmota

Host the tasmota-lite.bin file on a webserver, (not HTTPs)

  • Set the device in AP mode,
  • Connect to the device using your phone (using the AP wireless network)
  • Configure the device, for your network and let it reboot and connect to your WIFI

Scan the network (fing) and get the ipAddress of the device.

First we need to get the UnitID of the device.

wget -O- --post-data='{ "deviceid": "", "data": {} }' --header=Content-Type:application/json "http://192.168.3.64:8081/zeroconf/info"

Modify the DeviceID and the IP address to match the device you need to program.

 wget -O- --post-data='{ "deviceid": "100118285d", "data": {"switch": "on"} }' --header=Content-Type:application/json "http://192.168.3.64:8081/zeroconf/switch"
 wget -O- --post-data='{ "deviceid": "100118285d", "data": {} }' --header=Content-Type:application/json "http://192.168.3.64:8081/zeroconf/ota_unlock"
 wget -O- --post-data='{ "deviceid": "100118285d", "data": {"downloadUrl": "http://192.168.3.200/downloads/tasmota-lite.bin", "sha256sum":"cfadf180c59dc8d273af1feefd8ec4a49a679fa3ff40c1776a4fdbdd982cdb2c"} }' --header=Content-Type:application/json "http://192.168.3.64:8081/zeroconf/ota_flash"

Mosquito MQTT install

Add the necessary PPA for Mosquitto:

add-apt-repository ppa:mosquitto-dev/mosquitto-ppa

Allow unattended updates from this repository by add to /etc/apt/apt.conf.d/50unattended-upgrades:

Unattended-Upgrade::Allowed-Origins { 
 // Append to end of existing entries
  "LP-PPA-mosquitto-dev-mosquitto-ppa:focal";
};

Install Mosquitto:

apt install mosquitto

Create an MQTT password file:

mosquitto_passwd -c /etc/mosquitto/passwd mqttbr

Create /etc/mosquitto/conf.d/local.conf:

listener 1883
log_timestamp_format %Y-%m-%dT%H:%M:%S
allow_anonymous false                                 <- this is only needed for password only (set to true for anonymous access
password_file /etc/mosquitto/passwd                   <- Note required with anonymous access.

Edit /etc/mosquitto/mosquitto.conf:

persistence_location /home/nodered

Allow Mosquitto to write its database to the Node-RED home folder:

adduser mosquitto nodered
chmod 770 /home/nodered

Restart Mosquitto to read the changes:

systemctl restart mosquitto.service

Check it is running:

systemctl status mosquitto.service
journalctl -u mosquitto.service