Bridging OPC UA Data to MQTT with Node-RED

Connecting OPC UA Data Streams to MQTT Brokers for Enhanced IoT Communication and Monitoring

Back to Blog Posts
Image representing null

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 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:

  1. Drag the inject node onto the canvas.
  2. 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. (Left) Image of the Change node setting the 'msg.topic' to retrieve the cycle time data and (Right) the OPC UA Prosys interface.

  1. 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 client node with the opc ua server endpoint Configuring opc-ua node with the opc ua server endpoint

  1. 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 Configuring OpcUa-Client node to select the read operation

  1. 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.
  2. Drag the debug node onto the canvas. The output will help you verify the data retrieved from the OPC UA server.
  3. 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.
  4. 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.

  1. Drag the change node onto the canvas.
  2. 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, set msg.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. Setting the msg.topic with the change node to retrieve data from the OPC UA server.

  1. Drag the join node onto the canvas. Set the mode to manual, with the option to create msg.payload using the values of msg.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.
  2. 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.
  3. Connect the output of the change node to the input of the join node.
  4. 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:

  1. Drag the mqtt out node onto the canvas.
  2. Double-click on it and configure it with your MQTT broker details.

Configuring the mqtt out node with broker information Configuring the mqtt out node with broker information

  1. Set the topic for your data in the mqtt out node.
  2. Connect the output of the join node to the input of the mqtt out node.
  3. 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 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.

  1. 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.
  2. Drag the change node onto the canvas, Set the msg.topic to the node ID and datatype of the property you wish to update.
  3. 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.
  4. 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 MQTT data to OPC UA Image showing the successful bridging of OPC UA data to MQTT

Up Next

Written By:

Technical Writer

Published on:

Related Articles:

Sign up for updates