Do you want to spend more time building the perfect home automation system and less time writing boilerplate code? Perhaps you have little (or no) coding experience but still dream of creating powerful Internet of things (IoT) projects.
If you can’t, or don’t, want to spend hours writing code, then there’s plenty of software that can do the hard work for you! Here, we focus on Node-RED, a flow-based visual editor that can provide the code you need to power your IoT projects. Using Node-RED, you can connect smart devices, APIs, online services and more, using simple drag and drop.
In this tutorial, you’ll learn how to set up Node-RED on your Raspberry Pi and access its visual editor via your web browser. Once you have access to this editor, you’ll see exactly how to use Node-RED to build powerful home automation workflows by creating a simple debug flow that you can deploy and test on your Raspberry Pi.
How to use Node-RED in smart home projects
Node-RED has a library of ready-made nodes covering a range of functionality. You can use these nodes to build complete home automation workflows. Simply grab the nodes you want to use, drop them onto the Node-RED canvas, and wire your nodes together to form a completed workflow.
Some of these nodes are straightforward – for example, debugging nodes, whereas others are more complex. Some of the more advanced nodes even allow you to interact with the GPIO (general-purpose input/output) pins that run along the top edge of your Raspberry Pi.
What you’ll need
To complete this tutorial, you’ll need:
- Raspberry Pi that’s running Raspbian. If you don’t already have it, grab the latest version and flash it to an SD card using Etcher.
- Power cable that’s compatible with your Raspberry Pi
- External keyboard and a way to attach it to your Raspberry Pi
- HDMI or micro HDMI cable, depending on your model of Raspberry Pi
- External monitor
- Ethernet or connection to Wi-Fi
Raspbian: check for the latest version
To start, make sure you’re running the latest version of Raspbian:
- Attach all your peripherals to the Raspberry Pi.
- Connect your Raspberry Pi to a power supply; it should boot automatically.
- If this is your first time booting Raspbian, you’ll need to complete the standard setup dialog and connect to your Wi-Fi network if you’re not using an Ethernet cable.
Once your Raspberry Pi has booted, click the “Terminal” icon in the toolbar and run the following commands in the Terminal window:
If Raspbian installs any updates, then you’ll need to reboot your Raspberry Pi using the following command:
Once your Raspberry Pi has rebooted, you’re ready to move onto the next stage.
Installing Node-RED, Node.js and npm
Next, you need to install the
build-essential package, which is used to compile and install various Node-RED add-ons:
To install the Node-RED package, the current version of Node.js and npm are being installed as well in the following command. Alongside build-essential, npm is used to install additional Node-RED modules:
This download can take a while, so you may want to go grab yourself a coffee!
You can now start Node-RED. It’s possible to start Node-RED as a service (using the
sudo systemctl start nodered command), but if you start Node-RED directly, it allows you to access it via the Terminal.
To launch Node-RED without starting it as a service, run the following command:
You can now check the status of Node-RED:
The last few lines of the Terminal output should confirm that Node-RED is running successfully.
Node-RED runs silently in the background, so you may want it to start automatically, particularly if you find yourself using Node-RED on a regular basis. To start Node-RED at boot, run the following command:
The Terminal should confirm that it’s created a symbolic link shortcut for Node-RED.
If you change your mind and want to prevent Node-RED from starting automatically, then run the following Terminal command:
No code required: accessing Node-RED’s visual editor
Now that Node-RED is up and running on your Raspberry Pi, you can use it to build home automation workflows using simple drag and drop.
To access the Node-RED editor, you need your Raspberry Pi’s IP address. You can retrieve this information by running the following Terminal command:
Launch the default browser on your Raspberry Pi and enter the following URL, making sure to replace “IP-address” with your Raspberry Pi’s unique IP address: http://[IP-ADDRESS]:1880
For example, if your IP address was “192.168.1.56,” then you would use the following URL: http://192.168.1.56:1800
The Node-RED editor should now appear in your web browser.
Exploring Node-RED’s IoT editor
The Node-RED editor is divided into the following sections:
- The toolbar. This contains the “Deploy” button, main menu, and the user menu, if user authentication is enabled.
- The palette. This area contains all the nodes that are installed and available for you to use. If you select a node, then the “Info” tab will display more information about that specific node.
- Flow. This is the canvas where you’ll drop all of your nodes and create connections between them. If you’re working with multiple flows, then they’ll be displayed as a row of tabs along the top of the workspace. You can zoom in and out using the controls in the bottom-right of the workspace area.
The Node-RED editor also features a sidebar, which displays different panels depending on the content that’s currently selected:
- Information. This displays information about the selected node, including a summary of its properties. If nothing is selected, then the “Information” panel will display a description of the current flow.
- Debug. This displays the messages that are passed to Debug nodes within your flow, along with messages from the runtime, where available. By default, the Debug panel displays all the messages that are passed to it, although you can filter this output, if required.
- Config. This panel displays a list of all the config nodes, along with their type, label and how many workflows use this particular node.
- Context data. This displays the contents of the context data store, which allows you to share information between nodes without using the messages that pass through a workflow. For more information on context, check out the official Node-RED docs.
Automating with Node-RED: creating your first workflow
Now that you have a high-level overview of the Node-RED editor, you can create a simple debug flow to deploy and test your Raspberry Pi.
To start, drag an “Inject” node from the palette and drop it onto the workspace area.
Next, drag a “Debug” node and drop it onto the workspace.
These two nodes need to be wired together. Hover over one of the node’s handles until it turns orange and then drag so that a line appears. Drag this line over to the second node and release it: these two nodes should now be connected.
You can edit any node by double-clicking it. To verify that your debug workflow is running correctly, create a text “payload” that the workflow will deliver to Node-RED’s “Debug” panel.
Double-click the “Inject” node, which launches a new “Edit inject node” panel. You can make various changes in this panel. This example here opens the “Payload” drop-down and selects “String.”
Enter the text “Hello, World!” and then click “Done” to save your changes.
With your workflow completed, you can enable it by giving the big red “Deploy” button a click.
Run your debug workflow
To put your flow to the test, open the “Debug” panel (by clicking the little bug icon in the toolbar).
Hover over the “Hello, world” node and give its accompanying checkbox a click.
If the flow runs successfully, then your “Hello, world” payload will appear in the debug panel.
While this is a simple workflow, it demonstrates how you can create nodes and connect them to form a functioning workflow. You can use the same process to create all kinds of complex and advanced workflows: simply keep grabbing nodes from the palette, adding them to the workspace, and connecting them until you get the desired result.
Learn more about other Raspberry Pi projects here.