In this chapter, we will learn how the processing of data from an edge device takes place in the Internet of Things (IoT) using Node-RED. We will not only cover data handling but also sending data to a server application from an edge device. For the device, I would like to use a Raspberry Pi. After completing the tutorials in this chapter, you will be able to handle sensor data acquired by edge devices.
Let's get started with the following four topics:
To progress in this chapter, you will need the following:
In this chapter, we will learn how to handle the data acquired from the sensor device with Node-RED on the Raspberry Pi and publish the data to an MQTT broker.
For the sensor device, we will use the temperature/humidity sensor used in Chapter 5, Implementing Node-RED Locally. See each step in Chapter 5, Implementing Node-RED Locally, for details about connectivity and how to enable the sensor device on the Raspberry Pi.
Prepare to connect your temperature/humidity sensor to your Raspberry Pi. This is the edge device. You have already purchased and configured your edge device in Chapter 5, Implementing Node-RED Locally. Light sensors are not used in this chapter:
Please prepare the device to gather the temperature/humidity sensor data on your Raspberry Pi as follows:
When all the devices are ready, connect the Raspberry Pi and Grove Base HAT, and connect the Grove Temperature and Humidity Sensor (SHT31) to the I2C port (any I2C port is OK):
Figure 10.1 – Connecting the temperature/humidity sensor to your Raspberry Pi
We will go on to connect to the server side from the Raspberry Pi, so please ensure that you are connected to the internet via Wi-Fi. Of course, you can also access the internet by connecting to a modem using a LAN cable. The Raspberry Pi has a LAN cable port by default, so all you have to do is plug in the LAN cable:
Figure 10.2 – Connecting your Raspberry Pi to the internet
And that's all we need to proceed. Next, we will see how to get the data from the sensor node.
As you have already learned in Chapter 5, Implementing Node-RED Locally, it should be easy to get the data from the Grove Base temperature/humidity sensor module.
The following are the steps to get the data from the sensor node:
Figure 10.3 – Placing and wiring nodes
There are no values or items to be set on this settings screen. You just need to make sure that the port is indicated as I2C. After checking, close the settings screen.
Make sure you see a blue square icon and the text I2C underneath the grove-temperature-humidity-sensor-sht3x node. This indicates that the Grove Base temperature/humidity sensor module is successfully connected to your Raspberry Pi. If the color of this icon turns red, it means that the module is not properly connected to the I2C port, so please reconnect the hardware correctly:
Figure 10.4 – Checking the port is set as I2C
Figure 10.5 – Deploy and click the button on the inject node
It has worked successfully if you can confirm that the values of the sensor data collected are displayed in JSON in the debug tab of the flow editor. This way, data can be obtained from the sensor module:
Figure 10.6 – Making sure that the data is visible from the sensor module
Now we know that Node-RED on the Raspberry Pi can handle sensor data. Let's learn the process of publishing this data to an MQTT broker.
Now that the sensor data has been successfully acquired, let's send that data to the server.
We usually select a protocol suitable for the content being transmitted; for example, when exchanging mail, we use SMTP. Currently, HTTP is used as a general-purpose protocol on the internet.
For example, HTTP is used for various communications on the internet, such as displaying web pages in a browser and exchanging data between servers. HTTP is a protocol created for exchanging content on the internet. In many cases, network devices such as routers and firewalls on the internet are set to allow HTTP communication to be used for various purposes, and HTTP is compatible with the internet.
In the IoT world, MQTT is often used as a general protocol instead of HTTP. This means that the MQTT protocol is the standard of the IoT world, just as the HTTP protocol is the standard of the web world.
MQTT (short for MQ Telemetry Transport) is a communication protocol that was first created by IBM and Eurotech in 1999. In 2013, standardization of this protocol was promoted by an international standardization organization called OASIS.
MQTT is intended to be used over TCP/IP. In short, it specializes in machine-to-machine (M2M) communication over the internet, and communication between machines and other resources on the internet. The machines referred to here are microcomputer boards, such as PCs and small Linux boards (including the Raspberry Pi).
M2M has evolved over the years since 1999, the word IoT has appeared, and MQTT is now very often adopted when conventional machines communicate via the internet. Therefore, MQTT is the best protocol for IoT. One of the reasons that MQTT is important is that it offers a lightweight protocol to handle data in narrowband networks and on low-performance devices:
Figure 10.7 – Conceptual diagram of typical M2M communication
From the preceding information, you can see why the MQTT protocol is used in IoT. Now let's think about how Node-RED can transmit data using the MQTT protocol.
Node-RED provides the following two MQTT related nodes by default:
Figure 10.8 – An mqtt in node and mqtt out node
You can find these under the network category on the side panel of the Node-RED flow editor.
If you want to set the server address and topic for the MQTT broker and use publish and subscribe, it is fine to use these two nodes.
Let's now try to send the sensor data to a local MQTT broker.
Now, let's send the sensor data on the Raspberry Pi to an MQTT broker via Node-RED. Here we will use the popular MQTT broker Mosquitto. In this chapter, we will go as far as preparing the device to send the device data to the server. The task of actually receiving and processing data on the server side will be demonstrated in a hands-on example in the next chapter. Therefore, here we will use Mosquitto just for checking the data transmission is performed correctly.
Mosquitto is released under the open source BSD license and provides broker functionality for MQTT V3.1/v3.1.1.
It works on major Linux distributions such as RedHat Enterprise Linux, CentOS, Ubuntu, and OpenSUSE, as well as Windows. It also works on small computers such as the Raspberry Pi.
In this chapter, we will verify that the sensor data of the edge device can be sent via an MQTT broker to the localhost of the Raspberry Pi. This is very easy. I am confident that if we can send the data to MQTT broker in this way, we will be able to see the sensor data of the edge device immediately on the server side.
The following is a general configuration diagram showing an example use of Mosquitto:
Figure 10.9 – Mosquitto overview
In this chapter, we will implement the Node-RED flow from the edge device to send data to Mosquitto on your Raspberry Pi. Data visualization using IBM Cloud will be implemented in the next chapter.
Important note
Mosquitto is a very important and useful tool and is a platform for implementing the IoT mechanism in Node-RED. Developing a deeper understanding of Mosquitto will help you to make Node-RED more widely available.
You can learn more about the Mosquitto at https://mosquitto.org/.
Now, let's prepare Mosquitto on your Raspberry Pi.
In this section, we will enable Mosquitto so that it can run on the Raspberry Pi. The flow is simple. Just install Mosquitto and start the service. Follow these steps on your Raspberry Pi to prepare:
$ sudo apt install mosquitto
sudo systemctl start mosquitto
After starting, you can check the status of the Mosquitto service with the following command:
sudo systemctl status mosquitto
This is how it looks in the terminal:
Figure 10.10 – Mosquitto running status
$ sudo apt install mosquitto-clients
$ sudo apt install mosquitto-clients
$ mosquitto_sub -d -t packt
This is how it looks in the terminal:
Figure 10.11 – Start subscribing to Mosquitto with the topic packt
$ mosquitto_pub -d -t packt -m "Hello Packt!"
This is how it looks in the terminal:
Figure 10.12 – Publishing a message to Mosquitto with the topic packt
You will see the message you published on the terminal subscribing.
You are now ready to use Mosquitto. Next, we will implement Pub/Sub on Node-RED on your Raspberry Pi.
Now, launch the Node-RED flow editor on your Raspberry Pi and follow these steps to create a flow:
Figure 10.13 – Placing these nodes and wiring them
*It is possible to edit the Server and Port values by clicking the pencil icon.
This is how the settings window should look:
Figure 10.14 – Setting the properties of the mqtt out node
*It is possible to edit the Server and Port values by clicking the pencil icon.
This is how the settings window should look:
Figure 10.15 – Setting the properties of the mqtt in node
And with that, we have completed making the flow to subscribe to and publish the topic packt via the Mosquitto MQTT broker on your Raspberry Pi localhost. Next, we will check the status of our data on localhost.
In this section, we will check whether the sensor data sent from your Raspberry Pi can be received by Mosquitto via Node-RED on your Raspberry Pi with the following steps:
Figure 10.16 – Run the flow to publish the data
There are currently two flows in this Node-RED instance. One is the flow of publishing data to the Mosquitto MQTT broker, and the other is the flow of subscribing to data from that broker. The subscribed flow is normally in a standby state, so when the data is published, the subscribed data is automatically output to the debug tab.
Figure 10.17 – Check the result of the publishing and subscribing
Congratulations! Now you know how to handle the sensor data acquired by the Raspberry Pi and Grove Base sensor module on the edge device and send it to an MQTT broker.
In this chapter, in the form of a hands-on tutorial, we experienced how to handle sensor data on an edge device and send it to an MQTT broker. This is one of the ways to create an edge device-side application for IoT with Node-RED.
In the next chapter, we will look at a hands-on example of receiving sensor data and visualizing it on the server side (the cloud) via Node-RED.