Node-RED, wiring the Raspberry Pi to the IoT

When you want to connect an IoT device to anything (or, for that matter, anything to anything), you should take a serious look at Node-RED, a free, open source visual process wiring system from IBM

raspberry pi 2 node red

One of the many things that Raspberry Pi-based systems are excellent for is for building Internet of Things platforms. The price is right ($35), the performance is great for the price, the input/output options are great, there’s an enormous ecosystem of add-on and compatible sensors and other hardware, and an equally enormous supporting community.

As a result of this vibrant market, a slew of operating system choices for the Raspberry Pi have appeared (see my Ultimate Guide to Raspberry Pi Operating Systems parts 1, 2, and 3) along with some really creative software development tools such as Node-RED, a free, open source, visual wiring tool built by IBM Emerging Technologies

nodered 01

Node-RED, a tool for wiring the anything to anything but particularly useful for wiring the IoT to devices such as the Raspberry Pi

Node-RED was first released for x86 platforms in 2013 and the Raspberry Pi version now comes pre-installed in the Raspbian “Jessie” release of Debian officially released last December by the Raspberry Pi Foundation (although other companies such as Adafruit had Node-RED working on the Raspberry Pi almost a year earlier). And not only does Node-RED run on Windows, Linux, and OS X, it also runs on the Beaglebone Black single board computer and there’s support for connecting Node-RED to Arduino boards.

Node-RED is built on top of Node.js. Designed for building scalable network applications, Node.js is an event-driven JavaScript runtime which, in turn, is based on Chrome's V8 JavaScript engine. 

Node is similar in design to, and influenced by, systems like Ruby's Event Machine or Python's Twisted. Node takes the event model a bit further, it presents an event loop as a runtime construct instead of as a library. In other systems there is always a blocking call to start the event-loop. … In Node there is no such start-the-event-loop call. Node simply enters the event loop after executing the input script. Node exits the event loop when there are no more callbacks to perform. This behavior is like browser JavaScript — the event loop is hidden from the user.

What’s remarkable about Node.js is how quickly it’s grown in popularity and power though not everyone might agree on its general usefulness (NSFW).

Sorry, gotta do it; here’s “Hello World” for Node.js

In the following "hello world" example, many connections can be handled concurrently. Upon each connection the callback is fired, but if there is no work to be done Node is sleeping.

screen shot 2016 05 28 at 2.37.41 pm

Yes, it's the Node.js version of "hello world"

Node.js now has well over 225,000 available packages and its own package manager called, boringly, “npm” which is included when Node.js is installed. I mention this because you’ll need some npm “fu” if you’re going to extend Node-RED using the npm repositories.

Node-RED provides a set of building blocks, or “nodes,” which you drag from the left hand palette onto the canvas. You then drag a connection from an output connector on the right side of a node (for those that have outputs; some, such as the debug node which just displays input data, do not), to the input connectors on the left side of a node (again, for those nodes that take inputs). You then edit the properties of each node as needed, click on Deploy, and your process, called a “flow,” starts running with messages being sent a received by nodes.

In the default Node-RED installation there’s large number of nodes types preinstalled including searching for tweets, analyzing text for sentiment, running commands, sending and receiving HTTP requests, queuing and dequeuing message, sending and receiving UDP and TCP requests, and processing CSV, HTML, JSON, and XML messages.

Need more functionality? Nodes are available to support services such as accessing MongoDB and MySQL, interacting with IRC, and using Mustache templates by adding them from npm repositories. This can be done locally from the command line using either npm or the node-red-admin tool (this can also be used remotely).

screen shot 2016 05 28 at 3.00.07 pm

The Node-RED Function node you can use JavaScript code to add whatever functionality you need to a flow

If there isn’t a node to perform whatever operations you require, and you can’t code JavaScript in a function node to do what you want, you can create your own node and, optionally, publish it via npm repositories. 

screen shot 2016 05 28 at 3.36.31 pm

The Node-RED Library contains nodes and flows you can add to your installation

Pre-built flows are also available from the Node-RED Library which also indexes available nodes. To use a flow, you copy its JSON from the library, then paste it into the Node-RED user interface. You can also save and retrieve flows into your local library.

Node-RED also provides admin, runtime, and storage APIs although you’ll find the documentation pretty scant at present.

If you’re not got a preinstalled Node-RED system then, in theory, installation should be simple. First, if needed, install Node.js:

Check the version and if you get this message:

user@xxx:~ $ node --version

*The program 'node' can be found in the following packages:

  • node
  • nodejs-legacy

Ask your administrator to install one of them*


You need to do this:

sudo ln -s /usr/bin/nodejs /usr/bin/node

And node should work fine (though no guarantees). Now you can install Node-RED using npm.

As well as running Node-RED on a Raspberry Pi 2B, I’ve explored Node-RED using Ubuntu 14.04 installed in a VirtualBox VM on OS X, directly installed on OS X, in a Docker container running on a QNAP NAS and in all installations it’s been solid. As a tool for building IoT applications Node-RED is definitely worthing evaluating. It’s robust, flexible, and definitely addictive (you’ll find yourself thinking “can I just do X with Node-RED?” and then another hour or two will vanish).

Comments? Thoughts? Suggestions? Wire some feedback to me via email or comment below then follow me on Twitter and Facebook.

If you think this review contains lots of cool RPi stuff, you might also like:

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Copyright © 2016 IDG Communications, Inc.