Skip to content
Start free evaluation

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_pub so that the simulator subscriber receives it.
  • See the simulator reject messages whose Content Type or UAMessageType is missing or wrong.

You need these rules whenever another system has to send data to the simulator.

  • The mqtt-lab directory and local.conf from Run a local Mosquitto broker
  • bin/pdcomm-opcua-headless from the PDComm OPC UA simulator package
  • Three terminal windows
  1. In the first terminal, start the broker from the mqtt-lab directory:

    터미널 창
    mosquitto -c local.conf
  2. 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 to opcua/uadp/data/77/demo.

  3. In the third terminal, in the mqtt-lab directory, 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

    -N leaves out the newline after the payload, so the file holds only the payload bytes.

  4. 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_sub in the third terminal receives the message and exits. Look at the file:

    터미널 창
    xxd dataset.bin
    00000000: 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 ..*........hello

    UADP is little-endian: 4d 00 is PublisherId 77 and 11 00 is DataSetWriterId 17. At the end, 06 2a 00 00 00 is the Int32 value 42, and 0c 05 00 00 00 68 65 6c 6c 6f is the String value hello.

  5. In the second terminal, take the received message out of the simulator’s queue:

    {"action":"pubsub_received"}

    messages holds the message you just published. If it’s empty, run the command once more.

  6. 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.bin

    Check 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, so rejected becomes 1 and error is OPC UA PubSub Malformed.

  7. 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 sequence is still 1. A receiver can use the sequence number to recognize a repeated message.

  8. 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 error in {"action":"pubsub_status"} is now OPC 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 hello

    This time the error is OPC UA PubSub Unsupported and rejected is 3. The properties are right, but the payload isn’t a UADP NetworkMessage.

  1. Send {"action":"shutdown"} in the second terminal to stop the simulator.
  2. Stop the broker in the first terminal with Ctrl+C.
  3. Delete dataset.bin if you don’t need it.
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.