Skip to content

MQTT Example

The MQTT Device Service example uses an MQTT simulator that is provided with Edge Xpert.

Before getting started, please make sure the MQTT simulator is started as described in the MQTT Simulator section.

Start the MQTT Device Service

To start the MQTT Device Service, enter the following command:

edgexpert up device-mqtt

Device Onboarding

Once the MQTT Device Service has been started, MQTT devices can be onboarded to Edge Xpert using either the Edge Xpert Manager UI or through Edge Xpert REST API commands. Instructions on how to use both methods are described below.

Device Onboarding with the Edge Xpert Manager UI

Note

Please refer to the Edge Xpert Manager UI Device Management capability for full details.

  1. Ensure the Edge Xpert Manager UI is started, for example:
    edgexpert up xpert-manager
    
  2. Open a browser and go to localhost:9090. The default username and password is admin.

  3. Using the device profile upload capability, upload the provided MQTT example device profile can be found at /usr/share/edgexpert/examples/device-services/mqtt/simulator/simulator.profile.yml

  4. Onboard the MQTT device using the device onboarding capability. The following values can be used to provision the example MQTT simulation device:

    Field Example Value Description Required
    Name MQ_DEVICE The unique name used to identify device Required
    Description A simulated MQTT device Any additional information used to help identify the device Optional
    Label MQTT Any additional information use to help identify the device Optional
    Protocol MQTT The name of the protocol Required
    Device Profile MQTT-Device-Simulator The name of the device profile uploaded above Required
    Device Service device-mqtt The name of the appropriate device service Required
    Auto Events Interval: 30s
    OnChange: false
    ResourceName: roomNumber
    Automated events to retrieve the data at specific frequencies

    Note: If OnChange is set to true, values will only be pushed if a change in value has occurred during the specified interval. If set to false, values will be sent on the specified interval regardless of if there has been a change.
    Optional

Device Onboarding with the Edge Xpert REST API

Info

If Edge Xpert is running in secure mode, you will need to replace localhost in the URL with the IP address for the service. See CLI Service Ports for details.

  1. Upload the provided MQTT example device profile:

    curl http://localhost:59881/api/v2/deviceprofile/uploadfile -F "file=@/usr/share/edgexpert/examples/device-services/mqtt/simulator/simulator.profile.yml"
    
  2. Onboard the device using similar values as explained above:

    curl -X 'POST' \
      'http://localhost:59881/api/v2/device' \
      -H 'Content-Type: application/json' \
      -d '[
      {
        "apiVersion": "v2",
        "device": {
          "name": "MQ_DEVICE",
          "description": "Simulated MQTT Device",
          "adminState": "UNLOCKED",
          "operatingState": "UP",
          "labels": [
            "MQTT",
            "room sensor"
          ],
          "serviceName": "device-mqtt",
          "profileName": "MQTT-Device-Simulator",
          "protocolName": "mqtt",
          "autoEvents": [
            {
              "interval": "30s",
              "onChange": false,
              "sourceName": "roomNumber"
            }
          ],
          "protocols": {
            "MQTT": {
            }
          }
        }
      }
    ]'
    

Device Data Flow

Once the device is connected to Edge Xpert, you can view the data flow between the Device Service and the connected device in various ways.

The Edge Xpert Manager UI supports Read and Write functionality for each device. Readings can also be viewed and queried within the Edge Xpert Manager UI Data Center.

Alternatively, you can read and write data with cURL commands or through REST API applications, such as Postman.

Info

If Edge Xpert is running in secure mode, you will need to replace localhost in the URL with the IP address for the service. See CLI Service Ports for details.

Both the GET and PUT requests can be made to the following address:

http://localhost:59882/api/v2/device/name/{deviceName}/{commandName}

Using the example above, a GET request to this device service could be:

curl http://localhost:59882/api/v2/device/name/MQ_DEVICE/roomNumber

For more information on the GET and PUT commands, see the Core Command Microservice.

Any data collected by the device service can be processed like all other Edge Xpert sensor data. Data readings can be exported for data processing in a number of other applications and services. See the Support Services and Application Services documents for more details.