Node-RED is an example of a flow-based programming model .NODE RED is a powerful tool for building Internet of Things (IoT) applications by simply connecting & wiring together of code blocks & carry out tasks.It uses a visual programming approach that allows developers to connect predefined code blocks, known as ‘nodes’, together to perform a task.

The connected nodes, usually a combination of input nodes, processing nodes and output nodes, when wired together, make up a ‘flows’. Flows consist of multiple nodes wired together, with output tabs linked to input tabs of the next node in the flow. Messages flow along the nodes carrying data from node to node.

Today there is a vibrant user and developer community, with a core group working on the Node-RED code itself and most developers contributing nodes or flows to the flow library.

 

Image 4

In this post we use Raspberry PI 4 as hardware with Rasbian BURST OS & demonstrate a flow to control an LED connected to a GPIO pin of RPI.

Node RED is PRE-installed in Raspbian operating system by default.

Image 1

 

I’m accessing RPI from my Windows machine over WI-FI.

Both RPI & Windows PC are connected over WIFI to same Router.XRDP is installed on RPI & from Windows machine access is through Remote Desktop.

You can check out my previous blog on how to do this.

https://alselectro.wordpress.com/2019/12/10/raspberry-pi-how-to-auto-connect-wifi-and-view-on-windows-laptop/

 

Image 3

Though NODE RED comes preinstalled with Raspbian , you need to update initially.

Before starting Node Red , open Terminal & type in this command :

sudo apt-get install build-essential

—  to ensure npm is able to build any binary modules it needs to install. npm is Node Package Manager for the JavaScript .

Next Run the  following command to download and run the script

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

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.

It also installs the latest version of Node-RED using npm ,setup Node-RED to run as a service and provide a set of commands to work with the service

 

update1

This action will take around 10 minutes.

update2

 

Auto start on boot
——————–

If you want Node-RED to run when the Pi is turned on, or re-booted, you can enable the service to autostart by running the command:

sudo systemctl enable nodered.service

To disable the service, run the command:

sudo systemctl disable nodered.service

To start NODE RED  by clicking on NODE RED under Programming.As it starts running you can see an IP address link ending :1880  which is the default port of Nodered.

Once Node-RED is running you can access the editor in a browser.

Image 2

If you are using the built in browser on the Pi desktop, you can open the address:

http://localhost:1880.

I recommend using a browser outside of the PI and pointing it at Node-RED running on the Pi.

When browsing from another machine you should use the hostname or IP-address of the Pi: http://<hostname&gt;:1880.

You can find the IP address by running

hostname –I

on the Pi. In my case host name is 192.168.1.5 , so I open browser on Windows and type in :

http://192.168.1.5:1880

where 1880 is default port of NodeRed.

Now the NODE RED Window opens with colored NODES on the left side Palette.Center portion is the Flow Canvas where you create the flow by dragging in the nodes.Right side pane is for INFO & dEBUG MESSAGES.

Image 3

Scroll down the nodes to locate Raspberry PI.

rpi gpio  node is frequently used to control GPIO Pins of RPI , which has 2 options.

Image 11

 

One with the RPI icon to left of node is INPUT node (like button input) & the other with icon to Right is for OUTPUT node (like LED control).

Image 4

 

Input nodes allow you to input data into a Node-RED application or “flow”. They have at least one output endpoint represented by the small grey square only on their right side. You use input nodes to connect data from other services, for example the  Twitter, Google, serial, websockets or tcp nodes, or to manually input data into a flow using the inject node.

Output nodes allow you to send data outside of a Node-RED flow. They have a single input endpoint on their left side. You use output nodes to send data to other services, for example via Twitter, tcp, serial or email nodes, or to use the debug node to output to the debug pane

In this demo we control an LED connected to GPIO pin17 of RPI.

 

LED1

 

For this task we need an OUTPUT Node.

 

Image 5

Drag an Output Node to the canvas.

The output node with name PIN: is displayed with one RED triangle & a BLUE Circle over RPI icon.

The red triangle indicates that the properties are not yet set & the blue circle indicates that the node is not yet deployed to server.

Image 6

 

Double click on the Node to get PROPERTIES window.

As we have connected an LED at GPIO 17 (PIN 11) , select this pin.

Image 7

Select the type as DIGITAL OUTPUT.

Leave the Initial level of pin as LOW & provide a name as Red Led

 

Image 8

Click on DONE to see the RED Triangle disappear.

As we have not yet Deployed the node , the Blue circle remains.

Image 9

To feed a message we need an Input node like INJECT.

Drag and drop Inject node.It appears as timestamp node with a Grey button on left folloed by an Arrow.

 

Image 10

Double click on this Inject node to set properties.

Under PayLoad select String.

 

Image 11

Provide value of Payload as 1 & give a name as ON.

This is to inject 1 to RPI output node to make LED ON.

Click Done.

Image 12

Now drag on the small grey circle seen to the right of our ON inject node & connect to the RPI node Red Led.

The nodes for switching ON the LED are ready now.

Image 13

Next we need to create a node to switch OFF the LED.

Drag an input INJECT node again.

Image 14

 

Double click on this node and set the PayLoad String to 0.

Provide a name , like OFF.

Image 21

Connect the OFF node to Red Led node .

Now the FLOW is ready to be DEPLOYED.

Image 16

Click ON the red DEPLOY button on top right corner.

 

Image 17

The blue circles on Nodes disappear & you get Successfully Deployed message.

 

Image 19

Click on the Grey square button seen to the left of ON node.

This injects a String 1 to Red Led node , making the LED at gpio17 ON.

 

Image 20

 

IMG_20200110_151030

To make the LED OFF , click the grey button of OFF node.This injects string 0 , switching OFF the LED.

You can see the string o at the bottom of Red Led node when it receives the payload.

Image 22

Next drag a DEBUG node from Output list.

Image 23

No properties setting for this node.Just connect both the ON & OFF nodes to the

debug node (msg payload)

 

Image 24

At the right top click on the DEBUG icon to see the debug messages.

Clicking on the ON or OFF nodes will control the LED , as well display the debug messages.

Image 25

Next we shall add a BUTTON to control LED.

Remove the ON & OFF nodes we created earlier , by selecting the node and hitting Delete button.

Drag and drop an Input GPIO node from Raspberry PI list.

 

Image 27

Double click on node to set properties.

Select GPIO 13 , as we connect a button to this GPIO pin.

 

Image 28

 

IMG_20200110_164327

The LED is connected to GPIO 17  as shown below.

A 2 leg reset button is used , one end of button goes to 3.3v & the other end through a resistor to GND.

The junction point is connected to GPIO 13.

 

Image 20                    Image 30

Under properties select PULL DOWN , which applies an internal resistor to GND to the button.For safety we use an external resistor too.

Select Read initial state of pin & provide a name as Button.

Image 29

Connect the node to Red Led node & Click on Deploy.

Image 30

When the button is pressed a HIGH is applied to GPIO 13 , which in turn switches ON the LED ta GPIO 17.

 

Image 31

To import new NODES or to install DASHBOARD click on settings button next to Deploy button & select MANAGE PALETTE.

 

Image 8

To search for new nodes , you can click on NODES & download.

To install dashboard , click the install button & search for dashboard.

Select node red dashboard & install.

 

Image 9

 

Node-RED Dashboard is a module that provides set of nodes  to  create a live data dashboard.

To open the Node-RED UI, type your Raspberry Pi IP address in a web browser

followed by     :1880/ui

With Dashboard you can select your own nodes and create your own User Interface.

To access the settings file of Node Red

use command

ls –a

to list hidden files.

Then change directory to  .node-red

Here you can see the settings.js file where you can provide password for access to node red.

Image 40

 

VIDEO TUTORIAL :

blog_image