OPC UA with CODESYS and Node-RED
Introduction
OPC UA (Unified Architecture) is an evolution of the OPC specifications, enabling not only the transport of data from machines and systems but also the machine-readable semantic description of this data. It is a platform-independent standard based on TCP, available to members of the OPC Foundation. The OPC-UA specification includes various profiles to scale individual functions.
This article demonstrates how Node-RED can be used for communication with other systems using the OPC UA protocol (Open Platform Communications Unified Architecture). The OPC UA server runs on the controller, in this case, a CODESYS-based controller on a Revolution-Pi 4.
Prerequisites
Hardware
-
RevPi Connect 4
-
RevPi DIO Module
-
RevPi AIO Module
-
Inductive proximity sensor (3 wires, PNP)
-
PT-1000 temperature sensor (4 wires)
-
Connection cables (for wiring the DIO and LED).
Software
- Node-red
- OPC UA Client
- CODESYS
- OPC UA Server
Overview
- Setting up the RevPi4 and expansion modules (RevPi DIO, RevPi AIO)
- OPC UA Node Installation
- Setting up the OPC-UA server on CODESYS and defining variables
- OPC UA Client (UA-Expert)
- Node-red program
Step1: Setting up the RevPi4 and expansion modules (RevPi DIO, RevPi AIO)
Hardware Setup
Follow these steps to configure your system. For detailed instructions, refer to the Getting Started guide.
-
Power Up the RevPi Connect 4
▷ Connect the RevPi Connect 4 to a power source.
▷ Ensure it is connected to your local network for accessibility.
-
Power Up the RevPi DIO and Revpi AIO
▷ Power on the RevPi DIO module.
▷ Power on the RevPi AIO module.
-
Verify Connections
▷ Check that the Inductive proximity sensor is properly connected to the RevPi DIO.
▷ Ensure the RevPi DIO and RevPi AIO module is securely connected to the RevPi Connect 4 via the PiBridge interface.
▷ Connect the PT-1000 to the RTD channel 1 of the RevPi AIO.
-
Access the RevPi Interface
▷ Open a web browser on a device connected to the same network.
▷ Access the RevPi system using its IP address.
(../static/img/revpi-tutorial-Node-red.png)
note: For network troubleshooting or determining the IP address of your RevPi, consult the Getting Started guide.
Software Setup:
note: To ensure a compatible system with suitable software, please refer the following links:
Step 2 : OPC UA Node Installation
The standard installation of Node-RED includes basic nodes that are most commonly used. However, there is an extensive library of additional nodes that you can use. To use the OPC-UA protocol, you need to install the provided nodes "node-red-contrib-opcua". This installation can be done either via the command line with the Node Package Manager (npm) or simply via a terminal.
▷ Open the menu and select "Manage palette".
▷Then search for "node-red-contrib-opcua" to proceed with the installation.
Note: How to manually install a Node package can also be done by following the step shown here.
Step 3: Setting up the OPC-UA server on CODESYS and defining variables
▷ Open the installer under "Tools/CODESYS Installer".
▷ Before proceeding with the installer, close the main window of your CODESYS application.
▷ Click on "Install File" (EN) or "Datei installieren" (DE) in the CODESYS Installer.
▷ Follow the installation steps to install the packages from your PC to your CODESYS application.
▷ To install CODESYS Control Linux ARM 64 bit on RevPi devices with the Bullseye 64-bit image, use the following menu in the CODESYS Development System: "Tools" -> "CODESYS Installer". A window will open. Search for CODESYS Control for Linux ARM64 SL and then click on install.
The two packages are now listed as "Installed" in the CODESYS Installer and thus installed on your application.
Then use the following menu in the CODESYS Development System: "Tools" -> "Update Linux ARM 64" to transfer the installation to the controller (here your RevPi 4).
Step 4: Create a CODESYS Project
▷ Create a new standard project with CODESYS Control for Linux ARM64 SL in CODESYS.
▷ Select CODESYS Control for Linux ARM64 SL
▷ Right-click on "Device" in the project tab.
▷ Click on "Add Device" (EN) or "Gerät anhängen" (DE).
In the following window, you will find all RevPi base modules.
▷ Click on the desired base module (in this example RevPi Connect).
▷ Click on "Add Device" or "Gerät anhängen"
You can optionally use additional KUNBUS submodules.
▷ Right-click on the base module you added in the previous step (in this example on “Connect”).
▷ Click on "Add Device".
To arrange a submodule, select the device interface piBridgeLeft or piBridgeRight. ▷ Click on "Add Device".
▷ Select the attached device.
▷ Click on "Add Device".
Here, the available modules that can be connected to the left or right of the RevPi Connect 4 are displayed. For our demo: – DIO – Digital Input-Output Module
– AIO – Analog Input-Output Module
The configuration of pins on DIO:
The configuration of RevPi AIO for the PT-1000 temperature sensor. It uses the RTD channel 1 (RTD1).
Step 5: Create the CODESYS Program:
In the program, two variables are defined. The variable "A_OUT" accesses the channel of RTD_Channel 1 and returns the temperature value. The variable "Input" accesses input 2 of the digital input of the RevPi DIO, where the signal line of the inductive sensor is connected.
Step 6: Setting up the OPCUA Server in CODESYS
The OPC UA server function is natively included in the CODESYS development environment. In the program OPCUA-TUT, we will publish the status of an input and the value of a temperature sensor.
We have added the symbol configuration object to the project and enabled the OPC UA properties.
Step 7: Publishing Variables
In the symbol configuration editor, we find a list of all variables present in the program. We only activate those we want to publish: the BOOL type "Input", which returns the status of the digital input, and the temperature, represented by "A_OUT" of type INT, which returns the temperature value.
For each variable, we can change the permissions (Read, Write, Read/Write) by clicking on the access rights column. After the program is loaded and executed on the target, it is possible to access the published variables with an OPC UA client.
Now there is also the possibility to secure data transmission, and this can be done in two ways. First, through user authentication by changing a communication policy.
Second, it can be determined whether anonymous login is allowed, which means that you can access the OPC-UA servers without user management, or not. In this case, we have allowed it.
We also aim for secure data transmission via OPC UA. For this, we need a way to encrypt the data stream, and this is now done via the so-called "Security Manager". It checks whether a certificate is stored for the device to encrypt the data transmission via X.509. To do this, click on "Devices" and view the available information on the device.
In this case, the following information is available:
![Webstatus login](./img/revpi-tutorial-OPC UA-CODESYS-Node-RED-21.png) It is important to note that encryption of the OPC UA service is essential. First, it must be checked whether a certificate is stored. If not, as shown in the image above, one must be generated.
The OPC-UA server now has a valid certificate:
Step 8: Setting up OPC UA Client (UAExpert)
UaExpert® is a cross-platform OPC UA test client. The program, along with many other programs and documentation, can be downloaded for free from the Unified Automation company's website.
To connect to our system and interact with the published variables, we can define a server to connect to by right-clicking on "Servers" -> "Add" -> "Custom Discovery" -> "Double click to Add Server…". A popup will open where you need to set the system IP address. Confirm with "Ok" to proceed.
The program will connect to the server and display it in the list. An entry will appear similar to this: opc.tcp://192.xxx.xxx.xxx:, where "opc.tcp" indicates the protocol over TCP. When you expand the menu "opc.tcp://192.xxx.xxx.xxx:", an automatic detection of the server with the correct name will appear in this form: "OPCUAServer@RevPi99920(opc.tcp://192.168.1.214)".
Now select a communication option (e.g., "None-None(uatcp-uasc-uabinary)") and then click "Ok" to proceed.
To connect to the server, right-click on the server and then select "Connect" in the window that appears. Once the connection is established, you can search for and interact with the desired variable. We select a communication method (e.g., None-None(uatcp-uasc-uabinary)) and then click "Ok".
Step 9: create Node-RED Flow
Below you can see the Node-RED program with the associated dashboard that connects to the OPCUA-TUT program.
Communication with the CODESYS OPC UA Server is done via the OpcUa-Client node, where the endpoint is defined as the address and port of the OPC-UA server (in the example: opc.tcp://192.168.1.214:4840). For read operations (variables "Input" and "A_OUT"), the action "READ" is defined.
In the node “OpcUa Item, the “Item should be specified. The Item consists of the “NodeId of the variable to be accessed.
The "Item" can be retrieved in UaExpert Client.
In the example, we have:
Input: ns=4;s=|var|CODESYS Control for Linux ARM64 SL.Application.PLC_PRG.Input
A_OUT: ns=4;s=|var|CODESYS Control for Linux ARM64 SL.Application.PLC_PRG.A_OUT
In the Type field, the type of the variable must be defined:
Input: Boolean
A_OUT: Int16