Technical Article

Not Just Another IIoT Article: MQTT for Pneumatic Cylinder Maintenance

January 24, 2023 by Shawn Dietrich

IIoT is a daunting topic due to ambiguous interactions of hardware and software. This article outlines the basic steps to use an MQTT broker to collect the number of cycles from a pneumatic cylinder for PM software.

Pneumatic cylinder motion is a common subject of monitoring and data processing to ensure long equipment life.
Figure 1. Pneumatic cylinder motion is a common subject of monitoring and data processing to ensure long equipment life. Image used courtesy of Festo


Data Collection and IIoT

There are many aspects to the industrial internet of things (IIoT), from digital twinning to artificial intelligence (AI) to predictive maintenance, all of which require data collection of a manufacturing process. This data might be the number of cycles from a pneumatic cylinder or the max torque from a servo motor. In order for predictive maintenance software or AI software to use this data, it must be stored in a place where the software can access it locally or remotely. 

In this article, I will outline just one of many possible ways to collect this data to access it securely and quickly from anywhere in the world.


MQTT Protocol

Message Queueing Telemetry Transport (MQTT) has become the standard for IIoT communication protocol. This protocol is a lightweight publish and subscribe communication technology that allows sensors, controllers, processes, or servo drives to push data from the factory floor to a broker. Clients can then subscribe to these brokers and collect the published data. A client can be a website, software, or even an algorithm on a server. The broker can be local to the company network or reside on a remote server and be accessed over the internet.


MQTT Devices

Some automation devices today are being built with the ability to publish to an MQTT broker without going through a PLC, IoT gateway, or DAQ (data accusation) controller; however, if you are dealing with a brownfield system, you will probably not have the luxury of dealing with these devices. There are devices on the market that have analog I/O and digital I/O that will publish to a broker without the need for a PLCt, sometimes referred to as IoT gateways. 

Some PLCs today have built-in MQTT ability which makes this whole process easier, such as B&R and Beckhoff PLCs. Another option is using open platform communication (OPC) software that also has an MQTT option. Kepware makes use of the OPC protocol to access tags or variables within a PLC program. The MQTT option for Kepware then allows the accessed tags to publish their data to an MQTT broker. Whichever method or device you use, the name of the game is to gather the data and publish it to an MQTT broker. 


MQTT Broker

If you do a search for MQTT broker on your favorite internet search engine, you will notice there are quite a few of them out there, and choosing the right one for your project will require an in-depth understanding of your project and how much data you want to publish. 

There are two basic types of brokers: local and remote. Remote brokers will charge you for the amount of data being passed through the broker whereas the local ones may only charge a one-time fee. There are free brokers, both local and remote, that can be used to debug and develop your code. The security on the free brokers may not be as advanced, so using them in a production environment may not be advisable. 

Setting up the communication between the IoT device and the broker will vary with the selected device, so a specific step-by-step explanation is difficult. If you are using an IoT gateway or an IoT-ready PLC, there should be some function blocks, configuration wizards, or help files to assist you. 

Typically your setup will require a URL for the broker, usually a username and password, along with the topic name field to which you would like to publish. Quality of service will also need to be configured, QoS is used to determine how the message will be delivered. The QoS is a three-number variable 0, 1, 2 with the following meanings:

  • 0 - At most once

  • 1 - At least once

  • 2 - Exactly once 


MQTT: the connection between the physical and digital world.

Figure 2. MQTT: the connection between the physical and digital world. Image used courtesy of John Schnobrich


Factory Maintenance Application

Maintenance of production equipment requires downtime of the equipment, and if not done in a proactive manner, can result in damage to neighboring equipment. For these reasons, and many more, a very popular use for industrial IoT is to collect data for predictive maintenance software. 

Data, such as pneumatic cylinder cycles, will help the maintenance staff determine when a cylinder will need to be rebuilt. We can collect the number of cycles in the PLC, then, using Kepware, we can capture the value of that tag and publish it to our MQTT broker so that our maintenance software will have access to the data.


PLC Configuration

Nearly every pneumatic cylinder should have a retract sensor, so to capture a full cycle of the cylinder, we can simply use a counter on the retract sensor input. We will set the preset value to 100 and increment a variable every time the counter finishes and reset the counter. 


Path of bi-directional data flow from physical (at the bottom of the image) to cloud connection (top).

Figure 3. Path of bi-directional data flow from physical (at the bottom of the image) to cloud connection (top). Image used courtesy of Kepware


Kepware Configuration

First, we must set up the PLC and add the tags as per the instructions for your controller type, although each PLC is slightly different. 

There are two ways to add the MQTT function to Kepware. One is using the MQTT client driver and the other is using the IoT Gateway option

For this example, I’m going to use the IoT Gateway because I just want to publish data and not subscribe. Configure the IoT Gateway port and security certificate in the Kepware settings at the time of installation. The certificate requirement will depend on which broker you are using, for example, AWS allows you to download the certificate that can then be installed in Kepware—Hivemq public broker doesn’t require a certificate. 

Create a new agent: within the agent configuration, you will be able to enter the broker URL, username, password, topic, and QoS settings. The message section will allow you to format the message that will be sent to the broker. Kepware uses standard JSON formatting for MQTT messages—one of two templates can be selected depending on the complexity of the message you are sending. 

Once the agent is configured, you can then add the tag that was configured earlier. A wizard will open and allow you to select pre-configured tags. The published rate is important, if you publish too often, you might overload the broker or your PLC. It is best practice to only publish when the data changes. Kepware will take care of the connection message and disconnection messages when needed. 


Publishing is the act of sending a piece or pieces of data to a specific topic at the MQTT broker.

Figure 4. Publishing is the act of sending a piece or pieces of data to a specific topic at the MQTT broker. Image used courtesy of Kepware


Predictive Maintenance Software Configuration

There are many different predictive maintenance software options on the market. With MQTT being a standard protocol, you can rest assured that you will have a similar configuration procedure as with Kepware. 

Configure your maintenance software using the same broker URL, username, password, topic, and Qos as was used with the Kepware configuration. If you have an option for scaling cylinder cycles, you can enter that as 100 to 1 cycles. The software should now be able to subscribe to the topic configured. 

You can add more than one tag to a topic, but if your maintenance software can’t parse the JSON message, you will have to create a new topic for each device.



Using the MQTT protocol to send factory data around the world is not overly complicated and can be a very viable resource when it comes to predictive maintenance. There are many different options, hardware, and software solutions to assist you with your IIoT project, and this article is meant as an outline of one way to collect data on a pneumatic cylinder.