Siemens PLC and MQTT Protocol: IoT Application Examples

Hello everyone! Today, let’s talk about how to connect Siemens PLCs in the workshop to the Internet of Things (IoT). The MQTT protocol (think of it as a “WeChat group” in the IoT world) is being used more and more in factories, and it’s essential to know how to work with it! After over a decade of experience, I’ve noticed that many factories are starting to shift towards this direction, with equipment networking, data clouding, and remote monitoring. If you don’t understand this, you’ll be at a loss when upgrading old equipment in the future. Follow me step by step, from wiring to debugging, and I’ll teach you how to turn your PLC into a communicative network device!

△ Basic Knowledge of MQTT

MQTT is a lightweight communication protocol characterized by low bandwidth usage and high stability. Imagine it as a broadcasting system in a factory; anyone who subscribes can receive messages. The Siemens S7-1200/1500 series natively supports MQTT from firmware version 4.0 onwards, while older S7-300/400 models require communication modules or gateway devices.

Basic concepts:

  • Broker (a relay station that forwards all messages)
  • Topic (the communication channel)
  • Publisher/Subscriber (one sends messages, the other listens)
// Basic code for configuring MQTT connection on S7-1200
DATA_BLOCK "MQTT_DATA"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
NON_RETAIN
   VAR 
      broker_ip : String := '192.168.1.100'; // MQTT server address
      port : UInt := 1883;  // Port number
      client_id : String := 'S7_PLC_001';  // Client ID
   END_VAR
BEGIN
END_DATA_BLOCK

This piece of code is actually defining the basic parameters for the MQTT connection, telling the PLC which server to connect to and what name to use.

On-site Tip: When setting the client_id, it’s best to include the device location or number, such as “S7_2F_001” (indicating machine 001 on the second floor). During a project at a food factory, over 20 PLCs used the same ID, resulting in frequent disconnections, and it took me half a day to troubleshoot!

△ Practical Connection and Testing

Attention! First, prepare an MQTT server. For testing, you can use a free public broker like test.mosquitto.org, but for actual projects, it’s recommended to set up a local server (I usually use Mosquitto, which is lightweight and sufficient).

Steps to connect Siemens PLC to MQTT:

  1. Ensure the PLC can access the internet (network port is connected, IP is configured)
  2. Create an MQTT instruction block in TIA Portal
  3. Configure connection parameters and publish/subscribe topics
  4. Download the program and monitor the connection status

Warning ⚠️: The MQTT functionality of Siemens PLC requires the TCON_IP_v4 structure, which is not as simple as using strings in Mitsubishi. If any parameter is configured incorrectly, it won’t connect, especially the connection_type parameter, which must be set to 16#0C!

// Publish temperature data to MQTT
"MQTT_CLIENT_DB".REQ := TRUE;
"MQTT_CLIENT_DB".TOPIC := 'factory/line1/temp';
"MQTT_CLIENT_DB".DATA := STRING_TO_VARIANT(REAL_TO_STRING(#temperature));
"MQTT_PUB_DB"(REQ := "MQTT_CLIENT_DB".REQ,
              TOPIC := "MQTT_CLIENT_DB".TOPIC,
              DATA := "MQTT_CLIENT_DB".DATA);

This piece of code is actually packaging the temperature data to send to the MQTT server, equivalent to sending a message to the topic “factory/line1/temp”.

Your mobile app, industrial touch screen, or even an Excel spreadsheet can subscribe to this message for remote monitoring. Let me tell you, during a project at an injection molding factory, the owner could see the production status of each machine from abroad using his phone, and he was thrilled!

On-site Tip: The simplest way to test the MQTT connection is to install an “MQTT client” app on your phone, subscribe to the topic published by the PLC, and if you receive data, it proves the connection is successful. I often use MQTT.fx, which has an intuitive and user-friendly interface.

△ Complete Mini Project: Temperature and Humidity Monitoring System

Let’s create a simple project: use S7-1200 to read temperature and humidity sensors, publish data via MQTT, and create a simple monitoring interface with Node-RED.

Hardware preparation:

  • S7-1200 CPU (1214C is sufficient)
  • DHT11/DHT22 temperature and humidity sensor (connected to analog input)
  • Network cable and switch

First step, wiring the sensor: DHT11 output → PLC’s AI_0, remember to share the ground!

Second step, PLC reads the sensor:

// Analog value reading conversion
"Temperature" := NORM_X(MIN := 0.0, MAX := 27648.0, VAL := "IW64") * 100.0 / 27648.0;
"Humidity" := NORM_X(MIN := 0.0, MAX := 27648.0, VAL := "IW66") * 100.0 / 27648.0;

This piece of code is actually converting the analog input values (0-27648) into actual temperature and humidity values (0-100) proportionally.

Third step, write the MQTT publishing program to send data every 30 seconds:

// Timed trigger
#timer(IN := NOT #timer.Q, PT := T#30S);
IF #timer.Q THEN
   // Publish temperature data
   #mqtt_temp_publish(
      topic := 'plc/sensor/temperature',
      payload := REAL_TO_STRING("Temperature"),
      retain := FALSE,
      qos := 0
   );
   // Publish humidity data
   #mqtt_humi_publish(
      topic := 'plc/sensor/humidity',
      payload := REAL_TO_STRING("Humidity"),
      retain := FALSE,
      qos := 0
   );
END_IF;

This piece of code is actually publishing the temperature and humidity data every 30 seconds. Setting QoS to 0 is the most bandwidth-efficient way, as it does not care whether the message is delivered (just like sending a WeChat message without checking the read receipt).

On-site Tip: The data sending frequency should be determined based on the application scenario. For slow-changing data like temperature and humidity, once every 30 seconds is sufficient, while production line status may require once every second. I remember once in a pharmaceutical cold storage, even sending every five minutes was considered too frequent, as the owner said the electricity cost was high!

If the data is received but all shows as 0, it’s likely that the analog scaling is incorrect. Honestly, when I first learned this, I mistakenly divided the 0-10V signal directly by 0-27648, resulting in the temperature showing 0.xxx, and I got scolded by my supervisor!

Practice question: Try adding an alarm function that sends a message “Temperature too high” to the “plc/alarm” topic when the temperature exceeds 35 degrees.

Remember, the advantage of the MQTT protocol is that once you write the program, all the data in the factory flows like water through a pipeline. To connect new monitoring devices, just subscribe to the corresponding topic without modifying the PLC program. One of my apprentices, Xiao Li, managed over 30 remote monitoring devices in a battery factory by himself, and his monthly salary skyrocketed!

Leave a Comment