Skip to content

OPC-UA Sim Lua script tutorial

This tutorial will outline how to write a basic lua script to control the simulator. We will cover how to create a variable node and update its value with the Update function.

Create a new server namespace

tutorial_namespace = Server.addNamespace("Tutorial")

Create a new Folder to store our Node in

This will create a new folder under the Objects folder. We will also need to register this Node with the server.

tutorial_folder = ObjectNode.newRootFolder("Tutorial", tutorial_namespace)
Server.addObjectNode(tutorial_folder)

Create our Variable node

This will create a new node with a string node identifier. The name of the node will be called Counter and it will be placed under the tutorial_folder node as we have set this as the parent node. We have defined the permissions of the node so that it can be read from and written to:

name = "Counter"
parent_node = tutorial_folder:getNodeId()
initial_value = 0
data_type = DataType.DOUBLE
access_level = AccessLevel.READ | AccessLevel.WRITE --permissions

counter_node = VariableNode.newString (name, tutorial_namespace, parent_node, initial_value, data_type, access_level)
Server.addVariableNode(counter_node) --dont forget to register the node with the server

Update the nodes value

We first need to define our Update function, which will be called by the server every 100ms:

function Update

end

We can then get the value of the node each time the function is called and then increment it by one:

function Update
  local counter_value = counter_node:getValue()
  counter_value = counter_value + 1
  counter_node:updateValue(counter_value)
end

Full code

tutorial_namespace = Server.addNamespace("Tutorial")

tutorial_folder = ObjectNode.newRootFolder("Tutorial", tutorial_namespace)
Server.addObjectNode(tutorial_folder)

name = "Counter"
parent_node = tutorial_folder:getNodeId()
initial_value = 0
data_type = DataType.DOUBLE
access_level = AccessLevel.READ | AccessLevel.WRITE 

counter_node = VariableNode.newString (name, tutorial_namespace, parent_node, initial_value, data_type, access_level)
Server.addVariableNode(counter_node)

function Update
  local counter_value = counter_node:getValue()
  counter_value = counter_value + 1
  counter_node:updateValue(counter_value)
end
Back to top