|
4 | 4 | author: timlt
|
5 | 5 | ms.service: iot-develop
|
6 | 6 | ms.topic: include
|
7 |
| - ms.date: 04/28/2021 |
| 7 | + ms.date: 10/08/2021 |
8 | 8 | ms.author: timlt
|
9 | 9 | ms.custom: include file
|
10 | 10 | ---
|
|
14 | 14 | In this quickstart, you learn a basic Azure IoT application development workflow. First you create an Azure IoT Central application for hosting devices. Then you use an Azure IoT device SDK sample to run a simulated temperature controller, connect it securely to IoT Central, and send telemetry.
|
15 | 15 |
|
16 | 16 | ## Prerequisites
|
17 |
| -- [Visual Studio (Community, Professional, or Enterprise) 2019](https://visualstudio.microsoft.com/downloads/). |
18 |
| -- A local copy of the [Microsoft Azure IoT Samples for C# (.NET)](https://github.com/Azure-Samples/azure-iot-samples-csharp) GitHub repository. Download a copy of the repository and extract it: [Download ZIP](https://github.com/Azure-Samples/azure-iot-samples-csharp/archive/main.zip). |
| 17 | + |
| 18 | +This quickstart runs on Windows, Linux, and Raspberry Pi. It's been tested on the following OS and device versions: |
| 19 | + |
| 20 | +- Windows 10 |
| 21 | +- Ubuntu 20.04 LTS running on Windows Subsystem for Linux (WSL) |
| 22 | +- Raspberry Pi OS version 10 (Raspian) running on a Raspberry Pi 3 Model B+ |
| 23 | + |
| 24 | +Install the following prerequisites on your development machine: |
| 25 | + |
| 26 | +- If you don't have an Azure subscription, [create one for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 27 | +- [Git](https://git-scm.com/downloads). |
| 28 | +- .NET Core SDK 3.1. Be sure to install the .NET SDK, not just the runtime. To check the version of the .NET SDK and runtime installed on your machine, run `dotnet --info`. |
| 29 | + |
| 30 | + - For Windows and Linux (except Raspberry Pi), follow the instructions to [install the .NET Core SDK 3.1](/dotnet/core/install/) on your platform. |
| 31 | + - For Raspberry Pi, you'll need to follow the instructions to [manually install the SDK](/dotnet/core/install/linux-scripted-manual#manual-install). This is because on Debian, package manager installs of the .NET SDK are only supported for the x64 architecture. |
19 | 32 |
|
20 | 33 | [!INCLUDE [iot-develop-create-central-app-with-device](iot-develop-create-central-app-with-device.md)]
|
21 | 34 |
|
22 | 35 | ## Run a simulated device
|
23 |
| -In this section, you configure your local environment, and run a sample that creates a simulated temperature controller. |
| 36 | +In this section, you configure your local environment, install the Azure IoT C# samples, and run a sample that creates a simulated temperature controller. |
| 37 | + |
| 38 | +### Configure your environment |
| 39 | + |
| 40 | +1. Open a console such as Windows CMD, PowerShell, or Bash. |
| 41 | + |
| 42 | +1. Set the following environment variables, using the appropriate commands for your console. The simulated device uses these values to connect to IoT Central. For `IOTHUB_DEVICE_DPS_ID_SCOPE`, `IOTHUB_DEVICE_DPS_DEVICE_KEY`, and `IOTHUB_DEVICE_DPS_DEVICE_ID`, use the device connection values that you saved previously. |
| 43 | + |
| 44 | + **CMD (Windows)** |
| 45 | + |
| 46 | + ```console |
| 47 | + set IOTHUB_DEVICE_SECURITY_TYPE=DPS |
| 48 | + set IOTHUB_DEVICE_DPS_ID_SCOPE=<application ID scope> |
| 49 | + set IOTHUB_DEVICE_DPS_DEVICE_KEY=<device primary key> |
| 50 | + set IOTHUB_DEVICE_DPS_DEVICE_ID=<your device ID> |
| 51 | + set IOTHUB_DEVICE_DPS_ENDPOINT=global.azure-devices-provisioning.net |
| 52 | + ``` |
| 53 | + |
| 54 | + > [!NOTE] |
| 55 | + > For Windows CMD there are no quotation marks surrounding the variable values. |
| 56 | + |
| 57 | + **PowerShell** |
| 58 | + |
| 59 | + ```azurepowershell |
| 60 | + $env:IOTHUB_DEVICE_SECURITY_TYPE='DPS' |
| 61 | + $env:IOTHUB_DEVICE_DPS_ID_SCOPE='<application ID scope>' |
| 62 | + $env:IOTHUB_DEVICE_DPS_DEVICE_KEY='<device primary key>' |
| 63 | + $env:IOTHUB_DEVICE_DPS_DEVICE_ID='<your device ID>' |
| 64 | + $env:IOTHUB_DEVICE_DPS_ENDPOINT='global.azure-devices-provisioning.net' |
| 65 | + ``` |
| 66 | + |
| 67 | + **Bash** |
| 68 | + |
| 69 | + ```bash |
| 70 | + export IOTHUB_DEVICE_SECURITY_TYPE='DPS' |
| 71 | + export IOTHUB_DEVICE_DPS_ID_SCOPE='<application ID scope>' |
| 72 | + export IOTHUB_DEVICE_DPS_DEVICE_KEY='<device primary key>' |
| 73 | + export IOTHUB_DEVICE_DPS_DEVICE_ID='<your device ID>' |
| 74 | + export IOTHUB_DEVICE_DPS_ENDPOINT='global.azure-devices-provisioning.net' |
| 75 | + ``` |
| 76 | + |
| 77 | +### Install the SDK and samples |
| 78 | + |
| 79 | +1. Clone the [Microsoft Azure IoT Samples for C# (.NET)](https://github.com/Azure-Samples/azure-iot-samples-csharp) to your local machine. |
| 80 | + |
| 81 | + ```console |
| 82 | + git clone https://github.com/Azure-Samples/azure-iot-samples-csharp.git |
| 83 | + ``` |
| 84 | + |
| 85 | +1. Navigate to the sample directory. |
| 86 | + |
| 87 | + **Windows** |
| 88 | + |
| 89 | + ```console |
| 90 | + cd azure-iot-samples-csharp\iot-hub\Samples\device\PnpDeviceSamples\TemperatureController |
| 91 | + ``` |
| 92 | + |
| 93 | + **Linux or Raspberry Pi OS** |
| 94 | + |
| 95 | + ```console |
| 96 | + cd azure-iot-samples-csharp/iot-hub/Samples/device/PnpDeviceSamples/TemperatureController |
| 97 | + ``` |
24 | 98 |
|
25 |
| -To run the sample application in Visual Studio: |
| 99 | +1. Install the Azure IoT C# SDK and necessary dependencies: |
26 | 100 |
|
27 |
| -1. In the folder where you unzipped the Azure IoT Samples for C#, open the *azure-iot-samples-csharp-main\iot-hub\Samples\device\IoTHubDeviceSamples.sln"* solution file in Visual Studio. |
| 101 | + ```console |
| 102 | + dotnet restore |
| 103 | + ``` |
28 | 104 |
|
29 |
| -1. In **Solution Explorer**, select the **PnpDeviceSamples > TemperatureController** project file, right-click it, and select **Set as Startup Project**. |
| 105 | + This command installs the proper dependencies as specified in the *TemperatureController.csproj* file. |
30 | 106 |
|
31 |
| -1. Right-click the **TemperatureController** project, select **Properties**, select the **Debug** tab, and add the following environment variables to the project: |
| 107 | +### Run the code |
32 | 108 |
|
33 |
| - | Name | Value | |
34 |
| - | ---- | ----- | |
35 |
| - | IOTHUB_DEVICE_SECURITY_TYPE | DPS | |
36 |
| - | IOTHUB_DEVICE_DPS_ENDPOINT | global.azure-devices-provisioning.net | |
37 |
| - | IOTHUB_DEVICE_DPS_ID_SCOPE | The ID scope value you made a note of previously. | |
38 |
| - | IOTHUB_DEVICE_DPS_DEVICE_ID | sample-device-01 | |
39 |
| - | IOTHUB_DEVICE_DPS_DEVICE_KEY | The generated device key value you made a note of previously. | |
| 109 | +1. In your console, run the code sample. The sample creates a simulated temperature controller with thermostat sensors. |
40 | 110 |
|
41 |
| -1. Save the updated **TemperatureController** project file. |
| 111 | + ```console |
| 112 | + dotnet run |
| 113 | + ``` |
42 | 114 |
|
43 |
| -1. Press CTRL + F5 to run the sample. |
| 115 | + After your simulated device connects to your IoT Central application, it connects to the device instance you created in the application and begins to send telemetry. The connection details and telemetry output are shown in your console: |
44 | 116 |
|
45 |
| - After your simulated device connects to your IoT Central application, it begins to send telemetry. The connection details and telemetry output appear in the console: |
46 |
| - |
47 | 117 | ```output
|
48 |
| - [05/04/2021 11:53:50]info: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
49 |
| - Press Control+C to quit the sample. |
50 |
| - [05/04/2021 11:53:50]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
51 |
| - Set up the device client. |
52 |
| - [05/04/2021 11:53:50]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
53 |
| - Initializing via DPS |
54 |
| - [05/04/2021 11:53:56]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
55 |
| - Set handler for 'reboot' command. |
56 |
| - [05/04/2021 11:53:57]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
57 |
| - Connection status change registered - status=Connected, reason=Connection_Ok. |
58 |
| - [05/04/2021 11:53:57]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
59 |
| - Set handler for "getMaxMinReport" command. |
60 |
| - [05/04/2021 11:53:57]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
61 |
| - Set handler to receive 'targetTemperature' updates. |
62 |
| - [05/04/2021 11:53:57]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
63 |
| - Property: Update - component = 'deviceInformation', properties update is complete. |
64 |
| - [05/04/2021 11:53:58]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
65 |
| - Property: Update - { "serialNumber": "SR-123456" } is complete. |
66 |
| - [05/04/2021 11:53:58]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
67 |
| - Telemetry: Sent - component="thermostat1", { "temperature": 44.9 } in °C. |
68 |
| - [05/04/2021 11:53:58]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
69 |
| - Property: Update - component="thermostat1", { "maxTempSinceLastReboot": 44.9 } in °C is complete. |
70 |
| - [05/04/2021 11:53:58]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
71 |
| - Telemetry: Sent - component="thermostat2", { "temperature": 40.8 } in °C. |
| 118 | + [10/09/2021 00:29:18]info: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
| 119 | + Press Control+C to quit the sample. |
| 120 | + [10/09/2021 00:29:18]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
| 121 | + Set up the device client. |
| 122 | + [10/09/2021 00:29:18]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
| 123 | + Initializing via DPS |
| 124 | + [10/09/2021 00:29:38]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
| 125 | + Set handler for 'reboot' command. |
| 126 | + [10/09/2021 00:29:39]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
| 127 | + Connection status change registered - status=Connected, reason=Connection_Ok. |
| 128 | + [10/09/2021 00:29:39]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
| 129 | + Set handler for "getMaxMinReport" command. |
| 130 | + [10/09/2021 00:29:39]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
| 131 | + Set handler to receive 'targetTemperature' updates. |
| 132 | + [10/09/2021 00:29:39]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
| 133 | + Property: Update - component = 'deviceInformation', properties update is complete. |
| 134 | + [10/09/2021 00:29:39]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
| 135 | + Property: Update - { "serialNumber": "SR-123456" } is complete. |
| 136 | + [10/09/2021 00:29:40]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
| 137 | + Telemetry: Sent - component="thermostat1", { "temperature": 23.7 } in °C. |
| 138 | + [10/09/2021 00:29:40]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
| 139 | + Property: Update - component="thermostat1", { "maxTempSinceLastReboot": 23.7 } in °C is complete. |
| 140 | + [10/09/2021 00:29:40]dbug: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0] |
| 141 | + Telemetry: Sent - component="thermostat2", { "temperature": 25.8 } in °C. |
72 | 142 | ```
|
0 commit comments