What is Node-RED?
Node-RED is a tool originally created by IBM for flow-based programming, and has built-in support for the Raspberry Pi and it's GPIO capabilities. It runs on top of Node.js, and can be accessed with just a web browser to create a visual programming environment. This allows you to see an overview of what your program is doing, rather than having to understand all the lines of code needed to run it, which means you can get on with creating your project faster! You can then use it to connect to a plethora of devices and services, from IoT and GPIO pins to social media and creating your own dashboards.
How to install the server on Raspbian
Doing this is really simple. If you're using Raspbian with Desktop version 2018-04-18 or before, you don't need to do anything as it's already installed. In 2018-06-27 though, the Raspberry Pi Foundation removed Node-RED and a few other programs by default to reduce the image size. In this case, it's pretty simple to get it back: Click the Menu button (Raspberry symbol) and go to Preferences > Recommended Software. In there, you will find Node-RED (under Programming, or just search for "node"). Just tick the box, click Ok, and wait for it to install:
For any other Raspberry Pi distro you want to install Node-RED on, you just need to run this command in a terminal:
bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)
Once the server is installed, you can get it to run by going to Menu > Programming > Node-RED, or by the command node-red-start in a terminal. When it has finished starting up, you will see Server now running at http://127.0.0.1:1880/ near the bottom:
You can then just minimise the window and start working with it. If you'd like to run the server in the background every time the Pi turns on, you can open a Terminal window and run sudo systemctl enable nodered.service. To stop it running on boot, you can run sudo systemctl disable nodered.service. Reboot the Pi to commit those service changes.
How to connect to your server
If you're going to be developing on the Raspberry Pi directly like I am, you can open up the Web Browser and navigate to either http://127.0.0.1:1880/ or http://localhost:1880/. If you want to develop on a different computer on the same network, just navigate to http://raspberrypi:1880 (assuming "raspberrypi" is the hostname of your Pi; some connections might need "raspberrypi.local"). You should be greeted with the web interface, which hides the JSON layout of nodes underneath (you'll be able to access this later if you really want to).
Making sure it's working
Let's make a quick breadboard circuit to make sure that Node-RED can connect to our Pi's GPIO pins. It will look like this:
We will then be creating a simple flow to turn the LED on and off.
- Click and drag an "inject" node from the "input" section on the left. It should appear as a timestamp node.
- Double click it. Change the Payload type to "number" and give it a value of "1". Change the Name to "LED On".
- Click Done. Create another inject node. Double click it. Change the Payload type to "number" and give it a value of "0". Change the Name to "LED Off".
- Click Done. Scroll down the sections on the left, and drag a "rpi gpio" output node (dot on the left, raspberry logo on the right) to the flow.
- Double click it. Change the Pin to "12 - GPIO18". Tick the "Initialise pin state?" box, and make sure it's set to "initial level of pin - low (0)". Change the Name to Blue LED.
- Click Done.
- Click and drag from the dots on the right of the inject nodes to the ones on the left of the rpi gpio node to join them.
When you're done, click deploy in the top right corner. It should tell you that it was successful.
The big buttons on the left of the inject nodes should become clickable. Clicking on them should then send the values you specified to the Blue LED node, turning the LED on and off.