The Glue of the Internet: What is Node-RED?

Node-RED

Have you ever wanted to prototype a RESTful API, integrate Internet services or IoT devices in a few minutes without having to learn how to use several APIs and libraries? Here you go.

Node-WHAT?

Node-RED is “a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways”. It uses a visual web interface, where blocks – also called nodes – can receive and send messages to other nodes. It makes the whole integration process easier by abstracting a lot of repetitive and boilerplate code needed to make these simple things.

This tool is built on top of node.js, and each node runs some JavaScript code. You can also use function nodes to write your own logic. Here’s an example with a simple Hello World:

 

Shows a flow which outputs "Hello World"
A simple Hello World

In this example, an inject node is used to send a message object manually to a function node, where we’ve set a “Hello World” phrase as the payload of that message. Then, it passes the message forward to a debug node, which prints it on the debug tab.

You can run Node-RED on your computer using Linux, MacOS and Windows. It is also supported on other platforms, such as Raspberry Pi and BeagleBone Boards, cloud services like IBM Bluemix or AWS, and even on Android. I have two instances running on Raspberry Pis, one at home and another at our company’s office. We also host one on a DigitalOcean droplet.

Nice! But what can I do with it?

One of the things that Node-RED excels is integrating Internet services. Let’s say you want a notification on your phone every morning with the weather of the day. You may use something like darksky.net to get the forecast, format a message, and send it to you using something like Pushbullet or Pushover. You might do it easily writing some code, but can you do it in 10 minutes?

Sending weather information to mobile device
Sending weather information to mobile device

Done. A simple timer node fires on sunrise (the actual time is defined by your latitude and longitude), sending a flag to a function node that gets the current time and date. This information is sent to a darksky node that uses darksky’s API to get the weather, passing it as a JSON to a template node that formats it using the pattern below:

Good morning! Today we have {{payload.detail}} With temperatures from {{payload.mintemp}}˚C to {{payload.maxtemp}}˚C.

The message then goes to a pushover node, which makes a push notification pop on your phone every morning.

IoT devices

Ok, let’s make things more interesting. Say you have some LiFX bulbs and you’d like to integrate them with Apple HomeKit, so you can control them with your iPhone’s Home app and Siri. You could use something like Homebridge and the LiFX’s API, but that would require a lot of time and code. And if you decide to add a new bulb, you’d have to do it all over again.

A flow showing Integration of LiFX bulbs and HomeKit
Integration of LiFX bulbs and HomeKit

That’s what we did on our office. We made two group of lights that are now controllable by any authorized iPhone. We used a homekit node, making the whole process a lot easier than adding devices by code using Homebrigde. Those function nodes on the center just adapt the incoming messages to the format that the next node expects.

But at home, I’ve some light bulbs and sensors that don’t have any APIs, communicating only through the MQTT protocol. I wanted to integrate these devices and a few others, like a Lockitron Bolt and my PS4, with Amazon Echo and iPhone/Siri. Also, having a web dashboard to see some charts would totally be awesome. Here’s what I’ve came up with:

This shows all my home integration flows
All my home integration flows

Using some simple nodes like alexa home skill, dashboard, firebase and nodes that let me run shell commands, I’ve manage to effortlessly integrate everything with everything much quicker than if I tried to do it actually coding. Here’s how the integrations look like (in Portuguese, sorry):

Generated Dashboard showing Living Room's temperature, humidity and other variables
Resulting Dashboard
Gif turning on a table light using Siri and turning it off using Alexa
Table light integrating with Siri and Alexa

Prototyping APIs

Besides integrating existing services, you are able to create your own. Node-RED can integrate with most databases – SQL or NoSQL – as well as reading and writing from files. The possibilities here are endless. 

Once upon a time, I needed a very simple API with an endpoint that would return the “Hello World” phrase in a random language, and also another endpoint to fetch all possibles values of the “Hello World”. I made a simple CRUD (actually, without the Create part) that reads a list of “greetings” from a text file and operates over it.

shows a full api flow
The full API

Here’s the working API in Postman:

Postman calling the API
The API calls

Great!

So, that’s it. Node-RED is great to glue Internet services and devices. It’s probably not ideal or scalable enough for a final product, but you can quickly prototype how your product will behave, avoiding blocking anyone who is depending on, let’s say, your API.

Also it is great to automate your home or office, integrating smart bulbs, smart outlets, plants, TVs, video-games and everything else. You can find a lot of useful flows and nodes in the Node-RED’s website. There’s also a Slack Team, where you can get help and learn new things.

Thanks and keep hacking!

About the author.

Marcio Granzotto Rodrigues
Marcio Granzotto Rodrigues

A developer who loves to code, learn and build stuff. Also a 3D print enthusiast and full-time nerd.