Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OPC-UA Client Implementation with asyncua

This repository contains a Python OPC-UA client setup using asyncua. The client connects to an OPC-UA server with certificate-based security and reads server namespace and node data. Reference: asyncua documentation

Project structure

  • utils/settings.py - loads environment configuration from .env.
  • utils/opcua_client_cert_gen.py - helper to generate a self-signed client certificate and key.
  • scripts/opcua_test.py - simple OPC-UA polling test without certificate-based security, for server status checking.
  • scripts/opcua_client.py - main async OPC-UA client functions using client certificate/key and utils.settings configuration.
  • scripts/main.py - example entrypoint that can browse server namespace and object nodes.
  • requirements.txt - required Python package dependencies.

Prerequisites

  • Python 3.11+ recommended
  • pip installed
  • OPC-UA server endpoint and access credentials/certificates

Installation

Clone this project and navigate to the project root directory. Then install the required dependencies:

git clone <repository-url>
cd <repository-directory-on-your-machine>
pip install -r requirements.txt

Configuration

Create a .env file in the project root with the following values:

  • example of .env content:
log_level=INFO
OPC_SERVER_URL=opc.tcp://<server-host>:<port>
PATH_TO_CLIENT_CERT=client_cert.pem
PATH_TO_CLIENT_KEY=client_key.pem
APPLICATION_URI=urn:example.org:FreeOpcUa:opcua-asyncio
TARGET_NAMESPACE=http://your-namespace-uri
TARGET_NODE_ID=ns=3;s=YourNode

Update the values to match your OPC-UA server and certificate paths.

Running the client

Set PYTHONPATH to the project root to ensure utils can be imported.

export PYTHONPATH=$(pwd)

From the project root, run:

python scripts/main.py

After running for the first time, you will need to accept the client certificate on the server side if it is not already trusted. (If using Prosys OPC-UA Simulation Server, you can find the pending certificate in the server's Certificates tab, right-click it and choose "Trust".)

Or use module mode to ensure the project root is on Python's import path:

python -m scripts.opcua_client

Important

Run commands from the project root directory. If you execute the script from inside scripts/, Python may fail to import utils.

Generating a client certificate

If you need a self-signed certificate for testing, run:

python ./utils/opcua_client_cert_gen.py

This generates client_cert.pem and client_key.pem by default. Reference: text

Optional test script

The file scripts/opcua_test.py shows a simpler connection example using asyncua.Client and polling a node value every second. It is useful for basic connectivity testing.

Troubleshooting

  • ModuleNotFoundError: No module named 'utils'
    • Make sure you run Python from the project root.
    • Example:
      cd /path/to/project-root
      python ./scripts/opcua_client.py
  • If the OPC-UA server requires a trusted server certificate, you may need to configure server_certificate in opcua_client.py.

Notes

  • utils/settings.py uses pydantic-settings to load .env values.
  • The main OPC-UA client uses asyncua.crypto.security_policies.SecurityPolicyBasic256Sha256 and ua.MessageSecurityMode.SignAndEncrypt.

Feel free to adapt the .env settings and script logic for your specific OPC-UA server namespace and node IDs.


Contact

For questions or issues, please open an issue in this repository or contact the maintainer directly.

About

Implementation of `asyncua` library to setup an OPC UA client, with certificate-based security.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages