Monitoring Plant Floor Data Using MQTT, HiveMQ Cloud, InfluxDB, Grafana, & Schneider IIoT Gateway

Monitoring Plant Floor Data Using MQTT, HiveMQ Cloud, InfluxDB, Grafana, & Schneider IIoT Gateway

author Kudzai Manditereza

Written by Kudzai Manditereza

Category: MQTT Industry 4.0 MQTT Sparkplug

Published: September 29, 2021


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.

Architecture

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:

Hardware Setup

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 Cloud in the main menu, and select HiveMQ Cloud as shown below.
HiveMQ Homepage
  1. Select the free version of the HiveMQ Cloud that allows you to connect up to 100 clients for free.
HiveMQ Cloud Plans
  1. Go ahead and click on Sign up now, which will take you to the HiveMQ Cloud Portal home page.
  2. 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.
HiveMQ Cloud Your Clusters

With these few simple steps, you have your MQTT Broker up and running.

  1. Copy your MQTT broker details for publishing data from my Schneider IIoT gateway.
  2. Select Manage Cluster as shown in the image above.
  3. Copy the hostname or broker URL address. Here you’ll notice that your MQTT broker is using TLS encryption at port 8883.
  4. 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.
HiveMQ Cloud Cluster Details
  1. Copy the username and password.
HiveMQ Cloud Access Management

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.

  1. Open a web browser and type in the IP address of the IIoT gateway, which is 192.168.0.128.
  2. 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.

Node Red Inject Node

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.

Node Red Function Node

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.

Node Red Edit Modbus Client Node

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.

Node Red edit Function Node

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.

Node Red Potentionmeter Reading

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.

Potentiometer Reading

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:

  1. Go back to the Node-Red dashboard and add a function node.
  2. Copy and paste the code for converting payload to Influx textual format as shown below.
Edit Function Node
  1. Go ahead and publish this to your HiveMQ broker by dragging the MQTT publish node.
  2. Add a new mqtt-broker config node as shown below. Copy the hostname from your HiveMQ portal and paste it here.
  3. Input 8883 as the port number. Enable SSL.
  4. Under security, input my MQTT client username and password as set on the portal.
Add MQTT Broker Config Node
  1. Use plantsensors for the MQTT topic using mqtt out node as shown below.
Edit MQTT Out Node
  1. Go ahead and connect the output of the function block to the input of my mqtt publish node.
Node-Red Connect Output

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.

MQTT.fx Message Output

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.
Putty Configuration
  1. Get the Influxdb repository key using the command as shown below.
1
$ wget -qO- https://repos.influxdata.com/influxdb.key | sudo apt-key add -
  1. Add the InfluxDB repository.
1
$ echo "deb https://repos.influxdata.com/debian stretch stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
  1. Ensure your list of packages are up to date.
1
$ sudo apt update
  1. Once you’ve updated the repository lists, you can go ahead and perform the actual installation of InfluxDB on your Rasbperry Pi.
1
$ sudo apt install -y influxdb
  1. Set your InfluxDB database service to automatically run each time your Raspberry Pi boots up.
1
2
$ sudo systemct1 unmask influxdb.service
$ sudo systemctl enable influxdb.service
  1. Start the Influxdb database service.
1
$ sudo systemctl start influxdb
  1. 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.
1
$ influx
  1. Go ahead and create a user with admin rights using this command.
Create User
  1. Create your database for holding the telemetry data. For demonstration purposes, we’ll call it telemetry.
Create database called telemetry
  1. Exit from our client using exit command.
1
$ exit

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.
1
$ wget https://dl.influxdata.com/telegraf/releases/telegraf_1.12.2-1_armhf.deb
  1. After downloading Telegraf, go ahead and install it.
1
sudo dpkg -i telegraf_1.12.2-1_armhf.deb
  1. Enable it to automatically start when the Raspberry Pi reboots.
  2. Go ahead and start Telegraf.
1
2
3
$ sudo systemctl enable telegraf
$ sudo systemctl start telegraf
$ sudo nano /etc/telegraf/telegraf.conf

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.

Configure Telegraf

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:

  1. Scroll down in the terminal until you get to my output plugins, which are your Influxdb output.
  2. Uncomment the localhost url at port 8086.
  3. Uncomment database and put the database name. In our case, it is “telemetry.”
Give Database the name Telegraf
  1. Scroll down further and set username to admin Comment this out and put admin.
  2. Set the password as adminpassword.
Set Password
  1. Once you have finished configuring the Telegraf output, go ahead and configure the input.
  2. Alter the MQTT consumer input plugin.
  3. Under servers, put my HiveMQ Cloud MQTT broker. For topics, put plantsensors.
Set topic to plantsensors
  1. Put My MQTT client username and password.
  2. Set the data format to Influx. Set to json if you are using json.
1
2
3
4
$ sudo systemctl enable telegraf
$ sudo systemctl start telegraf
$ sudo nano /etc/telegraf/telegraf.conf
$ sudo systemctl reload telegraf.service
  1. Save the file and exit.
  2. 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.
1
$ influx
  1. Select the telemetry database.
Select telemetry database
  1. Select everything under Tank level using a query as shown below.
Select Tank Level
  1. Check if you are successfully logging the telemetry data into your Influxdb time-series database.
Logging the telemetry data to InfluxDB

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
1
$ wget https://dl.grafana.com/oss/release/grafana_8.0.3_armhf.deb
  1. Install Grafana.
1
$ sudo dpkg -i grafana_8.0.3_armhf.deb
  1. Enable Grafana service to automatically run each time the Raspberry Pi boots up.
1
$ sudo systemctl enable grafana-server
  1. Go ahead and start Grafana.
1
$ sudo systemctl start grafana-server
  1. 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.
  2. Go ahead and login. The default user is admin, and the default password is admin.
  3. Assign a new password.
Assign a new password
  1. 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.
Grafana Welcome Screen
  1. Click on add data source.
  2. Select the Influxdb time-series database as shown below.
Add Data Source
  1. Fill in the URL and database settings as configured earlier. Refer to the images below to check the configuration settings.
Grafana Configuration Grafana Configuration

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.
Create New Grafana Dashboard
  1. Add new panel and configure the panel using the data of InfluxDB database.
Add new panel
  1. Select the measurement, which is the tank level. Here, we are leaving it as value.
Select Measurement
  1. Group by time_interval, order the time in ascending, and format it as a time series.
  2. Name the panel. We will use Plant Telemetry.
  3. Click on apply.
Click Apply
  1. Zoom into your trend.
Zoom Trend
  1. Choose to see data for the last 5 minutes, or the data from the last 15 minutes.
Data from 5 minutes
  1. 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.
Set Autorefresh to 5s Set Autorefresh to 5s

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.

Stay tuned with us as we cover a series of blog posts around real-world MQTT for Industry 4.0.

Portrait of Kudzai Manditereza

About Kudzai Manditereza

Kudzai is Technology Communicator and Founder at Industry40.tv. He is currently involved in four efforts: Industry4.0 Research, Educational Videos, Podcast Host, and IIoT Systems Integration. He has a background in Embedded Systems Design, Software Engineering, and Industrial Automation.

author Kudzai Manditereza

About Kudzai Manditereza

Kudzai is an experienced Technology Communicator and Electronic Engineer based in Germany. As a Developer Advocate at HiveMQ, his goals include creating compelling content to help developers and architects adopt MQTT and HiveMQ for their IIoT projects. In addition to his primary job functions, Kudzai runs a popular YouTube channel and Podcast where he teaches and talks about IIoT and Smart Manufacturing technologies. He has since been recognized as one of the Top 100 global influential personas talking about Industry 4.0 online.

Follow Kudzai on LinkedIn

mail icon Contact Kudzai
newer posts HiveMQ and Vodafone Making Our Roads Safer with 5G and MQTT-Powered V2X Services
HiveMQ Testcontainer 2.0.0 Released older posts