If you've been keeping up with our Summer of IoT blog series, you've likely noticed our in-depth exploration of Software Toolbox solutions and the use of MQTT for IoT applications. In this latest installment of the series, our spotlight shifts to the OPC Data Client development toolkit. When speaking about IoT, OPC UA is a major player, which is why our focus for this post will be to delve into the integration of OPC UA PubSub (publish/subscribe) and how it leverages MQTT as its conduit for message transmission.
OPC Data Client is a rapid development toolkit for quickly adding OPC client connectivity to custom applications including OPC UA and OPC Classic (DA, AE and XML-DA). The OPC Data Client has been extensively tested for OPC interoperability, with various OPC Servers from many vendors, and on different computer environments. This also includes regular participation at the OPC Foundation's OPC Interoperability Workshops where the toolkit’s reference implementation has passed their independent lab testing process.
In the realm of message transmission, efficiency is key. Designed specifically for gathering data from numerous devices and seamlessly transporting it to IT infrastructure, MQTT excels in scenarios demanding lightweight protocols particularly in remote monitoring setups where minimizing code footprint and conserving network bandwidth are paramount. Employing a publish/subscribe mechanism, MQTT optimizes payload delivery and minimizes overhead, accommodating application-specific JSON or binary formats with ease. Standardization of the payload format is not defined by MQTT standards. This is where OPC UA excels, because it provides standardized formats, especially when used with OPC UA companion specifications, the subject of a recent post. Recognized and embraced by IT departments globally, MQTT stands as a trusted and versatile solution for data exchange. This post will delve into the intricacies of MQTT UADP, MQTT JSON for optimizing OPC UA PubSub messaging with MQTT, shedding light on best practices for enhancing communication efficiency and reliability between MQTT clients and brokers in the context of Data Client.
Examples of OPC UA PubSub utilizing MQTT
Given OPC Data Client's capability for custom applications, this blog explores sample code for MQTT JSON and MQTT UADP using TCP, serving as a foundation for your projects.
The OPC UA standard offers a publish-subscribe (Pub/Sub) model where clients can either send (publish) or receive (subscribe) data. A broker is needed to manage data exchange between Publishers and Subscribers.
For systems with multiple data senders and receivers, OPC UA Pub/Sub is more efficient than direct connections. It supports UDP and MQTT, offering flexibility with standardized methods for data organization and packaging, solving interoperability issues common in other Pub/Sub solutions.
In a system with numerous machine sensors, OPC UA Pub/Sub allows sensors to publish data to a broker. Client applications can then receive this data without 1-to-1 connections. Sensors can also receive feedback from clients, enhancing application responsiveness and efficiency.
Understanding JSON and UADP
What is JSON?
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. It uses a text format that is completely language-independent but uses conventions that are familiar to programmers of the C family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
What is UADP?
UADP (Universal Architecture Data Protocol) is a protocol used in the OPC UA (Open Platform Communications Unified Architecture) framework for industrial automation. UADP is designed to support efficient data transmission by encoding messages in a binary format, which allows for fast and reliable communication between devices and systems in industrial settings. It facilitates interoperability and scalability in complex industrial environments.
When should I use JSON or UADP?
JSON and UADP cater to different needs and use cases. JSON, or JavaScript Object Notation, offers users the flexibility to create payloads structured exactly as they need. This freedom allows for customized data interchange, which can be particularly useful in web applications and scenarios where human readability and ease of debugging are crucial. However, this customization means that receivers must understand the specific format to retrieve data successfully, which can add complexity and require additional effort to maintain.
In contrast, UADP (Universal Architecture Data Protocol) provides a predefined structure within the OPC UA framework. This structured approach means that any OPC UA Pub/Sub compliant solution already expects the data in a specific format, saving time and reducing the investment needed to create and maintain customized payloads. UADP's binary encoding facilitates secure, efficient and reliable communication, particularly suited for industrial automation environments where performance, scalability, and interoperability are critical. However, users are confined to the predefined structure, which may limit flexibility compared to JSON. Developers must consider their application, corporate and industry specific best practice security requirements, and operating environment needs to make the best choice for their applications.
MQTT JSON Mapping using TCP
Now that we have a clear understanding of JSON and the UADP protocol and considerations of when to utilize each, let’s walk through an OPC Data Client provided example that shows how to subscribe to dataset messages on an OPC-UA PubSub connection using JSON via TCP. This is useful for applications that need to handle real-time data from industrial devices or other OPC-UA compliant sources. It is important to note that while we are only exploring the JSON example, the process will be identical with a few minor changes if you wish to use UADP mapping instead. To follow along with this code for your own implementation, please find these examples in our Online User Guide:
- Examples - OPC UA PubSub - MQTT JSON mapping using TCP
- Examples - OPC UA PubSub - MQTT UADP mapping using TCP
Before diving into the code, you must ensure that the OpcLabs.MqttNet package is referenced in your project. This package is essential for the MQTT transport to work.
Breakdown of Sample Code:
-
Namespaces and Class Definition:
Namespaces: The code uses several namespaces for threading, OPC-UA PubSub functionalities, and operation models.
Class Definition: The SubscribeDataSet class contains the main method MqttJsonTcp for subscribing to dataset messages. -
PubSub Connection Descriptor for JSON:
Connection Descriptor: Defines the PubSub connection using MQTT over TCP.
Transport Protocol Mapping: Specifies the transport protocol mapping as MQTT JSON or MQTT UADP. This step is often optional due to automatic message mapping recognition. -
Subscription Arguments:
Subscription Arguments: Defines the arguments for subscribing to the dataset, including the MQTT topic name. - Subscriber Object and Event Handling:
Subscriber Object: Instantiates the subscriber object.
Event Handling: Hooks the DataSetMessage event to the subscriber_DataSetMessage_MqttJsonTcp method for handling incoming dataset messages. - Subscribing and Unsubscribing:
Subscribing: Subscribes to the dataset messages.
Processing: For this particular example, dataset message events are processed for 20 seconds. This can be modified and adjusted based on your needs/preferences.
Unsubscribing: Unsubscribes from all dataset messages and waits for one second to ensure all messages are processed. This can be modified and adjusted as well. - Event Handler Method:
Event Handling Logic: Displays the dataset if the message is successfully received. If there is an error, it prints the error message. - Example Output:
This example illustrates a basic, yet powerful, way to handle real-time data from OPC-UA compliant devices using MQTT JSON or UADP mapping over TCP. By subscribing to dataset messages, you can efficiently monitor and process industrial data streams. Be sure to update the connection descriptors and subscription arguments in the example to match your specific use case and environment.
Conclusion
In this article, we've witnessed the powerful combination of OPC Data Client and MQTT and how it optimizes PubSub messaging in IoT applications. We’ve also learned that the OPC Data Client can facilitate your OPC connectivity needs and provide a reliable solution that is sure to work with whatever OPC Server you use, thanks to extensive interoperability testing MQTT continues to grow in popularity and its implementation within the industrial automation community.
Through this exploration, we've highlighted using MQTT JSON and MQTT UADP to enhance communication efficiency and reliability in OPC UA PubSub messaging. As you continue to innovate and build IoT solutions, integrating these technologies can improve data exchange and system performance, ensuring your applications are both effective and efficient.
Don't forget to subscribe to our blog for the latest updates, how-to videos, and other resources for OPC Data Client and our other solutions. As always, please feel free to contact our technical team Monday-Friday, 8AM to 5PM US Eastern Time.
Ready to get started with custom OPC client development and expand your IoT integration even further? Download OPC Data Client today!