IoT Made Easy with Node-RED and InfluxDB

Navigate to:

In this article you will learn about Node-RED, a popular tool for rapidly gluing together different types of hardware and software. You’ll learn about some of the core concepts of Node-RED and then learn how to make some workflows like storing data from a sensor using an MQTT broker and InfluxDB.

What is Node-RED?

Node-RED is an open-source, low-code, visual programming tool based on the concept of flow-based development. The idea behind it is to make it very easy to connect APIs, hardware devices, and anything else accessible over some type of network connection.

Node-RED provides a number of pre-built “nodes” which allow non-developers to create custom integrations and automations using a visual interface, while also allowing developers to write their own custom nodes using JavaScript or external service written in another language when needed. In many ways Node-RED can be seen as one of the earliest examples of the low-code movement that has been rising in popularity.

Node-RED was initially developed by IBM and then contributed to the OpenJS foundation. Arguably the biggest strength of Node-RED is the ecosystem and community with over 14,000 stars on GitHub and a community library with over 3000 pre-built nodes and 2000 customizable flows that cover many common use cases.

In short, the benefits of using a tool like Node-RED are:

  • No reinventing the wheel - tons of pre-built functionality
  • Huge community with solutions to many common problems available
  • Easy to extend with custom functionality for your use case

Node-RED core concepts

In this section I will cover a few of the main components of Node-RED that will help you hit the ground running.

Nodes

As you might expect based on the name,  nodes are an important part of Node-Red. They are the fundamental building blocks for everything you do when working with Node-Red. Nodes are triggered by either receiving a message object from a previous node or an external event like an HTTP request. The node processes the message or event and then passes it on to the next node.

Here are a few of the most common and useful nodes provided by Node-RED:

  • Inject - The inject node allows you to manually start a flow by injecting a message payload. The payload can be a string, number, or object with multiple properties. This node can also be set to run at a set time or regular interval.
  • Change - The change node is a nice alternative to writing a custom function to do basic transformations or modifications like adding or removing properties on the message object.
  • Debug - The debug node can be used to help develop flows and find where any potential bugs are occurring. By default it will send the message payload to the debug sidebar but can also be customized to send different information and also print to the terminal where you started Node-RED.
  • Switch - The switch node allows messages to be routed to different branches of a flow based on defined rules. For example, if you have a sensor detecting temperature you might want to create an alert based on a certain threshold. If the temperature isn't over that threshold you can end the flow.
  • Function - The function node allows you to write custom JavaScript code for use cases where the pre-built nodes provided by Node-RED don't support your use case

Beyond these nodes, there are a number of other nodes for working with messages, storing data, and handling different types of network requests.

Flows

Flows can be thought of as containers for organizing a sequence of nodes. They are represented in the Node-RED editor as an independent tab. There are a number of different strategies and best practices you can use to determine when you should divide a flow into multiple different flows or keep it all together.

Flows

Messages

Messages are JavaScript objects that are passed along from the beginning to the end of a flow through nodes. Properties on the object can be added or modified by a node. Convention is to store the most relevant information in a property called payload on the message object, which itself is generally called msg.

Context

Context within Node-RED is essentially the ability to define global variables that can be accessed by nodes directly rather than from within the message being passed to the node. Context can be scoped at 3 different levels. The first is node level, where only the node that set the value can access it. The second is flow level where the value is visible to all nodes within that flow. The final context level is truly global, any node within the Node-RED instance can access the value.

Node-RED Flow Tutorial

Now let’s jump into the actual tutorial and make a few flows using Node-Red. If you want to follow along you will need a running instance of Node-RED. You have multiple options for how to install Node-RED, if you already have NPM installed that is probably the easiest option. You can also use Docker or install from source using Git. You can see all available options in the Node-Red documentation.

Once Node-RED is installed you can open your web browser and visit localhost at port 1880 and see the Node-Red editor. We’ll start out making a basic “hello world” flow to confirm everything is working properly.

First, drag an inject node from the left hand sidebar and put it onto the editor. Click on the node and change the payload to a string with value and whatever input you want, I’ll follow tradition and use “hello world” for my value.

Now grab a debug node and place it on the editor, by default it will output the message payload passed by the previous node. Click on the debug node and check the box for “system console” so our message will also be printed to the terminal as well as the debug sidebar. Your editor should look something like this:

Node-Red-editor

Now you can click deploy in the top right corner of the Node-Red editor. Click the box on the left side of your inject node to start your flow. If everything worked properly you should see values appearing in both your terminal and the Node-Red debug sidebar:

Terminal

Terminal

Node-Red-debug-sidebar

Node-Red debug sidebar

Node-RED MQTT flow

Now we’ll move onto something a little more complicated. For this flow we will connect Node-RED to an MQTT broker, transform the data using a Node-Red function node, and then store that data using InfluxDB for long term storage and analysis.

The first thing you need to do is install the InfluxDB Node-Red package. To do this click on the hamburger menu icon on the top right of the editor next to the deploy button. Then click on ‘manage palette’ and then click to the Install tab. Type InfluxDB into the search box and select the package called node-red-contrib-influxdb.

User-Settings-node-red-contrib-influxdb

With that package installed if you scroll to the bottom of your node palette on the left hand of your screen you should see three new nodes added for working with InfluxDB.

You’ll now want to create an InfluxDB instance. You can create a free InfluxDB Cloud account or install InfluxDB locally. Create a bucket and then grab an API token with access to that bucket. You can find instructions on how to do this and much more in the InfluxDB Documentation.

For this flow instead of using an inject node to start things, we will use the MQTT In node and connect to a free public MQTT broker provided by HiveMQ. If you already have an MQTT broker installed locally you can also use that, the configuration is the same. Click on the MQTT node after dragging it onto the editor and add a new broker by providing the server URL and port number. For HiveMQ the URL is broker.hivemq.com and the port is 1883. Leave the version as MQTT V3.1.1.

Edit-MQTT-broker-node

Set the Action for the node to subscribe to a single topic and put in the topic you want to listen to, it can be whatever you want in this case. I’ll be setting the topic as nodered for simplicity. Once you deploy this flow, Node-RED will connect to the broker and listen to any messages under that topic name.

We will be passing a temperature number through MQTT to simulate some sort of IoT sensor recording air temperature. By default this will be passed as a string, so we need to create a function to convert it into an integer before storing it with InfluxDB. Add a function node to the page and put the following code into the node:

msg.payload = Number(msg.payload)
return msg;

Now add the InfluxDB Out node and connect it to the function. Create an InfluxDB server instance by adding in your URL and API token. Then add in your organization ID and the name of the bucket where you want to store your data.

To test everything out you can use any MQTT client, HiveMQ also provides a websocket client that is easy to use here. Set the topic to match the topic in your MQTT node and then publish a message with a number as the value, something like this:

test-publish

If everything worked properly you should start seeing data in your InfluxDB instance every time you submit an MQTT message to the broker:

Date Explorer - graph

The flow itself should look similar to this, note the MQTT node provides a notification box letting you know if you are able to connect to the broker:

flow with notification box

Node-RED project ideas

So now you have a basic understanding of how to work with Node-RED, but what are some actual real-world use cases? In this section I’ll give you a few potential areas you can explore to build out your own custom projects with Node-Red.

Data visualization and analysis

Node-Red works best as the glue between different components, making it easy to move data around. While Node-RED does provide some basic storage and visualization capabilities it makes sense to use specialized tools for these things. As you saw with the MQTT example InfluxDB can be used for storing and visualizing data. For analysis the Flux scripting  language provides tons of functionality for analyzing time series data. The Node-RED InfluxDB package also gives you a node to query data from InfluxDB into Node-Red, so you have access to other tools in the ecosystem if you prefer those.

Smart home

Home Assistant is a popular home automation framework that provides integrations with numerous smart devices. But many users find it difficult to build out more customized workflows using the tools provided by Home Assistant. Node-RED is a common choice for these situations and there are a ton of potential project options that will help you learn more about Node-RED and also be useful from a practical perspective.

Node-RED as an event processing framework

While a lot of the focus in this article has been IoT related, I think that looking at Node-RED as a tool to process any kind of event is the best way to see how much you can really do with it. Node-RED gives you the ability to quickly connect and listen to HTTP, websockets, TCP, MQTT, and numerous other network protocols.

As a result it can be used to move data to and from almost any type of application and allows you access to the entire NodeJS ecosystem via NPM modules. You also always have the option to reach out to other services via API or running local commands via the Exec node. With Node-RED your only limitation is really your own creativity for how to combine the tools it provides.