Integrating Modbus with Modern Systems: OPC UA and MQTT
Imagine a world where ancient Latin-speaking scholars need to collaborate with modern English-speaking scientists. They’d need translators to bridge the language gap. Similarly, Modbus—an ancient industrial protocol—needs translators to communicate with modern systems. That’s where OPC UA and MQTT come in.
In this article, we’ll explore how to bridge Modbus with these modern protocols. We’ll examine architectural patterns, discuss when to use each approach, and provide configuration examples for Node-RED and Ignition.
Why Bridge Modbus with Modern Protocols?
Modbus has been the workhorse of industrial communication for over 40 years, but it faces limitations in today’s connected world:
- Data Context: Limited ability to include metadata with sensor readings
Modern protocols like OPC UA and MQTT address these limitations, making them ideal companions for Modbus.
Understanding Modern Industrial Protocols
Before diving into integration, let’s briefly understand the modern protocols we’ll be working with:
OPC UA (Open Platform Communications Unified Architecture)
What It Is: A platform-independent, secure, and reliable protocol for industrial communication
Key Benefits:
- Standardized: International standard (IEC 62541)
MQTT (Message Queuing Telemetry Transport)
What It Is: A lightweight, publish-subscribe messaging protocol for IoT devices
Key Benefits:
- Cloud-Friendly: Perfect for connecting industrial devices to cloud platforms
Architectural Patterns for Modbus Integration
Pattern 1: Modbus-to-OPC UA Gateway
How It Works: A gateway device or software acts as a translator between Modbus devices and OPC UA clients
Architecture:
“`
[Modbus Devices] ←RTU/TCP→ [Modbus-to-OPC UA Gateway] ←OPC UA→ [OPC UA Clients]
|
↓
[SCADA/HMI Systems]
[Enterprise Systems]
“`
Key Components:
- Security Layer: Implements OPC UA security features
When to Use This Pattern:
- You’re building a scalable industrial IoT architecture
Pattern 2: Modbus-to-MQTT Bridge
How It Works: A bridge device or software translates between Modbus and MQTT protocols
Architecture:
“`
[Modbus Devices] ←RTU/TCP→ [Modbus-to-MQTT Bridge] ←MQTT→ [MQTT Broker]
|
↓
[MQTT Clients]
[Cloud Platforms]
[Analytics Systems]
“`
Key Components:
- Configuration Layer: Defines which Modbus registers map to which MQTT topics
When to Use This Pattern:
- You’re building an IoT solution with edge computing
Comparison: OPC UA vs MQTT for Modbus Integration
| Feature | OPC UA | MQTT |
|————-|————|———-|
| Protocol Type | Request-response + publish-subscribe | Publish-subscribe |
| Complexity | More complex, feature-rich | Simple, lightweight |
| Security | Built-in encryption, authentication, authorization | Requires TLS for encryption, external authentication |
| Data Types | Complex data types with metadata | Primarily simple data types, metadata via JSON |
| Scalability | Enterprise-scale, up to millions of nodes | IoT-scale, up to millions of devices |
| Bandwidth Usage | Higher | Lower |
| Latency | Low | Very low |
| Use Case | SCADA, enterprise integration | IoT, cloud connectivity, edge computing |
| Standards | IEC 62541 international standard | OASIS standard |
Implementation Example 1: Modbus-to-OPC UA with Ignition
Ignition is a popular SCADA platform that makes it easy to create Modbus-to-OPC UA gateways.
Step 1: Install Ignition
1. Download Ignition from [Inductive Automation](https://inductiveautomation.com/downloads/ignition)
2. Install and run Ignition on a server
3. Access the Ignition Gateway at `http://your-server-ip:8088`
Step 2: Configure Modbus Device
1. Log in to the Ignition Gateway
2. Go to Config > OPC UA > Device Connections
3. Click Create new Device
4. Select Modbus TCP/IP from the device list
5. Configure the Modbus device:
– Name: `MyModbusDevice`
– Hostname: `192.168.1.100` (your Modbus device IP)
– Port: `502`
– Unit ID: `1`
6. Click Next > Create New Tag Provider
7. Name the tag provider `ModbusTags` and click Create
8. Click Save Changes
Step 3: Create Modbus Tags
1. Go to Config > Tags > Tag Browser
2. Select the `ModbusTags` provider
3. Click Create new Tag > Device Tag
4. Configure a tag for a holding register:
– Name: `Temperature`
– Device: `MyModbusDevice`
– Register Type: `Holding Register`
– Register Address: `0`
– Data Type: `Float4 Byte Swap`
– Scaling: Configure if needed (e.g., 0-65535 to 0-100°C)
5. Click Save Changes
6. Create additional tags as needed
Step 4: Access Tags via OPC UA
Ignition automatically exposes all tags as OPC UA nodes. To access them:
1. Your OPC UA clients can connect to Ignition’s OPC UA server at `opc.tcp://your-server-ip:4096`
2. The Modbus tags will be available under `ns=1;s=[ModbusTags]Temperature`
3. Configure security settings in Config > OPC UA > Server Settings if needed
Implementation Example 2: Modbus-to-MQTT with Node-RED
Node-RED is a visual programming tool that excels at creating protocol bridges.
Step 1: Install Node-RED and Required Nodes
1. Install Node-RED: `npm install -g node-red`
2. Start Node-RED: `node-red`
3. Access Node-RED at `http://localhost:1880`
4. Install required nodes:
– Click the menu (☰) > Manage palette > Install
– Search for and install: `node-red-contrib-modbus` and `node-red-dashboard`
– MQTT nodes are included by default
Step 2: Create a Modbus-to-MQTT Bridge Flow
1. Drag and drop the following nodes onto the workspace:
– `inject` (input palette) – to trigger periodic reads
– `modbus-read` (modbus palette) – to read Modbus registers
– `function` (function palette) – to format data for MQTT
– `mqtt out` (output palette) – to publish to MQTT
– `mqtt in` (input palette) – to subscribe to MQTT commands
– `function` (function palette) – to parse MQTT commands
– `modbus-write` (modbus palette) – to write to Modbus registers
– `debug` (output palette) – to debug
2. Connect the nodes as follows:
“`
inject → modbus-read → function → mqtt out
mqtt in → function → modbus-write
“`
3. Configure each node:
Inject Node:
– Set Repeat to “Interval”
– Set Interval to “10 seconds”
– Click Done
Modbus Read Node:
– Click the pencil icon to add a Modbus server
– Configure the server:
– Name: `MyModbusServer`
– Type: `TCP`
– Host: `192.168.1.100`
– Port: `502`
– Click Add
– Configure the read operation:
– Unit ID: `1`
– FC: `03 (Read Holding Registers)`
– Address: `0`
– Quantity: `2`
– Click Done
Function Node (Format for MQTT):
– Double-click to open
– Name it “Format MQTT Data”
– Add the following code:
“`javascript
// Extract temperature and pressure from Modbus response
const temperature = msg.payload.buffer.readFloatBE(0); // Assuming Float4 Big Endian
const pressure = msg.payload.buffer.readFloatBE(4); // Next register
// Create JSON payload with metadata
msg.payload = {
temperature: {
value: temperature,
unit: “°C”,
timestamp: new Date().toISOString()
},
pressure: {
value: pressure,
unit: “kPa”,
timestamp: new Date().toISOString()
}
};
// Set MQTT topic
msg.topic = “modbus/mydevice/sensors”;
return msg;
“`
– Click Done
MQTT Out Node:
– Click the pencil icon to add an MQTT broker
– Configure the broker:
– Name: `MyMQTTBroker`
– Server: `mqtt.eclipseprojects.io` (public test broker) or your own broker
– Port: `1883` (or `8883` for TLS)
– Click Add
– Set Topic to `modbus/mydevice/sensors` (will be overridden by function)
– Click Done
MQTT In Node:
– Set Server to `MyMQTTBroker`
– Set Topic to `modbus/mydevice/commands`
– Set QoS to `2`
– Click Done
Function Node (Parse MQTT Commands):
– Double-click to open
– Name it “Parse MQTT Commands”
– Add the following code:
“`javascript
// Parse JSON command
const command = msg.payload;
if (command.register === “setpoint” && command.value !== undefined) {
// Convert value to buffer for Modbus write
const buffer = Buffer.alloc(2);
buffer.writeInt16BE(Math.round(command.value * 10), 0); // Scale to integer if needed
msg.payload = {
value: buffer,
unitid: 1,
fc: 6, // Write Single Register
address: 10 // Register address for setpoint
};
return msg;
}
// Filter out invalid commands
return null;
“`
– Click Done
Modbus Write Node:
– Set Server to `MyModbusServer`
– Click Done
4. Deploy the flow by clicking the “Deploy” button
Step 3: Test the Bridge
1. Monitor Data Flow:
– Click the bug icon on the debug node to enable debugging
– Watch the debug panel for Modbus data and MQTT messages
2. Test MQTT Publication:
– Use an MQTT client (like MQTT Explorer) to subscribe to `modbus/mydevice/sensors`
– You should see sensor data published every 10 seconds
3. Test MQTT Commands:
– Publish a command to `modbus/mydevice/commands`:
“`json
{“register”: “setpoint”, “value”: 75.5}
“`
– The command should be sent to the Modbus device’s register 10
Real-World Use Case: Smart Factory Integration
Let’s look at how these integration patterns work in a real smart factory scenario:
Scenario: A manufacturing plant with legacy Modbus PLCs, temperature sensors, and motor drives needs to integrate with a cloud-based analytics platform and a local SCADA system.
Architecture:
1. Modbus-to-OPC UA Gateway: Connects Modbus devices to the local Ignition SCADA system for real-time monitoring and control
2. Modbus-to-MQTT Bridge: Sends selected sensor data to AWS IoT Core for cloud-based analytics and predictive maintenance
3. Edge Computing: Node-RED running on an edge server filters and processes data before sending to the cloud
4. Bidirectional Communication: MQTT commands from cloud analytics can adjust PLC setpoints via the bridge
Benefits:
- Future-proofed integration with modern protocols
Best Practices for Modbus Integration
1. Start with a Clear Architecture: Define your integration goals and choose the right protocol for each use case
2. Implement Security: Use TLS for MQTT, enable OPC UA security features, and segment networks
3. Add Metadata: Include device information, timestamps, and units with sensor data
4. Implement Data Filtering: Only send necessary data to reduce bandwidth and storage costs
5. Add Error Handling: Implement retries, timeouts, and error notifications
6. Monitor Performance: Track latency, packet loss, and device availability
7. Document Everything: Document device addresses, MQTT topics, and OPC UA node IDs
8. Test Thoroughly: Test with real devices in a lab environment before deployment
9. Plan for Scalability: Design your architecture to handle more devices and data in the future
10. Consider Edge Computing: Process data at the edge to reduce latency and bandwidth usage
Future Trends in Modbus Integration
- 5G Connectivity: Leveraging 5G for high-bandwidth, low-latency Modbus-to-cloud integration
Conclusion
Bridging Modbus with modern protocols like OPC UA and MQTT unlocks new possibilities for industrial systems. OPC UA excels at enterprise integration and SCADA systems, while MQTT shines for IoT and cloud connectivity.
By following the architectural patterns and implementation examples provided, you can create robust, secure, and scalable integrations that extend the life of your Modbus devices while leveraging the benefits of modern industrial communication.
Remember: the best integration solution depends on your specific requirements. Evaluate your use case, consider the trade-offs, and choose the approach that best meets your needs. With the right architecture, you can create a future-proof industrial communication system that connects legacy Modbus devices with modern applications.