Source: Device to Cloud Communication (Azure)

Azure IoT Protocols

Azure IoT Hub supports:

  • MQTT (including over WebSockets) — used by Python SDK by default
  • AMQP (including over WebSockets) — more overhead than MQTT, no broker required, extra security/flow control features
  • HTTPS

D2C — Device to Cloud Options

MethodUse case
Device-to-cloud messagesTime series telemetry, alerts
File uploadsMedia, large telemetry batches (intermittently connected)
Device twin reported propertiesReport device state / last known config

C2D — Cloud to Device Options

MethodUse case
Cloud-to-device messagesOne-way notifications to device
Direct methodsRequest-response, immediate confirmation (e.g., turn on fan)
Twin desired propertiesLong-running commands to put device in a desired state

Message Anatomy

An IoT Hub message has 3 parts:

{
  "message": {
    "systemProperties": {
      "contentType": "application/json",
      "contentEncoding": "UTF-8",
      "iothub-message-source": "deviceMessages",
      "iothub-enqueuedtime": "2017-05-08T18:55:31.8514657Z"
    },
    "appProperties": {
      "processingPath": "{cold | warm | hot}",
      "verbose": "{true, false}",
      "severity": "1-5",
      "testDevice": "{true | false}"
    },
    "body": "{\"Weather\":{\"Temperature\":50}}"
  }
}
  • systemProperties: auto-set, identify content type/encoding
  • appProperties: custom key-value pairs (ASCII alphanumeric only)
  • body: opaque binary payload (typically JSON)

Send via Python: IoTHubDeviceClient.send_message(message) — can pass string or Message object.

Direct Methods Lifecycle

  1. Service/back-end app sends Direct Method request to device (via SDK or az iot hub invoke-device-method)
  2. Device receives method, processes it, prepares response
  3. Response sent back to sender
  4. App handles response via callback

Python device side uses IotHubDeviceClient from azure.iot.device.aio.

Endpoints

IoT Hub = broker; endpoints = topics. Messages retained up to 7 days (1 day default).

See Also