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.
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:
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.
Next, we will create Node-RED on this cloud platform.
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.
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.
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.
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:
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:
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.
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:
We're almost done!
When you click the inject node, you will see the resulting value on the debug tab:
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.
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.
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:
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:
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:
After that, click on the stop option on the Node-RED record under the Cloud Foundry apps.
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:
Now, you can use Node-RED on IBM Cloud for IoT server-side situations.
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:
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:
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.
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:
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:
The inject node generates dummy data, while the cloudant out node stores the input value as-is in the Cloudant database.
You can switch between the Text editor and the Visual editor using tabs. Please refer to the following image:
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.
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:
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.
The first parameter, Service, is set automatically; it is your Cloudant service on IBM Cloud.
You will see that the result was output to the debug window:
You can also download this flow definition file here: https://github.com/PacktPublishing/-Practical-Node-RED-Programming/blob/master/Chapter06/cloudant-flows.json
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:
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 ;
This is what it looks like:
You can check the result on the dashboard window.
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.