Skip to content

MQTT Example Get command

Introduction

When the user executes Get command, the device service sends a read request to one MQTT topic and listens for the results of another MQTT topic. Get Command

Required attribute

  • reqTopic (request topic) is used for publishing a request to a specified device. The device receives a request and returns a response.
  • resTopic (response topic) is used for subscribing to the response and parsing the message using the specified message format.
  • reqTemp (request template) is to generate the request body. As we used the MQTT protocol, we can receive multiple responses from resTopic, so we define a UUID field to match the response and request.

Device Profile Example

name: "MQTT-Device-Simulator"
manufacturer: "IOTech"
model: "MQTT-Device"
deviceResources:
  - name: "switchbutton"
    isHidden: true
    attributes:
      reqTopic: "${deviceName}/Switch/Req"
      reqTemp: '{ "cmd":"number","reqId":"${uuid}" }'
      resTopic: "${deviceName}/Switch/Res"
      msgFormat: "json"
      jsonPath: "switch"
      qos: "0"
    properties:
      valueType: "String"
      readWrite: "RW"
deviceCommands:
  - name: "switch"
    readWrite: "R"
    resourceOperations:
      - { deviceResource: "switchbutton", mappings: {"true": "ON", "false": "OFF"}}

The complete sequence flow is as follows:

  1. User executes a GET command
  2. Device Service receives the request and subscribes to the ${deviceName}/Switch/Res topic
  3. Device Service replaces the ${uuid} in the reqTemp and publishes the Request to ${deviceName}/Switch/Reqwith the message {"cmd":"switch",”reqId”:”d1f8dc93-9b40-42c0” }
  4. MQTT device receives the Request from the ${deviceName}/Switch/Req topic
  5. MQTT device publishes the Response to the ${deviceName}/Switch/Res topic with the message: {"switch":"true",”reqId”:”d1f8dc93-9b40-42c0” }
  6. Device Service receives the Response from ${deviceName}/Switch/Res and parses "true" as the switch value, and map true to "ON" according to mappings: {"true": "ON", "false": "OFF"}, then returns "ON" as the command value

Sequence Diagram

Get Command Diagram