Node-RED Introduction

post hero image

Introduction

Node-RED, a project made by OpenJS Foundation and many contributors, is a programming tool that bind together hardware devices, APIs and online services. It provides a browser-based editor that makes it easy to write flows based on a wide range of nodes, which can be crafted by developers all around the world.

Node-RED is built on Node.js, so it can run on low-cost hardware such as Raspberry Pi devices and can communicate with MCUs like Arduino, Adafruit or ESP family boards.

Configure MQTT broker

Many projects rely on MQTT communication, which is a de-facto standard in the IoT world.

Let’s install local MQTT broker:

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt update
sudo apt install -y mosquitto mosquitto-clients
sudo apt clean

Starting with the release of mosquitto version 2.0.0, the default configuration will only bind to localhost as a move to a more secure default posture. If you want to be able to access the broker from other machines, you will need to explicitly edit the configuration files to either add a new listener that binds to the external IP address or add a bind entry for the default listener.

In /etc/mosquitto/mosquitto.conf set:

listener 1883
allow_anonymous true

Then, to start the broker, use the following command:

sudo mosquitto -c /etc/mosquitto/mosquitto.conf

You can now communicate through the mosquitto broker using default 1883 port of your local IP address.

Install & Run

On Debian-based Linux distributions like Ubuntu and Raspberry Pi OS, you can install Node-RED using one of the options below:

# 1) node package manager (suggested)
sudo npm install -g --unsafe-perm node-red

# 2) docker: https://nodered.org/docs/getting-started/docker
docker run -it -p 1880:1880 --name mynodered nodered/node-red

# 3) snap (not recommended)
sudo snap install node-red

Please Note: Do not run all the commands above. Choose only a method.

Once installed as a global module you can use the node-red command to start Node-RED in your terminal.

Add additional elements

Since Node-RED is community-driven, you can install additional nodes, flows and collections.

Run node-red command at least once to let it create the configuration files and .node-red directory.

Take a look at Node-RED Library and use the commands below to add a new element:

cd ./.node-red # change directory into Node-RED user directory
npm i <NODE_NAME> # https://flows.nodered.org/node/<NODE_NAME>

# some common nodes to install
npm i node-red-dashboard # https://flows.nodered.org/node/node-red-dashboard
npm i node-red-contrib-mqtt-broker # https://github.com/martin-doyle/node-red-contrib-aedes
npm i node-red-node-arduino # https://flows.nodered.org/node/node-red-node-arduino

Hello World Projects

In this section, I’ll briefly show some projects to demonstrate the powerful capabilities of Node-RED.

However, you can take a look at all the projects I made inside node-red directory of arduino-projects GitLab repository. Each project has an own README.md file: I suggest reading it carefully.

The projects below rely on an Arduino board provided with Wi-Fi connectivity. If not otherwise specified, the board used is an Arduino MKR WiFi 1010 which uses <WiFiNINA.h> library to connect itself to the network.

Please Note: You can also use an ESP family board, but you’d have to change the .ino sketch according to the way it connects to the network.

MQTT Hello World

Take a look at MQTT Hello World’s README.md file.

It demonstrates that the Arduino can be both published and/or subscriber in the MQTT protocol paradigm.

The Arduino has a servo motor connected to a PWM pin. This servo motor should open and close a door, or turn on and off a light or whatever you want. The board subscribes to /hello topic and listen for incoming messages. If the message’s payload is equal "open", the Arduino turn its built-in LED ON and move a servo motor to position 0. Else, if the payload is equal "close", the board turn its built-in LED OFF and move the given servo motor to position 180.

At the same time, the board publish to /hello topic the payload "world" once every a given period of time. It demonstrates that the Arduino can be both published and/or subscriber in the MQTT protocol paradigm.

You need to use (and install) the following flows:

You can use the commands below:

cd ./.node-red
npm i node-red-contrib-mqtt-broker node-red-dashboard

Conclusion

Before closing this blog post, let’s talk about an important topic mentioned above.

Static IP address

To avoid having to modify the arduino-secrets.h file everytime you shut down your machine, I suggest you to set up a local static IP address.

On a Linux machine with KDE as desktop environment, you can right-click on Internet icon and press Configure Network Connections... settings option. Then in IPv4 page, set Method as Manual and a new item as shown below:

static IP on KDE

As you can see, to choose the right address for the Gateway (and DNS Servers) you need to open a terminal and type:

nmcli d # show device list
DEVICE             TYPE      STATE         CONNECTION
wlp0s20f3          wifi      connected     FASTWEB-80F0D6
# ...

ip route # show networks information
default via 192.168.1.254 dev wlp0s20f3 proto dhcp metric 600
# ...

This is your gateway’s IP address. Then choose an arbitrary IP address for your machine, like 192.168.1.57 or 192.168.52.27 and apply the changes.

Disconnect and connect back to the network to get everything done properly.

Documentation