You have made the right choice! Investing in the NodeRED server will pay off soon! If you, however, are still not sure why would you need a NodeRED server in your life, please check the part one of the series. In this article, you will learn all you need to know to get started with NodeRED. I’m going to use RaspberryPi Zero as my server hardware.
NodeRED Getting started
The NodeRED has been removed from the Raspian image so to install it run:
bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)
NodeRED is a programming environment that runs constantly in the background as a server. If the server management seems daunting for you, don’t panic. As soon as you see the interface you will understand how accessible the NodeRED can be. To enable the NodeRED (to start the server) you have you log in to the Raspberry Pi via SSH or access the desktop (via VNC). Later on, I’d strongly recommend using a computer to access the web interface.
Open your terminal (Putty) and connect to the Raspberry Pi. If you have any problems connecting, check this guide. To start the NodeRED server, simply type:
node-red-start
You will see that the NodeRED is starting up. The server will be available under local IP address and port 1880. If my Raspberry’s IP is 192.168.1.193 then to access the web interface type:
192.168.1.193:1880 or https://127.0.0.1:1880/ (IP/localhost:1880)
If you want to restart the NodeRED server simply type:
node-red-stop
It makes plenty of sense to start the NodeRED when the Raspberry Pi is booting and to enable this use: sudo systemctl enable nodered.service
. Your NodeRED will boot now automatically, and you can connect to it via the web interface. For now, keep the SSH terminal running. It will be handy for other things.
How NodeRED works?
When I started to hack things, I used to google for a snippet of code, copy and paste it into my script and tried to edit the bits to suit my needs. Slowly, I would learn how to write the code of my own from a scratch. NodeRED provides you with snippets of code (node) which you can edit (fill in config fields) and connect with each other to create a functional script.
NodeRED comes with a palette of built-in nodes which you can use straight away. It also comes with over 1000 published user-created nodes, and probably even more custom ones available through the GitHub only. You will quickly realise that there is a node for everything, as passionate and skilled members of the NodeRED community often create and share nodes, created to solve very specific problems.
While I’m on the subject of user- made nodes, I will show you how to install new nodes and then we I’m going to create one flow to show you how it works.
Installing new nodes
There are two ways in which you can add new nodes to your NodeRED. The easy way involves the Palette Manager and will let you browse and install published nodes. All you have to do is to open the Palette Manager and search for the nodes, then press the button to install it.
Unpublished nodes
The other way of adding new nodes to your home automation, which works with both: published and unpublished nodes, is via terminal and npm.
If the node isn’t published and available via GitHub only:
To install node simply log in via SSH to Raspberry Pi and download (I’m using Join as an example) the new node into the NodeRED directory (/home/pi/.node-red/xxxxxx).
git clone https://github.com/joaomgcd/node-red-contrib-join-joaoapps.git /home/pi/.node-red/join
Navigate to the node folder and install downloaded files:
cd ~/.node-red sudo npm link join
Then restart the server to apply the changes and delete the git file
node-red-stop node-red-start
You should see the node in after logging in to your NodeRED server.
If the node is published:
If a node is published everything is much easier, just run the lines below:
cd ~/.node-red sudo npm install <nodename>
and restart your NodeRED server.
node-red-stop node-red-start
Updating nodes
From time to time nodes get updated and it’s good to keep on top of the updates. These updates can be done via Palette manager or terminal.
To update the node please make sure your Raspbian is up to date first as you may get the issues otherwise. Run:
sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade
Next, check if npm need an update too using:
npm i -g npm
And then you are ready to look for the outdated nodes:
npm outdated
To update the nodes simply type:
npm update <nodename from the list>
Once all the updates have been done, restart the NodeRED. To keep the node updated, or should you run into any issues you can try to resolve it by rebuilding and updating the NodeRED installation: update-nodejs-and-nodered
.
Interface
Now that you know how to install new nodes, let’s talk a little bit about the interface. I want to talk about four aspects of the NodeRED:
- layout
- config nodes
- data export/import
- sub-flows
Layout
Everything in NodeRED is stored in a flow. You could think of them as folders for your projects. Unless told specifically, the data is not shared between flows. All information processed within one flow – can’t be accessed from another (unless you make the data available globally).
Flows can be also enabled/disabled on request, which is handy if you want to disable one project for a while. Double click on the tab to access the Flow Menu.
Nodes
Nodes are categorised into three main groups. You can easily tell to which group the node belongs just by looking at the connection points. Nodes can link to multiple nodes at once and can have several connecting dots, each responsible for a different action/behaviour.
Input flows will receive information and send it to the next node. The connecting dot is on the right side only.
Output nodes will send the information from the NodeRED to an outside source. While the nodes could have more than one dot, all the dots would be placed on the left side of the node.
Function nodes will take the information in, and pass it over to the next one after processing the data. These can have multiple dost on each side of the node. They can also serve as input or output nodes, depending on your flow.
Config Nodes
It’s common to come across a node that needs to be permanently configured. This configuration is stored as a config node. These nodes and configuration are available through a special menu – that panel will show you all the nodes that have configuration requirements. If you are not able to remove a node from your NodeRED server – it’s often the case that there is an active config node in use. You can delete these from this menu.
Data Export/Import
Menu Import Export
From time to time, it’s good to find a nice complicated project online and simply import it, instead of painstakingly recreating every single connection. Thankfully, you can export/import selected nodes or even entire flows.
It usually leads to storing the export/import data in the clipboard (as data is stored as a text) and saving it as a txt file (export) or pasting it in the text box (import).
Subflows
What happens if you have a nice clever setup that you want to use more often without adding multiple nodes to your already crowded flow? Subflows are the answer. A creation made from several flows can be saved and stored as one subflow. Whenever you want to use that combination of flows again, you can simply drop in that predefined preset. It’s great if your projects are starting to look complicated!
Side panels – Info & debug
At first, you will spend a lot of time reading the information available on these panels. Info will always teach you how to use specific nodes, while debug will provide you with feedback that is posted by the debug node placed in a crucial place. We will talk more about it later.
Deploy
Let’s say you have completed your flow and you are ready to test the things out. To save the changes, you have to hit the DEPLOY button. This will take your script and make it active. The button comes with 3 flavours. You can deploy nodes only, the current flow or everything. Bear in mind that deploying everything will restart all the nodes, and may lead to undesired results.
Access from the internet
To access your NodeRED from outside of your local network, you have to forward the port 1880 (default) in your router and point it to your NodeRED address. So if your NodeRED has the IP 192.168.1.120 – this is where your port forwarding should be pointing.
Then you can access the server using your WAN IP or (recommended) DNS server at:
WAN_IP:1880 or your.domain.com:1880
You can get a free DNS service with www.noip.com. If you do this, make sure you secure the access to your server with a password.
Secure the NodeRED
To enable user authentication on the Editor and Admin API, add the following to your settings.js
file:
adminAuth: {
type: "credentials",
users: [{
username: "admin",
password: "$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN.",
permissions: "*"
}]
}
The users
property is an array of user objects. This allows you to define multiple users, each of whom can have different permissions.
This example configuration defines a single user called admin
who has permission to do everything within the editor and has a password of password
. Note that the password is securely hashed using the bcrypt algorithm.
httpAdminAuth
could be used to enable HTTP Basic Authentication on the editor. This option is deprecated and should not be used.Generating the password hash
To generate a suitable password hash, you can use the node-red-admin
command-line tool:
node-red-admin hash-pw
The tool will prompt you for the password you wish to use and then print out the hash that can be copied into the settings file.
Conclusion
You know now, what’s what, and you are ready to start creating your own flow. That’s the next part of this series. So keep your eyes peeled! Consider subscribing to my channel, or social media to get the notifications.