Skip to content

MQTT Exporter Examples

The MQTT exporter examples are located in the examples/mqtt_exporter directory. The examples show how to use the MQTT Exporter component from inside a container using JSON configuration files.

The MQTT Exporter component can be used to export data to the following:

Example MQTT Topic Export

This example uses the Mosquitto broker as a server to publish data from the MQTT exporter component. It is located in the examples/mqtt_exporter/mosquitto directory.

The container uses 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 Exporter component. This subscribes to the Bus and exports data that matches the subscription pattern
  • Log Exporter component. This subscribes to the data published by the Virtual Device Service on the same topic set by the MQTT Exporter component, and outputs it to the log

The container, uses component factories to manage components, which are specified in the config/main.json file. The MQTT exporter component is configured using the mqtt_exporter.json file. For further information on the configuration of the MQTT exporter component, see MQTT Exporter Component.

Note

There are three instances of the Logger component. On is configured to Info level and is used by the Log Exporter component that logs the messages available to the MQTT Exporter as input. The second is configured to log Debug level messages and is used by the MQTT exporter. 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

The MQTT exporter component connects to the MQTT broker and exports data, which is available on the Bus and matches the device/# pattern, on the test MQTT Topic.

Note

You must start the Mosquitto broker to run this example. By default, the configuration points to a Mosquitto server running locally on port 1883.

To run this MQTT exporter component example for five seconds using the Mosquitto broker, enter the following commands:

cd examples/mqtt_exporter/mosquitto
xrt config 5
If successful, the output is similar to the following:

[xrt:1622556305591448:console:Info] iot_threadpool_alloc (threads: 1 max_jobs: 500 default_priority: -1 affinity: -1)
[iot-1-0:1622556305592019:console:Debug] Thread iot-1-0 starting
[iot-0-1:1622556305593527:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":123,"type":"uint32"},"Uptime": {"value":456789,"type":"uint64"}}}
[xrt:1622556305726812:console:Info] xrt_mqtt_paho_connect: MQTT client connected
[iot-0-0:1622556306093264:console:Info] device/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-0:1622556306093298:console:Debug] iot_threadpool_add_work jobs/max: 1/500
[iot-0-1:1622556306593692:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":121,"type":"uint32"},"Uptime": {"value":456791,"type":"uint64"}}}
[iot-0-1:1622556306593738:console:Debug] iot_threadpool_add_work jobs/max: 1/500
[iot-0-0:1622556307093551:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":120,"type":"uint32"},"Uptime": {"value":456792,"type":"uint64"}}}
[iot-0-0:1622556307093590:console:Debug] iot_threadpool_add_work jobs/max: 1/500
[iot-0-1:1622556307593690:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":119,"type":"uint32"},"Uptime": {"value":456793,"type":"uint64"}}}
[iot-0-1:1622556307593729:console:Debug] iot_threadpool_add_work jobs/max: 1/500
...
In this output, the messages from the Virtual Device Service are available on the device/data topic, which is the input to the MQTT Exporter component, and the data exported entry is the output from the MQTT exporter.

Example MQTT Export to Google Cloud

This example publishes data from the MQTT exporter component to Google Cloud. It is located in the examples/mqtt_exporter/google directory.

The container uses 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 Exporter component. This subscribes to the Bus and exports data that matches the subscription pattern to Google Cloud
  • Log Exporter component. This subscribes to the data published by the Virtual Device Service on the same topic set by the MQTT Exporter component, and outputs it to the log

Note

There are three instances of the Logger component. One is configured to Info level and is used by the Log Exporter component that logs the messages available to the InfluxDB Exporter as input. The second is configured to log Debug level messages and is used by the MQTT exporter. The third is configured to log Warning level messages for reduced verbosity and is used by the other components in this example.

The MQTT exporter component is configured using the export_google.json file. For further information on the configuration of the MQTT exporter component for Google Cloud, see MQTT Exporter Component and Export Data to Google Cloud

You must set the following environment variables:

Environment Variable Description
MQTT_EXPORTER_GOOGLE_CLIENT_ID The client ID used in Google Cloud
MQTT_EXPORTER_GOOGLE_PROJECT_ID The name of the created project
MQTT_EXPORTER_GOOGLE_TRUST_STORE The path to the roots.pem file downloaded from Google Cloud
MQTT_EXPORTER_GOOGLE_PRIVATE_KEY The generated 2048-bit RSA key pair private key
MQTT_EXPORTER_GOOGLE_PUB_TOPIC The name of the topic on which data is published

To run this MQTT exporter component example for five seconds, enter the following commands:

cd examples/mqtt_exporter/google
xrt config 5

If successful, the output is similar to the following:

[xrt:1622556350665582:console:Info] iot_threadpool_alloc (threads: 1 max_jobs: 500 default_priority: -1 affinity: -1)
[iot-1-0:1622556350665664:console:Debug] Thread iot-1-0 starting
[iot-0-0:1622556350666598:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":123,"type":"uint32"},"Uptime": {"value":456789,"type":"uint64"}}}
[xrt:1622556351044313:console:Info] xrt_mqtt_paho_connect: MQTT client connected
[iot-0-1:1622556351166763:console:Info] device/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:1622556351166819:console:Debug] iot_threadpool_add_work jobs/max: 1/500
[iot-0-0:1622556351666800:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":121,"type":"uint32"},"Uptime": {"value":456791,"type":"uint64"}}}
[iot-0-0:1622556351666843:console:Debug] iot_threadpool_add_work jobs/max: 1/500
[iot-0-1:1622556352166965:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":120,"type":"uint32"},"Uptime": {"value":456792,"type":"uint64"}}}
[iot-0-1:1622556352167001:console:Debug] iot_threadpool_add_work jobs/max: 1/500
[iot-0-0:1622556352667113:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor54","type":"string"},"Temp":{"value":119,"type":"uint32"},"Uptime": {"value":456793,"type":"uint64"}}}
[iot-0-0:1622556352667152:console:Debug] iot_threadpool_add_work jobs/max: 1/500
...
In this output, the messages from the Virtual Device Service are available on the device/data topic, which is the input to the MQTT Exporter component, and the data exported entry is the output from the MQTT exporter.

Note

You can also verify the published data on the Google Cloud Monitoring console.

Example MQTT Export to AWS

This example publishes data from the MQTT exporter component to AWS IoT Core. It is located in the examples/mqtt_exporter/aws directory.

The container uses 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 Exporter component. This subscribes to the Bus and exports data that matches the subscription pattern to AWS IoT
  • Log Exporter component. This subscribes to the data published by the Virtual Device Service on the same topic set by the MQTT Exporter component, and outputs it to the log

Note

There are three instances of the Logger component. One is configured to Info level and is used by the Log Exporter component that logs the messages available to the InfluxDB Exporter as input. The second is configured to log Debug level messages and is used by the MQTT exporter. The third is configured to log Warning level messages for reduced verbosity and is used by the other components in this example.

The MQTT exporter component is configured using the export_aws.json file. For further information on the configuration of the MQTT exporter component for AWS, see MQTT Exporter Component and Export Data to AWS

You must set the following environment variables:

Environment Variable Description
MQTT_EXPORTER_AWS_CLIENT_ID The identifier of a Thing in AWS
MQTT_EXPORTER_AWS_TRUST_STORE The path to the downloaded root CA file for AWS
MQTT_EXPORTER_AWS_KEY_STORE The path to the generated certificate for the Thing
MQTT_EXPORTER_AWS_PRIVATE_KEY The path to the generated private key for the Thing

To run this MQTT exporter component example for five seconds, enter the following commands:

cd examples/mqtt_exporter/aws
xrt config 5
If successful, the output is similar to the following:

[xrt:1622556335646880:console:Info] iot_threadpool_alloc (threads: 1 max_jobs: 500 default_priority: -1 affinity: -1)
[iot-1-0:1622556335647005:console:Debug] Thread iot-1-0 starting
[iot-0-1:1622556335647743:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor55","type":"string"},"Temp":{"value":345,"type":"uint32"},"Uptime": {"value":67891,"type":"uint64"}}}
[iot-0-0:1622556336147905:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor55","type":"string"},"Temp":{"value":344,"type":"uint32"},"Uptime": {"value":67892,"type":"uint64"}}}
[xrt:1622556336196823:console:Info] xrt_mqtt_paho_connect: MQTT client connected
[iot-0-1:1622556336648047:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor55","type":"string"},"Temp":{"value":343,"type":"uint32"},"Uptime": {"value":67893,"type":"uint64"}}}
[iot-0-1:1622556336648121:console:Debug] iot_threadpool_add_work jobs/max: 1/500
[iot-0-0:1622556337148166:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor55","type":"string"},"Temp":{"value":342,"type":"uint32"},"Uptime": {"value":67894,"type":"uint64"}}}
[iot-0-0:1622556337148220:console:Debug] iot_threadpool_add_work jobs/max: 1/500
[iot-0-1:1622556337648280:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor55","type":"string"},"Temp":{"value":341,"type":"uint32"},"Uptime": {"value":67895,"type":"uint64"}}}
[iot-0-1:1622556337648313:console:Debug] iot_threadpool_add_work jobs/max: 1/500
[iot-0-0:1622556338148417:console:Info] device/data:{"device":"xrt-example-device1","resource":"SensorOne","readings":{"Origin": {"value":"Sensor55","type":"string"},"Temp":{"value":340,"type":"uint32"},"Uptime": {"value":67896,"type":"uint64"}}}
...
In this output, the messages from the Virtual Device Service are available on the device/data topic, which is the input to the MQTT Exporter component, and the data exported entry is the output from the MQTT exporter.

Note

You can also verify the published data on the AWS IoT console. To do this, select the IoT Core service, and then select Test.

Back to top