Python · Industrial Automation

Python OPC UA Client: Read PLC Data and Store in SQL Server

Build a Python OPC UA client for industrial automation. Connect to an OPC UA server, read PLC tags, validate values and prepare parameterized SQL Server logging.

Practical Python Automation Context Copy-Paste Examples Code Architecture

Learning Overview

Python Learning SeriesPython for Industrial AutomationPractical

Learning Goal

  • Build a Python OPC UA client for industrial automation. Connect to an OPC UA server, read PLC tags, validate values and prepare parameterized SQL Server logging.
  • Run the examples in VS Code using the project environment prepared in earlier topics.
  • Understand where this topic fits in the complete SQL, reporting and Industry 4.0 workflow.
Quick answer

Build a Python OPC UA client for industrial automation. Connect to an OPC UA server, read PLC tags, validate values and prepare parameterized SQL Server logging.

Architecture: Python OPC UA → SQL

Code-Based Architecture

Python OPC UA → SQL

1

PLC / SCADA

Expose variables through OPC UA

ns=...;s=Temp_Act
2

OPC UA Server

Secure endpoint

opc.tcp://...
3

Python Client

Connect with asyncua

Client(url=...)
4

Read Tags

Read typed values

read_value()
5

Validate

Check quality/range/type

if value ...
6

SQL Server

Log with parameters

cursor.execute(...)
PLC / SCADAOPC UA ServerPython ClientRead TagsValidateSQL Server

1. Install an OPC UA client library

python -m pip install asyncua

2. OPC UA endpoint and NodeId

An OPC UA client needs an endpoint URL plus NodeIds for the variables it is authorized to read. Exact security policy, certificates and namespaces depend on the server configuration.

3. Read one PLC tag asynchronously

import asyncio
from asyncua import Client async def main(): url = "opc.tcp://192.168.0.10:4840" async with Client(url=url) as client: node = client.get_node("ns=3;s=Temp_Act") value = await node.read_value() print("Temp_Act:", value, type(value).__name__) asyncio.run(main())

4. Read multiple process values

node_ids = { "Temp_Act": "ns=3;s=Temp_Act", "Cp_Act": "ns=3;s=Cp_Act", "Fan_Status": "ns=3;s=Fan_Status",
} for name, node_id in node_ids.items(): value = await client.get_node(node_id).read_value() print(name, value)

5. Prepare SQL parameter values

sql = """
INSERT INTO dbo.tblEvent (DT, SQF_No, Temp_Act, Cp_Act)
VALUES (?, ?, ?, ?)
"""
# cursor.execute(sql, datetime.now(), 2, temp_act, cp_act)

6. Industrial design checks

  • Use authorized OPC UA credentials/certificates
  • Confirm NodeIds after engineering changes
  • Validate status/quality before logging
  • Do not poll faster than the application requires
  • Reconnect/log communication failures

Frequently Asked Questions

What does a Python OPC UA client do?

It connects to an OPC UA server to read or write authorized industrial data using OPC UA services.

Can Python log OPC UA values to SQL Server?

Yes. Read and validate the values first, then use a SQL driver such as pyodbc with parameterized statements.

Are OPC UA NodeIds the same on every PLC?

No. NodeIds and namespaces depend on the server and project configuration.

Reviewed by Bhawesh Kumar SinghIndustrial Automation Trainer and Industry 4.0 Consultant · Softwell Automation · 21+ years industry experience
Complete Python for Industrial Automation Learning Path

Use Previous / Next for sequential training, or open any topic directly.

Complete

Continue with the next topic in the Python for Industrial Automation learning path.

Open Learning Path
Python for Industrial Automation

Python OPC UA → SQL

Build a Python OPC UA client for industrial automation. Connect to an OPC UA server, read PLC tags, validate values and prepare parameterized SQL Server logging.

Content reviewed: 14 September 2026

☎ Call WhatsApp ✉ Email Enquire Now