Implementing MQTT Protocol in PLC: Designing a Lightweight Messaging System

Introduction

In industrial automation, communication between devices is one of the core issues. Traditional PLC communication often uses protocols like Modbus, but with the rise of the Internet of Things (IoT), MQTT (Message Queuing Telemetry Transport) has gradually become a popular choice in the industrial field due to its lightweight, efficient, and cross-platform characteristics. Today, we will learn how to implement MQTT communication in PLCs to build a lightweight messaging system, allowing PLCs to interact with other systems just like “devices that can send WeChat messages”!

1. What is the MQTT Protocol?

Basic Concepts

MQTT is a publish/subscribe model network communication protocol originally designed for device communication over low-bandwidth and unreliable networks. Its core features are lightweight and efficient, making it particularly suitable for low-resource scenarios such as industrial devices and sensors.

Simple Analogy: MQTT can be understood as a “WeChat messaging system”:

  • Broker (message broker server) is the “group chat”, responsible for forwarding messages.
  • Publisher and Subscriber are the “group members”; when one person sends a message, others receive it.
  • Topic (subject) is the “classification of discussions”; for example, messages discussing “temperature data” will be categorized under the “temperature” topic.

In PLCs, MQTT’s role is to publish device status (such as sensor data and alarm information) through specific topics while also subscribing to specific topics to receive instructions.

2. Implementing MQTT in PLCs

Hardware and Software Requirements

  1. PLC supporting MQTT protocol: Common brands like Siemens S7-1200, Mitsubishi FX5U, Omron NX1P, and other high-end PLCs mostly support MQTT. Some unsupported PLCs can achieve this through external gateways (like Raspberry Pi).

  2. Communication module: If the PLC does not have a built-in Ethernet interface, a communication module (such as Ethernet communication module or WiFi module) needs to be added.

  3. MQTT Broker: You can use open-source platforms (like Mosquitto) or cloud services (like Alibaba Cloud IoT, Tencent Cloud IoT).

  4. Programming environment: Use the programming tools provided by the PLC manufacturer (like TIA Portal, GX Works, etc.) to write programs and configure MQTT parameters.

Code Implementation Example

Taking Siemens S7-1200 as an example, use TIA Portal to configure MQTT.

Step 1: Configure PLC Communication

  1. Open TIA Portal, create a new project, and add the S7-1200 device.
  2. Ensure the PLC is connected to Ethernet and enable TCP/IP communication in the network settings.

Step 2: Install MQTT Library

  1. Download the MQTT function block (like MQTT_PubSub) from the Siemens official website or third-party libraries.
  2. Import the MQTT library into the project.

Step 3: Write the Program

Here is a simple MQTT publishing program:

 1// Function block call
 2// MQTT_PubSub is a function block that supports publishing and subscribing
 3
 4// Configure MQTT Broker parameters
 5VAR
 6    BrokerAddress : STRING[50] := '192.168.1.100'; // Broker IP address
 7    BrokerPort : INT := 1883;                      // MQTT default port
 8    Topic : STRING[50] := 'PLC/Temperature';       // Topic
 9    Payload : STRING[100];                         // Data payload
10    PublishEnable : BOOL := TRUE;                  // Publish enable signal
11END_VAR
12
13// Send temperature data
14Payload := '25.5'; // Assume the temperature is 25.5℃
15MQTT_PubSub(BrokerAddress, BrokerPort, Topic, Payload, PublishEnable);

Note: The names of MQTT function blocks may vary between different PLC brands; refer to the manufacturer’s documentation for specifics.

3. Practical Application Case

Case: Temperature Monitoring System

Assuming a factory has a temperature sensor connected to the PLC’s analog input channel, we want to send temperature data to the cloud in real-time via MQTT.

Hardware Connection

  1. The temperature sensor is connected to the PLC’s AI channel (such as 0-10V or 4-20mA).
  2. The PLC connects to the local area network where the MQTT Broker is located via Ethernet.

Program Logic

  1. Read the analog input value and convert it to the actual temperature value.
  2. Package the temperature value as a string and publish it to the MQTT topic<span>PLC/Temperature</span>.

Data Flow

  1. The PLC publishes the temperature data to the Broker via MQTT.
  2. The cloud application (like HMI or database) subscribes to the<span>PLC/Temperature</span> topic to receive data in real-time.

4. Common Issues and Solutions

Issue 1: Unable to Connect to Broker

  • Check if the PLC’s network configuration is correct (such as IP address, subnet mask).
  • Ensure the Broker is running properly and allows access from the PLC’s IP.

Issue 2: Data Delay or Loss

  • Check network quality to ensure latency and packet loss rates are within acceptable limits.
  • Adjust the MQTT QoS (Quality of Service) level; it is recommended to use QoS 1 (at least once delivery).

Issue 3: Confusing Topics

  • Use clear topic naming conventions, such as<span>DeviceName/DataType</span>, to avoid conflicts.

Notes:

  • Security: In industrial environments, it is recommended to enable encryption (like TLS/SSL) to protect data transmission.
  • Performance: Low-performance PLCs may not handle high-frequency data publishing; program logic may need optimization.

5. Practical Suggestions

  1. Select an Appropriate MQTT Broker: Beginners can use the open-source Mosquitto or try cloud platform services (like Alibaba Cloud IoT).

  2. Use Network Debugging Tools: It is recommended to use MQTT.fx or Node-RED to test the PLC’s publishing and subscribing functions.

  3. Optimize PLC Logic: Use timers to control the data publishing frequency to avoid performance issues from frequent data sending.

  4. Gradually Expand Applications: Once you master the basic publishing function, you can try the subscription function to achieve remote control or alarm push from the PLC.

Through this article, I believe you now have a preliminary understanding of implementing the MQTT protocol in PLCs. Hurry up and give it a try, and let your PLC “speak up”!

Leave a Comment