Skip to content
Start free evaluation

MQTT quickstart

In about ten minutes, you send one OPC UA PubSub message through an MQTT broker and receive it back. A single simulator acts as both publisher and subscriber, and mosquitto_sub shows you what passes through the broker.

  • The PDComm OPC UA simulator package. You use bin/pdcomm-opcua-headless in the extracted directory. To install the package, follow the OPC UA documentation.
  • The Eclipse Mosquitto 2.x broker and its mosquitto_sub client
  • Three terminal windows
  1. Install Mosquitto.

    On macOS, install it with Homebrew:

    터미널 창
    brew install mosquitto

    On Linux, use your distribution’s packages; Debian and Ubuntu call them mosquitto and mosquitto-clients. On Windows, use the installer from mosquitto.org.

    Check the installation:

    터미널 창
    mosquitto_sub --version
    mosquitto_sub version 2.1.2 running on libmosquitto 2.1.0.
  2. In the first terminal, start the broker:

    터미널 창
    mosquitto -v

    Without a configuration file, Mosquitto runs in local only mode. It accepts connections on port 1883 from this computer only, and requires no authentication.

    mosquitto version 2.1.2 starting
    Using default config.
    Starting in local only mode. Connections will only be possible from clients running on this machine.
    Opening ipv4 listen socket on port 1883.
    Opening ipv6 listen socket on port 1883.
    mosquitto version 2.1.2 running

    Mosquitto prefixes each line with a timestamp, which the log examples in these pages leave out.

  3. In the second terminal, watch the OPC UA messages that pass through the broker:

    터미널 창
    mosquitto_sub -V 5 -t 'opcua/#' -F '%t qos=%q retain=%r content-type=%C user-properties=%P %l bytes'

    -V 5 connects with MQTT 5.0 so that message properties are visible. -F prints the topic, QoS, RETAIN flag, Content Type, user properties and payload length on one line.

  4. In the third terminal, go to the simulator package directory and run the headless simulator:

    터미널 창
    bin/pdcomm-opcua-headless

    The headless simulator reads one JSON command per line from standard input and prints one result line for each. On Windows, run bin\pdcomm-opcua-headless.exe.

  5. Paste this line to configure PubSub over MQTT:

    {"action":"configure","config":{"pubsub":{"transport":"mqtt","role":"both","broker":"127.0.0.1:1883","topic":"opcua/uadp/data/77/quickstart"}}}

    A long line that starts with "ok":true means the configuration is in place. With role set to both, this simulator both publishes and subscribes.

  6. Start PubSub:

    {"action":"pubsub_start"}
    {"ok":true,"result":{"acknowledged":0,"broker":"127.0.0.1:1883","destination":null,"dropped":0,"error":null,"listen":null,"queued":0,"received":0,"rejected":0,"role":"both","running":true,"sent":0,"topic":"opcua/uadp/data/77/quickstart","transport":"mqtt"}}

    The broker log in the first terminal shows the simulator connecting and subscribing:

    New client connected from 127.0.0.1:64658 as pdcomm-opcua-sim-79619-17 (p5, c1, k30).
    Received SUBSCRIBE from pdcomm-opcua-sim-79619-17
    opcua/uadp/data/77/quickstart (QoS 1)

    p5 means MQTT 5.0, c1 means Clean Start, and k30 means a keep alive of 30 seconds.

  7. Publish one DataSet:

    {"action":"pubsub_publish","fields":[{"type":"Int32","value":42},{"type":"String","value":"hello"}]}
    {"ok":true,"result":{"bytes":48,"fields":2,"sequence":1}}

    One line appears in the second terminal:

    opcua/uadp/data/77/quickstart qos=0 retain=0 content-type=application/opcua+uadp user-properties=UAMessageType:ua-data 48 bytes

    mosquitto_sub subscribed at QoS 0, so it receives qos=0. The Content Type and UAMessageType are the MQTT 5.0 properties that OPC UA PubSub defines.

  8. Check that the simulator received its own message back from the broker:

    {"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]}]}}

    If messages is empty, run the command again a moment later. pubsub_received takes the messages out of the queue, so each message appears only once.

  9. Check the status:

    {"action":"pubsub_status"}
    {"ok":true,"result":{"acknowledged":1,"broker":"127.0.0.1:1883","destination":null,"dropped":0,"error":null,"listen":null,"queued":0,"received":1,"rejected":0,"role":"both","running":true,"sent":1,"topic":"opcua/uadp/data/77/quickstart","transport":"mqtt"}}

    sent counts publications, acknowledged counts the ones the broker confirmed with PUBACK, and received counts the messages the subscriber accepted.

  10. Clean up:

    {"action":"shutdown"}
    {"ok":true,"result":{"client_closed":null,"pubsub_stopped":true,"server_stopped":false}}

    The headless simulator exits. Press Ctrl+C in the other two terminals to stop mosquitto_sub and the broker.

Output Cause Fix
"error":"MQTT broker connection failed: MQTT transport I/O: ConnectionRefused" The broker isn’t running, or it uses another port. Check that the broker from step 2 is running on port 1883.
"error":"MQTT broker address must be an IP socket address" You used a host name such as localhost:1883. Use an IP address, such as 127.0.0.1:1883.
"error":"PubSub is already running" PubSub is already started. Send {"action":"pubsub_stop"}, then start again.
Nothing appears in mosquitto_sub The filter doesn’t match the topic. Compare the filter opcua/# with the topic opcua/uadp/data/77/quickstart.

For any other error, see MQTT troubleshooting.