Integrate an OPC UA Server into an embedded or PC application using the QT platform

Integrate an OPC UA Server into an embedded or PC application using the QT platform

In today's post we will introduce you to automating industrial or Internet of Things equipment through an API developed with the OPC UA standard using QT as a development platform. QT allows you to create cross-platform applications, so you could create an application that runs both on a PC and on an embedded system like a raspberry pi.

Let's start with the basic questions:

1. What is OPC UA?

OPC UA stands for Open Connectivity-Unified Architecture. It is an extensible and platform-independent standard that allows the secure exchange of information in industrial systems and arose to respond to the problem of the great diversity of communication protocols that existed in the beginning in the field of automation. It enables the exchange of information and data on devices within machines, between machines, and from machines to systems. OPC UA bridges the gap between information technology and operational technology.

OPC UA is supported on Windows, macOS, Android, and Linux. It can also be used on embedded systems and bare metal systems (where there is no use of an operating system). OPC UA works on PCs, cloud-based infrastructures, PLCs, microcontrollers, and cyber-physical systems (CPS).

The objective of OPC UA is to improve the interoperability between hardware and software for automation and business planning by providing a framework that allows industrial companies to converge between disparate technologies.

https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/PROBLEOPC.JPG/500px-PROBLEOPC.JPG
https://upload.wikimedia.org/wikipedia/commons/a/aa/SOLUCIOPC.JPG

2. How does OPC UA work?

Through the use of models, OPC UA specifies basic rules for exposing data to any application or device that wants to consume it. OPC UA is itself an information-centric data model. It comprises a generic object model with an extensible type system with built-in models for data access. OPC UA defines an integrated addressing and information model in which process data, history data, alarms, and function calls with parameter passing can be represented. Information items are defined as typed objects and can be related to each other, supporting even the use of complex data structures.

The data can also be accessed through custom models, called supplementary models, which are used in different industry sectors.

OPC UA is based on SOA, the client-server communication framework. Therefore, in OPC UA there are servers and clients. An OPC UA server provides an OPC UA client with applications and control systems, such as MES and SCADA, and secure access to industrial automation data using OPC UA information models that specify how data is organized, stored, and collected data. The term "OPC UA server" refers to the OPC UA software standard on the machine and not to the hardware itself, which could be a virtual server.

An OPC UA client can support an OPC UA information model. OPC UA clients request and write data to the components of a system via OPC UA servers.

3. Where is OPC UA used?

OPC UA is used in industrial systems such as oil and gas, agriculture, healthcare, and pharmaceuticals, as well as critical services such as power grids and wastewater treatment plants, and IoT systems such as smart city applications.

Common OPC UA applications include device diagnostics, asset management, production management, quality control, data acquisition, enterprise reporting, information security, data integration for GUI interfaces, remote worker support, and event monitoring. .

We can consider several real-world examples, such as remotely managing automated machines, sending alerts for sensor failures, monitoring office temperatures, linking embedded devices, and supporting remote workers.

4. What tools do we have available?

Open62541 library

The open62541 library (http://open62541.org) is a free and open source implementation of OPC UA (OPC Unified Architecture) written in the common subset of the C99 and C++98 languages. The library can be used with all major compilers and provides the necessary tools to implement dedicated OPC UA clients and servers, or to integrate OPC UA-based communication into existing applications. The open62541 library is platform independent. All platform-specific functionality is implemented through interchangeable plugins. Plugin implementations are provided for all major operating systems.

Qt OPC UA

The Qt OPC UA module implements a Qt API to interact with OPC UA on top of a third-party OPC UA stack. Stacks are built in as plugins and can be selected at runtime. Currently, the following implementations are supported:

Open source Open62541, MPLv2

Unified Automation C++ SDK (UACpp), Commercial

Below is an example of how to use the OPC UA standard using QT as the platform and Windows as the operating system. Let's do it:

Compiling QtOpcUa 5.15.x on Win

In windows, set the following PATH:

Download for Win64 OpenSSL v1.1.1k from https://slproweb.com/products/Win32OpenSSL.html 

and install to

C:\

In C:\Qt\5.15.x\mingw81_64\

Proceed by:

Manually copy the following files:

You can manually create the model of the plant to be automated in XML and generate all the necessary files to implement the OPC Server using the UA-ModelCompiler tool.

  1. Install Visual Studio
  2.  git clone https://github.com/OPCFoundation/UA-ModelCompiler.git
  3. cd UA-ModelCompiler
  4. git checkout 8817477c7b481fd6eb6e9cccc05b76e83c7329ba
  5. git submodule update --init --recursive

Likewise, the model can also be done with the Free OPC UA Modeler which is a tool to design OPC UA address spaces. It uses the XML format specified by OPC UA which allows the XML produced to be imported into any OPC UA SDK. (https://github.com/FreeOpcUa/opcua-modeler)

For our example, we will use the Free Modeler to model a simple server, where we have 2 methods to start and stop the system.  2 folders with ambient and system pressure and temperature sensors.

XML nodeset compiler

When writing an application, it is more comfortable to create information models using some GUI tools. Most tools can export data according to the OPC UA Nodeset XML Schema. The open62541 library contains a Python-based node set compiler that can transform these information model definitions into a server.

Note that the node set compiler that you can find in the tools/nodeset_compiler subfolder is not an XML transformation tool but a compiler. That means it will create an internal representation by parsing the XML files and try to understand and verify the correctness of this representation to generate the C code.

Emtech has modified the nodeset compiler scripts and has created new scripts that allow generating *.h and *.cpp files that generate the namespace of the created model to be used directly in a Qt project. In addition to the namespace, it generates templates of the callbacks functions that are called both for the execution of a method and for the reading/writing of a variable within the server.

For this basic example we execute:

Which generates the following files that we use on our server:

We set up the project as follows:

Server_OpcUa.pro:

OpcUaServer.pri:

Project structure:

We put together a simple class that allows us to control the server. OpcUaServer.cpp:

And a visual interface that allows us to view the IP address of the server and some controls to modify temperatures and pressures:

In order to test our server we can use the UA Expert Client tool. Download (free account required): https://www.unified-automation.com/downloads/opc-ua-clients.html

We add a new server:

We configure the name, set address and port of the server:

And then we make the connection to it. At that moment the client polls the server and shows us the workspace with its variables and methods:

Alejandro Casanova

Senior Embedded System Engineer at Emtech S.A

Any Comments or questions, please feel free to contact us: info@emtech.com.ar