Skip to content

Commit 2902b16

Browse files
authored
Merge pull request #225811 from kgremban/jan31-hubtesttutorial
Review IoT Hub test connectivity tutorial and clean up related includes
2 parents 91a5f62 + db9d6ca commit 2902b16

8 files changed

+113
-234
lines changed

articles/azure-web-pubsub/tutorial-serverless-iot.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ In this tutorial, you learn how to:
3737

3838
[!INCLUDE [quickstarts-free-trial-note](../../includes/quickstarts-free-trial-note.md)]
3939

40-
[!INCLUDE [iot-hub-include-create-hub](../../includes/iot-hub-include-create-hub-quickstart.md)]
40+
## Create an IoT hub
41+
42+
[!INCLUDE [iot-hub-include-create-hub-cli](../../includes/iot-hub-include-create-hub-cli.md)]
4143

4244
## Create a Web PubSub instance
4345

articles/iot-hub/tutorial-connectivity.md

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ title: Tutorial - Check device connectivity to Azure IoT Hub
33
description: Tutorial - Use IoT Hub tools to troubleshoot, during development, device connectivity issues to your IoT hub.
44
services: iot-hub
55
author: kgremban
6-
76
ms.author: kgremban
87
ms.custom: [mvc, amqp, mqtt, 'Role: Cloud Development', 'Role: IoT Device', devx-track-js, devx-track-azurecli]
98
ms.date: 10/26/2021
@@ -21,60 +20,62 @@ If you don't have an Azure subscription, [create a free account](https://azure.m
2120

2221
In this tutorial, you learn how to:
2322
> [!div class="checklist"]
23+
>
2424
> * Check your device authentication
2525
> * Check device-to-cloud connectivity
2626
> * Check cloud-to-device connectivity
2727
> * Check device twin synchronization
2828
29-
[!INCLUDE [azure-cli-prepare-your-environment.md](~/articles/reusable-content/azure-cli/azure-cli-prepare-your-environment.md)]
30-
31-
[!INCLUDE [iot-hub-cli-version-info](../../includes/iot-hub-cli-version-info.md)]
29+
## Prerequisites
3230

33-
The device simulator application you run in this tutorial is written using Node.js. You need Node.js v10.x.x or later on your development machine.
31+
* This tutorial uses the Azure CLI to create cloud resources. There are two ways to run CLI commands:
3432

35-
You can download Node.js for multiple platforms from [nodejs.org](https://nodejs.org).
33+
* Use the Bash environment in [Azure Cloud Shell](../cloud-shell/quickstart.md). For more information, see [Azure Cloud Shell Quickstart - Bash](../cloud-shell/quickstart.md).
34+
[![Launch Cloud Shell in a new window](../../includes/media/cloud-shell-try-it/hdi-launch-cloud-shell.png)](https://shell.azure.com)
35+
* If you prefer to run CLI reference commands locally, [install](/cli/azure/install-azure-cli) the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see [How to run the Azure CLI in a Docker container](/cli/azure/run-azure-cli-docker).
3636

37-
You can verify the current version of Node.js on your development machine using the following command:
37+
* Sign in to the Azure CLI by using the [az login](/cli/azure/reference-index#az-login) command.
38+
* When you're prompted, install Azure CLI extensions on first use. For more information about extensions, see [Use extensions with the Azure CLI](/cli/azure/azure-cli-extensions-overview).
39+
* Run [az version](/cli/azure/reference-index?#az-version) to find the version and dependent libraries that are installed. To upgrade to the latest version, run [az upgrade](/cli/azure/reference-index?#az-upgrade).
3840

39-
```cmd/sh
40-
node --version
41-
```
41+
[!INCLUDE [iot-hub-cli-version-info](../../includes/iot-hub-cli-version-info.md)]
4242

43-
Download the sample device simulator Node.js project from https://github.com/Azure-Samples/azure-iot-samples-node/archive/master.zip and extract the ZIP archive.
43+
* The sample application that you run in this tutorial uses Node.js. You need Node.js v10.x.x or later on your development machine.
4444

45-
Make sure that port 8883 is open in your firewall. The device sample in this tutorial uses MQTT protocol, which communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see [Connecting to IoT Hub (MQTT)](iot-hub-mqtt-support.md#connecting-to-iot-hub).
45+
* You can download Node.js for multiple platforms from [nodejs.org](https://nodejs.org).
46+
* You can verify the current version of Node.js on your development machine using the following command:
4647

47-
## Create an IoT hub
48+
```cmd/sh
49+
node --version
50+
```
4851
49-
If you created a free or standard tier IoT hub in a previous quickstart or tutorial, you can skip this step.
52+
* Clone or download the sample Node.js project from [Azure IoT samples for Node.js](https://github.com/Azure-Samples/azure-iot-samples-node).
5053
51-
[!INCLUDE [iot-hub-tutorials-create-free-hub](../../includes/iot-hub-tutorials-create-free-hub.md)]
54+
* Make sure that port 8883 is open in your firewall. The device sample in this tutorial uses MQTT protocol, which communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see [Connecting to IoT Hub (MQTT)](iot-hub-mqtt-support.md#connecting-to-iot-hub).
5255
53-
## Check device authentication
54-
55-
A device must authenticate with your hub before it can exchange any data with the hub. You can use the **IoT Devices** tool in the **Device Management** section of the portal to manage your devices and check the authentication keys they're using. In this section of the tutorial, you add a new test device, retrieve its key, and check that the test device can connect to the hub. Later you reset the authentication key to observe what happens when a device tries to use an outdated key. This section of the tutorial uses the Azure portal to create, manage, and monitor a device, and the sample Node.js device simulator.
56+
## Create an IoT hub
5657
57-
Sign in to the portal and navigate to your IoT hub. Then navigate to the **IoT Devices** tool:
58+
[!INCLUDE [iot-hub-include-create-hub-cli](../../includes/iot-hub-include-create-hub-cli.md)]
5859
59-
:::image type="content" source="media/tutorial-connectivity/iot-devices-tool.png" alt-text="IoT Devices tool":::
60+
## Check device authentication
6061
61-
To register a new device, click **+ New**, set **Device ID** to **MyTestDevice**, and click **Save**.
62+
A device must authenticate with your hub before it can exchange any data with the hub. You can use the **IoT Devices** tool in the **Device Management** section of the portal to manage your devices and check the authentication keys they're using. In this section of the tutorial, you add a new test device, retrieve its key, and check that the test device can connect to the hub. Later you reset the authentication key to observe what happens when a device tries to use an outdated key.
6263
63-
:::image type="content" source="media/tutorial-connectivity/add-device.png" alt-text="Add new device":::
64+
### Register a device
6465
65-
To retrieve the connection string for **MyTestDevice**, click on it in the list of devices and then copy the **Primary Connection String** value. The connection string includes the *shared access key* for the device.
66+
[!INCLUDE [iot-hub-include-create-device-cli](../../includes/iot-hub-include-create-device-cli.md)]
6667
67-
:::image type="content" source="media/tutorial-connectivity/copy-connection-string.png" alt-text="Retrieve device connection string}":::
68+
### Simulate a test device
6869
69-
To simulate **MyTestDevice** sending telemetry to your IoT hub, run the Node.js simulated device application you downloaded previously.
70+
To simulate a device sending telemetry to your IoT hub, run the Node.js simulated device application you downloaded previously.
7071
7172
In a terminal window on your development machine, navigate to the root folder of the sample Node.js project you downloaded. Then navigate to the **iot-hub\Tutorials\ConnectivityTests** folder.
7273
73-
In the terminal window, run the following commands to install the required libraries and run the simulated device application. Use the device connection string you made a note of when you added the device in the portal.
74+
In the terminal window, run the following commands to install the required libraries and run the simulated device application. Use the device connection string you made a note of when you registered the device.
7475
7576
```cmd/sh
7677
npm install
77-
node SimulatedDevice-1.js "{your device connection string}"
78+
node SimulatedDevice-1.js "{your_device_connection_string}"
7879
```
7980

8081
The terminal window displays information as it tries to connect to your hub:
@@ -87,24 +88,21 @@ You've now successfully authenticated from a device using a device key generated
8788

8889
In this section, you reset the device key and observe the error when the simulated device tries to connect.
8990

90-
To reset the primary device key for **MyTestDevice**, run the following commands:
91+
To reset the primary device key for your device, run the following commands:
9192

9293
```azurecli-interactive
9394
# Generate a new Base64 encoded key using the current date
9495
read key < <(date +%s | sha256sum | base64 | head -c 32)
9596
96-
# Requires the IoT Extension for Azure CLI
97-
# az extension add --name azure-iot
98-
99-
# Reset the primary device key for MyTestDevice
100-
az iot hub device-identity update --device-id MyTestDevice --set authentication.symmetricKey.primaryKey=$key --hub-name {YourIoTHubName}
97+
# Reset the primary device key for test device
98+
az iot hub device-identity update --device-id {your_device_id} --set authentication.symmetricKey.primaryKey=$key --hub-name {your_iot_hub_name}
10199
```
102100

103101
In the terminal window on your development machine, run the simulated device application again:
104102

105103
```cmd/sh
106104
npm install
107-
node SimulatedDevice-1.js "{your device connection string}"
105+
node SimulatedDevice-1.js "{your_device_connection_string}"
108106
```
109107

110108
This time you see an authentication error when the application tries to connect:
@@ -123,10 +121,10 @@ In some scenarios, such as in a cloud protocol gateway or as part of a custom au
123121
To generate a known-good SAS token using the CLI, run the following command:
124122

125123
```azurecli-interactive
126-
az iot hub generate-sas-token --device-id MyTestDevice --hub-name {YourIoTHubName}
124+
az iot hub generate-sas-token --device-id {your_device_id} --hub-name {your_iot_hub_name}
127125
```
128126

129-
Make a note of the full text of the generated SAS token. A SAS token looks like the following: `SharedAccessSignature sr=tutorials-iot-hub.azure-devices.net%2Fdevices%2FMyTestDevice&sig=....&se=1524155307`
127+
Make a note of the full text of the generated SAS token. A SAS token looks like the following example: `SharedAccessSignature sr=tutorials-iot-hub.azure-devices.net%2Fdevices%2FMyTestDevice&sig=....&se=1524155307`
130128

131129
In a terminal window on your development machine, navigate to the root folder of the sample Node.js project you downloaded. Then navigate to the **iot-hub\Tutorials\ConnectivityTests** folder.
132130

@@ -161,12 +159,12 @@ If the outbound port is blocked by a firewall, the device can't connect:
161159

162160
## Check device-to-cloud connectivity
163161

164-
After a device connects, it typically tries to send telemetry to your IoT hub. This section shows you how you can verify that the telemetry sent by the device reaches your hub.
162+
After a device connects, it can start sending telemetry to your IoT hub. This section shows you how you can verify that the telemetry sent by the device reaches your hub.
165163

166164
First, retrieve the current connection string for your simulated device using the following command:
167165

168166
```azurecli-interactive
169-
az iot hub device-identity connection-string show --device-id MyTestDevice --output table --hub-name {YourIoTHubName}
167+
az iot hub device-identity connection-string show --device-id {your_device_id} --output table --hub-name {your_iot_hub_name}
170168
```
171169

172170
To run a simulated device that sends messages, navigate to the **iot-hub\Tutorials\ConnectivityTests** folder in the code you downloaded.
@@ -175,14 +173,16 @@ In the terminal window, run the following commands to install the required libra
175173

176174
```cmd/sh
177175
npm install
178-
node SimulatedDevice-3.js "{your device connection string}"
176+
node SimulatedDevice-3.js "{your_device_connection_string}"
179177
```
180178

181179
The terminal window displays information as it sends telemetry to your hub:
182180

183181
![Simulated device sending messages](media/tutorial-connectivity/sim-3-sending.png)
184182

185-
You can use **Metrics** in the portal to verify that the telemetry messages are reaching your IoT hub. Select your IoT hub in the **Resource** drop-down, select **Telemetry messages sent** as the metric, and set the time range to **Past hour**. The chart shows the aggregate count of messages sent by the simulated device:
183+
You can use **Metrics** in the portal to verify that the telemetry messages are reaching your IoT hub.
184+
185+
In the [Azure portal](https://portal.azure.com), select your IoT hub in the **Resource** drop-down. Select **Metrics** from the **Monitoring** section of the navigation menu. Select **Telemetry messages sent** as the metric, and set the time range to **Past hour**. The chart shows the aggregate count of messages sent by the simulated device:
186186

187187
:::image type="content" source="media/tutorial-connectivity/metrics-portal.png" alt-text="Screenshot showing left pane metrics." border="true":::
188188

@@ -195,13 +195,13 @@ This section shows how you can make a test direct method call to a device to che
195195
In a terminal window, use the following command to run the simulated device application:
196196

197197
```cmd/sh
198-
node SimulatedDevice-3.js "{your device connection string}"
198+
node SimulatedDevice-3.js "{your_device_connection_string}"
199199
```
200200

201201
Use a CLI command to call a direct method on the device:
202202

203203
```azurecli-interactive
204-
az iot hub invoke-device-method --device-id MyTestDevice --method-name TestMethod --timeout 10 --method-payload '{"key":"value"}' --hub-name {YourIoTHubName}
204+
az iot hub invoke-device-method --device-id {your_device_id} --method-name TestMethod --timeout 10 --method-payload '{"key":"value"}' --hub-name {your_iot_hub_name}
205205
```
206206

207207
The simulated device prints a message to the console when it receives a direct method call:
@@ -214,20 +214,20 @@ When the simulated device successfully receives the direct method call, it sends
214214

215215
## Check twin synchronization
216216

217-
Devices use twins to synchronize state between the device and the hub. In this section, you use CLI commands to send _desired properties_ to a device and read the _reported properties_ sent by the device.
217+
Devices use twins to synchronize state between the device and the hub. In this section, you use CLI commands to send *desired properties* to a device and read the *reported properties* sent by the device.
218218

219219
The simulated device you use in this section sends reported properties to the hub whenever it starts up, and prints desired properties to the console whenever it receives them.
220220

221221
In a terminal window, use the following command to run the simulated device application:
222222

223223
```cmd/sh
224-
node SimulatedDevice-3.js "{your device connection string}"
224+
node SimulatedDevice-3.js "{your_device_connection_string}"
225225
```
226226

227227
To verify that the hub received the reported properties from the device, use the following CLI command:
228228

229229
```azurecli-interactive
230-
az iot hub device-twin show --device-id MyTestDevice --hub-name {YourIoTHubName}
230+
az iot hub device-twin show --device-id {your_device_id} --hub-name {your_iot_hub_name}
231231
```
232232

233233
In the output from the command, you can see the **devicelaststarted** property in the reported properties section. This property shows the date and time you last started the simulated device.
@@ -237,7 +237,7 @@ In the output from the command, you can see the **devicelaststarted** property i
237237
To verify that the hub can send desired property values to the device, use the following CLI command:
238238

239239
```azurecli-interactive
240-
az iot hub device-twin update --set properties.desired='{"mydesiredproperty":"propertyvalue"}' --device-id MyTestDevice --hub-name {YourIoTHubName}
240+
az iot hub device-twin update --set properties.desired='{"mydesiredproperty":"propertyvalue"}' --device-id {your_device_id} --hub-name {your_iot_hub_name}
241241
```
242242

243243
The simulated device prints a message when it receives a desired property update from the hub:
@@ -248,7 +248,7 @@ In addition to receiving desired property changes as they're made, the simulated
248248

249249
## Clean up resources
250250

251-
If you don't need the IoT hub any longer, delete it and the resource group in the portal. To do so, select the **tutorials-iot-hub-rg** resource group that contains your IoT hub and click **Delete**.
251+
If you don't need the IoT hub any longer, delete it and the resource group in the portal. To do so, select the resource group that contains your IoT hub and select **Delete**.
252252

253253
## Next steps
254254

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: include file
3+
description: include file
4+
author: kgremban
5+
ms.author: kgremban
6+
ms.service: iot-hub
7+
ms.topic: include
8+
ms.date: 01/31/2023
9+
---
10+
11+
A device must be registered with your IoT hub before it can connect. In this section, you use Azure CLI to create a device identity.
12+
13+
If you already have a device registered in your IoT hub, you can skip this section.
14+
15+
To create a device identity:
16+
17+
1. Run the [az iot hub device-identity create](/cli/azure/iot/hub/device-identity#az-iot-hub-device-identity-create) command in your CLI shell. This command creates the device identity.
18+
19+
*your_iot_hub_name*. Replace this placeholder below with the name you chose for your IoT hub.
20+
21+
*myDevice*. You can use this name for the device ID throughout this article, or provide a different device name.
22+
23+
```azurecli-interactive
24+
az iot hub device-identity create --device-id myDevice --hub-name {your_iot_hub_name}
25+
```
26+
27+
1. Run the [az iot hub device-identity connection-string show](/cli/azure/iot/hub/device-identity/connection-string#az-iot-hub-device-identity-connection-string-show) command.
28+
29+
```azurecli-interactive
30+
az iot hub device-identity connection-string show --device-id myDevice --hub-name {your_iot_hub_name}
31+
```
32+
33+
The connection string output is in the following format:
34+
35+
```Output
36+
HostName=<your IoT Hub name>.azure-devices.net;DeviceId=<your device id>;SharedAccessKey=<some value>
37+
```
38+
39+
1. Save the connection string in a secure location.
40+
41+
> [!NOTE]
42+
> Keep your CLI app open. You'll use it in later steps.

0 commit comments

Comments
 (0)