Skip to content

Commit 2f4c54c

Browse files
authored
Merge pull request #174971 from JimacoMS4/add-linux-pi-to-charp-device-qs
Convert C# QS articles to dotnet and add Linux and Raspberry Pi support
2 parents 9fa98d6 + 91b3d0c commit 2f4c54c

File tree

2 files changed

+192
-59
lines changed

2 files changed

+192
-59
lines changed

includes/iot-develop-send-telemetry-central-csharp.md

Lines changed: 113 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
author: timlt
55
ms.service: iot-develop
66
ms.topic: include
7-
ms.date: 04/28/2021
7+
ms.date: 10/08/2021
88
ms.author: timlt
99
ms.custom: include file
1010
---
@@ -14,59 +14,129 @@
1414
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.
1515

1616
## 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.
1932

2033
[!INCLUDE [iot-develop-create-central-app-with-device](iot-develop-create-central-app-with-device.md)]
2134

2235
## 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+
```
2498

25-
To run the sample application in Visual Studio:
99+
1. Install the Azure IoT C# SDK and necessary dependencies:
26100

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+
```
28104

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.
30106

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
32108

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.
40110

41-
1. Save the updated **TemperatureController** project file.
111+
```console
112+
dotnet run
113+
```
42114

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:
44116

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-
47117
```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.
72142
```

includes/iot-develop-send-telemetry-iot-hub-csharp.md

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
author: timlt
55
ms.service: iot-develop
66
ms.topic: include
7-
ms.date: 08/03/2021
7+
ms.date: 10/07/2021
88
ms.author: timlt
99
ms.custom: include file
1010
---
@@ -14,37 +14,100 @@
1414
In this quickstart, you learn a basic Azure IoT application development workflow. You use the Azure CLI and IoT Explorer to create an Azure IoT hub and a device. Then you use an Azure IoT device SDK sample to run a simulated temperature controller, connect it securely to the hub, and send telemetry.
1515

1616
## Prerequisites
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 except where noted for Raspberry Pi:
25+
1726
- If you don't have an Azure subscription, [create one for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
18-
- [Visual Studio (Community, Professional, or Enterprise) 2019](https://visualstudio.microsoft.com/downloads/).
19-
- 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).
20-
- [Azure IoT Explorer](https://github.com/Azure/azure-iot-explorer/releases): Cross-platform, GUI-base utility to monitor and manage Azure IoT.
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.
32+
33+
- [Azure IoT Explorer](https://github.com/Azure/azure-iot-explorer/releases): Cross-platform, GUI-based utility to monitor and manage Azure IoT. If you're using Raspberry Pi as your development platform, we recommend that you install IoT Explorer on another computer. If you don't want to install IoT Explorer, you can use Azure CLI to perform the same steps.
2134
- Azure CLI. You have two options for running Azure CLI commands in this quickstart:
2235
- Use the Azure Cloud Shell, an interactive shell that runs CLI commands in your browser. This option is recommended because you don't need to install anything. If you're using Cloud Shell for the first time, log into the [Azure portal](https://portal.azure.com). Follow the steps in [Cloud Shell quickstart](../articles/cloud-shell/quickstart.md) to **Start Cloud Shell** and **Select the Bash environment**.
23-
- Optionally, run Azure CLI on your local machine. If Azure CLI is already installed, run `az upgrade` to upgrade the CLI and extensions to the current version. To install Azure CLI, see [Install Azure CLI]( /cli/azure/install-azure-cli).
36+
- Optionally, run Azure CLI on your local machine. If Azure CLI is already installed, run `az upgrade` to upgrade the CLI and extensions to the current version. To install Azure CLI, see [Install Azure CLI]( /cli/azure/install-azure-cli). If you're using Raspberry Pi as your development platform, we recommend that you use Azure Cloud Shell or install Azure CLI on another computer.
2437

2538
[!INCLUDE [iot-hub-include-create-hub-iot-explorer](iot-hub-include-create-hub-iot-explorer.md)]
2639

2740
## Run a simulated device
41+
2842
In this section, you'll use the C# SDK to send messages from a simulated device to your IoT hub. You'll run a sample that implements a temperature controller with two thermostat sensors.
2943

30-
To run the sample application in Visual Studio:
44+
1. Open a new console such as Windows CMD, PowerShell, or Bash. In the following steps, you'll use this console to install the Node.js SDK and work with Node.js sample code.
3145

32-
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.
46+
> [!NOTE]
47+
> If you're using a local installation of Azure CLI, you might now have two console windows open. Be sure to enter the commands in this section in the console you just opened, not the one that you've been using for the CLI.
3348
34-
1. In **Solution Explorer**, select the **PnpDeviceSamples > TemperatureController** project file, right-click it, and select **Set as Startup Project**.
49+
1. Clone the [Microsoft Azure IoT Samples for C# (.NET)](https://github.com/Azure-Samples/azure-iot-samples-csharp) to your local machine:
3550

36-
1. Right-click the **TemperatureController** project, select **Properties**, select the **Debug** tab, and add the following environment variables to the project:
51+
```console
52+
git clone https://github.com/Azure-Samples/azure-iot-samples-csharp.git
53+
```
54+
55+
1. Navigate to the sample directory:
3756

38-
| Name | Value |
39-
| ---- | ----- |
40-
| IOTHUB_DEVICE_SECURITY_TYPE | *connectionString* |
41-
| IOTHUB_DEVICE_CONNECTION_STRING | The connection string you saved previously. |
57+
**Windows**
58+
```console
59+
cd azure-iot-samples-csharp\iot-hub\Samples\device\PnpDeviceSamples\TemperatureController
60+
```
4261

43-
1. Save the updated **TemperatureController** project file.
62+
**Linux or Raspberry Pi OS**
63+
```console
64+
cd azure-iot-samples-csharp/iot-hub/Samples/device/PnpDeviceSamples/TemperatureController
65+
```
66+
67+
1. Install the Azure IoT C# SDK and necessary dependencies:
68+
69+
```console
70+
dotnet restore
71+
```
4472

45-
1. In Visual Studio, press CTRL + F5 to run the sample.
73+
This command installs the proper dependencies as specified in the *TemperatureController.csproj* file.
74+
75+
1. Set both of the following environment variables, to enable your simulated device to connect to Azure IoT.
76+
* Set an environment variable called `IOTHUB_DEVICE_CONNECTION_STRING`. For the variable value, use the device connection string that you saved in the previous section.
77+
* Set an environment variable called `IOTHUB_DEVICE_SECURITY_TYPE`. For the variable, use the literal string value `connectionString`.
78+
79+
**CMD (Windows)**
80+
81+
```console
82+
set IOTHUB_DEVICE_CONNECTION_STRING=<your connection string here>
83+
set IOTHUB_DEVICE_SECURITY_TYPE=connectionString
84+
```
85+
86+
> [!NOTE]
87+
> For Windows CMD there are no quotation marks surrounding the string values for each variable.
88+
89+
**PowerShell**
90+
91+
```azurepowershell
92+
$env:IOTHUB_DEVICE_CONNECTION_STRING='<your connection string here>'
93+
$env:IOTHUB_DEVICE_SECURITY_TYPE='connectionString'
94+
```
95+
96+
**Bash**
97+
98+
```bash
99+
export IOTHUB_DEVICE_CONNECTION_STRING="<your connection string here>"
100+
export IOTHUB_DEVICE_SECURITY_TYPE="connectionString"
101+
```
102+
1. Run the code sample:
103+
104+
```console
105+
dotnet run
106+
```
107+
> [!NOTE]
108+
> This code sample uses Azure IoT Plug and Play, which lets you integrate smart devices into your solutions without any manual configuration. By default, most samples in this documentation use IoT Plug and Play. To learn more about the advantages of IoT PnP, and cases for using or not using it, see [What is IoT Plug and Play?](../articles/iot-develop/overview-iot-plug-and-play.md).
46109

47-
A console window opens. The sample securely connects to your IoT hub as the device you registered and begins sending telemetry messages. The sample output appears in the console.
110+
The sample securely connects to your IoT hub as the device you registered and begins sending telemetry messages. The sample output appears in your console.
48111

49112
## View telemetry
50113

0 commit comments

Comments
 (0)