java iot_快速入门 - 与已连接到 Azure IoT 解决方案的 IoT 即插即用设备交互 | Microsoft Docs...

本文档提供了一种使用C#连接和控制已与解决方案连接的IoT即插即用设备的方法。首先确保环境设置完成,包括IoT中心。然后,在Windows上安装必要的软件,克隆SDK存储库并运行C#示例设备。设备会发送部分信息并报告在线状态。接着,运行服务示例,通过设备孪生更新属性和调用命令。
摘要由CSDN通过智能技术生成

IoT 即插即用简化了 IoT 的使用,使你无需了解底层设备实现,就能与某个设备的功能交互。IoT Plug and Play simplifies IoT by enabling you to interact with a device's capabilities without knowledge of the underlying device implementation. 本快速入门介绍如何使用 C# 来连接和控制已与解决方案连接的 IoT 即插即用设备。This quickstart shows you how to use C# to connect to and control an IoT Plug and Play device that's connected to your solution.

先决条件Prerequisites

请确保设置环境(包括 IoT 中心)后再继续。Before you continue, make sure you've set up your environment, including your IoT hub.

若要在 Windows 上完成本快速入门,需要在开发计算机上安装以下软件:To complete this quickstart on Windows, you need the following software installed on your development machine:

使用示例代码克隆 SDK 存储库Clone the SDK repository with the sample code

为 C# GitHub 存储库克隆 Azure IoT 示例中的示例。Clone the samples from the Azure IoT Samples for C# GitHub repository. 在所选文件夹中打开命令提示符。Open a command prompt in a folder of your choice. Run the following command to clone the Microsoft Azure IoT Samples for .NET GitHub repository:

git clone https://github.com/Azure-Samples/azure-iot-samples-csharp.git

运行示例设备Run the sample device

在本快速入门中,你将使用一个以 C# 编写的示例恒温器设备作为 IoT 即插即用设备。In this quickstart, you use a sample thermostat device that's written in C# as the IoT Plug and Play device. 运行示例设备:To run the sample device:

在 Visual Studio 2019 中打开“azure-iot-samples-csharp\iot-hub\Samples\device\PnpDeviceSamples\Thermostat\Thermostat.csproj”项目文件。Open the azure-iot-samples-csharp\iot-hub\Samples\device\PnpDeviceSamples\Thermostat\Thermostat.csproj project file in Visual Studio 2019.

在 Visual Studio 中,导航到“项目”>“恒温器属性”>“调试”。In Visual Studio, navigate to Project > Thermostat Properties > Debug. 然后,将以下环境变量添加到项目:Then add the following environment variables to the project:

名称Name

值Value

IOTHUB_DEVICE_SECURITY_TYPEIOTHUB_DEVICE_SECURITY_TYPE

DPSDPS

IOTHUB_DEVICE_DPS_ENDPOINTIOTHUB_DEVICE_DPS_ENDPOINT

global.azure-devices-provisioning.netglobal.azure-devices-provisioning.net

IOTHUB_DEVICE_DPS_ID_SCOPEIOTHUB_DEVICE_DPS_ID_SCOPE

在完成设置环境时记下的值The value you made a note of when you completed Set up your environment

IOTHUB_DEVICE_DPS_DEVICE_IDIOTHUB_DEVICE_DPS_DEVICE_ID

my-pnp-devicemy-pnp-device

IOTHUB_DEVICE_DPS_DEVICE_KEYIOTHUB_DEVICE_DPS_DEVICE_KEY

在完成设置环境时记下的值The value you made a note of when you completed Set up your environment

现在可以在 Visual Studio 中生成示例,并在调试模式下运行它。You can now build the sample in Visual Studio and run it in debug mode.

你会看到一些消息,指出设备已发送部分信息并报告其自身处于联机状态。You see messages saying that the device has sent some information and reported itself online. 这些消息表明设备已开始向中心发送遥测数据,现在可以接收命令和属性更新。These messages indicate that the device has begun sending telemetry data to the hub, and is now ready to receive commands and property updates. 请勿关闭此 Visual Studio 实例,你需要使用它确认服务示例正常工作。Don't close this instance of Visual Studio, you need it to confirm the service sample is working.

运行示例解决方案Run the sample solution

在为 IoT 即插即用快速入门和教程设置环境中,已创建了两个环境变量以将示例配置为连接到 IoT 中心和设备:In Set up your environment for the IoT Plug and Play quickstarts and tutorials you created two environment variables to configure the sample to connect to your IoT hub and device:

IOTHUB_CONNECTION_STRING:之前记下的 IoT 中心连接字符串。IOTHUB_CONNECTION_STRING: the IoT hub connection string you made a note of previously.

IOTHUB_DEVICE_ID:"my-pnp-device"。IOTHUB_DEVICE_ID: "my-pnp-device".

在本快速入门中,你将使用以 C# 编写的示例 IoT 解决方案与刚设置的示例设备进行交互。In this quickstart, you use a sample IoT solution in C# to interact with the sample device you just set up.

在 Visual Studio 的另一个实例中,打开“azure-iot-samples-csharp\iot-hub\Samples\service\PnpServiceSamples\Thermostat\Thermostat.csproj”项目。In another instance of Visual Studio, open the azure-iot-samples-csharp\iot-hub\Samples\service\PnpServiceSamples\Thermostat\Thermostat.csproj project.

在 Visual Studio 中,导航到“项目”>“恒温器属性”>“调试”。In Visual Studio, navigate to Project > Thermostat Properties > Debug. 然后,将以下环境变量添加到项目:Then add the following environment variables to the project:

名称Name

值Value

IOTHUB_DEVICE_IDIOTHUB_DEVICE_ID

my-pnp-devicemy-pnp-device

IOTHUB_CONNECTION_STRINGIOTHUB_CONNECTION_STRING

在完成设置环境时记下的值The value you made a note of when you completed Set up your environment

现在可以在 Visual Studio 中生成示例,并在调试模式下运行它。You can now build the sample in Visual Studio and run it in debug mode.

获取设备孪生Get device twin

以下代码片段演示了服务应用程序如何检索设备孪生:The following code snippet shows how the service application retrieves the device twin:

// Get a Twin and retrieves model Id set by Device client

Twin twin = await s_registryManager.GetTwinAsync(s_deviceId);

s_logger.LogDebug($"Model Id of this Twin is: {twin.ModelId}");

备注

此示例使用 IoT 中心服务客户端中的 Microsoft.Azure.Devices.Client 命名空间 。This sample uses the Microsoft.Azure.Devices.Client namespace from the IoT Hub service client. 若要了解有关 API(包括数字孪生 API)的详细信息,请参阅服务开发人员指南。To learn more about the APIs, including the digital twins API, see the service developer guide.

此代码生成以下输出:This code generates the following output:

[09/21/2020 11:26:04]dbug: Thermostat.Program[0]

Model Id of this Twin is: dtmi:com:example:Thermostat;1

以下代码片段演示了如何使用补丁通过设备孪生来更新属性:The following code snippet shows how to use a patch to update properties through the device twin:

// Update the twin

var twinPatch = new Twin();

twinPatch.Properties.Desired[PropertyName] = PropertyValue;

await s_registryManager.UpdateTwinAsync(s_deviceId, twinPatch, twin.ETag);

当服务更新 targetTemperature 属性时,此代码将从设备生成以下输出:This code generates the following output from the device when the service updates the targetTemperature property:

[09/21/2020 11:26:05]dbug: Thermostat.ThermostatSample[0]

Property: Received - { "targetTemperature": 60°C }.

[09/21/2020 11:26:05]dbug: Thermostat.ThermostatSample[0]

Property: Update - {"targetTemperature": 60°C } is InProgress.

...

[09/21/2020 11:26:17]dbug: Thermostat.ThermostatSample[0]

Property: Update - {"targetTemperature": 60°C } is Completed.

调用命令Invoke a command

下面的代码片段演示如何调用命令:The following code snippet shows how to call a command:

private static async Task InvokeCommandAsync()

{

var commandInvocation = new CloudToDeviceMethod(CommandName) { ResponseTimeout = TimeSpan.FromSeconds(30) };

// Set command payload

string componentCommandPayload = JsonConvert.SerializeObject(s_dateTime);

commandInvocation.SetPayloadJson(componentCommandPayload);

CloudToDeviceMethodResult result = await s_serviceClient.InvokeDeviceMethodAsync(s_deviceId, commandInvocation);

if (result == null)

{

throw new Exception($"Command {CommandName} invocation returned null");

}

s_logger.LogDebug($"Command {CommandName} invocation result status is: {result.Status}");

}

当服务调用 getMaxMinReport 命令时,此代码将从设备生成以下输出:This code generates the following output from the device when the service calls the getMaxMinReport command:

[09/21/2020 11:26:05]dbug: Thermostat.ThermostatSample[0]

Command: Received - Generating max, min and avg temperature report since 21/09/2020 11:25:58.

[09/21/2020 11:26:05]dbug: Thermostat.ThermostatSample[0]

Command: MaxMinReport since 21/09/2020 11:25:58: maxTemp=32, minTemp=32, avgTemp=32, startTime=21/09/2020 11:25:59, endTime=21/09/2020 11:26:04

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值