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.
Before you begin
Section titled “Before you begin”- The PDComm OPC UA simulator package. You use
bin/pdcomm-opcua-headlessin the extracted directory. To install the package, follow the OPC UA documentation. - The Eclipse Mosquitto 2.x broker and its
mosquitto_subclient - Three terminal windows
-
Install Mosquitto.
On macOS, install it with Homebrew:
터미널 창 brew install mosquittoOn Linux, use your distribution’s packages; Debian and Ubuntu call them
mosquittoandmosquitto-clients. On Windows, use the installer from mosquitto.org.Check the installation:
터미널 창 mosquitto_sub --versionmosquitto_sub version 2.1.2 running on libmosquitto 2.1.0. -
In the first terminal, start the broker:
터미널 창 mosquitto -vWithout 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 startingUsing 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 runningMosquitto prefixes each line with a timestamp, which the log examples in these pages leave out.
-
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 5connects with MQTT 5.0 so that message properties are visible.-Fprints the topic, QoS, RETAIN flag, Content Type, user properties and payload length on one line. -
In the third terminal, go to the simulator package directory and run the headless simulator:
터미널 창 bin/pdcomm-opcua-headlessThe 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. -
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":truemeans the configuration is in place. Withroleset toboth, this simulator both publishes and subscribes. -
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-17opcua/uadp/data/77/quickstart (QoS 1)p5means MQTT 5.0,c1means Clean Start, andk30means a keep alive of 30 seconds. -
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 bytesmosquitto_subsubscribed at QoS 0, so it receivesqos=0. The Content Type andUAMessageTypeare the MQTT 5.0 properties that OPC UA PubSub defines. -
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
messagesis empty, run the command again a moment later.pubsub_receivedtakes the messages out of the queue, so each message appears only once. -
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"}}sentcounts publications,acknowledgedcounts the ones the broker confirmed with PUBACK, andreceivedcounts the messages the subscriber accepted. -
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_suband the broker.
Common errors
Section titled “Common errors”| 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.
What’s next
Section titled “What’s next”- Run a local Mosquitto broker shows QoS, retained messages and Will messages hands-on.
- OPC UA PubSub over MQTT splits the publisher and the subscriber into separate processes.
- The simulator PubSub tab does the same in the GUI.