Create and Connect to the OPC UA Server
This example will show you how to establish a connection to the system’s OPC UA Server using an external OPC UA client. The first steps involve setting up an instance of the OPC UA Server in the System Model and creating an endpoint. We will then use the freely available Unified Automation UaExpert client to connect to the server.
In a very simple installation scenario, a single Node will be created in the System Model that by default will take the hostname as its Object Name. In this example we have changed the Object Name of the Node to Home.
Creating the OPC UA TCP Server
-
Open the System Config App in a browser and sign in by going to:
https://<hostname>:<port>/apps/webstudioThis is the url for the secure https connection to the WebServer. It is recommended to only the secure channel when connecting to the system. -
Select "System Config" from the index page and open the System Model in the Object Model panel.
-
Right-click on the "Home" Node and select from the context menu to open the Create Object dialog.
-
Expand the Services section in the left-hand panel and select "OPC UA Servers" (this is a group object for all OPC UA Server objects).
-
Enter an Object Name for the OPC UA Server group. The Owner Path property will be set automatically on creation.
Figure 1. Create OPC UA Server Group -
Click Create. The OPC UA Server Group object will be created below the Node, the Types object and related sub-objects will also be created automatically below the OPC UA Server Group.
-
Right-click on the newly created OPC UA Server Group and select from the context menu to open the Create Object Dialog.
-
Select "OPC UA TCP Server" and enter an Object Name for the OPC UA TCP Server.
Figure 2. OPC UA TCP Server Create Object DialogThe Model Access section will by default allow access to the I/O Model but other model access can also be granted in this section if required. -
In the left-hand panel, open the Communication section. The Listener Port is set to 4840 by default. If this port is already being used by another application, a different port number can be specified.
Figure 3. Create OPC UA TCP Server - Communication OptionsThe Bind to Address and Discovery Path properties are left empty here but can be configured for your particular UA client or system. -
Open the Certificate Management section. The Trust Mode property is by default set to "Reject" certificates from new client connections. It is recommended to retain this setting and manage new client certificates manually
Figure 4. Create OPC UA TCP Server - Certificate Management Options -
Click Create to create the OPC UA TCP Server object in the Server Model.
Figure 5. System Model - OPC UA TCP Server Object
Creating an Endpoint
The OPC UA TCP Server is created, however, we need an endpoint to establish a session with an external OPC UA Client. The endpoint specifies the security policy, message security mode and supported user token policies. For this example, we will use the simplest case, using no security mode and the Anonymous User Token policy.
-
Select the OPC UA TCP Server object in the System Model.
-
Right-click and select to open the Create Object Dialog.
-
In the left-hand panel, expand the Default Theme section and select OPC UA TCP Server Endpoint.
-
Enter an Object Name.
Figure 6. System Model - Create OPC UA TCP Server Endpoint -
On the Communication section of the Create Endpoint wizard, you can enter an Endpoint Path (see note below for more details). However, to keep this example simple we will leave the field blank. Importantly, The Security Mode should be set to "Sign and Encrypt" and the Security Policy to "Basic256Sha256" to use the most secure communication protocol between Server and any connecting clients, the minimum recommended for production environments.
Figure 7. Create Endpoint - CommunicationIf the Endpoint Path is provided, then this will be appended to the endpoint URL that you use to connect with a UA client. For example, if myendpointis set as the Endpoint Path property, then the endpoint URL used by the client will beopc.tcp://hostmachine:4840/myendpoint. -
Open the User Token Policies section, expand the User Name Policy option and select the "Enable" checkbox. Also set the Security Policy to "Basic256Sha256" (minimum recommended encryption for production environments). This User Name policy requires authentication against a Profile in the Access Model using the Built-In Authentication. It is recommended that the Profile used for OPC UA access have limited access rights, General Access Rights set to "OPC UA Connections" only and Model Access Rights set to "I/O Model" only. When Setting the Security References in the I/O Model, permissions should be limited to "Read", "List" and "Inheritable".
Figure 8. Create Endpoint Wizard - User Token PoliciesThe Anonymous Policy can also be used and maps to any existing Profile in the Access Model. However, this is only recommended for read-only data access in isolated networks. The UA Server grants the same permissions to a profile as is defined in the Access model when Object Level Security is set. In this way the Anonymous User Token Policy can be used to restrict access to the namespace when connecting with a UA client. -
Click Create to create the Endpoint object.
Figure 9. System Model Tree - OPC UA Server and Endpoint
Connecting to the OPC UA Server with a UA Client
Using the configuration set up in the above examples, we can connect to the OPC UA server with an external UA client using an User Name token policy and Sign and Encrypt/Basic256Sha256 security mode and policy. You can check the connection using your own choice of UA client, however, here a connection will be demonstrated using the Unified Automation UaExpert client.
-
Open UaExpert and select 'Servers' in the Project panel. Right-click and select Add from the Context Menu to open the Add Server dialog.
Figure 10. UaExpert - Adding a Server -
In the Add Server dialog you will find the Inmation OPC UA Server in the ServersOnNetwork section. Expanding the server shows the security policy of the server.
Figure 11. UaExpert - Add Server Dialog
|
If the OPC UA client does not detect the server automatically, it also provides an option to enter the URL manually (e.g. in the 'Advanced' tab of UaExpert’s 'Add Server' dialog). The URL follows this schema: opc.tcp://<hostname>:<port>/<endpoint> The system’s default port for the OPC UA server is 4840 but it can be reconfigured in Listener Port property in the Communication compound of the OPC UA TCP Server object. |
-
Make sure that the User Name/Password Authentication Settings are selected and enter the Profile name you want to use for OPC UA connections (the "UA Users" Profile in this example has the limited access rights and permissions described in the previous section). It is not necessary to enter the password at this time as this will be prompted upon connection. Click OK to close the Add Server dialog.
-
In the Project panel, the OPC UA server should now be visible under 'Servers'. Select it, then right-click and select Connect from the context menu.
Figure 12. UaExpert - Connect to Server -
You will be prompted to enter the password for the User Name specified earlier. Enter the password for the UA Users Profile and click OK.
Figure 13. UaExpert - Enter user credentials -
The first time you try to connect to the OPC UA Server, the client will inform you that the certificate issued by the server isn’t trusted yet. Click Trust Server Certificate to add the new certificate to the trusted certificates.
Figure 14. UaExpert - Untrusted Certificate -
The client will confirm the addition of the new certificate. Click Continue to close the dialog.
Figure 15. UaExpert - Confirmation of the New Certificate -
The OPC UA TCP Server was configured to reject new client certificates (Trust Mode property set to "Reject"), so the client log will repeatedly show a "BadSecurityChecksFailed" error until the certificate is trusted. To do this, go to the "C:\ProgramData\AspenTech\inmation.root.Home\state\certificates" directory and open the "rejected" folder. The UA Expert client certificate can be moved from "certificates/rejected" to "certificates/certs" to trust the client certificate.
Figure 16. UA Expert - Client Certificate rejectedThe client certificate can also be trusted by changing the OPC UA TCP Server Trust Mode property to "Trust", applying the changes, and then changing back to "Reject". This is not recommended best practice however as other client connections certificates could be potentially be unintentionally trusted during the configuration change. Moving the certificates manually allows for more control and oversight. -
The OPC UA Server should now be visible in the Project panel, under 'Servers'. Right-click it and select Connect from the Context Menu.
Figure 17. UaExpert - Connect to ServerThe client certificate is self signed so the server can return a "BadApplicationSignatureInvalid" error when starting the session. This can be ignored depending on the policies on use of self signed certificates. -
When the connection is established, the Address Space panel reveals the data tree provided by the server. Expand the tree and drag some I/O items to the Data Access panel to see the values updating.
Figure 18. UaExpert - Connected UA Server with Updating Values