Read your first OPC UA variable
===============================
Run a Lua server on your computer, then read its Temperature variable from
a second process. You need `Mako Server `_ with
either OPC UA profile, two terminals, and a free local TCP port 4850.
The client connects to ``localhost`` using an anonymous session and the None
security policy. Run this example on a trusted development network. The BAS
listener can accept connections on other interfaces. For a network
deployment, see :doc:`secure_policy` and :doc:`server/server_config`.
1. Start the temperature server
-------------------------------
Create a directory named ``temperature_server``. Save the
:example:`server source ` in it as
``.preload`` (including the leading dot). In a source checkout, both
examples are already under ``doc/source/examples/tutorial``.
From the directory containing ``temperature_server``, run:
.. code-block:: sh
mako -l::temperature_server
Leave this process running. When it prints
``Temperature available at opc.tcp://localhost:4850``, the listener is ready.
The server creates a variable under **Objects → Tutorial → Temperature**.
Its NodeId is ``ns=1;i=6001`` and its value is a double-precision number.
.. literalinclude:: examples/tutorial/temperature_server/.preload
:language: lua
:lines: 13-20
:example:`Full source `
2. Read the value
-----------------
Save the :example:`client source ` as
``06_client_read.lua``. Open the second terminal in the directory containing
that file and run:
.. code-block:: sh
mako 06_client_read.lua
The client connects to port 4850, opens a channel, creates and activates a
session, and reads the Value attribute. It checks for **21.5** before printing
``Client read Temperature from the learning server.``
.. literalinclude:: examples/tutorial/06_client_read.lua
:language: lua
:lines: 37-46
:example:`Full source `
Stop the server with Ctrl+C when finished. If the client cannot connect,
check the first terminal for the ready message and confirm the port in both
files. If the read returns ``BadNodeIdUnknown``, browse from Objects and
compare the variable's NodeId with the one in the request.
Next, :doc:`connect a variable to device data ` or
:doc:`write a value from Lua `. The
:doc:`learning examples ` include a writable
setpoint and a callable method.