Free for commercial use! A low-code visual development platform for industrial web with over 1500 components.
Bookmark!!! Is this all about PLC communication?
Industrial islands? Connect Modbus and MQTT in one minute.
With the rapid development of the Industrial Internet of Things (IoT), real-time data communication between devices has become particularly important. MQTT, as a lightweight and efficient IoT communication protocol, is widely used in industrial IoT. The Siemens S7-1200 series PLC, known for its cost-effectiveness and powerful features, has significant practical value when combined with the MQTT protocol for data publishing. This article will detail how to implement data publishing to MQTT using the Siemens S7-1200 PLC, helping you quickly get started with this technology.

Basics of Implementing Siemens S7-1200 PLC with MQTT
The Siemens S7-1200 PLC does not directly support the MQTT protocol, but it can be implemented through the following methods:
- Using CP communication modulesIf your S7-1200 PLC is equipped with a CP communication module, such as CP1242-7, you can customize programming to send data using the MQTT protocol.
- Using third-party gateway devicesOutput data from the Siemens S7-1200 via Modbus or other protocols to a gateway device that supports MQTT, which then converts the data to MQTT protocol and publishes it. For example, we can use industrial routers (like MOXA or Ethernet to MQTT modules) to bridge protocols.
- Directly implementing MQTT protocol based on Tia Portal’s TCP/UDPAlthough Tia Portal does not have a direct MQTT implementation, you can simulate MQTT client behavior through PLC programming, including connecting to an MQTT Broker, assembling data, and publishing.
Steps to Implement MQTT Communication Using Tia Portal

The following are the key steps to publish MQTT data using Tia Portal:
-
Prepare the environment:
-
Tia Portal programming environment (V15 or higher).
-
Siemens S7-1200 PLC hardware.
-
An accessible MQTT Broker server (e.g., HiveMQ, Mosquitto, or a self-hosted server).

-
Define MQTT function blocks or use Libraries:If you prefer modular programming, you can use third-party developed function block libraries, such as Siemens’ dedicated “MQTT library”. Alternatively, you can write your own socket communication code based on PLC high-level languages (like SCL) to handle MQTT protocol logic.

-
Establish Socket connection:Use the socket interface in the PLC to connect to your MQTT Broker, ensuring that TCP/IP parameters, including server address and port, are correctly configured.

-
Construct MQTT data packets:MQTT sends data in a specific protocol format. When constructing packets, ensure the following items:
You can define strings or byte arrays in your program to encode and decode these packets.
- Connect packet (CONNECT): includes username, password (if needed), and clear session identifier, etc.
- Publish packet (PUBLISH): includes topic, QoS level, and payload data (the actual data to be published).
-
Testing and debugging:Use tools like MQTT Explorer or MQTT.fx to subscribe to test topics and verify whether the messages published by the PLC are received correctly.
Example Case Illustration
Suppose we need to push temperature data from the production line to a remote server via MQTT, here is a brief process of implementation:
- Connection configuration: Set the IP of the S7-1200 PLC and ensure connectivity with the Broker.
- Construct publish content: Package the production line temperature data in JSON format, such as
<span>{ "temperature": 35.5 }</span>. - Publish test: Run the program and use tools to check if the data is successfully sent.
Considerations
- Choosing QoS levels:MQTT supports three levels of Quality of Service (QoS Level 0, 1, 2). Generally, QoS 1 is sufficient for industrial data communication needs.
- Network security issues:It is recommended to use TLS encryption in data communication to avoid sensitive information leakage.
- Performance optimization:When publishing large amounts of data, avoid frequent sending, set appropriate sending intervals to reduce PLC load.
By combining the Siemens S7-1200 PLC with the MQTT communication protocol, efficient industrial data collection and publishing can be achieved, thereby promoting the implementation of intelligent manufacturing. In practical applications, different scenarios can choose direct, indirect, or library function implementation methods, which not only expand the PLC’s functionality but also showcase the vast application prospects of the Industrial Internet of Things.
Recent hot articles:
| One Ethernet cable connects the entire factory! Why is it so powerful?Incredible! Supports both Ethernet and serial ports, domestic open-source Modbus project.Stop messing around! Understand PLC inverter communication in 5 minutes.Regret not reading this earlier! This is the key to learning PLC.You can’t write the PLC program all at once! Is it that hard?10 years in industrial control! Still unclear about protocols and interfaces??? |