Discover and register servers and browse their address space, read and publish nodes via REST API in Postman
This article will walk you through the steps to discover and register OPC UA servers, browse through endpoints, read node values and publish them using the REST API via Postman.
You should have already deployed all Microservices and at least one IoT Edge Gateway with the Industrial IoT Modules. If not, please follow these instructions:
-
To run the demo OPC UA server you will also need Docker installed on a machine that is visible to the IoT Edge from a network point of view. If you don't have it, follow the instructions for Linux, Mac, or Windows.
Make sure you have Git installed on the same machine that will run the demo OPC UA server. Otherwise follow the instructions for Linux, Mac or Windows to install it.
-
Open a terminal or command prompt and run
hostname
Note the hostname of the machine running the OPC server. You will need it to specify the discovery URL of the server.
-
Run
docker run -it -p 50000:50000 mcr.microsoft.com/iotedge/opc-plc -aa
to start the OPC PLC demo server. The server is instructed to accept all certificates.
Note: The Postman requests will use an Authorization Code flow to authenticate to the service.
- Go into the Azure Portal > Azure Active Directory > App Registrations.
- The deployment of the services creates the following application registrations:
- [yourprefix]-client
- [yourprefix]-service
- Select the '-client' app registration. From the 'Overview' tab, copy the following:
-
Application (client) ID, later: [YOUR AAD CLIENT ID]
-
Directory (tenant) ID, later: [YOUR TENANT ID]
-
In the 'Authentication' tab, if 'Web' platform doesn't exist, then click 'Add a platform' > 'Web' > add a new 'Redirect URI': e.g.
https://[yourprefix].azurewebsites.net/.auth/login/aad/callback
.This redirect uri is later used as [YOUR CALLBACK URI]
-
In the 'Certificates & Secrets' tab, create a new secret and copy its 'Value', later [YOUR AAD CLIENT SECRET]
-
- Go back to the list of app registrations and select '-service':
-
From the 'Expose an API' tab, copy the application id uri : [YOUR_APPLICATION_ID_URI]
The default scope would be :
[YOUR_APPLICATION_ID_URI]/user_impersonation
, later [YOUR SCOPE URI]
-
- Download the collection as a raw JSON file
- Import the collection by using the 'Import' button at the top left of Postman
- At the top right, click Manage Environments and add a new Environment named OPC Twin
- Add a new variable:
- OPC-SERVICEURL: e.g.
[yourprefix].azurewebsites.net
- OPC-SERVICEURL: e.g.
- Click Add and close the screen.
-
In Postman, on the left you will see your collections, click the '...' (three dots) next to the OPC Twin collection and choose 'Edit'
-
Choose 'Get New Access Token'
-
Fill in the following fields:
- Token Name: Some name
- Grant type: Authorization Code
- Callback URL:
[YOUR CALLBACK URI]
- Auth URL:
https://login.microsoftonline.com/[YOUR TENANT ID]/oauth2/v2.0/authorize
- Access Token URL:
https://login.microsoftonline.com/[YOUR TENANT ID]/oauth2/v2.0/token
- Client ID:
[YOUR AAD CLIENT ID]
- Client Secret:
[YOUR AAD CLIENT SECRET]
- Scope:
[YOUR SCOPE URI]
- Click 'Request Token'
- Click 'Use Token', and then on 'Update'
-
Test your connectivity with the Microservices by running the request named
Registry: Get applications
- This should fire a GET request with the inherited auth token. It should return a 200 OK.
Note: Your token will expire regularly. If you get an authentication error during any of the calls, repeat the process to refresh the token. All requests in the Postman collection inherit the same auth configuration.
-
Execute the
Registry: Get applications
request. The result should be empty. -
Locate the
Registry: Add application
request. Edit the request body to point to your hostname:{ "discoveryUrl": "opc.tcp://[yourhostname]:50000" }
-
Execute the
Registry: Get applications
request a few times until you see the application discovered and registered.Note all the information about the application that the IoT Edge added during the registration, including public certificate, product URI and more. Copy the
applicationId
for the entry that hasproductUri
set tohttps://github.com/azure-samples/iot-edge-opc-plc
.
-
Execute the request
Registry: Browse endpoints
to see the application and its endpoints. Select theid
from one of the registration objects. -
Select the request
Registry: Activate endpoint
, replace the endpoint GUID in the URL with theid
from above and execute it. -
Run
Registry: Browse endpoints
again and you should see the endpoint as active, connected and ready. Since the request uses the device registry query, which caches state, switching to this view might take a couple of tries. A live view of the twin can be retrieved usingRegistry: Get endpoint
with the endpoint'sid
.[ommitted]... "applicationId": "............................", "activationState": "ActivatedAndConnected", "endpointState": "Ready" ...
-
Select the request
Twin: Browse nodes
, replace the endpoint GUID in the URL with theid
from above and execute it to browse the root node of the endpoint.If you fail to browse a real OPC UA server, it is because the server does not trust the module client certificate. For now, please follow the server's manual to move the rejected certificate into the trust list and try again.
- Once you have an application and endpoints, execute
Registry: Get endpoints
to get your base endpoint ID for the OPC PLC. This will be one where theendpointUrl
is on port 50000. Copy theid
(endpoint id). - Execute the request
Twin: Browse nodes
, using theid
in the URL. - Copy the
nodeId
of the objects (displayName: Objects) node. In our case this wasi=85
. - Execute the request
Twin: Browse target node
, replace the ID in the URL with theid
from above, and add the node ID to the query parameter. If the node id includes special characters such as a '/' or ':' then you should URL Encode the parameter by right-clicking the selection and choosing the Postman option 'EncodeUriComponent'. - From the result, copy
nodeId
of the OpcPlc target node. - Execute the same request, but now use the above
nodeId
value in the request parameter. Remember to URI-encode. - From the result, copy the
nodeId
for Telemetry target node. Repeat the above with thisnodeId
. This should give you a set of target nodes with variables, their data and values.
- From the above last step, copy the
nodeId
of the variableSpikeData
. - Execute the request
Twin: Read value
. Replace the ID in the URI with theendpointId
. - You should see the current value of the variable.
- This step is about remotely configuring the OPC Publisher.
- Execute the request
Twin: Get published nodes
, replace the endpointId in the URL with yourapplicationId
. - This will show an empty list if you have not yet published any nodes.
- Execute the request
Twin: Publish node
, make sure the URI has yourapplicationId
, and update the request's 'Body' to reflect the node you wish to publish. - Re-execute the request
Twin: Get published nodes
.
Note the applicationId
by executing the request Registry: Get applications
. Execute the request Registry: Remove application
, make sure to change the applicaitonId in the URI.
Note: This step does not remove the published nodes from the OPC Publisher.
Try to run your own OPC UA server in the same network as your IoT Edge gateway and follow the instructions above with the relevant variations (e.g. discovery URL).
- See the architectural flow diagrams for more information.
- Explore Microservices REST APIs