MQTT - Export Service Example

This topic provides instructions on how to setup and run the MQTT export service. This guide makes use of Eclipse Mosquitto, which provides an example MQTT publisher and subscriber. You must install Mosquitto to complete this example.

This example assumes that the Edge Xpert services are running and data is being read with at least the --xpert-manager, --device-virtual and --mqtt-broker parameters. A suitable command would be as follows:

edgexpert up --xpert-manager --device-virtual --mqtt-broker

In this example, we do the following:

Note

In this example, we export the data using MQTT to a local MQTT broker provided by Edge Xpert. If you want to export to a different MQTT broker, either at the edge or in the cloud, you must set the Address, Port and Encryption Method to match your broker.

Creating the Export Registration

You can use EdgeXpert Manager or a cURL command to create the export registration, as described in the following sections.

Creating the Export Registration Using Edge Xpert Manager

To create the export registration using Edge Xpert Manager, complete the following steps:

  1. In the Edge Xpert Manager, select EXPORT from the main menu

  2. Select the Add (+) icon, as illustrated below:

    Export Pane
  3. On the first page of the Export Wizard, complete the following information:

Add Export Fields
Field Value Comments
Destination MQTT Topic Select from drop-down options
Name MQTTExport Enter in text box
Enable Set When selected, the toggle switch is to the right and is highlighted in blue
Encryption method None Select from drop-down options
Compression method None Select from drop-down options
Export Format JSON Select from drop-down options
  1. Select the Next button. Selecting the Cancel button dismisses the Export Wizard without creating the export registration
  2. On the second page of the Export Wizard, complete the following information:
Add Export Fields
Field Value Comments
Protocol TCP Enter in text box
Address mqtt-broker Enter in text box
Port 1883 Enter in text box
Topic MQTTExportTopic Enter in text box
  1. Select the Next button. Selecting the Back button returns you to the second page of the Export wizard. Selecting the Cancel button dismisses the Export Wizard without creating the export registration
  2. On the third page of the Export Wizard, complete the following information:
Add Export Fields
Field Value Comments
Device filter The device-generated data to collect. In this example, to only export the data for the Random-Integer-Device device, enter Random-Integer-Device Enter in text box
Reading filter The device resource-generated reading data to collect. In this example, to only export the data for the Int16 resource, enter Int16 Enter in text box
  1. Select the OK button. Selecting the Back button returns you to the second page of the Export wizard. Selecting the Cancel button dismisses the Export Wizard without creating the export registration

Creating the Export Registration Using cURL

To create the export registration using cURL, enter the following:

curl -X POST -d \
'{
   "addressable":{
      "protocol":"TCP",
      "method":"POST",
      "name":"MQTTExport",
      "address":"mqtt-broker",
      "port":1883,
      "topic":"MQTTExportTopic"
   },
   "format":"JSON",
   "destination":"MQTT_TOPIC",
   "compression":"NONE",
   "encryption":{
      "encryptionAlgorithm":"NONE"
   },
   "enable":true,
   "name":"MQTTExportRegistration"
}' localhost:48071/api/v1/registration

To apply a Device filter to only export the data for the Random-Integer-Device device with the export registration using cURL, enter the following:

curl -X POST -d \
'{
   "addressable":{
      "protocol":"TCP",
      "method":"POST",
      "name":"MQTTExport",
      "address":"mqtt-broker",
      "port":1883,
      "topic":"MQTTExportTopic"
   },
   "format":"JSON",
   "filter":{
      "deviceIdentifiers":[
      "Random-Integer-Device"
      ]
    },
    "enable":true,
   "destination":"MQTT_TOPIC",
   "compression":"NONE",
   "encryption":{
      "encryptionAlgorithm":"NONE"
   },
   "enable":true,
   "name":"MQTTExportRegistration"
}' localhost:48071/api/v1/registration

To apply a Reading filter to only export the data for the Int16 resource with the export registration using cURL, enter the following:

curl -X POST -d \
'{
   "addressable":{
      "protocol":"TCP",
      "method":"POST",
      "name":"MQTTExport",
      "address":"mqtt-broker",
      "port":1883,
      "topic":"MQTTExportTopic"
   },
   "format":"JSON",
   "filter":{
      "deviceResources":[
      "Int16"
      ]
    },
    "enable":true,
   "destination":"MQTT_TOPIC",
   "compression":"NONE",
   "encryption":{
      "encryptionAlgorithm":"NONE"
   },
   "enable":true,
   "name":"MQTTExportRegistration"
}' localhost:48071/api/v1/registration

Viewing the Exported Data Using the Mosquitto Subscriber

To view the exported data using the Mosquitto Subscriber, complete the following steps:

  1. Open a terminal

  2. Enter the following command:

    mosquitto_sub -v -t 'MQTTExportTopic'
    

The data from the Virtual Device Service is displayed in the terminal, as illustrated below:

MQTTExportTopic {"id":"5c41e5ba0e360800013d3cbb","pushed":0,"device":"Random-Integer-Device",
"created":1547822522002,"modified":0,"origin":1547822522001,"schedule":null,"event":null,"readings":[{
"id":"","pushed":0,"created":0,"origin":1547822522000,"modified":0,"device":"Random-Integer-Device"
,"name":"Int16","value":"223"}]}