Bridging OPC UA Data to MQTT with Node-RED
Connecting OPC UA Data Streams to MQTT Brokers for Enhanced IoT Communication and Monitoring
Have you ever found yourself trying to connect old industrial systems with new IoT tools? This is a common scenario when trying to digitally transform while setting up your Unified Name Space. Maybe you have machinery that uses OPC UA, but your data is sent through MQTT. How do you make these systems work together smoothly?
In this guide, we'll demonstrate how to use Node-RED to bridge OPC UA data to MQTT. This integration will streamline your data flow and enhance real-time monitoring, helping you modernize your setup and improve communication between systems.
# Why Bridge OPC UA to MQTT
Diagram showing the data flow when bridging OPC UA to MQTT to enable communication between non-OPC UA compatible systems and devices.
In modern industrial environments, integrating systems with different communication protocols can be a significant challenge. For example, a CNC machine on the factory floor might use OPC UA, while some cloud solutions, edge devices, and other systems, such as custom ERP solutions and IoT applications, might rely on MQTT protocol. This is where bridging OPC UA to MQTT becomes highly beneficial.
By converting OPC UA data into MQTT messages, you make the data from the CNC machine accessible to a broader range of systems that use MQTT, which is a more universally supported messaging protocol. This bridging solution simplifies the integration process, allowing diverse systems to communicate effectively without needing direct OPC UA support.
Node-RED is perfect for this job. It can connect both OPC UA and MQTT, making it easy to transform and route data between different systems. Its flexibility and support for many protocols make it great for integrating various industrial hardware and software. For more on how Node-RED can improve industrial operations, check out Building on FlowFuse: Remote Device Monitoring.
# Bridging OPC UA Data to MQTT with Node-RED
In this section, I'll demonstrate how to bridge OPC UA data to MQTT using Node-RED. We will use simulated OPC UA server data from a CNC machine as an example. The goal is to show how you can efficiently transfer this data to an MQTT broker, making it accessible to various applications and systems.
# Prerequisite
-
OPC UA Server: Make sure you have an OPC UA server configured and running with the necessary data. For this blog, we'll use the Prosys OPC UA Simulation Server, which simulates data from CNC machines designed for testing OPC UA client applications and learning the technology. You can download it from here.
-
FlowFuse Account: A FlowFuse account lets you quickly create, deploy, and manage Node-RED instances in the cloud. sign up now.
-
node-red-contrib-opcua: install the node-red contrib package that will enable integration of opcua in Node-RED.
-
MQTT Broker: We’ll need an MQTT broker for data communication. FlowFuse offers an integrated MQTT Broker Service within Platform for easy setup. For more details, check out FlowFuse's MQTT Broker Announcement.
# Retrieving Data from the OPC UA Server
To begin retrieving data from your OPC UA server using Node-RED, follow these steps:
- Drag the inject node onto the canvas.
- Drag the change node onto the canvas and double-click on the node to open its configuration settings. Set the
msg.topic
to the node ID and datatype of the property you wish to read.
(Left) Image of the Change node setting the 'msg.topic' to retrieve the cycle time data and (Right) the OPC UA Prosys interface.
- Drag the OpcUa-Client node onto the canvas. Double-click on it to open its configuration settings. Click the "+" icon next to the Endpoint field and enter the URL of your running OPC UA server. Configure the security policy and mode according to your server setup. If you use the Prosys OPC UA Simulation Server and have not enabled any security features, you can leave the security policy and mode as "None."
Configuring opc-ua node with the opc ua server endpoint
- In the OpcUa-Client node settings, select the action type as "READ." This instructs Node-RED to read data from the OPC UA server.
Configuring OpcUa-Client node to select the read operation
- If your OPC UA server uses security features, specify the path to your certificate files in the relevant fields. If no security is configured, this step can be skipped.
- Drag the debug node onto the canvas. The output will help you verify the data retrieved from the OPC UA server.
- Connect the output of the inject node to the input of the change node and the output of the change node to the input of the OpcUa-Client node. Then, connect the output of the OpcUa-Client node to the input of the debug node. This setup ensures that when the inject node triggers, it sends data to the OpcUa-Client node, and the results are displayed in the Debug node.
- Deploy the flow by clicking the "Deploy" button in the top right corner. To test the setup, press the Inject button.
You can follow the same steps to retrieve other property values from the OPC UA server. In this example, we are retrieving four simulated data properties: the cycle time, temperature, and spindle speed of the simulated CNC machine. Your setup might differ depending on the properties and data available on your OPC UA server.
# Transforming and Aggregating Data
Once you have successfully retrieved data from your OPC UA server, the next step is to transform and aggregate this data to make it suitable for publishing to an MQTT broker. This demonstration, we will aggregate the retrieved individual property values into a single object. Depending on your specific needs, you might choose to split the object properties and send them separately or perform various calculations and transformations on the data.
- Drag the change node onto the canvas.
- Double-click on the node and set
msg.topic
to the name of the property you want to set for the retrieved data. In this context, setmsg.topic
to'cycle-time'
, which will be the key in the object that we will create.
Setting the msg.topic with the change node to retrieve data from the OPC UA server.
- Drag the join node onto the canvas. Set the mode to manual, with the option to create
msg.payload
using the values ofmsg.topic
as keys. Set the count to 3 and ensure that the interval for all of the inject nodes triggering data retrieval is the same. This ensures that the data is collected and aggregated correctly at the same time. - Connect the output of the OpcUa-Client node (which retrieves the data) to the input of the change node. For example, if I have set the change node for the 'cycle-time' data property, connect it to the OpcUa-Client node that retrieves this data.
- Connect the output of the change node to the input of the join node.
- Repeat this process for all of your data properties.
# Sending Data to the MQTT Broker
Now, in this section, we will show you how to send the collected data to an MQTT broker:
- Drag the mqtt out node onto the canvas.
- Double-click on it and configure it with your MQTT broker details.
Configuring the mqtt out node with broker information
- Set the topic for your data in the mqtt out node.
- Connect the output of the join node to the input of the mqtt out node.
- Deploy the flow. After deploying, you will see the status "connected" with a green dot at the bottom of each node, indicating that you have successfully connected to your MQTT broker.
Image showing the successful bridging of OPC UA data to MQTT
# Bridging MQTT Data to OPC UA
In addition to bridging data from OPC UA to MQTT, you might also need to send data from MQTT back to an OPC UA server. This is often required in scenarios where external systems, such as Manufacturing Execution Systems (MES), need to update or control machinery settings.
For example, an MES can send commands or configuration changes via MQTT, which then need to be applied to an OPC UA-controlled machine.
- Drag an mqtt in node onto the Node-RED canvas and configure it with your MQTT broker details and the appropriate topic where the MES publishes commands.
- Drag the change node onto the canvas, Set the
msg.topic
to the node ID and datatype of the property you wish to update. - Add an OpcUa-Client node to the canvas and configure it with your OPC UA server. Set the action type to "WRITE" to send the received data.
- Connect the output of the mqtt in node to the input of the change node, and the output of the change node to the input of the OpcUa-Client node.
Image showing the successful bridging of OPC UA data to MQTT
# Up Next
-
Using MQTT with Node-RED Learn how to integrate MQTT with Node-RED to enhance your IoT solutions with real-time data messaging.
-
How to Build an OPC UA Client Dashboard in Node-RED Follow a step-by-step guide to create a comprehensive OPC UA client dashboard in Node-RED for effective monitoring and control.
-
Building a Secure OPC UA Server in Node-RED Explore best practices for configuring a secure OPC UA server in Node-RED to ensure safe and reliable data exchange.
-
How to Deploy a Basic OPC UA Server in Node-RED Learn how to quickly deploy a basic OPC UA server in Node-RED for testing and development purposes.
-
Node-RED as a No-Code EtherNet/IP to S7 Protocol Converter Discover how to use Node-RED to seamlessly convert EtherNet/IP to S7 protocols with Node-RED.
Written By:
Published on:
Related Articles:
- Using MQTT Sparkplug B with Node-RED
- How to Use MQTT in Node-RED.
- How to Deploy a Basic OPC-UA Server in Node-RED - Part 1