Publish and subscribe with the simulator
When you finish this tutorial, you can:
- Capture a UADP payload published by the simulator in a file and read its structure.
- Send the same payload again with
mosquitto_pubso that the simulator subscriber receives it. - See the simulator reject messages whose Content Type or
UAMessageTypeis missing or wrong.
You need these rules whenever another system has to send data to the simulator.
Before you begin
Section titled “Before you begin”- The
mqtt-labdirectory andlocal.conffrom Run a local Mosquitto broker bin/pdcomm-opcua-headlessfrom the PDComm OPC UA simulator package- Three terminal windows
-
In the first terminal, start the broker from the
mqtt-labdirectory:터미널 창 mosquitto -c local.conf -
In the second terminal, go to the simulator package directory, run the headless simulator, and paste these two lines:
터미널 창 bin/pdcomm-opcua-headless{"action":"configure","config":{"pubsub":{"transport":"mqtt","role":"both","broker":"127.0.0.1:1883","topic":"opcua/uadp/data/77/demo"}}}{"action":"pubsub_start"}When the result of the second line contains
"running":true, the simulator is connected and subscribed toopcua/uadp/data/77/demo. -
In the third terminal, in the
mqtt-labdirectory, wait for the next message and save it to a file:터미널 창 mosquitto_sub -V 5 -t opcua/uadp/data/77/demo -C 1 -N > dataset.bin-Nleaves out the newline after the payload, so the file holds only the payload bytes. -
In the second terminal, publish one DataSet:
{"action":"pubsub_publish","fields":[{"type":"Int32","value":42},{"type":"String","value":"hello"}]}{"ok":true,"result":{"bytes":48,"fields":2,"sequence":1}}mosquitto_subin the third terminal receives the message and exits. Look at the file:터미널 창 xxd dataset.bin00000000: f101 4d00 0f01 0001 0000 0001 0001 0001 ..M.............00000010: 1100 7901 0000 0001 0000 0001 0000 0002 ..y.............00000020: 0006 2a00 0000 0c05 0000 0068 656c 6c6f ..*........helloUADP is little-endian:
4d 00is PublisherId 77 and11 00is DataSetWriterId 17. At the end,06 2a 00 00 00is the Int32 value 42, and0c 05 00 00 00 68 65 6c 6c 6fis the String valuehello. -
In the second terminal, take the received message out of the simulator’s queue:
{"action":"pubsub_received"}messagesholds the message you just published. If it’s empty, run the command once more. -
In the third terminal, publish the same payload without any properties:
터미널 창 mosquitto_pub -V 5 -q 1 -t opcua/uadp/data/77/demo -f dataset.binCheck the status in the second terminal:
{"action":"pubsub_status"}{"ok":true,"result":{"acknowledged":1,"broker":"127.0.0.1:1883","destination":null,"dropped":0,"error":"OPC UA PubSub Malformed","listen":null,"queued":0,"received":1,"rejected":1,"role":"both","running":true,"sent":1,"topic":"opcua/uadp/data/77/demo","transport":"mqtt"}}The payload is valid UADP, but the MQTT 5.0 message has no Content Type and no
UAMessageType, sorejectedbecomes 1 anderrorisOPC UA PubSub Malformed. -
Now publish it with both properties:
터미널 창 mosquitto_pub -V 5 -q 1 -t opcua/uadp/data/77/demo \-D publish content-type application/opcua+uadp \-D publish user-property UAMessageType ua-data \-f dataset.bin-D publish <property> <value>adds an MQTT 5.0 property to the PUBLISH packet. Check the second terminal:{"action":"pubsub_received"}{"ok":true,"result":{"messages":[{"bytes":48,"datasets":[{"fields":[{"type":"Int32","value":42},{"type":"String","value":"hello"}],"major":1,"minor":1,"sequence":1}],"publisher_id":77,"source":"mqtt","writer_group":1,"writer_ids":[17]}]}}You sent the same bytes again, so the
sequenceis still 1. A receiver can use the sequence number to recognize a repeated message. -
Try two property values that don’t match:
터미널 창 mosquitto_pub -V 5 -q 1 -t opcua/uadp/data/77/demo \-D publish content-type application/json \-D publish user-property UAMessageType ua-data \-m '{}'The
errorin{"action":"pubsub_status"}is nowOPC UA PubSub Conflict: the Content Type doesn’t match the encoding of this subscription (UADP).터미널 창 mosquitto_pub -V 5 -q 1 -t opcua/uadp/data/77/demo \-D publish content-type application/opcua+uadp \-D publish user-property UAMessageType ua-data \-m helloThis time the
errorisOPC UA PubSub Unsupportedandrejectedis 3. The properties are right, but the payload isn’t a UADP NetworkMessage.
Clean up
Section titled “Clean up”- Send
{"action":"shutdown"}in the second terminal to stop the simulator. - Stop the broker in the first terminal with Ctrl+C.
- Delete
dataset.binif you don’t need it.
Why a message is rejected
Section titled “Why a message is rejected”error |
Meaning |
|---|---|
OPC UA PubSub Malformed |
The Content Type or UAMessageType is missing or badly formed. |
OPC UA PubSub Conflict |
A property value doesn’t match the encoding (UADP) or message type (ua-data) of this subscription. |
OPC UA PubSub Unsupported |
The payload can’t be read as a UADP NetworkMessage. |
Delivery problems has the complete list.