Skip to content
Start free evaluation

PubSub over MQTT

  • Run a local MQTT broker.
  • Publish over MQTT from one simulator and receive the message back.
  • Exchange messages between a publisher process and a subscriber process on the same topic.
  • The apps package and the OPCUA_APPS variable from the lab setup.
  • A broker that supports MQTT 5. This page was run with Eclipse Mosquitto 2.1.2.
  • Three terminals.

The simulator’s MQTT transport uses MQTT 5 with QoS 1 (at least once). The payload is the same UADP NetworkMessage as over UDP.

In the first terminal, run Mosquitto on port 1883:

터미널 창
mosquitto -p 1883

Without a configuration file, Mosquitto 2 accepts connections from this computer only and allows anonymous clients. It’s ready when the output shows mosquitto version 2.1.2 running. If a broker already listens on port 1883, you can use that one instead.

In the second terminal, start the simulator:

터미널 창
"$OPCUA_APPS/bin/pdcomm-opcua-headless"

Switch the transport to mqtt. The broker defaults to 127.0.0.1:1883 and the topic to pdcomm/opcua/simulator.

{"action":"configure","config":{"pubsub":{"transport":"mqtt","broker":"127.0.0.1:1883"}}}
{"action":"pubsub_start"}
{"ok":true,"result":{"acknowledged":0,"destination":null,"dropped":0,"error":null,"listen":null,"queued":0,"received":0,"rejected":0,"role":"both","running":true,"sent":0,"transport":"mqtt"}}

With the both role, the simulator subscribes to the topic and publishes to it:

{"action":"pubsub_publish"}
{"ok":true,"result":{"bytes":60,"fields":4,"sequence":1}}
{"action":"pubsub_status"}
{"ok":true,"result":{"acknowledged":1,"destination":null,"dropped":0,"error":null,"listen":null,"queued":1,"received":1,"rejected":0,"role":"both","running":true,"sent":1,"transport":"mqtt"}}

acknowledged counts the messages the broker confirmed with a QoS 1 PUBACK. Take the message that came back through the broker:

{"action":"pubsub_received"}
{"ok":true,"result":{"messages":[{"bytes":60,"datasets":[{"fields":[{"type":"Boolean","value":true},{"type":"Int32","value":42},{"type":"Double","value":3.5},{"type":"String","value":"pdcomm"}],"major":1,"minor":1,"sequence":1}],"publisher_id":77,"source":"mqtt","writer_group":1,"writer_ids":[17]}]}}

A source of mqtt means the message came from the broker. End the program:

{"action":"shutdown"}

In the second terminal, start the simulator again as a subscriber on the topic plant/line1/opcua:

터미널 창
"$OPCUA_APPS/bin/pdcomm-opcua-headless"
{"action":"configure","config":{"pubsub":{"transport":"mqtt","role":"subscriber","broker":"127.0.0.1:1883","topic":"plant/line1/opcua"}}}
{"action":"pubsub_start"}
{"ok":true,"result":{"acknowledged":0,"destination":null,"dropped":0,"error":null,"listen":null,"queued":0,"received":0,"rejected":0,"role":"subscriber","running":true,"sent":0,"transport":"mqtt"}}

In the third terminal, start another simulator as the publisher:

터미널 창
"$OPCUA_APPS/bin/pdcomm-opcua-headless"
{"action":"configure","config":{"pubsub":{"transport":"mqtt","role":"publisher","broker":"127.0.0.1:1883","topic":"plant/line1/opcua"}}}
{"action":"pubsub_start"}
{"action":"pubsub_publish","fields":[{"type":"Double","value":21.5},{"type":"String","value":"line-1"}]}
{"ok":true,"result":{"bytes":53,"fields":2,"sequence":1}}
{"action":"pubsub_status"}
{"ok":true,"result":{"acknowledged":1,"destination":null,"dropped":0,"error":null,"listen":null,"queued":0,"received":0,"rejected":0,"role":"publisher","running":true,"sent":1,"transport":"mqtt"}}

In the second terminal, take the received messages:

{"action":"pubsub_received"}
{"ok":true,"result":{"messages":[{"bytes":53,"datasets":[{"fields":[{"type":"Double","value":21.5},{"type":"String","value":"line-1"}],"major":1,"minor":1,"sequence":1}],"publisher_id":77,"source":"mqtt","writer_group":1,"writer_ids":[17]}]}}

Type {"action":"shutdown"} into both simulators and press Ctrl+C in the broker’s terminal.

error in the reply Cause and fix
MQTT broker connection failed: MQTT transport I/O: ConnectionRefused No broker listens at the broker address. Start the broker and check the address and port.
MQTT topic must be 1 to 256 bytes without wildcards A publish topic can’t contain wildcards (#, +).
MQTT broker address must be an IP socket address Write the broker address as an IP address and port, such as 127.0.0.1:1883; host names aren’t accepted.

The simulator’s MQTT connection uses no user name, password or TLS, so it can’t connect to brokers that require authentication. In the GUI, change Transport to UADP over MQTT on the PubSub tab.