Hello everyone! Today we are going to create a “tech medley”โthe multi-protocol communication gateway + Siemens PLC industrial IoT data fusion system! ๐๐ค Imagine, old Modbus instruments, trendy OPC UA servers, and the factory’s traditional Profibus production line, all relying on a “protocol translator” gateway to let them communicate and share data in the cloud! ๐๐บ Sounds super cool, right? Donโt worry! Follow me from a “protocol novice” to a “data fusion master” as we learn while having fun! ๐ฎ๐
Application Overview
What is the biggest headache in factories? Itโs not the old equipment, but theincompatibility of protocols! ๐คฏ๐ก
- Production line A usesModbus RTU, dashboard B isOPC UA, and robot C is still shoutingProfibusโฆ Engineers are stuck with their “protocol conversion tables” every day! ๐๐ค
- The boss wants to analyze data in the cloud, but finds that data from different protocols is like “talking in different languages” and cannot be pieced together! ๐๐ฆ๐
This time we are going to create a “protocol fusion artifact”โ
- Multi-Protocol Communication Gateway: Like a “language master”, it can convert Modbus to OPC UA, Profibus to MQTT, and even customize protocols! ๐ฃ๏ธ๐
- Siemens PLC: Acts as a “data dispatcher”, packaging the data sent by the gateway into JSON and sending it directly to the cloud! ๐ฆ๐
- Cloud Platform: Use Power BI to create a “factory heat map” to clearly see where energy consumption is high and where faults occur! ๐๐
Goal:Increase data collection efficiency by 300%, reduce equipment fault response time to under 5 minutes, and make the boss smile when looking at reports! ๐๐ฆข Letโs go for it! ๐จ
Hardware Configuration
Let me present my “protocol fusion toolkit”:
- CPU 1515-2 PN: A PLC with two Profinet ports, one connected to the gateway and the other to the cloud! ๐ป๐
- SCALANCE XC208 Switch: Creates a “data highway” for the gateway, PLC, and cameras! ๐ฃ๏ธ๐ฅ๏ธ
- Multi-Protocol Communication Gateway (e.g., Moxa AWK-6222):
- Serial Port 1: Connects to Modbus RTU instruments (485 bus, supports 16 sensors) ๐ก๐ก๏ธ
- Serial Port 2: Connects to Profibus DP slave (the “mouthpiece” of the old robotic arm) ๐ค๐ฃ๏ธ
- Ethernet Port 1: Connects to OPC UA server (newly purchased smart meter) โก๐
- Wireless Module: Supports MQTT protocol, sending data directly to the cloud! ๐ก๐
- Sensors/Devices:
- Modbus RTU: Pressure sensors, flow meters (accuracy 0.1%) ๐ก๏ธ๐ง
- Profibus DP: Old robotic arm (speed, position data) ๐ค๐
- OPC UA: Smart meter (real-time power, energy consumption) โก๐
- Cloud Platform: Using AWS IoT Core + Power BI to create a “digital twin” display for the factory! ๐๐ผ๏ธ
(Just a secret: the gateway and PLC are hardwired with Profinet, with a latency of <10ms, much more stable than WiFi! ๐ถ๐ซ)
Program Design Approach
When designing the program, I sketched out a “four-step data fusion” process:1๏ธโฃ Protocol Translation Layer: The gateway “translates” data from Modbus/OPC UA/Profibus into a unified format and sends it to the PLC! ๐ฃ๏ธ๐ฆ2๏ธโฃ Data Cleaning Layer: The PLC filters out “dirty data” (like the occasional -9999 value from sensors) and fills in missing values! ๐งผ๐3๏ธโฃ Business Logic Layer: Automatically matches cloud API interfaces based on data types (temperature/power/position)! ๐ง ๐ง4๏ธโฃ Cloud Interaction Layer: The cloud sends commands (like “slow down the robotic arm”), and the PLC forwards them to the device via the gateway! ๐ก๐ค
(Imagine this: the gateway is the “translator”, the PLC is the “courier”, and the cloud is the “command center”, with data flowing like packages! ) ๐ฆ๐จ
Program Implementation
Variable Definitions
Letโs start with a wave of “variable definitions”:
// Protocol data (from the gateway)
VAR_INPUT
// Modbus RTU data (pressure, flow)
fPressure_Tank1 : REAL; // Pressure of tank 1 (MPa)
fFlow_Pipe2 : REAL; // Flow of pipe 2 (L/min)
bModbusError : BOOL; // Modbus communication error flag
// Profibus DP data (robot arm position/speed)
iArmPosition : INT; // Current position of the robotic arm (encoder value)
fArmSpeed : REAL; // Current speed of the robotic arm (mm/s)
bProfibusError : BOOL; // Profibus communication error flag
// OPC UA data (smart meter)
fPower_Total : REAL; // Total energy consumption (kWh)
fPower_Now : REAL; // Real-time power (kW)
bOPCUAError : BOOL; // OPC UA communication error flag
// MQTT commands (from the cloud)
sCmd_SetSpeed : STRING(20); // Speed command for the robotic arm from the cloud (e.g., "SET_SPEED:50")
bCmdReceived : BOOL; // Command received flag
END_VAR
// Output to devices (via gateway)
VAR_OUTPUT
// Robotic arm control commands
fCmdSpeed_Arm : REAL; // Target speed (mm/s)
bCmdStop_Arm : BOOL; // Emergency stop signal
// Alarm outputs
bAlarm_PressureHigh : BOOL; // High pressure alarm
bAlarm_PowerOverload : BOOL; // Overload alarm
bAlarm_CommLost : BOOL; // Communication loss alarm
END_VAR
// Internal variables (for processing intermediate data)
VAR
// Data buffer (to avoid frequent updates)
fLastPressure : REAL := 0.0;
fLastFlow : REAL := 0.0;
tLastUpdate : TIME := TIME_OF_DAY;
// Alarm thresholds
fPressureHighLimit : REAL := 1.5; // High pressure alarm threshold (MPa)
fPowerOverloadLimit : REAL := 100.0; // Overload alarm threshold (kW)
tCommTimeout : TIME := T#5S; // Communication timeout duration
// Cloud command parsing
sCmdPrefix : STRING(10) := 'SET_SPEED:'; // Command prefix
fTargetSpeed : REAL; // Parsed target speed
END_VAR
Main Program Implementation
The main program OB1 is the “data fusion commander”, and the code looks like this:
// 1. Initialization (on first run)
IF TIME_OF_DAY - tLastUpdate >= T#1S THEN
tLastUpdate := TIME_OF_DAY;
// 2. Data cleaning (filtering out abnormal values)
// Pressure sensor data cleaning
IF ABS(fPressure_Tank1 - fLastPressure) > 0.5 THEN // Jump threshold 0.5MPa
fPressure_Tank1 := fLastPressure; // Keep previous value
ELSE
fLastPressure := fPressure_Tank1; // Update previous value
END_IF;
// Flow sensor data cleaning (using sliding average)
fFlow_Pipe2 := (fFlow_Pipe2 + "DB_History".Flow_Last1 + "DB_History".Flow_Last2) / 3.0;
"DB_History".Flow_Last2 := "DB_History".Flow_Last1;
"DB_History".Flow_Last1 := fFlow_Pipe2;
// 3. Alarm logic (communication loss/high pressure/overload)
// Modbus communication loss alarm
IF bModbusError OR TIME_OF_DAY - "DB_CommTime".ModbusLastOK >= tCommTimeout THEN
bAlarm_CommLost := TRUE;
ELSE
bAlarm_CommLost := FALSE;
END_IF;
// High pressure alarm
IF fPressure_Tank1 >= fPressureHighLimit THEN
bAlarm_PressureHigh := TRUE;
fCmdSpeed_Arm := 0.0; // Emergency stop for the robotic arm
bCmdStop_Arm := TRUE;
ELSE
bAlarm_PressureHigh := FALSE;
bCmdStop_Arm := FALSE;
END_IF;
// Overload alarm
IF fPower_Now >= fPowerOverloadLimit THEN
bAlarm_PowerOverload := TRUE;
fCmdSpeed_Arm := 0.0; // Emergency stop for the robotic arm
ELSE
bAlarm_PowerOverload := FALSE;
END_IF;
// 4. Cloud command parsing (e.g., "SET_SPEED:50")
IF bCmdReceived THEN
IF sCmd_SetSpeed >= sCmdPrefix THEN
// Extract speed value (e.g., "SET_SPEED:50" -> 50)
fTargetSpeed := STR_TO_REAL(MID(sCmd_SetSpeed, LEN(sCmdPrefix)+1, 20));
fCmdSpeed_Arm := LIMIT(0.0, fTargetSpeed, 100.0); // Limit to 0-100mm/s
bCmdReceived := FALSE; // Reset flag
END_IF;
END_IF;
// 5. Output to devices (via gateway)
"DB_ToGateway".ArmSpeedCmd := fCmdSpeed_Arm;
"DB_ToGateway".ArmStopCmd := bCmdStop_Arm;
"DB_ToGateway".AlarmPressHigh := bAlarm_PressureHigh;
"DB_ToGateway".AlarmPowerOver := bAlarm_PowerOverload;
"DB_ToGateway".AlarmCommLost := bAlarm_CommLost;
// 6. Data packaging to the cloud (in JSON format)
"DB_ToCloud".JSON_Data := CONCAT(
'{"Pressure":', REAL_TO_STRING(fPressure_Tank1),
',"Flow":', REAL_TO_STRING(fFlow_Pipe2),
',"Power":', REAL_TO_STRING(fPower_Now),
',"ArmSpeed":', REAL_TO_STRING(fArmSpeed),
',"Timestamp":"', TIME_TO_STRING(TIME_OF_DAY),
'"}'
);
// 7. Update communication timestamp (for timeout judgment)
IF NOT bModbusError THEN
"DB_CommTime".ModbusLastOK := TIME_OF_DAY;
END_IF;
IF NOT bProfibusError THEN
"DB_CommTime".ProfibusLastOK := TIME_OF_DAY;
END_IF;
IF NOT bOPCUAError THEN
"DB_CommTime".OPCUALastOK := TIME_OF_DAY;
END_IF;
END_IF;
Template Function Blocks
This time I used three “protocol fusion artifact” function blocks:1๏ธโฃ Protocol Conversion Function Block (<span>FB_ProtocolTranslator</span>
) automatically identifies Modbus/OPC UA/Profibus data, for example:
// Read Modbus RTU data (register address 40001)
"FB_ProtocolTranslator"(
Protocol := MODBUS_RTU,
DeviceAddr := 1,
FuncCode := READ_HOLDING_REG,
StartAddr := 0,
Quantity := 2,
DataBuffer => "DB_Modbus".RawData,
Error => bModbusError
);
2๏ธโฃ Data Cleaning Function Block (<span>FB_DataCleaner</span>
) uses sliding average + jump suppression, for example:
// Clean flow data (window size 3)
"FB_DataCleaner"(
RawData := fFlow_Pipe2,
WindowSize := 3,
Threshold := 0.5,
CleanedData => fFlow_Pipe2_Cleaned
);
3๏ธโฃ Cloud Command Parsing Function Block (<span>FB_CloudCmdParser</span>
) automatically parses JSON/string commands, for example:
// Parse cloud command (e.g., "SET_SPEED:50")
"FB_CloudCmdParser"(
RawCmd := sCmd_SetSpeed,
Prefix := 'SET_SPEED:',
Value => fTargetSpeed,
Valid => bCmdValid
);
Function Extensions
To make the system smarter, I added these features:
- AI Predictive Maintenance: Train models using historical data to predict the lifespan of robotic arm bearings, reporting one month in advance! ๐ค๐
- Energy Consumption Optimization: Dynamically adjust device power based on order volume, for example, automatically “slacking off” to save energy when orders are low! ๐๐ก
- AR Remote Assistance: Workers wear AR glasses, and cloud experts can directly annotate fault points on the screen! ๐๐ง
Debugging Methods
During debugging, I almost had a “split personality”โprotocol incompatibility? Data garbled? Commands not executing? ๐คฏ๐ป
- Step 1: Use a “protocol tester” to test Modbus/OPC UA communication separately, for example, using Modbus Poll to simulate instruments sending data! ๐ก๐
- Step 2: Draw “data waveform charts” in the PLC to observe pressure/flow curves before and after cleaning, ensuring there are no “spikes”! ๐๐งผ
- Step 3: Use Postman to simulate cloud commands and see if the PLC correctly parses and forwards them to the robotic arm! ๐ฉ๐ค
(A painful lesson: the gateway’s IP address must not conflict with the PLC, or data will “get lost” in outer space! ๐๐ซ)
Application Extensions
This system can also be “transformed” for these scenarios:
- Smart Agriculture: Integrate data from Modbus weather stations, LoRa sensors, and OPC UA irrigation systems, making it possible to farm with a smartphone! ๐ฑ๐ฑ
- Smart Buildings: Integrate BACnet air conditioning, Modbus elevators, and OPC UA lighting systems, allowing buildings to “self-regulate temperature”! ๐ข๐ก๏ธ
- Smart Mining: Integrate ZigBee personnel positioning, CAN bus mining trucks, and OPC UA gas sensor data, maximizing safety! โ๏ธ๐
Troubleshooting
Let me share a real case:
- Phenomenon: The cloud suddenly stops receiving data from the Modbus instrument, but the PLC log shows “communication normal”.
- Investigation:
- Using Wireshark to capture packets, I found that data from the gateway to the PLC was normal, but packets from the PLC to the cloud were “mysteriously disappearing”.
- Checked firewall rules and found that the cloud IP was mistakenly blocked!
- After unblocking, data resumed, but I found that the gateway’s MQTT heartbeat interval was too long (60 seconds), changing it to 10 seconds completely resolved the issue.
Conclusion
This project made me deeply realize that:the multi-protocol communication gateway + PLC is not just “decorative”, but the “glue of the industrial IoT”! ๐งฒ๐ From protocol translation to data cleaning, from cloud interaction to self-healing faults, every step requires “attention to detail”, but seeing data from different protocols “dancing hand in hand” in the cloud brings an overwhelming sense of accomplishment! ๐
Finally, I want to tell my friends: **Data fusion is not a “high and mighty” technology; start with the entry-level case of converting Modbus to MQTT, master protocol conversion and command parsing, and then gradually expand to AI prediction and AR interaction, and you too can become an “industrial data translator”!** ๐๐ฌ
(See you next time! Donโt forget to like and bookmark, and Iโll unlock more “magic to make devices speak” for you!) ๐๐