Skip to main content

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.

Webstatus login

▷ 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.

Webstatus login

The two packages are now listed as "Installed" in the CODESYS Installer and thus installed on your application.

Webstatus login

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).

Webstatus login

Step 4: Create a CODESYS Project

▷ Create a new standard project with CODESYS Control for Linux ARM64 SL in CODESYS.

Webstatus login

▷ Select CODESYS Control for Linux ARM64 SL

Webstatus login

▷ Right-click on "Device" in the project tab.

▷ Click on "Add Device" (EN) or "Gerät anhängen" (DE).

Webstatus login

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"

Webstatus login

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".

Webstatus login

To arrange a submodule, select the device interface piBridgeLeft or piBridgeRight. ▷ Click on "Add Device".

Webstatus login

▷ Select the attached device.

▷ Click on "Add Device".

Webstatus login

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

Webstatus login

The configuration of pins on DIO:

Webstatus login

The configuration of RevPi AIO for the PT-1000 temperature sensor. It uses the RTD channel 1 (RTD1). Webstatus login

Webstatus login

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.

Webstatus login

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. Webstatus login

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.

Webstatus login 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.

Webstatus login

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.

Webstatus login

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.

Webstatus login

The OPC-UA server now has a valid certificate:

Webstatus login

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.

Webstatus login

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.

Webstatus login

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".

Webstatus login

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.

Webstatus login

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.

Webstatus login

In the node “OpcUa Item, the “Item should be specified. The Item consists of the “NodeId of the variable to be accessed.

Webstatus login

The "Item" can be retrieved in UaExpert Client.

Webstatus login 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