Azure IoT Hub 十分钟入门系列 (5)- 10分钟实现云到设备的消息(direct method)

 

本文主要分享一个案例:

10分钟内从云端向设备发送控制指令

本文主要有如下内容:

1.了解云到设备的消息种类

2. 了解Direct Method的使用场景

2. 使用Direct Method方式在云端向设备发送控制指令

视频讲解:

图文内容:

云到设备有三种方式发送/传递消息

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. For example, set the telemetry send interval to 30 minutes.

旨在将设备置于某个所需状态的长时间运行命令。 例如,将遥测发送间隔设置为 30 分钟。

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.

设备孪生会保留属性值。 设备会在下次重新连接时读取属性值。 

Messages can be retained by IoT Hub for up to 48 hours.

IoT 中心可保留消息长达 48 小时。

Targets

Single device using deviceId, or multiple devices using jobs.

通过 deviceId与单个设备通信,或通过 作业与多个设备通信。

Single device using deviceId, or multiple devices using jobs.

通过 deviceId与单个设备通信,或通过 作业与多个设备通信。

Single device by deviceId.

通过 deviceId与单个设备通信。

Size

Maximum direct method payload size is 128 KB.

最大128KB

Maximum desired properties size is 8 KB.

最大8KB

Up to 64 KB messages.

最大64KB

Frequency

High. For more information, see IoT Hub limits.

最高

Medium. For more information, see IoT Hub limits.

中等

Low. For more information, see IoT Hub limits.

最低

Protocol

Available using MQTT or AMQP.

MQTT或AMQP时可用

Available using MQTT or AMQP.

MQTT或AMQP时可用

Available on all protocols. Device must poll when using HTTPS.

在所有协议上可用。 使用 HTTPS 时,设备必须轮询。

案例:

本文请参考官网链接:https://docs.azure.cn/zh-cn/iot-hub/quickstart-control-device-node

使用两个预先编写的 Node.js 应用程序:

  • 可响应直接方法的模拟设备应用程序。
  • 后端应用程序,可在模拟设备上调用直接方法。

示例代码参见: https://github.com/Azure-Samples/azure-iot-samples-node/archive/master.zip

运行模拟设备

应用程序会连接到 IoT 中心上特定于设备的终结点,发送模拟遥测数据,并侦听中心的直接方法调用。

在本快速入门中,中心的直接方法调用告知设备对其发送遥测的间隔进行更改。 执行直接方法后,模拟设备会将确认发送回中心。

  1. 在本地终端窗口中,导航到示例 Node.js 项目的根文件夹。 然后导航到 iot-hubQuickstartssimulated-device-2 文件夹。
  2. 在所选文本编辑器中打开 SimulatedDevice.js 文件 。
    connectionString 变量的值替换为设备连接字符串。 然后将更改保存到 SimulatedDevice.js
  3. 在本地终端窗口中,运行以下命令以安装所需的库,并运行模拟设备应用程序:
npm install
node SimulatedDevice.js

如下图,表示设备发送数据成功:

服务端调用直接方法

后端应用程序会连接到 IoT 中心上的服务端终结点。 应用程序通过 IoT 中心对设备进行直接方法调用,并侦听确认。 IoT 中心后端应用程序通常在云中运行。

  1. 在另一个本地终端窗口中,导航到示例 Node.js 项目的根文件夹。 然后导航到 iot-hubQuickstartsack-end-application 文件夹。
  2. 在所选文本编辑器中打开 BackEndApplication.js 文件 。
    connectionString 变量的值替换为Azure IoT Hub的连接字符串。 然后将更改保存到 BackEndApplication.js
    连接字符串(注意,此处是IoT Hub的连接字符串)可以在如下位置得到:
  3. 在本地终端窗口中运行以下命令,以安装所需的库并运行后端应用程序:
npm install
node BackEndApplication.js

如下图所示,status:200, 即是由设备侧返回的执行结果。

同时可以看到设备侧执行结果如下(直观的结果是,原来2秒上报一次数据,现在变成了10秒一次):

 
 
原文地址:https://www.cnblogs.com/shuzhenyu/p/12358783.html