In this chapter, we will learn how to utilize Node-RED, which can be used standalone on a cloud platform (mainly Platform as a Service). Platform as a Service (PaaS) provides an instance that acts as the execution environment for an application, and the application developers only focus on executing the application created by themselves without using their power to build the environment. Node-RED is actually a Node.js application, so you can run it wherever you have a runtime environment for Node.js.
There are various major mega clouds such as Azure, AWS, and GCP, but Node-RED is prepared as a Starter App (a web application that can be launched on IBM Cloud is called a Starter App) by default in IBM Cloud, so we will use it in this chapter.
In this chapter, we'll cover the following topics:
By the end of this chapter, you will have mastered how to build a flow for handling sensor data on the cloud.
The code that will be used in this chapter can be found in the Chapter06 folder at https://github.com/PacktPublishing/-Practical-Node-RED-Programming.
This time, we will use IBM Cloud. The reason for this is that IBM Cloud has Node-RED Starter Kit on it. This is a kind of software boilerplate that includes services needed for Node-RED on the cloud, such as a database, CI/CD tools, and more.
If you have not used IBM Cloud yet, don't worry – IBM provides a free IBM Cloud account (Lite account) with no credit card registration needed. You can register for an IBM Cloud Lite account at http://ibm.biz/packt-nodered.
Before using Node-RED on IBM Cloud, you need to finish the registration process for your IBM Cloud Lite account.
Important Note
In this book, we strongly recommend that you select a Lite account when using IBM Cloud. You can upgrade from a Lite account to a standard account (PAYG/Pay as you go) at your own will. This means you can automatically upgrade to PAYG by registering your credit card.
Please note that services that can be used free of charge with a Lite account may be charged for with PAYG.
Now, let's launch Node-RED on IBM Cloud by following these steps:
Important Note
The instructions/screenshots provided here are correct at the time of writing. The UI of IBM Cloud changes so often that it might be different from the current UI.
Figure 6.1 – Logging in via your Lite account
Figure 6.2 – IBM Cloud dashboard
Next, we will create Node-RED on this cloud platform.
Figure 6.3 – Get a Starter Kit button
Figure 6.4 – Node-RED Starter Kit
You can freely change each item by providing your own values, but in this chapter, the values that have been set here will be used for explanation purposes.
See Figure 6.5 for the settings and values to configure. Please note that once they are set, these items cannot be changed later.
Figure 6.5 – Create Node-RED as a Node.js application
You have now created the framework for the applications that make up Node-RED. After this, you will be redirected to the App Details screen automatically, where you will be able to see that the Cloudant instance of the linked service has also been provisioned.
However, only the application source code and the instance of the cooperation service are created, and they haven't been deployed to the Node.js execution environment on IBM Cloud yet. The actual deployment will be done when the CI/CD toolchain is enabled.
Figure 6.6 – Deploying your Node-RED application
IBM Cloud API Key
The IBM Cloud API Key is used to control your IBM Cloud account and various services (for example, it's Cloud Foundry in this tutorial). You can use this to issue a token for external access to services on IBM Cloud, for example. You can find out more about the IBM Cloud API Key here: https://cloud.ibm.com/docs/account?topic=account-manapikey.
Figure 6.7 – Generating an IBM Cloud API Key
This time, we are using IBM Cloud with a Lite account, so we have only 256 MB of memory available for all our services on IBM Cloud. So, if we use 256 MB for the Cloud Foundry Node.js service, we won't be able to use more memory for other services. But Node-RED needs 256 MB to run on IBM Cloud, so please use 256 MB here. It is already allocated 256 MB for the instance by default, so click the Next button, with no parameters changed:
Figure 6.8 – Node.js runtime instance details
Once you've done this, a DevOps toolchain setting screen will be displayed.
You can change the DevOps toolchain name to any name you like. This is the name that identifies the toolchain you've created in IBM Cloud:
Figure 6.9 – Configure the DevOps toolchain window
Now, you are ready to use the environment (Node.js runtime and DevOps toolchain) to run the Node-RED application you created in the previous step. The Node-RED application you created is automatically deployed on the Node.js runtime through the toolchain.
Figure 6.10 – Checking the status of Node-RED and moving to the Pipeline tool
In Delivery Pipelines, check that the statuses of both the BUILD and DEPLOY panels are green and displaying STAGE PASSED.
Figure 6.11 – Checking the status of each stage and moving to App Console
Figure 6.12 – Checking that Node-RED is running and opening Flow Editor
Great work! You opened the Node-RED flow editor on IBM Cloud. Next, we will start to use the Node-RED flow editor you just opened.
If you got any errors while performing these steps, it would be best for you to delete Cloud Foundry App, Cloudant, and DevOps toolchain and recreate them by following the same steps mentioned previously.
After clicking on Visit App URL, you will be redirected to the initial setup dialog so that you can use Node-RED flow editor on IBM Cloud.
You can proceed through this dialog by clicking each Next button, though please note that you should select Secure your editor so only authorised users can access it with Username and Password in order to log in to your own flow editor. This is because this flow editor is on IBM Cloud as a public web application. This means that anybody can access your flow editor if the URL is known. So, I strongly recommend that you select this option and set your own Username and Password values:
Figure 6.13 – Setting a username and password to access flow editor
We're almost done!
Figure 6.14 – Logging into your Node-RED flow editor
Next, we will check Node-RED flow editor on IBM Cloud and see if it is available.
Figure 6.15 – Default sample flow
When you click the inject node, you will see the resulting value on the debug tab:
Figure 6.16 – Checking the result
Now, you can create a flow in Node-RED on IBM Cloud. The Node-RED flow editor is always running as a Node.js application on IBM Cloud. This means that the Node.js runtime service (instance) is enabled on IBM Cloud. In other words, unlike Node-RED running on Raspberry Pi, this version of Node-RED accesses the flow editor via the internet.
In the next section, I will explain a little about situations where Node-RED is used on such a cloud.
Let's revisit the situation where Node-RED is used in the cloud.
As we mentioned in the previous chapter, Node-RED is both a tool and an execution environment for creating Node.js applications written in Node.js. As a reason to build an application with Node-RED, I explained that by black boxing individual units of data processing, the role of each process becomes very clear, and it is easy to build and maintain.
This is the same reason not only on the edge device, but also on the server side (cloud side), for persisting, analyzing, and visualizing the data that's collected by the edge device.
The biggest feature of Node-RED is that it connects the processing of Node.js in a sequential manner or in parallel with input/output data chunks in the form of messages. It can be said that this is very suitable for IoT data handling.
Again, as we discussed in the previous chapter, the standard architecture for IoT solutions is built on edge devices and cloud platforms. It sends the sensor data acquired by the edge device to the cloud, makes it persistent, and processes it for the desired processing chain.
This chapter will focus on that part of the cloud.
The edge device and the cloud don't actually connect yet. Assuming that the data has been passed to the cloud, let's make the data persistent in the database and visualize it.
We're going to use a dashboard node that is popular with all developers for Node-RED on IBM Cloud.
Before you use Node-RED on IBM Cloud, please install a new node; that is, node-red-dashboard.
Node-RED provides the palette manager, which is easy to install and is used to install extra nodes directly. This is very helpful when you're using lots of nodes. However, it might have issues due to the limited memory of the Node-RED application of an IBM Cloud Lite Account.
So, here, we need to get the node-red-dashboard node in order to edit the application's package.json file and redeploy the Node-RED application on IBM Cloud.
You can read about this node at https://flows.nodered.org/node/node-red-dashboard.
Follow these steps to make changes in the package.json file:
Figure 6.17 – Accessing your application source
Figure 6.18 – Selecting package.json
"node-red-dashboard": "2.x",
Figure 6.19 – Editing package.json and adding node-red-dashboard
After this, the Continuous Delivery Pipeline will automatically start to build and deploy the Node-RED application. You can check the status on the Delivery Pipeline at any time, just like you did while creating Node-RED Starter App:
Figure 6.20 – Rebuilding and redeploying your application automatically
When you get Deploy Stage failed with the memory limit error for lite account, please stop your Node-RED service on your IBM Cloud dashboard and after that run the Deploy Stage. You can stop your Node-RED service by accessing your IBM Cloud dashboard and clicking on Cloud Foundry apps under Resource summary:
Figure 6.21 Selecting Cloud Foundry apps
After that, click on the stop option on the Node-RED record under the Cloud Foundry apps.
Figure 6.22 Clicking the Stop option
That's all. You can confirm that the dashboard node has been added by closing the Palette management screen and scrolling down the left-hand side of the flow editor, as shown in the following screenshot:
Figure 6.23 – Checking that the dashboard node has been installed
There's one more thing: we need to use a database, but IBM Cloud's version of Node-RED has a Cloudant database by default. We will use Cloudant for the case study in the next section.
Now, you can use Node-RED on IBM Cloud for IoT server-side situations.
Now, let's consider a server-side case study for IoT.
It does not depend on the case of each edge device. It primarily serves to process data and store it in a database for visualization.
In this chapter, we'll consider the use case of IoT; that is, assuming that the sensor data that's received using the sensor module is received on the server side, and the subsequent processing part.
The difference from the previous chapter is that in this server-side processing tutorial, the content of the data doesn't make much sense. The main purpose is to save the received data and visualize it as needed, so I would like to define the following two use cases.
The first case is to store data. Let's create an application (flow) that stores data you receive from devices. In this section, we don't use real data from devices; we just use the data generated by the inject node instead:
Figure 6.24 – Use case 1 overview
Now, let's look at the second use case.
The second case is to show data as graphs or charts. Let's create an application (flow) that publishes data you received from devices, on the dashboard. We won't be using real data from any devices, just the data that's been generated by the inject node:
Figure 6.25 – Use case 2 overview
As we mentioned earlier, we will use Cloudant for the database for case 1 and the dashboard for the graph display for case 2. These have already been prepared.
Now, let's create these two server-side case flows on the Node-RED flow editor.
Please check again that the Cloudant node and the Dashboard node have already been installed on your flow editor. If you don't have them, please install these nodes by following the steps mentioned in the What is the specific situation for using Node-RED on the cloud? section of this chapter.
Now, you need to prepare a specific database for this tutorial on Cloudant. Follow these steps:
Figure 6.26 – IBM Cloud dashboard view
Figure 6.27 – Selecting the Cloudant service from the Resource list
Figure 6.28 – Launching the Cloudant dashboard
Figure 6.29 – Creating a new database on Cloudant
Now that you have created the database for this tutorial, you can use it at any time!
With IoT, server-side processing starts from the point where it is received from the edge device. However, as we mentioned earlier, we will focus on storing the data in the database, so we will be using dummy data that will be generated by the inject node. The chunk of data that's received as a message is persisted in the Cloudant database on Node-RED.
We can make the flow by following these steps:
Figure 6.30 – Placing the Inject node and cloudant out node
The inject node generates dummy data, while the cloudant out node stores the input value as-is in the Cloudant database.
Figure 6.31 – Wiring these two nodes
Figure 6.32 – JSON on the first parameter of the inject node
You can use both types of editor here; that is, the Text editor or the Visual editor. You can add any values to the JSON style, but here's what we have used for the JSON data:
{"temp":"29.18", "humi":"55.72"}
You can switch between the Text editor and the Visual editor using tabs. Please refer to the following image:
Figure 6.33 – Two types of JSON editor are available
The first parameter, Service, is set automatically; it is your Cloudant service on IBM Cloud. The third parameter, Operation, does not need to be changed from its default value.
Figure 6.34 – Setting the database name on the cloudant out node
At this point, we can't check the data on Cloudant via the Node-RED flow editor; we can only check it on the Cloudant dashboard:
Figure 6.35 – Result on the Cloudant dashboard
Now, let's make a flow that gets the data from Cloudant by following these steps:
The inject node just executes this flow as a trigger, so there's no need to change the parameters in it. The cloudant in node gets the data from your Cloudant database. The debug node outputs a log on the debug tab.
Figure 6.36 – Placing new three nodes and wiring them to get data
The first parameter, Service, is set automatically; it is your Cloudant service on IBM Cloud.
Figure 6.37 – Setting the database name and searching for a target on cloudant in the node
You will see that the result was output to the debug window:
Figure 6.38 – Result of getting the data from Cloudant
Congratulations! With this, we have successfully created a basic flow (application) that stores sensor data on a database with Node-RED.
You can also download this flow definition file here: https://github.com/PacktPublishing/-Practical-Node-RED-Programming/blob/master/Chapter06/cloudant-flows.json
Important Note
This flow has no values for the Cloudant service name in cloudant in/out flows. Please check if your service name is set on that automatically once this flow definition has been imported.
You now understand how to handle data on Node-RED. We'll visualize that data in the next section.
The first use case was for storing sensor data in a database, while the second one was for visualizing sensor data on Node-RED. In IoT, after acquiring sensor data, we must visualize it in some form. The focus here is on retrieving and visualizing the data stored in use case 1. We will do this by following these steps:
Figure 6.39 – Placing the nodes and wiring them to show data
The Inject node just executes this flow as a trigger, so there's no need to change the parameters in it. The function node generates numeric data to be shown on the Node-RED as a chart. Finally, the chart node makes it possible for the data to appear on the chart.
// Set min and max for random number
var min = -10 ;
var max = 10 ;
// Generate random number and return it
msg.payload = Math.floor( Math.random() * (max + 1 - min) ) + min ;
return msg;
This is what it looks like:
Figure 6.40 – Code for generating a random number
Figure 6.41 – Setting a parameter on the chart node
You can check the result on the dashboard window.
Figure 6.42 – Clicking the dashboard icon button and opening the window icon button
Figure 6.43 – Line chart with values
Congratulations! With this, we have successfully created a basic flow (application) that shows sensor data as a chart with Node-RED.
You can also download this flow definition file here: https://github.com/PacktPublishing/-Practical-Node-RED-Programming/blob/master/Chapter06/dashboard-flows.json.
In this chapter, you learned how to create a server-side sample flow (application) by following a real IoT use case. These were simple tutorials, but I am sure it will be beneficial for you so that you understand how to make flows for IoT server-side applications.
The flow steps we created here will help you create different flows for other server-side applications in the future.
In the next chapter, we will use the same IoT use case we used in this chapter, but we will create a practical sample flow (application) that will call a web API.
44.212.96.86