Skip to content

MQTT Bridge Example

The MQTT Bridge example is located in the examples/mqtt_bridge directory. This example shows how to use the MQTT Bridge component from inside a container using JSON configuration files.

The example uses the following instances of the XRT application, each with its own configuration:

  • bridge_pub. This is used to publish data available on the Bus and re-publish the data using an MQTT client

    The main configuration is located in the config/bridge_pub/main.json file. The corresponding MQTT Bridge component for one node is configured using the bridgepub.json file.

  • bridge_sub. This is used to subscribe to the same MQTT topic. When it receives data, it re-publishes on another Bus

    The main configuration is located in the config/bridge_sub/main.json file. The corresponding MQTT Bridge component for one node is configured using the bridgesub.json file. For further information on the configuration of the MQTT Bridge component, see MQTT Bridge Component

Both applications use component factories to manage components. The registered factories are as follows:

  • Logger component
  • ThreadPool component
  • Scheduler component
  • Core Bus component
  • Virtual Device Service component. This publishes data on the Bus
  • MQTT Bridge component. This subscribes to the Bus and publishes using an MQTT client
  • Log Exporter component. There are two instances of this component for each configuration; that is, for bridge_pub and bridge_sub. One subscribes to the data published by the Virtual Device Service, and outputs the input to a node on the bridge.

The other subscribes to data published by the same node of the bridge, which has been received from another node, and outputs the output from a node on the bridge

Notes

  1. You must start the Mosquitto broker to run this example.
  2. In the provided configuration, MQTTVersion is set to 5. This is necessary to avoid loopback of messages; that is, to prevent a publisher from receiving it's own publications.
  3. There are three instances of the Logger component for each configuration. One is configured to Info level and is used by the Log Exporter component that logs the messages available to the MQTT Bridge as input. The second is configured to log Debug level messages and is used by the MQTT Bridge. The third is configured to log Warning level messages for reduced verbosity and is used by the other components in this example.

You must set the following environment variable:

Environment Variable Description
XRT_MQTT_BROKER The address of the MQTT server
If mosquitto is running locally, set to tcp://127.0.0.1:1883

This example requires two instances of the XRT application to represent two nodes of the bridge. One loads the configuration from bridge_pub and the other from bridge_sub. Both instances communicate with the MQTT broker, which is setup using the environment variable described above. The instance using the bridge_pub configuration publishes data on the Bus on the test/data MQTT Topic and subscribes to receive data that matches the bridge/# pattern. The instance using the bridge_sub configuration publishes data on the Bus on the bridge/data MQTT Topic and subscribes to receive data that matches the test/# pattern.

To run the MQTT Bridge component example for five seconds, enter the following commands on one terminal:

cd examples/mqtt_bridge
xrt config/bridge_pub 5

Then enter the following commands on another terminal:

cd examples/mqtt_bridge
xrt config/bridge_sub 5

If successful, the output from the terminal running the bridge_pub configuration is similar to the following:

[xrt:1622560780700971:console:Info] iot_threadpool_alloc (threads: 1 max_jobs: 500 default_priority: -1 affinity: -1)
[iot-1-0:1622560780701101:console:Debug] Thread iot-1-0 starting
[iot-0-0:1622560780701869:console:Info] test/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":123,"type":"uint32"},"Uptime": {"value":456789,"type":"uint64"}}}
[xrt:1622560780832560:console:Info] xrt_mqtt_paho_connect: MQTT client connected
[iot-0-1:1622560781702122:console:Debug] MQTTBridge Publish on MQTT Topic test/data
[iot-0-1:1622560781702180:console:Debug] iot_threadpool_add_work jobs/max: 1/500
[iot-0-1:1622560781702193:console:Info] test/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":122,"type":"uint32"},"Uptime": {"value":456790,"type":"uint64"}}}
[xrt:1622560782507272:console:Debug] MQTT Msg received: {"device":"xrt-exampledevice-2","resource":"SensorOne","readings":{"Origin": {"value":"Sensor55","type":"string"},"Temp":{"value":122,"type":"uint32"},"Uptime": {"value":456790,"type":"uint64"}}}
[xrt:1622560782507333:console:Debug] MQTTBridge Creating bus publisher for topic: bridge/data
[xrt:1622560782507363:console:Debug] MQTTBridge Publishing on Bus Topic: bridge/data
[xrt:1622560782507529:console:Info] bridge/data:{"device":"xrt-example-device2","resource":"SensorOne","readings":{"Origin": {"value":"Sensor55","type":"string"},"Temp":{"value":122,"type":"uint32"},"Uptime": {"value":456790,"type":"uint64"}}}
[iot-0-0:1622560782702121:console:Debug] MQTTBridge Publish on MQTT Topic test/data
[iot-0-0:1622560782702184:console:Debug] iot_threadpool_add_work jobs/max: 1/500
...
In this output, one node of the application publishes data on the test/data topic and receives MQTT messages from another node, which are republished on the bridge/datatopic.

The output from terminal running the bridge_sub configuration is similar to the following:

[xrt:1622560781505404:console:Info] iot_threadpool_alloc (threads: 1 max_jobs: 500 default_priority: -1 affinity: -1)
[iot-1-0:1622560781505556:console:Debug] Thread iot-1-0 starting
[iot-0-0:1622560781506521:console:Info] bridge/data:{"device":"xrt-example-device2","resource":"SensorOne","readings":{"Origin": {"value":"Sensor55","type":"string"},"Temp":{"value":123,"type":"uint32"},"Uptime": {"value":456789,"type":"uint64"}}}
[xrt:1622560781637163:console:Info] xrt_mqtt_paho_connect: MQTT client connected
[xrt:1622560781702619:console:Debug] MQTT Msg received: {"device":"xrt-exampledevice-1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":122,"type":"uint32"},"Uptime": {"value":456790,"type":"uint64"}}}
[xrt:1622560781702657:console:Debug] MQTTBridge Creating bus publisher for topic: test/data
[xrt:1622560781702668:console:Debug] MQTTBridge Publishing on Bus Topic: test/data [xrt:1622560781702846:console:Info] test/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":122,"type":"uint32"},"Uptime": {"value":456790,"type":"uint64"}}}
[iot-0-1:1622560782506757:console:Info] bridge/data:{"device":"xrt-example-device2","resource":"SensorOne","readings":{"Origin": {"value":"Sensor55","type":"string"},"Temp":{"value":122,"type":"uint32"},"Uptime": {"value":456790,"type":"uint64"}}}
[iot-0-1:1622560782506791:console:Debug] MQTTBridge Publish on MQTT Topic bridge/data
[iot-0-1:1622560782506823:console:Debug] iot_threadpool_add_work jobs/max: 1/500
In this output, one node of the application publishes data on the bridge/data topic and receives MQTT messages from another node, which are republished on the test/datatopic.

Back to top