Back

OPC_UA

Loading views...

OPC-UA

Category: Networking/IoT

Overview

OPC UA (Unified Architecture) is an open, platform-independent, service-oriented architecture for industrial automation data exchange. It provides secure, reliable communication between PLCs, sensors, SCADA, MES, and ERP systems, and is the foundation of Industry 4.0 and IIoT integration.

1. Theory & Fundamentals

  • Cross-platform: Runs on Windows, Linux, embedded; replaces DCOM-based OPC Classic
  • Address Space: Hierarchical information model with nodes (Objects, Variables, Methods)
  • Services: Read, Write, Subscribe, Browse, Call (RPC)
  • Security: Certificate-based authentication; encrypted communication (AES-256)
  • Transport: TCP binary (port 4840), HTTPS (port 443), WebSocket
  • Pub/Sub extension (v1.04): MQTT/AMQP transport for cloud integration
  • Companion specs: OPC UA for PLCopen, ISA-95, FDI, AutoID, etc.

2. Frame / Packet Structure

OPC UA Binary Protocol (UA-TCP):
  MessageType(3B) | ChunkType(1B) | MessageSize(4B)
  For SecureChannel: SecureChannelId | SecurityToken | SeqHeader | Payload

Chunk types: F=Final, C=Continuation, A=Abort
MessageType: OPN (OpenSecureChannel), CLO (Close), MSG (Message), HEL (Hello), ACK

OPC UA Services (key examples):
  GetEndpoints, CreateSession, ActivateSession, CloseSession
  Browse, BrowseNext, TranslateBrowsePathsToNodeIds
  Read, Write, Subscribe (CreateSubscription, CreateMonitoredItems)
  Call (for Methods)

NodeId format: ns=<namespace>;i=<numeric> or s=<string> or g=<GUID>

3. Protocol Mechanics

  • Session: Client establishes SecureChannel, then Session; session has timeout
  • Subscription: Client creates subscription; server sends data change notifications
  • Monitored items: Variables watched for change; server publishes at subscription rate
  • Security modes: None, Sign, SignAndEncrypt
  • Certificate exchange: Client/server verify each other's certificates
  • Namespace 0: OPC UA standard types; custom types in ns=2+

4. Hardware Implementation

  • PLC/controllers: Siemens S7-1500, Beckhoff TwinCAT, Rockwell ControlLogix (with add-on)
  • Embedded OPC UA: open62541 (open-source C library) for STM32/Pi
  • Server: Ignition SCADA, Kepware, open62541 server
  • Client: UaExpert (free tool by Unified Automation), Python opcua library
  • Network: Ethernet; can run over Wi-Fi, cellular with appropriate transport

5. Register-Level / Configuration

// open62541 (embedded OPC UA server)
#include <open62541/server.h>
UA_Server *server = UA_Server_new();
UA_ServerConfig_setDefault(UA_Server_getConfig(server));

// Add a variable node
UA_VariableAttributes attr = UA_VariableAttributes_default;
UA_Float temperature = 23.5f;
UA_Variant_setScalar(&attr.value, &temperature, &UA_TYPES[UA_TYPES_FLOAT]);
attr.displayName = UA_LOCALIZEDTEXT("en-US", "Temperature");
UA_NodeId nodeId = UA_NODEID_STRING(1, "temperature");
UA_Server_addVariableNode(server, nodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
    UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
    UA_QUALIFIEDNAME(1, "Temperature"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
    attr, NULL, NULL);

UA_Server_run(server, &running);

6. Driver / Software Development

# Python OPC UA client (opcua library)
from opcua import Client
c = Client("opc.tcp://192.168.1.100:4840")
c.connect()
node = c.get_node("ns=2;s=temperature")
value = node.get_value()  # Read variable
node.set_value(25.0)      # Write variable
# Subscribe to changes
sub = c.create_subscription(500, handler)  # 500ms interval
handle = sub.subscribe_data_change(node)

7. Debugging & Testing

  • UaExpert: GUI client; browse address space, read/write, subscribe
  • Wireshark: OPC UA binary protocol dissector built-in
  • open62541 logger: Enable verbose logging for protocol debug
  • Common issues: Certificate trust error; wrong endpoint URL; session timeout

8. Real-World Applications

  1. PLC data to SCADA/MES integration
  2. Robot controller to MES (ISA-95 companion spec)
  3. Energy management systems (smart factory)
  4. Pharmaceutical manufacturing data collection (FDA 21 CFR Part 11)
  5. IIoT cloud gateway (OPC UA Pub/Sub to Azure/AWS)

9. Advanced Topics & Edge Cases

  • OPC UA Pub/Sub: Decoupled publish/subscribe; MQTT or AMQP transport
  • OPC UA over TSN: Time Sensitive Networking for deterministic industrial Ethernet
  • OPC UA Companion Specs: 100+ domain-specific information models
  • FX (Field Exchange): OPC UA for controller-to-controller communication
  • GDS (Global Discovery Server): Certificate management for large deployments

10. Standards & Variants

Version Key Features
OPC UA 1.0 Core architecture
OPC UA 1.03 Pub/Sub, security improvements
OPC UA 1.04 UADP (pub/sub transport)
OPC UA FX Field-level real-time

๐Ÿ’ก Practical Examples

Example 1: Browse server address space

root = c.get_root_node()
print(root.get_children())  # List all top-level nodes

Example 2: Subscribe to PLC variable

sub = c.create_subscription(100, MyHandler())
sub.subscribe_data_change(c.get_node("ns=2;s=Motor.Speed"))
# Handler called every time Motor.Speed changes

Example 3: Call method

result = node.call_method("ns=2;s=StartMotor", 100)  # Call with RPM param

๐Ÿงช Practice Questions

Beginner

  1. What transport does OPC UA use by default?
  2. What is an OPC UA node?
  3. What is a subscription in OPC UA?
  4. What is OPC UA security based on?
  5. What is the OPC UA default port?

Intermediate

  1. Implement an OPC UA server with 10 variable nodes using open62541.
  2. Explain OPC UA SecureChannel establishment.
  3. Implement monitored items with 100ms publishing interval.
  4. How does OPC UA Pub/Sub differ from client-server mode?
  5. What is an OPC UA information model?

Advanced

  1. Design an OPC UA server for a complete machine with ISA-95 hierarchy.
  2. Implement OPC UA security (SignAndEncrypt) with certificate management.
  3. Build OPC UA Pub/Sub to MQTT bridge for cloud integration.
  4. Implement custom companion spec for a sensor type.
  5. Deploy OPC UA on embedded STM32 using open62541.

Hands-on Projects

  1. PLC Monitor: OPC UA client reads 20 PLC variables, logs to InfluxDB.
  2. Embedded Server: open62541 on Raspberry Pi exposing sensor data.
  3. MES Integration: OPC UA server mediates between PLC and ERP system.

Checklist

  • [ ] Explain OPC UA architecture and node model
  • [ ] Install and use UaExpert to browse server
  • [ ] Implement OPC UA server with open62541
  • [ ] Implement OPC UA client in Python
  • [ ] Create subscriptions and monitored items
  • [ ] Configure security (certificates)
  • [ ] Implement OPC UA method call
  • [ ] Build OPC UA Pub/Sub publisher
  • [ ] Debug with Wireshark OPC UA dissector
  • [ ] Integrate OPC UA with cloud (MQTT bridge)