Multi-Protocol Communication Gateway and Siemens PLC for Industrial IoT Data Fusion

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 &gt;= T#1S THEN
    tLastUpdate := TIME_OF_DAY;

    // 2. Data cleaning (filtering out abnormal values)
    // Pressure sensor data cleaning
    IF ABS(fPressure_Tank1 - fLastPressure) &gt; 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 &gt;= tCommTimeout THEN
        bAlarm_CommLost := TRUE;
    ELSE
        bAlarm_CommLost := FALSE;
    END_IF;

    // High pressure alarm
    IF fPressure_Tank1 &gt;= 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 &gt;= 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 &gt;= sCmdPrefix THEN
            // Extract speed value (e.g., "SET_SPEED:50" -&gt; 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 =&gt; "DB_Modbus".RawData,
    Error =&gt; 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 =&gt; 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 =&gt; fTargetSpeed,
    Valid =&gt; 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:
  1. 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”.
  2. Checked firewall rules and found that the cloud IP was mistakenly blocked!
  3. 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.
  • Result: The client now requires all gateways to be configured with “dual links” (MQTT + HTTP) and to include a watchdog program! ๐Ÿถ๐Ÿ”„
  • 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!) ๐Ÿ˜˜๐Ÿ‘‹

    Leave a Comment