|
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](/dotnet/core/install/). Be sure to install the .NET SDK, not just runtime. 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 for Debian, package manager installs of the .NET SDK are only supported on the x64 architecture. |
| 29 | + |
| 30 | + To check the version of the .NET SDK and runtime installed on your machine, run `dotnet --info`. |
19 | 31 |
|
20 | 32 | [!INCLUDE [iot-develop-create-central-app-with-device](iot-develop-create-central-app-with-device.md)]
|
21 | 33 |
|
22 | 34 | ## Run a simulated device
|
23 |
| -In this section, you configure your local environment, and run a sample that creates a simulated temperature controller. |
| 35 | +In this section, you configure your local environment, install the Azure IoT C# device SDK, and run a sample that creates a simulated temperature controller. |
| 36 | + |
| 37 | +### Configure your environment |
| 38 | + |
| 39 | +1. Open a console such as Windows CMD, PowerShell, or Bash. |
| 40 | + |
| 41 | +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. |
| 42 | + |
| 43 | + **CMD (Windows)** |
| 44 | + |
| 45 | + ```console |
| 46 | + set IOTHUB_DEVICE_SECURITY_TYPE=DPS |
| 47 | + set IOTHUB_DEVICE_DPS_ID_SCOPE=<application ID scope> |
| 48 | + set IOTHUB_DEVICE_DPS_DEVICE_KEY=<device primary key> |
| 49 | + set IOTHUB_DEVICE_DPS_DEVICE_ID=<your device ID> |
| 50 | + set IOTHUB_DEVICE_DPS_ENDPOINT=global.azure-devices-provisioning.net |
| 51 | + ``` |
| 52 | + |
| 53 | + > [!NOTE] |
| 54 | + > For Windows CMD there are no quotation marks surrounding the variable values. |
| 55 | + |
| 56 | + **PowerShell** |
| 57 | + |
| 58 | + ```azurepowershell |
| 59 | + $env:IOTHUB_DEVICE_SECURITY_TYPE='DPS' |
| 60 | + $env:IOTHUB_DEVICE_DPS_ID_SCOPE='<application ID scope>' |
| 61 | + $env:IOTHUB_DEVICE_DPS_DEVICE_KEY='<device primary key>' |
| 62 | + $env:IOTHUB_DEVICE_DPS_DEVICE_ID='<your device ID>' |
| 63 | + $env:IOTHUB_DEVICE_DPS_ENDPOINT='global.azure-devices-provisioning.net' |
| 64 | + ``` |
| 65 | + |
| 66 | + **Bash** |
| 67 | + |
| 68 | + ```bash |
| 69 | + export IOTHUB_DEVICE_SECURITY_TYPE='DPS' |
| 70 | + export IOTHUB_DEVICE_DPS_ID_SCOPE='<application ID scope>' |
| 71 | + export IOTHUB_DEVICE_DPS_DEVICE_KEY='<device primary key>' |
| 72 | + export IOTHUB_DEVICE_DPS_DEVICE_ID='<your device ID>' |
| 73 | + export IOTHUB_DEVICE_DPS_ENDPOINT='global.azure-devices-provisioning.net' |
| 74 | + ``` |
| 75 | + |
| 76 | +### Install the SDK and samples |
| 77 | + |
| 78 | +1. Clone the [Microsoft Azure IoT Samples for C# (.NET)](https://github.com/Azure-Samples/azure-iot-samples-csharp) to your local machine. |
| 79 | + |
| 80 | + ```console |
| 81 | + git clone https://github.com/Azure-Samples/azure-iot-samples-csharp.git |
| 82 | + ``` |
| 83 | + |
| 84 | +1. Navigate to the sample directory. |
| 85 | + |
| 86 | + **Windows** |
| 87 | + ```console |
| 88 | + cd azure-iot-samples-csharp\iot-hub\Samples\device\PnpDeviceSamples\TemperatureController |
| 89 | + ``` |
| 90 | + |
| 91 | + **Linux or Raspberry Pi OS** |
| 92 | + ```console |
| 93 | + cd azure-iot-samples-csharp/iot-hub/Samples/device/PnpDeviceSamples/TemperatureController |
| 94 | + ``` |
24 | 95 |
|
25 |
| -To run the sample application in Visual Studio: |
| 96 | +1. Install the Azure IoT C# SDK and necessary dependencies: |
26 | 97 |
|
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. |
| 98 | + ```console |
| 99 | + dotnet restore |
| 100 | + ``` |
28 | 101 |
|
29 |
| -1. In **Solution Explorer**, select the **PnpDeviceSamples > TemperatureController** project file, right-click it, and select **Set as Startup Project**. |
| 102 | + This command installs the proper dependencies as specified in the *TemperatureController.csproj* file. |
30 | 103 |
|
31 |
| -1. Right-click the **TemperatureController** project, select **Properties**, select the **Debug** tab, and add the following environment variables to the project: |
| 104 | +### Run the code |
32 | 105 |
|
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. | |
| 106 | +1. In your console, run the code sample. The sample creates a simulated temperature controller with thermostat sensors. |
40 | 107 |
|
41 |
| -1. Save the updated **TemperatureController** project file. |
| 108 | + ```console |
| 109 | + dotnet run |
| 110 | + ``` |
42 | 111 |
|
43 |
| -1. Press CTRL + F5 to run the sample. |
| 112 | + 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 | 113 |
|
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 | 114 | ```output
|
48 | 115 | [05/04/2021 11:53:50]info: Microsoft.Azure.Devices.Client.Samples.TemperatureControllerSample[0]
|
49 | 116 | Press Control+C to quit the sample.
|
|
0 commit comments