Lesson 1 – A brief introduction to Node-RED – Node RED Programming Guide (2023)

Node-RED is a powerful tool for building Internet of Things (IoT) applications with a focus on simplifying "connecting" blocks of code to accomplish tasks. It uses a visual programming approach that allowsdevelopers to connect predefined blocks of code, known as 'nodes', together to perform a task. The connected nodes, usually a combination of input nodes, processing nodes and output nodes, when connected together form a “flow”.

Originally developed as an open source project at IBM in late 2013 to address the need to quickly connect hardware and devices to web services and other software – as a kind of glue for the IoT – it quickly evolved to be IoT programming of general purpose tool. Importantly, Node-RED has rapidly developed a significant and growing user base and an active community of developers who are contributing new nodes that allow programmers to reuse Node-RED code for a wide variety of tasks.

Although Node-RED was originally designed to work with the Internet of Things, i.e. devices that interact with and control the real world, as it has evolved it has become useful for a variety of applications.

This talk will present some examples of Node-RED, both to give you a sense of its capabilities and to introduce its basic concepts. We'll also discuss the history of Node-RED and its origins to give you an idea of ​​what Node-RED is for and what types of tasks are best left to a more traditional programming language and environments.

By the end of this talk, you should have a high-level understanding of Node-RED, its programming model and some simple pre-built nodes, as well as an insight into the origins and best use of Node-RED.

Let's start by looking at some simple examples of the kinds of things you can do with Node-RED. Our goal in this talk is not to get you programming with Node-RED, but to give you a sense of its capabilities through some high-level examples. If you really want to dive right in, you can skip ahead to Lesson 2, where you'll be taken through the steps to create and run your first flow.

This first example flow will receive a message from a Twitter feed containing a hashtag and turn an LED on and off when a new tweet arrives. For this example, let's assume that NODE-RED is running on a Raspberry PI – a common situation for many Node-RED users.

Example 1.1 Using Twitter to control a Raspberry PI

The Node-RED flow to check Twitter and see if a #led hashtag is detected turns on an LED connected to a Pi is shown in Fig. 1.1. As you can see, it's pretty simple; It consists of 3 connected nodes - a twitternode on the left, a triggernode in the middle and a Raspberry Pi node (gpionode) on the right. (gpio stands for General Purpose Input/Output and is the name given to the generic Raspberry PI node that handles IO). Nodes, represented by rounded boxes, usually have an icon to represent their type, for example the Twitter bird icon on the first node and a text name that can be defined by the programmer.

Each of these three nodes is built into the Node-RED visual programming tool and can be dragged from a node palette into the main workspace. They are then 'connected' by joining their output tabs to the input tab of the next node in the stream (the little gray blobs to the right or left of the node boxes). The next class will look at the user interface in detail, but for now let's focus on the high-level features.

Lesson 1 – A brief introduction to Node-RED – Node RED Programming Guide (1)

Figure 1.1 A simple 3-node flow that uses a Twitter tag to drive an LED on a Raspberry PI.

Twitternode is a node built into Node-RED and hides all the complexity of using the Twitter API. It can be configured with a user's account credentials and a variety of search strings; in our case, just look for the hashtag ‘#led’.

When Twitternode sees the tag in a tweet for the user or in the public tweet feed, it creates a new message with the details of the tweet, which is forwarded to the next node in the stream. In our example, as the Twitternode's output is connected to the triggernode's input, the triggernode receives a message with the details of the tweet.

triggernode is another node built into Node-RED and its default behavior is to wait for any message on its input. Upon receiving a message, it 'triggers' and sends a message in its output with the value "1" as the body of the message. It then waits 1 second and sends a second message with the value “0” in the message body.

As the triggernode is connected to the gpionode, which controls the input/output or IO pins on the Raspberry PI, it receives these two messages, 1 second apart, on its input.

Again, gpionode is a built-in node in Node-RED that hides the configuration details of the IO pins in the PI. When it receives a message with a “1” value in the body, it takes the IO pin high, and when it receives a message with a “0” body, it takes the pin down. In this example, gpionode is configured to control pin number 12, so the node shows this in the node icon.

If you have your PI connected with an LED connected to pin 12 the gpionode will go high for 1 second and then low will cause the LED to blink for 1 second.

If you think about what's going on here, it's actually an incredibly powerful example of Node-RED and the Internet of Things (IoT). It's connecting the 'Twitterverse' to a PI and an LED, but it requires no programming - all tasks are performed by pre-built nodes in Node-RED that just need configuration. This really demonstrates the power of Node-RED when running on a simple device like a Raspberry PI.

So just to recap. Node-RED is a flow-based programming tool with a visual editor that lets you connect nodes to create flows. In this case, 3 nodes create our first flow. Real world events, eg. a tweet with the hashtag #led, are converted into messages, which 'flow' along the wires between the nodes. Nodes process messages on their inputs and, as a result, send messages on their outputs to the next node in the flow. In this simple example, we look for a tweet with the hashtag #led and use it to trigger a message to a Raspberry PI node that causes IO pin 12 to go high for 1 second, which in turn causes an LED blink for 1 second.

This example – and the rest of this talk – are mainly to show you the kinds of things we can do with Node-RED. If you want to play around with the example, check out Lesson 2 to see how to set up an account with FRED (the cloud-based Node-RED service we use) and how to get started. You can find the node-red description of this flow at:

https://raw.githubusercontent.com/SenseTecnic/nrguideflows/master/lesson1/1-1_twitter.json

Example 1.2 Using Node-RED to alert you when you are behind on your exercise schedule

Our second example shows a different use of Node-RED. Rather than controlling a device like a Raspberry PI, this flow combines information about the weather and fitbit usage to send you an email alert when the weather is good and you're behind on your exercise schedule. The flow is a little more complicated, using 6 nodes (see Fig. 1.2), but again, using built-in nodes, you can accomplish complex tasks with little or no programming.

Lesson 1 – A brief introduction to Node-RED – Node RED Programming Guide (2)

Figure 1.2 A simple flow to alert you when you are not exercising and the weather is fine

As before, nodes are dragged from the node palette in the Node-RED UI and connected in the flow canvas. Starting at the leftmost node, let's start with an openweather node that you can configure to check the weather of any major city at defined intervals. When checking, it packages the results into a message that is forwarded to the next node, in this case to a switchnode. switchnode performs a simple test, checking the input message for a value and sending a message with the value “1” if the test is true. In our case, it was set to test the temperature value (tempc) to see if it's 15 degrees Celsius or higher.

When switchnode tests true, or 'bind', it sends a message to fitbitnode, which is the next node in the stream. fitbitnode is another powerful built-in node that you can configure to get stats about your fitbit device on the fitbit.com portal. The fitbit node packages your device's stats into a message and forwards it to the next node in the stream.

The next node is another switchnode. This has been set up to check that the number of steps you've taken today is less than 1000. It does this by testing to see if the fitbit data field, summary.steps, is less than 1000. Remember, it just received these stats in a message from fitbitnode, which in turn got them by making a call to the fitbit API at fitbit.com.

If you walked less than 1000 steps today, the switchnode generates a message that is passed down the stream to a functionnode. The functionnode is an all-purpose generic node that allows you to write your own programming logic in Javascript. This node will be discussed in more detail in the next lecture. For now, you can assume that this node just creates a new message with an email subject line and an emailable body text.

Once this message is created inside the functionnode it is sent through the stream to the final node which is an emailnode. This node will take the body of any incoming messages and send it to the configured email account when the node starts up. In that case, you would configure it to send a message to itself.

So again, a very simple flow that uses the power of Node-RED's built-in nodes to check the local weather. And if it's reasonably warm and you haven't walked very far today, it will send you an email reminder.

While the example looks pretty simple, just checking the weather and your fitbit activity, you can use similar flows to check the status of home devices, computers in your server farm, devices in a factory, etc. output nodes for sending email, tweets, making API calls to back-office software, controlling real-world devices - really, the options are endless. Node-RED is an extremely powerful tool for connecting inputs and outputs and offers many nodes, either built in or developed by the Node-RED community, to perform an amazing variety of tasks.

You can find the node-red description of this flow at:

https://raw.githubusercontent.com/SenseTecnic/nrguideflows/master/lesson1/1-2_weatheralert.json

Example 1.3 Building a simple web service using Node-RED's built-in HTTP nodes

Our final example in this high-level introduction is another class of flows. It shows how to create a simple web service that responds to a browser sending an HTTP request querying some real-world data and then delivering a web page with a graphical representation of that data back to the browser - essentially Node- RED is being used to provide a simple web server and service, all with just a few nodes.

Continuing with the fitbitnode featured in the previous example, this example creates a simple web service that allows you to query your fitbit stats for the day and, using a nice donut chart, display how many calories you've 'burned' through exercise.

The flow is shown in Fig. 1.3 and consists of 4 nodes. The first and last nodes are the httpinput and output nodes that work together to listen for HTTP requests and send HTTP responses. This flow listens for an HTTP request from any source, but let's assume a regular browser here. When it arrives, it looks up its fitbit stats at fitbit.com, uses a templatenode to create an HTTP page, and passes it to the httpoutput node, which sends the webpage back to the browser.

Lesson 1 – A brief introduction to Node-RED – Node RED Programming Guide (3)

Figure 1.3 A simple web server example for graphing fitbit data

Again, this is just to give you a sense of the power and flexibility of Node-RED without going into all the details of how this flow works. You will learn more about this in later lectures. At a high level, the httpinput node has been configured to listen for HTTP requests on a URL, which is a combination of the hosting service used in these talks plus your login name {username} and the string /fitbit. This will be explained in more detail in lecture two.

When an HTTP request arrives, the httpinput node creates a message to trigger fitbitnode, which is the next node in the stream. fitbitnode gets the current stats for the user and then passes those stats, as a message, to a templatenode. The html templatenode is another pre-built node in Node-RED, which, like the functionnode, allows you to create arbitrary code. However, instead of using JavaScript like functionnode, templatenode works with text like HTML.

The HTML code in the templatenode is shown in Listing 1.1, so you can see what's going on. However, there is no need to go over it in detail if you don't want to. Basically, templatenode takes the calorie consumption data from fitbit and, using a third-party graph library, creates a simple HTML file that will instruct the receiving browser to use the third-party graph library to display a donut graph of calorie consumption. calories.

Listing 1.1 A simple HTML template for displaying a calorie usage donut chart

  1. An Example of a RED Node
  2. Morris.Donut({
  3. element: 'example-graphic',
  4. dice: [
  5. {label: “Calories from activity”, value: {{payload.summary.activityCalories}} },
  6. {label: “Calories BMR”, value: {{payload.summary.caloriesBMR}} },
  7. {tag: “Calories Out”, value: {{payload.summary.caloriesOut}} }
  8. ]
  9. });

After templatenode generates the HTML file, it passes it as a message to the final node, which is an httpresponse node. This node packages the HTML as an HTTP response that it sends back to the browser.

The user will see a simple donut graph of calorie usage for the day (see Fig. 1.4) – all built and served by a simple Node-RED flow!

Lesson 1 – A brief introduction to Node-RED – Node RED Programming Guide (4)

Figure 1.4 A donut chart, served by Node-RED showing fitbit calorie count

You can find the node-red description of this flow at:

https://raw.githubusercontent.com/SenseTecnic/nrguideflows/master/lesson1/1-3_webservice.json

As the examples in the previous section showed, Node-RED is a powerful tool for building IoT applications and services. Its genesis was triggered precisely by this need to quickly prototype IoT applications and Node-RED was created as an open source project by the IBM Emerging Technologies group, in particular by two researchers, Nick O’Leary and Dave Conway-Jones. They initially created Node-RED as a tool for themselves while working on IoT projects and "looking for a way to simplify the process of connecting systems and sensors when creating proof-of-concept technologies for customers."

An early version of Node-RED was released as an open source project in late 2013 and built a small but active group of users and developers during 2014. At the time of this writing, Node-RED is still an emerging technology , but it has already seen significant adoption by manufacturers, experimentalists, and a number of companies large and small experimenting with using it for their needs.

Today there is a vibrant community of users and developers, with a core group working on the Node-RED code itself and most developers contributing nodes or streams to the streams library. You can check out the mailing list and github repositories using the links provided in Appendix A or just google them.

As Node-RED is still a rapidly evolving technology, please be aware that things can change quickly. These talks were written in Node-RED version 0.11.0 and the examples will be updated as Node-RED evolves. However, it's always wise, as with any new technology, to check compatibility if things aren't working the way you expect.

When the folks at IBM created Node-RED, they were primarily focused on the Internet of Things, i.e. connecting devices for processing and processing to devices. As a tool for rapid IoT application development, Node-RED is both powerful and flexible. Its power comes from a combination of two factors:

  • Node-RED is an example of a flow-based programming model – messages representing events flow between nodes, triggering processing that results in output. The flow-based programming model maps well to typical IoT applications, characterized by real-world events that trigger some kind of processing that in turn results in real-world actions. Node-RED packages these events as messages that provide a simple, uniform model for events as they flow between the nodes that make up the flows.
  • The set of built-in nodes is the second part of the Node-RED story. By building a set of powerful input and output nodes, each of which hides much of the complexity of interacting with the real world, Node-RED gives developers powerful building blocks to allow them to quickly assemble flows that accomplish a lot, without having to worry about programming details.

These two factors make Node-RED a powerful tool for IoT application development. When combined with the flexibility of creating and using nodes such as functionnode, which allows a developer to quickly write arbitrary JavaScript, plus the Node-RED community, which is constantly creating and sharing new nodes, Node-RED is likely to be one of the top tools from the IoT developer toolbox.

However, Node-RED is not always the best tool for IoT application development. While powerful and flexible, it's not always the right solution. Some situations where Node-RED might not be the first choice include:

  • Complex multifunctional IoT applications. Node-RED excels at rapid application development and acting as the glue to connect events to actions or sensors to actuators, if you will. However, once an application grows above a certain size, it becomes complex to visually program and manage through Node-RED. There are some resources to help with that, eg. subflows (see Lecture 5), but eventually the UI becomes a bottleneck.
  • Flow-based programming is a programming model that is not always the best fit for application development. In the same way that certain programming languages ​​excel at some tasks but not others, flow-based programming has its weaknesses. A good example is loops: Node-RED is tricky when dealing with loops.
  • Specific use cases. Flow-based programming is a general purpose model and not targeted or optimized for specific needs, for example data analysis or UI development. Node-RED currently has no specific support for this type of application and no easy way to add this support. Of course, as Node-RED's underlying technology is JavaScript, you can leverage its features for these needs. If you want to use Node-RED to prototype something, one approach is to implement parts or all of your application in another language better suited to your task and use Node-RED for overall control.

That said, as shown in this talk and will be shown in future talks, Node-RED is a powerful tool for a large number of IoT applications. As it evolves, it becomes more sophisticated and more usable in a wider range of circumstances.

Perhaps just as important as you explore Node-RED in subsequent lessons, you will realize that while Node-RED's roots are in the IoT, it is a tool that can be used to build a wide variety of applications, not just IoT applications. In fact, in these talks you'll see examples of Node-RED being used for web apps, social media apps, back office integration, IT task management, i.e. across the gamut of modern computing tasks.

Our hope is that by the end of this course, you will see Node-RED, as we do, as a flexible and powerful tool that can be used in many situations, both for prototyping and production-level development.

In this talk, we present a high-level view of Node-RED. You've seen how Node-RED connects the building blocks together, using a visual tool to quickly create simple flows that actually perform sophisticated real-world tasks. You also learned a bit about Node-RED's origins as a rapid application development tool for IoT and how, as it has evolved, it is now being used for a variety of tasks, not just IoT programming. Finally, we discuss Node-RED's limitations and point out some areas where other languages ​​and tools can serve you better. In the next class, we'll start with real programming and show you how to build your own flows using the basic nodes that Node-RED provides.

About the Sense Technique:Sense Technical Systems Inc.we have been developing IoT applications and services since 2010. We provide these talks and FRED, Node-RED hosted in the cloud as a service to the community. We also offer a commercial version for ourcustomers, as well as professional services.To know more.

© Lea, Blackstock, CalderonLesson 1 – A brief introduction to Node-RED – Node RED Programming Guide (5)

This work is licensed under aCreative Commons Attribution-NonCommercial 4.0 International License.

Lesson 1 – A brief introduction to Node-RED – Node RED Programming Guide (6)

Author:Rodger Lea

Currently CEO of the Internet of Things startup, Sense Tecnic, Dr. Lea has over 25 years of experience spanning academics, large corporations and startups. Over the past 10 years, he has started or helped start 4 new companies while managing an active research program (University of British Columbia, Canada and Lancaster University, UK) in distributed and ubiquitous computing, IoT and Smart Cities.See all posts by Rodger Lea

Top Articles
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated: 05/30/2023

Views: 6170

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.