fg71
Here is a example in Python using opcua package. The example use OPC-UA Client/Server.
The OPC-UA server include a simulator of temperature increase every 2 seconds. So we could get new temperature read
from OPC-UA client. I have include some comments in the code and some logging.
 
This code use: pip install opcua
 
If you setup modbus client the read the state, and include it in OPC-UA Server. You could update the OPC-UA Server.
And use OPC-UA Client to read the value from OPC-UA Server. Siemens include OPC-UA Server that is configure in Tia Portal
for S7-1200 and 1500 PLC device. Let pretend OpenPLC_v3 runtime had OPC-UA Server we could then use OPC-UA Client to read it value.
 
It may look I have duplicate the temperature simulator and that is true, but it also showing integer identifier and string-based node id.
In other words we decide what we like to use identifier or string-based (that could be more readable)
 
from opcua import Client

#------------------------------
# Connect to the OPC-UA Server
#------------------------------
opcua_client = Client("opc.tcp://localhost:4840/freeopcua/server/")
opcua_client.connect()
print("Connected to OPC-UA Server")


#-----------------------------
# OPC-UA Client Logging
#-----------------------------

def opcua_client_logging():
    print("---[logging info begin]---")
    objects_node = opcua_client.get_objects_node()
    for node in objects_node.get_children():
        print(f"Node ID: {node.nodeid}, Name: {node.get_display_name().Text}")

    temp_value = temperature_node.get_value()
    temp_data_type = temperature_node.get_data_type_as_variant_type()
    print(f"Temperature: {temp_value} {temp_data_type}")
    print("Browse Name: ", temperature_node.get_browse_name())
    print("Description: ", temperature_node.get_description())
    print("---[logging info end]---")


#------------------
# Read Temperature
#------------------
# Read the temperature using namespace index 2 and integer identifier 1
temperature_node = opcua_client.get_node("ns=2;i=1")
print("Temperature: ", temperature_node.get_value(), "°C")

# Read the temperature using namespace index 2 and string-based node id
temperature_node = opcua_client.get_node("ns=2;s=Temperature")
print("Temperature:", temperature_node.get_value(), "°C")

# Get OPC-UA Client logging
opcua_client_logging()

# Disconnect
opcua_client.disconnect()
 
from opcua import Server
from opcua.ua import VariantType
import time

#-----------------------------
# OPC-UA Server configuration
#-----------------------------

# Create a instance of object
opcua_server = Server()

# Set the endpoint with address
opcua_server.set_endpoint("opc.tcp://localhost:4840/freeopcua/server/")

# Register a namespace with organizes OPC-UA variables
namespace = opcua_server.register_namespace("MyOPCUAServer")

#-----------------------------
# Create temperature variable
#-----------------------------

# Get the root node where variables will be stored
node = opcua_server.get_objects_node()

# Add the temperature variable in the OPC-UA namespace
temperature = node.add_variable(namespace, "Temperature", 25.0)

# Add string identifier
temperature_string = node.add_variable(
    nodeid=f"ns=2;s=Temperature",
    bname="Temperature",
    val=25.0,
    varianttype=VariantType.Double)

# Makes temperature and temperature_string modifiable by clients
temperature.set_writable()
temperature_string.set_writable()

#------------------
# Start the server
#------------------

# Initiates the OPC-UA Server
opcua_server.start()
print("OPC-UA Server started...")

#-------------------------------------------------
# Prints all available nodes in the OPC-UA Server
#-------------------------------------------------
def opcua_server_logging():
    print("---[logging info begin]---")
    for child_node in node.get_children():
        node_id = child_node.nodeid
        node_name = child_node.get_display_name().Text
        print(f"Node ID: {node_id}, Name: {node_name}")

    print("---[logging info end]---")

namespace = opcua_server.register_namespace("MyOPCUAServer")
print("Namespace Index:", namespace)

# Get logging
opcua_server_logging()


#----------------------------------------------------------
# Simulated change every 2 seconds by increasing the value
#----------------------------------------------------------

try:
    while True:
        # Update the temperature variable every 2 seconds
        temperature.set_value(temperature.get_value() + 0.05)  # Simulated change
        temperature_string.set_value(temperature_string.get_value() + 0.05)
        time.sleep(2)
finally:
    opcua_server.stop()
 
Reply 0 0
fg71
And if we like to collect temperature data for 60 seconds with measure point every second. Not directly realistic for temperature (it could be very slow)
But for demonstration its enough to get a graph using matplotlib.
 
[203128%2F9336970%2Fmceclip0]
 
The code below use the OPC-UA Server (Same code) the first post. It will collect 60 measure point (1 measure point every second). And when that is done
it will plot the values for graph.
import matplotlib.pyplot as plt
import time
from opcua import Client

# Connect to OPC-UA Server
client = Client("opc.tcp://localhost:4840/freeopcua/server/")
client.connect()

# Get Temperature Node
temperature_node = client.get_node("ns=2;s=Temperature")  # Adjust Node ID as needed

def collect_data_plot(interval):
    # Collect temperature values over time
    temperature_values = []
    timestamps = []
    for i in range(interval):  # Run for 60 seconds
        temp_value = round(temperature_node.get_value(), 2)  # Round to 2 decimal places
        temperature_values.append(temp_value)
        timestamps.append(i)  # Store time step
        print(f"Time: {i}s | Temperature: {temp_value:.2f}°C")  # Display with 2 decimals
        time.sleep(1)  # Wait for 1 second

    # Plot Temperature Data
    plt.plot(timestamps, temperature_values, marker='o', linestyle='-')
    plt.xlabel("Time (s)")
    plt.ylabel("Temperature (°C)")
    plt.title("Temperature Variation Over 60 Seconds")
    plt.grid(True)
    plt.show()


collect_data_plot(60)

# Disconnect from OPC-UA Server
client.disconnect()
 
Reply 0 0
Reply