Monitoring Plant Floor Data Using MQTT, HiveMQ Cloud, InfluxDB, Grafana, & Schneider IIoT Gateway
In part 2 of this blog series, How to Send & Receive MQTT Sparkplug B Messages Using Raspberry Pis, Node-RED, and HiveMQ Cloud, we showed how to simulate a simple greenhouse remote monitoring system using Raspberry Pis, Node-Red, and HiveMQ Cloud as the MQTT broker. In part 3 of this blog series, we will show you how to monitor plant-floor data in real-time using MQTT protocol, InfluxDB time series database, Grafana visualization platform, Schneider IIoT gateway and HiveMQ Cloud (as the centralized MQTT broker).
Real-time Plant-Floor Data Monitoring in a Factory Using MQTT
A majority of production equipment in today’s factories still use legacy communication protocols, such as Modbus. To collect and monitor telemetry data in real-time in such a legacy setup requires a protocol translator. To this end, we used Schneider IIoT gateway and Node-RED to read the Modbus protocol on one end and convert it to an IIoT protocol, such as MQTT, on the other end.
As shown in the architecture diagram below, the Schneider IIoT Gateway will collect tank level telemetry data simulated by a potentiometer on a legacy controller via Modbus TCP. The telemetry data will be published as MQTT messages to a centralized HiveMQ Cloud MQTT broker, which will forward that data to an InfluxDB time-series database running on Raspberry Pi. This is the overview of the set up.Let’s look at some more details of the architecture. For demonstration purposes, in this blog post, we are using InfluxDB’s Telegraf server agent to receive MQTT messages from HiveMQ Cloud as input and persist them to the InfluxDB database as output. The telemetry data is further visualized on the InfluxDB database using the Grafana Visualisation platform.
Before we visualize the data, we will store it on a time-series database, such as InfluxDB. To host the time-series InfluxDB time-series database and Grafana visualisation platform, either in the cloud or on-prem, we will use a Raspberry Pi as the server. To program the IIoT gateway to perform the Modbus TCP protocol conversion and publishing of MQTT data, we will use Node-Red, which is a low-code drag and drop platform.
To receive MQTT messages from our IIoT gateway and forward them to the Raspberry Pi acting as the server, we will use a centralized MQTT broker. This is where HiveMQ Cloud MQTT broker comes in.
However, the MQTT messages are not going to be automatically sent to InfluxDB storage from HiveMQ Cloud broker. What you need is a bridge for catching these MQTT messages and persisting them to the database. We will use Telegraf, which is an InfluxDB server agent that is capable of subscribing to receive MQTT messages from the HiveMQ Cloud MQTT broker as input, and persists them to the InfluxDB database as its output.
The hardware setup of the above architecture is as show below:
Step-by-Step Guide to Monitor Plant Floor Data in Real-Time
To start building the above system, follow the below steps to set up everything:
Step 1: Setup Your HiveMQ Cloud MQTT Broker
The first thing you need to do before anything else is to make sure that your MQTT broker is up and running. To do that, you need access to your HiveMQ Cloud portal. Follow the below steps to set up your MQTT broker.
1. Click on Login in the main menu, and select the HiveMQ Cloud.
2. Go ahead and click on Sign up now, which will take you to the HiveMQ Cloud Portal home page. Sign up using your email address or GitHub credentials.
3. Follow the simple steps to set up Your Clusters. Once your cluster is set up, you can configure or change your MQTT client credentials under Access Management tab as shown in the next image.
4. With these few simple steps, you have your MQTT Broker up and running.
5. Copy your MQTT broker details for publishing data from my Schneider IIoT gateway.
6. Select Manage Cluster as shown in the image above.
7. Copy the hostname or broker URL address. Here you’ll notice that your MQTT broker is using TLS encryption at port 8883
.
8. Select Access Management as shown below to either configure or update your MQTT client credentials – for example, you can delete the username and password that you created when setting up your cluster and recreate it.
9. Copy the username and password.
With the above steps, you have finished setting up your MQTT broker for free using HiveMQ Cloud portal. The next step is to start publishing your plant-floor telemetry data from the Schneider IIoT Gateway.
Step 2: Configure the Schneider IIoT Gateway
One of the simplest ways to collect Modbus TCP data from an industrial controller and publish it as MQTT messages is using Node-Red. The best part about the Schneider IIoT gateway is that it comes pre-installed with Node-Red. All we have to do is to access its interface via a web browser. Here are the steps to take.
Open a web browser and type in the IP address of the IIoT gateway, which is
192.168.0.128
.Type in the port number
1880
for Node-Red.
We have already put together a Node-Red flow for reading the tank level value simulated by the potentiometer, on our legacy controller, via Modbus TCP. Let’s go through each of the Nodes on Node-Red to configure the Schneider IIoT gateway .
Inject Node:
As shown in the image below, we are reading data every 5 seconds.Function Node:
We have got a function block for preparing the Modbus Message as shown in the image below. If you double click on the node, you’ll see that we are specifying the function code for reading holding register as 3, ModbusId as 110, and the Modbus register that holds the potentiometer value, which is 4096.
Note that the quantity is set to 2 because the Modbus is two 16 bit registers.Modbus-client Node:
The modbus-client node is responsible for sending the Modbus requests to your controller and helps configure your Modbus communication parameters.
As shown in the image below, we are using Modbus TCP, and we have my legacy controller IP address. The Modbus port number is 502.As mentioned earlier, the response from your Modbus controller consists of two 16 bit integer values. We need to convert that to a float value. For that, we are using the function block. When you double click on it, you can see few lines of JavaScript code that perform the conversion.Debug Node:
Lastly, you can use the debug node to see the value coming out of your controller on the debug window.
Before you publish your tank level value as MQTT messages to your HiveMQ Cloud MQTT broker, you will have to deploy this to see if you are getting the right data.
Let’s assume that the potentiometer value is reading 77.8 as shown in the image below in Node-Red.As soon as you turn down the potentiometer from the set up as shown below, the reading in the Node-Red will update accordingly. For example, you will see the value as 58.6.
Step 3: Understanding InfluxDB Data Model
Once you start getting the right data from your controller, you can go ahead and publish the tank level value as MQTT messages to your broker. As this data will ultimately be consumed by InfluxDB, you need to first understand how InfluxDB consumes this data and structure it accordingly.
While InfluxDB is capable of handling data in JSON format and primitive types such as float, they’ve got their own textual format called the line protocol, which is as follows:
Measurement comma Tag Set space Value.
For this demonstration, we are going to use the Influxdb native format, and in our case the text will be
"tank-level,location=plant_a value=58.6"
What does this mean? This means that we have to concatenate our tank level to this string and publish it to the MQTT broker.
Step 4: Publishing Telemetry Data to HiveMQ Cloud MQTT Broker
To publish telemetry data to HiveMQ Cloud MQTT broker, you need to add a function block. This will help create an Influxdb textual format. Follow the below steps to perform the same:
Go back to the Node-Red dashboard and add a function node.
Copy and paste the code for converting payload to Influx textual format as shown below.
Go ahead and publish this to your HiveMQ broker by dragging the MQTT publish node.
Add a new mqtt-broker config node as shown below. Copy the hostname from your HiveMQ portal and paste it here.
Input 8883 as the port number. Enable SSL.
Under security, input my MQTT client username and password as set on the portal.
Use plantsensors for the MQTT topic using mqtt out node as shown below.
Go ahead and connect the output of the function block to the input of my mqtt publish node.
These steps should enable you to publish to your HiveMQ Cloud MQTT broker. To test it, you can use MQTT.fx MQTT client, which is already connected to my broker as shown in the image below.
For demonstration purposes here, we will subscribe to plant sensors. Observe that we are receiving our MQTT messages as shown below.Once you start publishing your telemetry data to your MQTT Broker, the next step is to set up InfluxDB to store this data.
Step 5: Installing InfluxDB on Raspberry Pi
To install InfluxDB, open my Raspberry Pi terminal. We are using Putty to access the terminal from the PC for this demonstration. Follow the below steps:
1. Go ahead and log in.
2. Get the Influxdb repository key using the command as shown below.
3. Add the InfluxDB repository.
4. Ensure your list of packages are up to date.
5. Once you’ve updated the repository lists, you can go ahead and perform the actual installation of InfluxDB on your Rasbperry Pi.
6. Set your InfluxDB database service to automatically run each time your Raspberry Pi boots up.
7. Start the Influxdb database service.
8. Once your Influxdb server is running, you can start creating users and databases on it. To do that, you need to run the Influx client using the command influx
.
9. Go ahead and create a user with admin rights using this command.
10. Create your database for holding the telemetry data. For demonstration purposes, we’ll call it telemetry.
11. Exit from our client using exit
command.
Step 6: Understanding How Telegraph Works
For those unaware, Telegraf is basically a plugin-driven server agent. It is used for collecting and reporting metrics. It has a variety of input plugins including MQTT, which we are going to be using in this demo to grab the telemetry data from HiveMQ Cloud. It also has a variety of output plugins including InfluxDB, which we are going to use to persist telemetry data onto the Influxdb database.
Step 7: Installing Telegraph on Raspberry Pi
To start collecting the telemetry data coming from the HiveMQ Cloud broker and to store the data in the InfluxDB database, you need to install Telegraf. To install Telegraf, follow the below steps:
1. Download the package using the command as shown below.
2. After downloading Telegraf, go ahead and install it.
3. Enable it to automatically start when the Raspberry Pi reboots.
4. Go ahead and start Telegraf.
Step 8: Configuring Telegraf
In order to tell where to pull the telemetry data from and where to store it, open up the Telegraf config file and use the correct command as shown below.
Here, we are going to leave everything else as default. The only configuration that we want to alter is to configure our Telegraf influxdb output plugin to point to our telemetry database on the local instance of influxdb, and to configure our Telegraf MQTT input plugin to point to our HiveMQ broker. You can also follow the same settings.
Follow the below steps to start configuring Telegraf:
Scroll down in the terminal until you get to my output plugins, which are your Influxdb output.
Uncomment the localhost url at port 8086.
Uncomment database and put the database name. In our case, it is “telemetry.”
Scroll down further and set username to admin Comment this out and put admin.
Set the password as adminpassword.
Once you have finished configuring the Telegraf output, go ahead and configure the input.
Alter the MQTT consumer input plugin.
Under servers, put my HiveMQ Cloud MQTT broker. For topics, put plantsensors.
Put My MQTT client username and password.
Set the data format to Influx. Set to json if you are using json.
11. Save the file and exit.
12. Once configuration is complete, reload Telegraf.
Step 9: Querying InfluxDB
As you are already publishing MQTT data to your HiveMQ broker, you should see some telemetry data in your Influxdb time-series database. To find out, you need to query the database. Follow the below steps to do that:
1. Run the Influxdb client using the influx command as shown below.
2. Select the telemetry database.3. Select everything under Tank level using a query as shown below.4. Check if you are successfully logging the telemetry data into your Influxdb time-series database.
The next step is to visualize the data using Grafana.
Step 10: Installing and Configuring Grafana on Raspberry Pi
To visualize the data using Grafana, you need to first install Grafana on our Raspberry Pi. Follow the below steps:
1. Download the Grafana package
2. Install Grafana.
3. Enable Grafana service to automatically run each time the Raspberry Pi boots up.
4. Go ahead and start Grafana.
5. Go ahead and access it using a web browser. Put the IP address of the Raspberry Pi and put port number as 3000, which is the port on which your Grafana runs.
6. Go ahead and login. The default user is admin, and the default password is admin.
7. Assign a new password.
8. Once you’ve successfully logged into our Grafana visualisation platform, add InfluxDB as the data source so that you can start visualizing your plant-floor data.9. Click on add data source.
10. Select the Influxdb time-series database as shown below.11. Fill in the URL and database settings as configured earlier. Refer to the images below to check the configuration settings.
Step 11: Visualizing Plant-Floor Data Using Grafana
After configuration, you need to create a new dashboard to start visualizing the plant-floor data in real-time. Follow the below steps:
1. Create a new dashboard on Grafana.2. Add new panel and configure the panel using the data of InfluxDB database.3. Select the measurement, which is the tank level. Here, we are leaving it as value.4. Group by time_interval, order the time in ascending, and format it as a time series.
5. Name the panel. We will use Plant Telemetry.
6. Click on apply.7. Zoom into your trend.8. Choose to see data for the last 5 minutes, or the data from the last 15 minutes.9. Set auto refresh to 5 seconds. At this point, if you turn your potentiometer up, you can observe that the value of the potentiometer rises too. Here, in our example the value is at 87.5 as shown in the below images.With this, you have successfully developed a solution for monitoring plant-floor data in real-time using MQTT, InfluxDB time-series database, Grafana visualization platform, Schneider IIoT Gateway and HiveMQ Cloud as the centralized MQTT Broker.
Conclusion: Easily Monitor Plant-floor Data in Real-time
With this demonstration, you can see how easy it is to monitor plant-floor data in real-time using MQTT. The publish/subscribe architecture of MQTT helps in decoupling communication between the different clients, which allow different vendors and different systems to implement MQTT independently.
Additionally, you could publish MQTT messages as Sparkplug payload for plug-and-play interoperability using the HiveMQ InfluxDB sparkplug extension, which works on the Enterprise or self-hosted HiveMQ MQTT Broker.
If you want to watch this demonstration in action, watch this video Monitoring Plant Floor Data Using HiveMQ Cloud, InfluxDB, Grafana, & Schneider IIoT Gateway.
Ready to kickstart your MQTT projects? Dive into HiveMQ Cloud Starter—our free platform perfect for testing and small-scale production!
Kudzai Manditereza
Kudzai is a tech influencer and electronic engineer based in Germany. As a Sr. Industry Solutions Advocate at HiveMQ, he helps developers and architects adopt MQTT and HiveMQ for their IIoT projects. Kudzai runs a popular YouTube channel focused on IIoT and Smart Manufacturing technologies and he has been recognized as one of the Top 100 global influencers talking about Industry 4.0 online.