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
Python OPC UA → SQL
PLC / SCADA
Expose variables through OPC UA
ns=...;s=Temp_ActOPC UA Server
Secure endpoint
opc.tcp://...Python Client
Connect with asyncua
Client(url=...)Read Tags
Read typed values
read_value()Validate
Check quality/range/type
if value ...SQL Server
Log with parameters
cursor.execute(...)1. Install an OPC UA client library
python -m pip install asyncua2. 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.
