{{announcement.body}}
{{announcement.title}}

Node-RED: Flow-Based Modelling

DZone 's Guide to

Node-RED: Flow-Based Modelling

In this article, we discuss how to perform flow-based modeling with Node-RED to get you better acquainted with this popular, IoT framework.

· IoT Zone ·
Free Resource

Node-RED is a powerful, extensible, graphical programming environment. It’s a flow-based modeling tool, which makes it easy to create event-driven applications.

It's built on top of Node and provides a browser-based editor for wiring flows together. Node-RED development environment is actually a web page running on your pi or it can run in cloud environments making it a very versatile tool.

It is used in scenarios spanning a wide range of industries from manufacturing and utilities, healthcare and agriculture, and home and industrial automation.

Originally developed as an open-source project at IBM in late 2013 to meet their need to quickly connect hardware and devices to web services and other software – as a sort of glue for the IoT – it has quickly evolved to be a general-purpose IoT programming tool. 

Importantly, Node-RED has rapidly developed a significant and growing userbase and an active developer community who are contributing new nodes that allow programmers to reuse Node-RED code for a wide variety of tasks.

The official website provides a great collection of resources for getting started with Node-Red. They also have video tutorials and other documentation. I recommend going through those to have a better understanding of this exciting platform. You can find the link in the reference section of this post.

Node-Red is pre-installed in Raspberry Pi installations or you can run it on your local machine easily via docker.

Running Under Docker

Shell


Here is the running container:

Running container
Running container

That's all you need to run it locally via Docker. Once the container is running, you can just browse the URL, http://localhost:1880, via a web browser, and you will see the IDE as follows. Simple, right?

Node-RED IDE
Node-RED IDE

Node-RED Editor Components

Editor components
Editor components
  • Palette
    • List of all nodes you can use to create flows. These are separated into categories to keep them organized.
  • Workspace
    • This is where you create your workflows.
  • Header
    • Deploy button is what you click to take your current flow configuration and send it to the node-RED run-time.
  • Menu
    • Many useful features
  • Side-Bar
    • Number of Tabs with different tools.
    • Info Tab, Debug Tab (show messages passed to debug node), and Config Tab

Create a Flow

The process of creating a flow involves adding Nodes to the workspace and wiring them together.

Create a Simple Flow

  • Drag an inject node from the palette to the workspace. Inject-Node allows you to manually inject messages to flow.
  • Add a debug node.
  • Wire inject-node and debug-node together by clicking the output from inject-node to the input of debug-node.
  • Now, whenever the inject-node sends a message, it will be received by debug-node.
  • Click the Deploy button to push it to the runtime. Once deployed, click the button next to inject-node and check the debug-window for messages.
Checking debug-window for messages
Checking debug-window for messages

Function Node

  • The Function-Node allows you to write JavaScript, which will be executed for every message.
  • Add a function-node (toISOFormat) between the inject and debug node and write JavaScript as shown in the picture below.
  • Deploy and check the debug messages.
Creating function nodes
Creating function nodes

Here is the JavaScript code.

Example JavaScript code
Example JavaScript code

Once, deploy notice the change in debug messages.

Changes in debug messages
Changes in debug messages

Node-RED With Twitter API

You can do a lot of interesting things with Node-RED platforms (e.g. I experimented working with twitter API). I created the flow, which monitors the Twitter API for particular hash-tags. (In my case, I used #Hitachi, but you can monitor whatever you like).

So, whenever someone uses this tag, my NODE-Red flow will get notification and information about the tweet (so this part is about reading from Twitter).

I also added the write functionality. In my case, Node-RED will create a tweet and post on Twitter.

Please find below, the screenshots of that flow. It's a quite simple example, and you will find similar flows on the Node-RED website as well. The point is, that I was able to set up all this very easily without having extensive underlying Twitter API knowledge.

Here is the tweet. I used the #Hitachi tag:

Example tweet
Example tweet

Here is the basic flow to get the tweets for a particular hashtag.

Basic example flow
Basic example flow

Here is the debug information showing the tweet.

Debug information with tweet
Debug information with tweet

Saving Tweets to a File

This is just a small update of previous flow, so I use another built-in Node to write to a File:

Writing a built-in Node to write a file
Writing a built-in Node to write to a file
Console output
Console output

Creating a Tweet

In this update, Node-Red allows me to create a tweet easily. So, whenever it receives a Tweet, it can automatically create a reply and post it to Twitter.

Tweet workflow
Tweet workflow
Output from Twitter
Output from Twitter

Here is the Tweet-Reply screenshot

Tweet output
Tweet output

Summary

So, we saw the basic components of Node-RED; you can create very interesting flows. For the Twitter API example above, you will need a developer account from Twitter and API keys to use in your Node-RED flow.

I will also suggest to check my other articles about MQTT and IoT to have a better understanding of this and upcoming posts.

In my next blog posts, I will bring in MQTT and some Raspberry PI or Arduino to see how all of these worked together. Until next time, happy coding!

References

Topics:
iot, javascript, node, node-red, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}