Skip to content

Instantly share code, notes, and snippets.

@jtuttas
Last active March 24, 2024 15:33
Show Gist options
  • Save jtuttas/94f70ba211db4eae04c4b0b94c384246 to your computer and use it in GitHub Desktop.
Save jtuttas/94f70ba211db4eae04c4b0b94c384246 to your computer and use it in GitHub Desktop.
OPC_UA-Server
import asyncio
import logging
from asyncua import Server, ua
from asyncua.common.methods import uamethod
async def main():
_logger = logging.getLogger(__name__)
# setup our server
server = Server()
delta = 5.0
await server.init()
server.set_endpoint("opc.tcp://0.0.0.0:4830/freeopcua/server/")
# set up our own namespace, not really necessary but should as spec
uri = "http://examples.freeopcua.github.io"
idx = await server.register_namespace(uri)
# populating our address space
# server.nodes, contains links to very common nodes like objects and root
myobj = await server.nodes.objects.add_object(idx, "MyObject")
myvar = await myobj.add_variable(idx, "MyVariable", 6.7)
# Set MyVariable to be writable by clients
await myvar.set_writable()
_logger.info("Starting server!")
async with server:
while True:
await asyncio.sleep(1)
new_val = await myvar.get_value() + delta
if (new_val > 100.0) or (new_val < 0.0):
delta = -delta
_logger.info("Set value of %s to %.1f", myvar, new_val)
await myvar.write_value(new_val)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
asyncio.run(main(), debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment