Getting Started with Node-RED on Raspberry Pi

Get started with Node-RED on the Raspberry Pi. Node-RED is a powerful open-source tool for visual programming to build Internet of Things (IoT) applications. In this tutorial, we’ll cover what is Node-RED, how to install it, and how to use the visual interface to create a simple flow.

Getting Started with Node-RED on Raspberry Pi

Prerequisites

We assume that you are familiar with the Raspberry Pi, you know how to install the operating system, and you know how to establish an SSH connection with your Pi. You can take a look at the following tutorials first:

What’s Node-RED?

Node-RED is a powerful open-source tool for building Internet of Things (IoT) applications with the goal of simplifying the programming component.

Node-RED runs on the web browser and it uses visual programming that allows you to connect code blocks, known as nodes, together to perform a task. The nodes when wired together are called flows.

node-red overview

Why is Node-RED a great solution?

  • Node-RED is open source and developed by IBM.
  • The Raspberry Pi runs Node-RED perfectly.
  • It is a visual programming tool, which makes it more accessible to a wider range of users.
  • With Node-RED you can spend more time making cool stuff, rather than spending countless hours writing code.

What can you do with Node-RED?

Node-RED makes it easy to:

  • Access your RPi GPIOs;
  • Establish an MQTT connection with other devices (Arduino, ESP8266, ESP32 etc);
  • Create a responsive graphical user interface for your projects;
  • Communicate with third-party services (IFTTT.com, Adafruit.io, ThingSpeak, Home Assistant, InfluxDB etc);
  • Retrieve data from the web (weather forecast, stock prices, emails. etc);
  • Create time-triggered events;
  • Store and retrieve data from a database.

Here’s a library with some examples of flows and nodes for Node-RED.

Installing Node-RED on Raspberry Pi

Having an SSH connection established with your Raspberry Pi, enter the following to install Node-RED:

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

Node-RED is installed by default on the Raspberry Pi OS (32-bit). However, it is recommended to run the previous command to install the required packages and update it to the most recent version. You’ll get a message informing you about this. Press Y and press Enter to accept.

Node-RED is not installed by default on Raspberry Pi OS (64-bit).

Then, you’ll be asked: “Would you like to install Pi-specific nodes?” Press Y and Enter.

It will take a few minutes to install Node-RED. In the end, you should get a similar message on the Terminal window:

Installing Node-RED on Raspberry Pi

Configure Node-RED Settings

After installing, it is recommended to configure initial options and settings. Run the following command:

node-red admin init
Configure Node-RED Settings
  • Press Enter to create a Node-RED Settings file on /home/pi/.node-red/settings.js
  • Do you want to set up user security? Yes.
  • Enter a username and press Enter (you need to remember it later).
  • Enter a password and press Enter (you need to remember it later).
  • Then, you need to define user permissions. We’ll set full access, make sure the full access option is highlighted in blue and press Enter.
  • You can add other users with different permissions if you want. We’ll just create one user for now. You can always add other users later.
  • Do you want to enable the Projects feature? No.
  • Enter a name for your flows file. Press Enter to select the default name flows.json.
  • Provide a passphrase to encrypt your credentials file. Learn more about what is a passphrase.
  • Select a theme for the editor. Simply press Enter to select default.
  • Press Enter again to select the default text editor.
  • Allow Function nodes to load external modules? Yes.

Node-RED configuration was successful. All settings are saved on settings.js.

Start Node-RED

Run the following command to start Node-RED:

node-red-start

You should get a similar message on the Terminal:

Node-RED start Terminal Window

Autostart Node-RED on boot

To automatically run Node-RED when the Pi boots up, you need to enter the following command. This means that as long as your Raspberry Pi is powered up, Node-RED will be running.

sudo systemctl enable nodered.service

Now, restart your Pi so the autostart takes effect. The next time the Raspberry Pi restarts, Node-RED will be already running.

sudo reboot

If, later on, you want to disable autostart on boot, you can run:

sudo systemctl disable nodered.service

For more information about the installation process, check the official documentation.

Access Node-RED

Node-RED runs on port 1880. To access Node-RED open a browser and type the Raspberry Pi IP address followed by :1880. For example:

192.164.1.106:1880

To get your Raspberry Pi IP address, you can run the following command:

hostname -I
Get Raspberry Pi IP Address

After entering the Raspberry Pi IP address followed by :1880 on the web browser, you’ll get access to the Node-RED login page. Log in with the username and password you’ve set previously.

Node-RED Login Page

Now, you have access to Node-RED. You can start building your flows.

Node-RED Welcome Screen

Node-RED Overview

Let’s take a look at the Node-RED visual interface.

Node-RED Visual Interface

Node-RED Interface Main sections

The following picture shows the Node-RED main sections labeled.

Node-RED Overview

Nodes

On the left sidebar, you can see a list with a bunch of blocks. These blocks are called nodes and they are separated by their functionality. If you select a node, you can see how it works in the nodes documentation tab.

Nodes have input and/or output ports to receive and send information to other nodes. For example, a node receives an input from a previous node, processes that information, and outputs a different message to another node that will do something with that information. The information passed between nodes is called a message.

Flow

The nodes are the building blocks of a flow. You wire nodes together to create a flow that will perform a certain task. A Flow is also a tab in the workspace where you place and organize the nodes.

In the center, you have the Flow and this is where you place the nodes.

Right Sidebar

The sidebar at the right has several tools.

  • Information: shows information about the flows;
  • Help: shows the nodes’ documentation;
  • Debug: the bug icon opens a debugging window that shows messages passed to debug nodes—it’s useful for debugging purposes;
  • Config nodes: the gear icon shows information about configuration nodes. Configuration nodes do not appear on the main workspace, and they are special nodes that hold reusable configurations that can be shared by several nodes in a flow.

Deploy

The deploy button saves all changes made to the flow and starts running the flow.

Creating a simple flow

To get you used to the Node-RED interface, let’s create a simple flow. The flow we’ll create, simply prints a message to the debug console, when triggered.

Drag an inject node and a debug node to your flow and wire them together.

Node-RED debug flow

Now, let’s edit the inject node. Double-click the node. In the figure below, you can see the different settings you can change.

On the msg.payload field, select string and type Hello. Then, click Done.

Node-RED Edit Inject Node

Messages (msg) in Node-RED are javascript objects that can have multiple properties. The payload is the default property most nodes work with. You can think of it as the main content of the message you want to send to the next node. In our case, we’re simply sending a text message.

We won’t edit the debug node, but you can double-click on it to check its properties.

Node-RED debug node

You can select the output of the debug node, which is msg.payload and where we want to send that output. In our case, we want to send it to the debug window.

To save your application, you need to click the Deploy button in the top right corner.

Node-RED deploy button

Your application is saved.

Testing the flow

Let’s test our simple flow. Open the debug window and click the inject node to trigger the flow.

Node-RED Hello World Example

As you can see, our message is printed in the debug window when you trigger the inject node. This is a very basic example and it doesn’t do anything useful. However, the purpose of this post is to get you familiar with the Node-RED interface. In no time, you’ll start creating your own flows.

Exporting and Importing Nodes

In this section, you’ll learn how to save your nodes. This is useful if you need to:

  • Backup your Node-RED flow
  • Move your flow to another Raspberry Pi (or machine)
  • Share your Node-RED project with others

Open the main menu, and select the Export option.

Node-RED export nodes

A new window opens. You can select if you want to save the selected nodes, the current flow, or all flows. You can also download the nodes as a JSON file or copy the JSON to the clipboard.

Node-RED export nodes window

To show you how it works, click on Download for the selected nodes. It will download a JSON file called flows.json.

You can import those nodes later to another Raspberry Pi or another machine with Node-RED installed, by going to the main menu and selecting the Import option.

On the Import nodes window, you can upload a JSON file or paste raw JSON.

Import flow Node-RED

Installing Pallete Nodes

As we’ve seen previously, Node-RED comes with a bunch of pre-installed nodes on the Pallete (left sidebar). There are many more nodes available that you can install and use for your projects. You can find them in the Node-RED library. If you need some specific task for your project, there’s probably already a node for that.

For example, if you need to add the feature to send an email to your flow, you can google something like this: “send email node-red node”. One of the first search results is this page with the node-red-node-email. It comes with some nodes to send and receive emails.

If you want to install those nodes (or any other nodes) so that you can use them on your flow, go to the main menu and select the option Manage palette.

Node-RED manage palette

The following window will open. Select the install tab and search for the nodes you want to install, for example, node-red-node-email.

Install Node-RED Nodes

Node-RED Dashboard

Node-RED Dashboard is a module that provides a set of nodes in Node-RED to quickly create a live data dashboard. You can install those nodes using the Menu > Manage Palette. Then, search for node-red-dashboard and install it.

Install Node-RED dashboard palette

After installing, the dashboard nodes will show up on the palette.

Node-RED dashboard nodes

Nodes from the dashboard section provide widgets that show up in your application user interface (UI). The user interface is accessible on the following URL:

http://Your_RPi_IP_address:1880/ui

For example, in my case:

http://192.168.1.106:1880/ui
welcome node-red dashboard

At the moment, you’ll see the previous screen when you access the UI. That’s because you haven’t added any of those dashboard nodes to the flow. We won’t cover how to create a dashboard user interface in this tutorial. If you want to learn more, please read:

Wrapping Up

This tutorial is a quick getting started guide for Node-RED. You learned how to install Node-RED on a Raspberry Pi, how to create a simple flow, import and export nodes, install nodes and install the Node-RED dashboard.

You can also install Node-RED on a cloud server like Digital Ocean, for example. We have a tutorial about that: Access Node-RED Dashboard from Anywhere using Digital Ocean.

You may also like:

Node-RED is very useful for IoT projects that use MQTT, so you may want to check all our MQTT projects:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

23 thoughts on “Getting Started with Node-RED on Raspberry Pi”

      • In the latest distro (buster) dubbed “Raspberry Pi OS, Node Red is not part of the image, it has to be installed. I am a novice with the Pi, and found it a bit frustrating to try to put it on a Raspberry Pi 0 W. After ignoring many warnings and error reports, it seemed to run, but much too slow to functional. I wanted to use the Pi 0 W in my project due to it’s size and built-in wifi but that may not be feasible. In the meantime I’ll try learning about Node Red and the next phase “Dashboard” using a Laptop with Ubuntu with Node Red and Dashboard installed on it.
        Thank you Rui and Sara for all your work!

        Reply
  1. Thanks a lot! I’ve been looking for something more refine then scratch to introduce my high school students to Raspberry Pie programming, I think I may have found it!

    Reply
  2. Thanks for the great video. I am presently using Adafruit.io as a broker and ESP8266 as devices.
    This arrangement works well for me. My concern is that I depend on Adafruit as a broker. My question is: does your system give me independence by running on Raspberry Pi? Can I connect to it from anywhere in the world? is it limited to “Home Automation” or can it be autonomous ?

    Jacob

    Reply
    • With the system that I should you install the MQTT broker (mosquitto broker) locally in your Raspberry Pi and you install a software called ngrok that allows you access your device securely from anywhere in the world.
      You could always change your MQTT broker from Adafruit to another service quite easily.

      Thanks for asking,
      Rui

      Reply
  3. I want to thank you on the great tutorials of raspberry-Pi and mosquito installation. I like your format of line by line instructions rather than lengthy Youtube videos.
    My question is: I prefer to use the mosquito as a broker rather than Adafruit.io and be independent, so now, do I need the both Node-RED and the Python server for MQTT?
    I have mosquito installed and tested, now I am not sure if I should just continue with Node-RED or what? I am not too interested in Home Automation very much but I want to access my mosquito from anywhere on the web. Will I be able to do it with Node-RED?

    Thank you very much,
    Jacob

    Reply
    • Hi Leonardo.
      Our MQTT tutorials use Raspberry Pi, Node-RED, and Mosquitto.
      We don’t have tutorials with OpenHAB or Home Assistant.
      Regards,
      Sara 🙂

      Reply
  4. loaded a freshly made SD card and after it finished installing, I looked under the programming flyout and there is no node-red. I had to load it with the link you provided.

    Reply
  5. If I understand correctly: NODE-RED is a GUI and it is easy to build communication flows. But I am looking for something different: I want to send messages from one system to many subscribed ESP32 boards with a program. I understand that a broker only runs on a Raspberry or even better a PC, but do I really need a broker? If I only want to broadcast messages from one system to several others (in my project 30..40 ESP32 boards) is there an easier solution, like using the MQTT client library on an ESP32 and broadcast messages to all other connected ESP32 in the local network?

    Reply
    • In order to control the flow of messages you need a broker. The esp XXX can receive and send messages, the broker distributes them. Mosquitto MQTT broker seems to be the favorite. Everything I’ve read says that the ESP family of controllers don’t have the power to “broker” the messages. Node Red is a great interface to connect messages to their destination in whatever fashion you desire. For example rather than simply a 1 or 0 (binary), or a number, or even text, you can create web pages that load on your cell phone or other mobile (or fixed) device. I currently have 5 esp8266 modules around monitoring temperature& humidity in various places, one monitoring heating oil level in a tank, and lock status in various locations. I plan more, the beauty of it all is the broker running on a pi or equivilant is the central reporting point and “flows” can be easily added to it. I’m currently using a Raspberry Pi 2b, and and have several other versions lying around I want to try. I’ve been told that the Rpi zero will work, and the zero 1 and 2 W have wifi built in; a great advantage if you want to go wireless. Hope that helps.

      Reply
      • Hi John, thank you, your answer was a great help to me. It looks like, that Node-RED is a good “dashboard” for looking at status and controlling IoT devices.
        But I am looking for something different: I want to write a program (for example C++) that broadcasts messages to all connected devices. If I understand correctly I can write a program that talks to a broker, and the broker sends messages to the clients. I think that using the Rest API from a broker like Mosqitto could work.
        This looks a little bit oversized to me, all I want is a simple way to broadcast messages from one client in a network to all others and to receive answers from them.

        Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.