OPCUA Use - pierrep67/IoTPlatform GitHub Wiki

Introduction

Prerequisites

Please read the wiki page dedicated to OPCUA presentation before following this tutorial. You will nead a Python IDE to create and run Python scripts.

Server

We will see how to create a simple OPCUA server.

Here are the imports needed for the server script

  import sys
  import time
  from opcua import ua, Server

Firstly, an instance of the Server class is created, the endpoint is defined. It is a physical or virtual address where the server is accessible.

  if __name__ == "__main__":

            ## The server is set up
            server = Server()
            server.set_endpoint("opc.tcp://localhost:40840/iotproject/server/") 
            #endpoint url : where the webservice can be accessed by a client application

Then, a URI (Uniform Resource Identifier) is used to register a namespace on the server. It is a string that will be a reference to organize the server.

       uri = "http://iotproject.communication" 
       #"URI : a Uniform Resource Identifier (URI) is a string of characters used to identify a resource."
       idx = server.register_namespace(uri)
       #"namespace : set of symbols that are used to organize objects of various kinds, so that these objects may be 
       referred to by name"   

Working with objects and variables

Objectis and variables can be created and their value can be defined in the server. Everything starts at the root node.

  objects = server.get_objects_node() 
  #this is where we should put our nodes

Variables can be writable or not.

  myobj = objects.add_object(idx, "MyObject") #Object creation
  myvar = myobj.add_variable(idx, "MyVariable", 1.0) #Variable creation
  myvar.set_writable()  #The previous variable is defined as writable
  mystring  = myobj.add_variable(idx, "MyStringVariable", "Hello world !") #A string variable is added to the object 
  "myobj"
  mystring.set_writable() #The string variable is defined as writable

Server start

  server.start()
  print("Server started !")

  ##The server is running
  try:
    while True:
     time.sleep(1)
    finally:
     server.stop()
     print('Server stopped !')

Client

Imports

  from opcua import Client, ua, Subscription
  from opcua.common.methods import call_method
  from opcua.ua import VariantType

Client creation and connection

The client is created and is connected to the server thanks to the namespace address.

  if __name__ == '__main__':

    ## Client creation and connection
    client = Client("opc.tcp://localhost:40840/iotproject/server/")
    client.connect()

The root node of the server is got and dislayed in the console.

    root = client.get_root_node() # The root node is got
    print("Root node is: ", root)

The same procedure is executed for its children.

    print("Children of root are: ", root.get_children()) # The root node children are got
    objects = client.get_objects_node()

The client object nodes are listed.

    print("Objects node is: ", objects) 

A variable can also be browsed using its path.

    myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyStringVariable"])
    print("myvar is: ", myvar) 

A string variable data value is got mystrg = myvar.get_data_value() print("Mystrg is :",mystrg)

A variable value is got, modified and checked myval = myvar.get_value() print("My value is :",myval) myvar.set_value("Good bye !") myval = myvar.get_value() print("My value is :",myval)