OPC UA#
OPC UA is a standard for reading data from industrial systems by name. This page covers how a client finds a value, the two ways to get it, and how to send it to Nominal.
Download this page as a notebook to run its code in JupyterLab. Running the notebooks explains how to set it up.
How OPC UA works#
OPC UA is “a platform independent service-oriented architecture” that integrates all the functionality of the individual OPC Classic specifications into one extensible framework.[1] It models data with its meaning, such as units and relationships, and supports both client-server requests and publish-subscribe.[1]
A server holds its data in an address space, which represents data hierarchically, like files and folders.[1] A node is a “fundamental component of an AddressSpace”.[2] A client gets a node by its node ID, such as i=85 in asyncua’s example, and reads its value.[3]
Polling and subscriptions#
A client can get a value in two ways:
Polling. The client reads the node on its own schedule, and gets a value each time, changed or not.
A subscription. A subscription is a client-defined endpoint in the server, used to return notifications to the client.[2] The client adds monitored items to it, each of which is a client-defined entity that monitors an attribute for new values.[2] The server reports by exception: when a value changes, according to the client’s criteria.[1]
Two intervals control a subscription. The sampling interval is the fastest rate at which the server samples its source for changes. The publishing interval is the rate at which the subscription tries to send a message to the client.[4]
This simulation compares the two for a temperature that sits at one value for a while and then steps to another. It reads the temperature 10 times a second, and counts the values a simple report-on-change rule would send, which are the values that differ from the last one:
import matplotlib.pyplot as plt
import numpy as np
from learning.style import set_matplotlib_style
set_matplotlib_style()
t = np.arange(0, 60, 0.1) # s: a read every 0.1 s
rng = np.random.default_rng(3)
temperature = np.where(t < 20, 21.0, np.where(t < 40, 24.5, 22.0)) # °C, in steps
temperature = np.round(
temperature + rng.normal(0, 0.02, t.size), 1
) # 0.1 °C resolution
changed = np.r_[
True, np.diff(temperature) != 0
] # values that differ from the last one
fig, ax = plt.subplots()
ax.plot(t, temperature, color="0.7", label=f"Polled: {t.size} values")
ax.plot(
t[changed],
temperature[changed],
"o",
label=f"Reported on change: {changed.sum()} values",
)
ax.set_xlabel("Time (s)")
ax.set_ylabel("Temperature (°C)")
ax.legend()
plt.show()
The changed values describe the same temperature as the polled ones with far fewer values. The cost is that a value that hasn’t changed isn’t re-sent, so a quiet signal and a dead connection look alike unless the server’s keep-alive messages, which it sends when there’s nothing to report, tell them apart.[4] Which to use depends on the data: poll when you want a value at a steady rate, and subscribe when changes matter more than the rate.
In Nominal#
Nominal’s channels hold timestamped values, so each value you read becomes a point. This code reads a node with the asyncua library and streams it, polling once a second:[3]
import asyncio
from datetime import datetime, timezone
from asyncua import Client
from nominal.core import NominalClient
client = NominalClient.from_profile("default")
dataset = client.get_dataset("<dataset RID>")
async def main():
async with Client(url="opc.tcp://localhost:4840/freeopcua/server/") as opc:
node = opc.get_node("ns=2;s=Furnace.Temperature") # your node's ID
with dataset.get_write_stream() as stream:
while True:
value = await node.read_value()
now = datetime.now(timezone.utc) # when the computer read the value
stream.enqueue("furnace.temperature", now, float(value))
await asyncio.sleep(1.0)
asyncio.run(main())
get_write_stream sends the values in batches.[5] The timestamp is the time of the read, not of the measurement, so it can lag the real change by up to the polling period. What the link means for your data explains why that matters.
Further reading#
Communications, on the other links a test system uses, and how to check what each does to your data.
OPC Foundation, Unified Architecture.
OPC Foundation, OPC 10000-4: Services, the full definitions of subscriptions and monitored items.
opcua-asyncio, the asyncua library.