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.
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:
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.
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:
Configure Node-RED Settings
After installing, it is recommended to configure initial options and settings. Run the following command:
node-red admin init
- 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.
Run the following command to start Node-RED:
You should get a similar message on the Terminal:
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.
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.
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:
To get your Raspberry Pi IP address, you can run the following command:
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.
Now, you have access to Node-RED. You can start building your flows.
Let’s take a look at the Node-RED visual interface.
Node-RED Interface Main sections
The following picture shows the Node-RED main sections labeled.
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.
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.
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.
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.
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.
We won’t edit the debug node, but you can double-click on it to check its properties.
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.
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.
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.
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.
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.
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.
The following window will open. Select the install tab and search for the nodes you want to install, for example, node-red-node-email.
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.
After installing, the dashboard nodes will show up on the palette.
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:
For example, in my case:
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:
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:
- Install InfluxDB 2 on Raspberry Pi
- Install Mosquitto MQTT Broker on Raspberry Pi
- What is MQTT and How It Works
Node-RED is very useful for IoT projects that use MQTT, so you may want to check all our MQTT projects:
Thanks for reading.