Developing with Node-RED

Developing with Node-RED

Node-RED is a powerful browser-based tool for building apps for the Internet of Things (IoT) whose visual programming environment makes it easy to develop apps as flows. Discover Node-RED and learn how you can develop IoT apps using flows.

The programming language landscape is broad, covering a variety of programming styles and paradigms. Imperative and object-oriented languages dominate the world, but there are alternatives for the development of production software as well as rapid prototyping of ideas.

Node-RED takes an alternative approach to the development of software. First, it’s a visual programming language. Instead of developing apps as sequences of textual statements, Node-RED focuses on programs as flows. These flows are made up of interconnected nodes, with each node performing a particular function (see Figure 1). Although Node-RED was designed for the Internet of Things (IoT), it is general purpose and can be used for many app types.

Figure 1. Sample flow within the Node-RED development environment

Node-RED is a relatively new environment, originating at IBM, then being released as open source in 2013. The tool’s focus was as a general-purpose IoT programming tool, but it has grown in use because of its simplicity and power for a wide range of apps. Let’s look at the ideas behind Node-RED and how you can exploit it for the development of an IoT app.

Develop a Simple Flow in Node-RED

Let’s return to Figure 1 to understand app development in Node-RED. On the left is the node pane, which lists the nodes available for your app. The center pane is where you’ll build your flow. Note that in this sample flow two nodes are defined; the first is an inject node that creates a message at a user-defined interval (I’ve configured this one to generate a timestamp message every 5 seconds, as shown in Figure 2). This node is connected to a debug node, which you can use to emit data to a log. The node is named msg.payload, which indicates what it will emit. Finally, on the right pane is the debug output. Each of the three segments is an individual output from the debug node, which you’ll notice are timestamps exactly 5 seconds apart.

Figure 2. Configuration of the inject node

This is a simple flow to illustrate the idea of development in Node-RED. An inject node generates a message that contains a timestamp, and a debug node receives the message and emits the payload to a debug log.

Nodes and Node Types

Node-RED includes an impressive set of nodes that will make you immediately productive. Figure 3 shows the input and output nodes, which permit the subscription and receipt of MQ Telemetry Transport (MQTT) topics and output of MQTT topics to a broker; development of web services through HTTP requests (along with the construction of HTTP responses); and lower-level TCP and User Datagram Protocol services that can create servers, receive input, and generate output.

Figure 3: Node-RED input and output nodes

Processing nodes are shown in Figure 4. With these nodes, you can write your own node functions (in JavaScript*), generate messages as a trigger based on time, and delay messages to rate limit flows. You can also alter the path of messages by using switch and a rich set of conditional expressions, change the path of a message (or delete a message) based on the message’s contents, and remap the range of a numeric message based on a user-defined action. Finally, you interpret a variety of input data formats and convert them.

Figure 4. Node-RED function nodes

The final set of standard nodes is the advanced nodes (Figure 5). These nodes cover a variety of use cases, including receiving and sending emails, and performing sentiment analysis on tweets. You can send or receive messages from storage and watch for changes within the file system. Finally, you can monitor and RSS/Atom feed for new entries and even spawn or exec a command to the operating system.

Figure 5. Node-RED advanced nodes

You can also download new nodes if you don’t see what you need (such as facial recognition and interfaces to a variety of physical devices). Now, let’s look at the development of a more complex flow.

Build a More Complex Flow in Node-RED

The real power of Node-RED comes with its application in the IoT. In this example, you read sensors from embedded devices, then distribute them for use. In the following flow, shown in Figure 6, the first thing you’ll notice is that instead of a single flow, you have two for the two sensors you want to monitor. Data from the first sensor is collected periodically, then filtered to report only when it exceeds a range (as an alarm) using a function. When the range is exceeded, you route the resulting message to both MQTT as an alarm topic and to a log file.

Data from the second sensor is also collected periodically, but it’s always interesting, so you always route it to MQTT as another topic and to a log file. Node-RED provides this parallelism for you as part of its programming model.

Figure 6. A more complex polling flow

Node-RED permits the construction of many flows made up of many nodes, so you can use it to perform local data collection and manipulation in an embedded device or within a server to accept data and perform even more complex processing.

Use Models

Node-RED is dependent on Node.js*, an open source runtime environment for developing server-side web apps. It supports an event-driven model, which is ideal for the Node-RED asynchronous flow-based environment.

You can build flows with Node-RED that run on embedded edge devices, such as the Intel® Edison compute module and Arduino*-certified Intel® Galileo development board. These devices sit at the edge of an IoT ecosystem and interact directly with legacy and new hardware interfaces, such as temperature sensors and drive motors.

You can also run Node-RED on a gateway device such as the Intel® IoT Gateway. In this model, you can use Node-RED to communicate with the many local edge compute devices to collect and process their data, then distribute this data to an MQTT broker to further disseminate it for use.
In either of these use models, Node-RED executes on the device: You simply attach a browser over a network to the device to build and test flows. In this way, the development and test environment as well as the runtime all exist on the devices, requiring only a browser to connect.

Summary

Node-RED is a powerful programming tool for wiring together, in a visual way, flows that collect, process, and disseminate data. Node-RED provides a rich set of nodes to introduce you to the programming model but also a massive collection of nodes and flows that you can download and apply to your application. Best of all, you can take your favorite Intel development board or IoT gateway, install Node-RED, and be executing flows with minimal time and effort. This makes Node-RED an integral part of any IoT ecosystem.

For more such intel resources and tools from Intel, please visit the Intel® Developer Zone

Source: https://software.intel.com/en-us/articles/developing-with-node-red

Digit.in
Logo
Digit.in
Logo