从云到设备通信指南Cloud-to-device communications guidance

IoT 中心提供三个选项,允许设备应用向后端应用公开功能:IoT Hub provides three options for device apps to expose functionality to a back-end app:

  • 直接方法,适用于需要立即确认结果的通信。Direct methods for communications that require immediate confirmation of the result. 直接方法通常用于以交互方式控制设备,例如打开风扇。Direct methods are often used for interactive control of devices such as turning on a fan.
  • 孪生的所需属性,适用于旨在将设备置于某个所需状态的长时间运行命令。Twin's desired properties for long-running commands intended to put the device into a certain desired state. 例如,将遥测发送间隔设置为 30 分钟。For example, set the telemetry send interval to 30 minutes.
  • 云到设备消息,适用于向设备应用提供单向通知。Cloud-to-device messages for one-way notifications to the device app.

Note

本文中所述的功能仅可在 IoT 中心的标准层中使用。The features described in this article are only available in the standard tier of IoT hub. 有关基本和标准 IoT 中心层的详细信息,请参阅如何选择合适的 IoT 中心层For more information about the basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.

下面详细比较了各种从云到设备的通信选项。Here is a detailed comparison of the various cloud-to-device communication options.

直接方法Direct methods 克隆的所需属性Twin's desired properties 云到设备的消息Cloud-to-device messages
方案Scenario 需要立即确认的命令,例如打开风扇。Commands that require immediate confirmation, such as turning on a fan. 旨在将设备置于某个所需状态的长时间运行命令。Long-running commands intended to put the device into a certain desired state. 例如,将遥测发送间隔设置为 30 分钟。For example, set the telemetry send interval to 30 minutes. 提供给设备应用的单向通知。One-way notifications to the device app.
数据流Data flow 双向。Two-way. 设备应用可以立即响应方法。The device app can respond to the method right away. 解决方案后端根据上下文接收请求结果。The solution back end receives the outcome contextually to the request. 单向。One-way. 设备应用接收更改了属性的通知。The device app receives a notification with the property change. 单向。One-way. 设备应用接收消息The device app receives the message
持续性Durability 不联系已断开连接的设备。Disconnected devices are not contacted. 通知解决方案后端:设备未连接。The solution back end is notified that the device is not connected. 设备孪生会保留属性值。Property values are preserved in the device twin. 设备会在下次重新连接时读取属性值。Device will read it at next reconnection. 属性值可通过 IoT 中心查询语言检索。Property values are retrievable with the IoT Hub query language. IoT 中心可保留消息长达 48 小时。Messages can be retained by IoT Hub for up to 48 hours.
目标Targets 通过 deviceId与单个设备通信,或通过 作业与多个设备通信。Single device using deviceId, or multiple devices using jobs. 通过 deviceId与单个设备通信,或通过 作业与多个设备通信。Single device using deviceId, or multiple devices using jobs. 通过 deviceId与单个设备通信。Single device by deviceId.
大小Size 直接方法有效负载的最大大小为 128 KB。Maximum direct method payload size is 128 KB. 所需属性大小最大为 8 KB。Maximum desired properties size is 8 KB. 最多 64 KB 消息。Up to 64 KB messages.
频率Frequency 高。High. 有关详细信息,请参阅 IoT 中心限制For more information, see IoT Hub limits. 中。Medium. 有关详细信息,请参阅 IoT 中心限制For more information, see IoT Hub limits. 低。Low. 有关详细信息,请参阅 IoT 中心限制For more information, see IoT Hub limits.
协议Protocol 使用 MQTT 或 AMQP 时可用。Available using MQTT or AMQP. 使用 MQTT 或 AMQP 时可用。Available using MQTT or AMQP. 在所有协议上可用。Available on all protocols. 使用 HTTPS 时,设备必须轮询。Device must poll when using HTTPS.

在以下教程中学习如何使用直接方法、所需属性以及从云到设备的消息:Learn how to use direct methods, desired properties, and cloud-to-device messages in the following tutorials: