Skip to content

MQTT Example Put Command

Introduction

The Put Command is used in the publishing flow. Whenever the user executes a put command, the Device Service publishes the message to the broker. Put Command

Case 1 - Define the device profile with the simple value case

Required attribute

  • pubTopic is used for Device Service to publish the message.

Device Profile Example

name: "MQTT-Device-Simulator"
manufacturer: "IOTech"
model: "MQTT-Device"
labels:
  - "mqtt"
  - "mock"
description: "The MQTT-Device is a Simulator for mocking a MQTT device"
deviceResources:
  - name: "temperature"
    isHidden: true
    description: "room temperature"
    attributes:
      pubTopic: "${deviceName}/room/humidity"
    properties:
      valueType: "Float32"
      readWrite: "R"
  - name: "humidity"
    isHidden: true
    description: "room humidity"
    attributes:
      pubTopic: "${deviceName}/room/humidity"
    properties:
      valueType: "Int16"
      readWrite: "R"
deviceCommands:
  -
    name: "room"
    isHidden: false
    readWrite: "RW"
    resourceOperations:
      - { deviceResource: "humidity" }

Executes the put command

Create an "MQTT_Device_A" device with the device profile.

Note

When the user executes the following put command, Device Service publishes the humidity binary data (encoded from the int16/ value 26) to the MQTT_Device_A/room/humidity topic

$ curl -X PUT http://localhost:59882/api/v2/device/name/MQTT_Device_A/room -H "Content-Type:application/json"  \
  -d '{"humidity":"26"}'

Case 2 - Define the device profile with the json string cases.

Note

If the user wants to publish using a JSON format message, they must use a JSON string with the escape character.

Device Profile Example

name: "MQTT-Device-Simulator"
manufacturer: "IOTech"
deviceResources:
  - name: "switchbutton"
    properties:
      valueType: "String"
      readWrite: "W"
    attributes:
      pubTopic: ${deviceName}/Switch/Set"
deviceCommands:
  - name: "switch"
    isHidden: false
    readWrite: "W"
    resourceOperations:
     - { deviceResource: "switchbutton", mappings: {"true": "ON", "false": "OFF"}}

Executes the put command

Create an "MQTT_Device_A" device with the device profile.

Note

When the user executes the following put command, Device Service publishes the switch binary data (encoded from the string value "{\"n\":\"device-1-switch\", \"switch\":\"on\"}") to the MQTT_Device_A/Switch/set topic.

curl http://localhost:59882/api/v2/device/name/MQTT_Device_A/switch -H "Content-Type:application/json" -X PUT  \
  -d '{ "Switch" : "{\"n\":\"device-1-switch\", \"switch\":\"on\"}" } '

Sequence Diagram

Put Command Diagram