Introduction
OPC UA (Open Platform Communications Unified Architecture) is a widely used protocol for industrial automation and data exchange between devices, sensors, and software applications.
In this blog post, we will discuss an OPC UA server implementation in Python that simulates multiple equipment nodes, each with its own set of tags like temperature, pressure, torque, humidity, light, voltage, and watts.
The server continuously updates the tags with random simulated data. This can be helpful for testing OPC UA clients, data analysis pipelines, or visualization tools without requiring access to real equipment.
Once the server is up and running, you can use
SAP Plant Connectivity (SAP PCo) for connecting and testing.
Prerequisites
- Python 3.6 or higher
- The "opcua" library installed using pip:
pip install opcua
Server Implementation
The server implementation is divided into several parts, including generating simulated data, creating equipment nodes, updating tag values, and running the server.
- Generating Simulated Data: The
generate_simulated_data()
function generates random values for each tag. These values are simulated and used for updating the tags of each equipment node.
def generate_simulated_data():
return {
"Temperature": random.uniform(20, 30),
"Pressure": random.uniform(1000, 1050),
"Torque": random.uniform(5, 30),
"Humidity": random.uniform(40, 60),
"Light": random.uniform(500, 1000),
"Voltage": random.uniform(110, 240),
"Watts": random.uniform(50, 500)
}
- Creating Equipment Nodes: The
create_equipment_node()
function creates an equipment node under the specified parent node with the given equipment name and adds tags for the equipment using the namespace index.
def create_equipment_node(parent_node, equipment_name, namespace_idx):
equipment_node = parent_node.add_object(namespace_idx, equipment_name)
tags = {}
for tag_name in generate_simulated_data().keys():
tags[tag_name] = equipment_node.add_variable(namespace_idx, tag_name, 0)
return tags
- Updating Tag Values: In the main loop, the script continuously updates the tag values for each equipment node with new simulated data generated by the
generate_simulated_data()
function.
- Running the Server: The server is initialized, and the endpoint, namespace, and object nodes are configured. Then, the equipment nodes and their respective tags are created. Finally, the server is started, and the main loop begins updating the tags.
if __name__ == "__main__":
# Initialize the OPC UA server
# ...
# Set up the server's endpoint, namespace, and object nodes
# ...
# Create equipment nodes and their respective tags
# ...
# Start the server and run the main loop
# ...
A Working Implementation
You can clone a working implementation of a Python OPC UA Server from
my GitHub repository.
To do so, follow these steps:
- Clone the repository to your local machine using the command line:
git clone https://github.com/manoelfranklins/my-opcua-python-server.git
- Change to the cloned repository directory:
cd my-opcua-python-server
- Ensure you have Python 3.6 or higher installed on your system. To install the required "opcua" library, use pip:
pip install opcua
- Review myopcua.py script to ensure the server endpoint, namespace, and other settings match your requirements.
- Run the server script:
python myopcua.py
The OPC UA server will start, and you can connect to it using an OPC UA client to interact with the simulated equipment nodes and their respective tags.
You can use
SAP Plant Connectivity (SAP PCo) to connect to this OPC UA server by following these steps:
Create a new OPC UA Source System
Click "Select Endpoint" and inform your OPC UA Server endpoint
Click "Test Connection" to see if the connection is ok
Create a new Agent Instance
Open "Subscription Items" tab and click "Browse for Tags"
Click "Browse" and expand the Three View to view several simulated Equipment and tags
Conclusion
In this blog post, we demonstrated how to create an OPC UA server in Python to simulate multiple equipment nodes with various tags. This can be a valuable tool for testing, development, and learning purposes. With some modifications, you can also use this server to simulate different types of equipment, add more tags, or implement custom logic.
We also covered how to use
SAP Plant Connectivity (SAP PCo) to connect to this OPC UA Server for testing.
Experiencing SAP Digital Manufacturing
You can have a glimpse and experience several aspects of SAP Digital Manufacturing via the Interactive Value Journeys below:
Do you like this post? Please let me know in the comments section what you think. Any feedback is highly appreciated.
Or, if you have any questions, please check
SAP Community Q&A Area, or comment down below.
Thanks,
Manoel Costa