Getting start with Node-Red: Installation and discovery on Raspberry Pi 3 (or 2)

Node-Red is a graphical programming language for developing connected objects (and many other things). Node-Red is an open source (and free) project supported by IBM. It is an ideal language to start programming. To write this article, I used a Raspberry Pi 3 with the Raspbian distribution. Node-Red is available on Windows, Mac OS and Linux. The tips and explanations of this article are also valid for other platforms.

Node-Red on Raspbian Jessie with Pixel

Before carrying out our first program, we will start by discovering its operation and add some useful plugins. Node-Red is now included by default in Raspbian, you have nothing to install to start.

Start Node-Red

To start Node-Red on Raspberry in Menu -> Programming

You can also start from the Terminal with the

sudo node-red-start

If you discover the Pi (and can be Linux at the same time), by clicking on the icon of Node-Red you must have been disappointed to see that lines of text appear in the window of the Terminal. It is quite normal, its use will be entirely done in a web browser. You simply started a web server accessible from any computer (or tablet !!) connected to your local network.

Now that Node-Red is started, we will retrieve the ip address from it. It is located on the 2nd line.

If you want to program directly on your Pi, open the web browser and enter http://localhost:1880/ or http://127.0.0.1:1880/

If you want to use Node-Red from another computer or tablet on your network, open your browser and enter in the address bar

http://RASPBERRY_IP:1880/

node-red start demarrage naviguateur

Discovering the programming environment

Here is a small overview of the proposed development environment

On the left side of the screen are all available functions. In the center is the programming area. The right pane contains an info tab containing contextual information (example corresponding to the selected function …) and a debug tab which returns the error messages as well as the messages of the debug function which is in the output tools.

Each time you want to deploy your program, press the red Deploy button in the upper right corner.

The menu next to the Deploy button contains some additional tools:

  • View: Allows to show / hide the grid which can also be magnetic (snap to grid)
  • Import: Allows you to import nodes. It is with this option that you can add to your flow of nodes retrieved on the internet or on http://flows.nodered.org/
  • Export: You can export to the clipboard (a text string in JSON format) or to a file you will have to give a name. You can find this file in the directory ./home/pi/.node-red/lib/flows
  • Flows: You can add a new flow (Add), rename (Rename), delete (Delete) the current flow

Using Node-Red on a tablet

It is quite possible to develop on a tablet connected to the local network on which the Node-Red server is started. The only notable difference is the manipulation of the blocks in the programming area. The keyboard and mouse have been replaced by a context menu which will be displayed by a long press in the programming area or on the block to be modified. It allows you to copy/cut/paste, delete/modify the object and cancel the last modification.

node-red tablette ipad galaxy tab android

On the tablet, press long on the object or the programming area to bring up the menu.

Install (or update) npm

Before going any further, let’s see how to add packages (called flow).

Let’s start by adding a new tab to the Terminal or open a new Terminal and enter the following command that will stop NodeRed.

sudo node-red-stop

Node-Red uses the nodejs npm package manager (the Node-Red javascript engine). Here are the steps to install it. The first step is to update the system

sudo apt-get update

Then install npm

sudo apt-get install npm

Finally, we update npm

sudo npm i npm -g

Installing node-red-admin

The first module that I propose to install is node-red-admin. As its name indicates it adds some useful functions:

  • List to list the installed nodes
  • Enable or disable to enable/disable a node.
  • Search to search for a node on a keyword. For example, sudo node-red-admin search weather will return you 4 nodes to connect to online weather services (Forecast.io, openweathermap, underground and yr.no).
  • Install to install a Node
  • Remove to uninstall a Node
  • Info to display information about a particular Node

To install node-red-admin, go to the node-red directory.

cd ./home/pi/.node-red

For those who discover Linux, the point in front of the directory means that it is a hidden directory. It is not displayed in the file manager.

Now enter the following command in the Terminal

npm install -g node-red-admin

Install new modules (Node)

The Node-Red community develops a large number of plugins (about 480 nodes currently) in all domains: connected objects (ZWave, Sonos ..), online services (weather, twitter …), databases … All The nodes are listed here.

For this article we will install node-red-node-openweathermap (page) module. It allows to recover the local weather from the site openweathermap. Open the Palette Manager (read this article to know more).

node-red manage palette

Then search  node-red-node-openweathermap and click install

node-red dashboard ui module

In case of problem, stop Node-RED and execute this command to install the module directly with npm.

npm install node-red-node-openweathermap

Full of flows to discover Node-Red

If you discover Node-Red or you already have a project in mind, start by going to the flows filed on fows.nodered.org. There are already over 250 in all domains (check flows below the search box to filter them).

Start automatically Node-Red when starting the Raspberry Pi

Your program is ready and you would like it to start automatically when starting your Raspberry Pi. It is quite possible, enter the following command in the Terminal.

sudo systemctrl enable nodred.service

To stop the automatic startup, enter the following command

sudo systemctrl disable nodred.service

Start automatically with PM2

There is another option to start automatically. The PM2 Process Manager for Nodejs is recommended. PM2 provides other tools to more easily manage startup, shutdown, opening of the execution log, and so on. It’s much more flexible than using the Linux and systemd services. Begin by installing PM2 with this command:

sudo npm install -g pm2

If you do not know Node-RED path, run this command

which node-red

On Linux / Raspbian, it should be in /usr/bin/node-red .

To run NodeRED, run this command PM2

pm2 start /usr/bin/node-red -- -v

Finally, the configuration is saved and automatic start is activated. That’s all.

pm2 save
pm2 startup

First program with Node-Red: flashing a Led

To start, we will simply flash a connected LED on the GPIO4 of the Raspberry Pi. To do this we will use the inject function which sends a pulse every second (at choice). At each pulse, the “Reverse Led” function is used to turn the LED on or off.

node-red blink led example clignote led

Copy and then import this code directly (Menu -> Import -> Clipboard) into your flow.

[{"id":"3ff9c2cd.ef914e","type":"function","z":"16966388.6f0e2c","name":"Inverse état Led","func":"\ncontext.state = context.state || 0;\n\n(context.state == 0) ? context.state = 1 : context.state = 0;\nmsg.payload = context.state;\n\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":320,"wires":[["b91a94fa.ec1e48","59b95cea.56d4f4"]]},{"id":"b91a94fa.ec1e48","type":"debug","z":"16966388.6f0e2c","name":"","active":true,"x":570,"y":360,"wires":[]},{"id":"9b2414e2.c584c8","type":"inject","z":"16966388.6f0e2c","name":"chaque seconde","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"x":149,"y":320.0000009536743,"wires":[["3ff9c2cd.ef914e"]]},{"id":"59b95cea.56d4f4","type":"rpi-gpio out","z":"16966388.6f0e2c","name":"Led sur GPIO4","pin":"16","set":false,"out":"out","x":580,"y":280,"wires":[]}]

Flash a Led with each Tweet on a keyword

Let’s go a little further now. We will flash a Led whenever a Tweet is published on the #arduino keyword on your account.

node-red blink led tweet filter

Let’s start by adding a node twitter (take the one with the bird on the left). Double click to open the control panel and click on the pencil.

1 node-red twitter add credential

Click on “Click here to authenticate with Twitter”

2 node-red twitter authenticate with twitter

You will be redirected to Twitter. Click “Allow application” (sorry, I’m French)

3 node-red twitter autoriser application

Finally click “Add” to complete the connection with your Twitter account.

4 node-red twitter twitter id

From now on, you can retrieve or post Tweets. For this article, we are interested in filtering the Tweets posted on the keyword #arduino. Choose “all public tweets” in front of Search and enter #arduino in the for field. You can add several keywords separated by a comma but also people (symbol @).

Whenever a Tweet is detected, we will do 3 things:

  • Show the Tweet in the debug console
  • Trigger the LED flashing on the GPIO4
  • Increment a counter and display the one in the debug console.

node-red tweet search filtrer filtrer tweet

Voici le code de cet exemple qu’il vous suffit de copier puis importer dans un flow.

[{"id":"d40bbabd.933568","type":"debug","z":"7acbed96.061554","name":"Tweet reçu","active":true,"console":"false","complete":"payload","x":390,"y":160,"wires":[]},{"id":"ff741292.77408","type":"twitter in","z":"7acbed96.061554","twitter":"","tags":"#arduino","user":"false","name":"Filtre les tweets sur #Arduino","topic":"tweets","x":140,"y":220,"wires":[["d40bbabd.933568","d06dda9f.451c58","ba64a3f8.03aea"]]},{"id":"881cc7d6.ea3578","type":"rpi-gpio out","z":"7acbed96.061554","name":"Led sur GPIO4","pin":"16","set":false,"out":"out","x":600,"y":220,"wires":[]},{"id":"d06dda9f.451c58","type":"trigger","z":"7acbed96.061554","op1":"1","op2":"0","op1type":"val","op2type":"val","duration":"750","extend":false,"units":"ms","reset":"","name":"Fait clignoter la led ","x":410,"y":220,"wires":[["881cc7d6.ea3578"]]},{"id":"ba64a3f8.03aea","type":"function","z":"7acbed96.061554","name":"Compteur de Tweet","func":"\ncontext.state = context.state || 0;\n\ncontext.state ++;\nmsg.payload = context.state;\n\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":280,"wires":[["600494ad.09533c"]]},{"id":"600494ad.09533c","type":"debug","z":"7acbed96.061554","name":"Compteur","active":true,"console":"false","complete":"payload","x":580,"y":280,"wires":[]}]

Get local weather on OpenWeatherMap

To recover the local weather, we will use the openweathermap node that allows to recover the weather via the OpenWeatherMap API. To use it, you must retrieve an API key that is free for personal use (link to the OpenWeatherMap homepage). You will be able to make 50,000 queries a day or 600 every 10 minutes, that should be enough to make some tests!

To recover a key, create an account (free) then go to the setup tab of My Home.

Add an openweathermap node (the bottom one) in a flow. Double-click:

  • Paste your API key
  • Indicate the city (or country) or GPS coordinates of the place you want to know the weather
  • Plug a node debug to send the recovered weather into the console

node-red openweathermap

It’s as simple as that!

node-red openweathermap météo

[{"id":"6fa26a7b.c92204","type":"openweathermap in","z":"77ff7e2d.d10ff","name":"Météo à Paris","lon":"","lat":"","city":"Paris","country":"France","x":270,"y":100,"wires":[["ec5c6880.844ee8"]]},{"id":"ec5c6880.844ee8","type":"debug","z":"77ff7e2d.d10ff","name":"","active":true,"console":"false","complete":"false","x":450,"y":100,"wires":[]}]

What to do if Node-Red no longer works

Even if Node-Red is already a very successful solution, there are still tools to develop. There is no tool available (neither from the browser interface, nor from the command line) to manage flows (to my knowledge at least). If Node-Red refuses to start, this can happen for example if you have an error in a flow, you will have to intervene “by hand” on the file flows_raspberrypi.json.

This file is located in the /home/pi/.node-red/  directory

The most difficult is to edit this JSON file. In a text editor, you will have a single line of text. Not obvious with the nano text editor that does not refer to the line automatically. The best way is to use the Raspbian leafpad text editor instead of nano.

sudo leafpad flows_raspberrypi.json

On the Options menu, check the “Automatic line wrap” option. After correcting the problem, do “Save as” without worrying about the warning.

You can also simply delete the file using the command

rm flows_raspberrypi.json

A new file will be created the next time you start it.

Usefull links

Updates

[06/27/2017] – Palette manager. Auto startup with PM2

To learn more about Node-RED, see the dedicated category.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

DIY Projects