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.
What you need
Section titled “What you need”- The
appspackage and theOPCUA_APPSvariable 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.
1. Start the broker
Section titled “1. Start the broker”In the first terminal, run Mosquitto on port 1883:
mosquitto -p 1883Without 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.
2. Publish and receive in one process
Section titled “2. Publish and receive in one process”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"}3. Split subscriber and publisher
Section titled “3. Split subscriber and publisher”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"}}4. Check the subscriber
Section titled “4. Check the subscriber”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]}]}}5. Clean up
Section titled “5. Clean up”Type {"action":"shutdown"} into both simulators and press Ctrl+C in the broker’s terminal.
Common errors
Section titled “Common errors”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.