Skip to content

Commit 7799190

Browse files
committed
Review IoT Hub test connectivity tutorial and clean up related includes
1 parent 6a0169f commit 7799190

File tree

5 files changed

+95
-140
lines changed

5 files changed

+95
-140
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: 44 additions & 48 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,58 @@ 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)]
29+
## Prerequisites
3030

31-
[!INCLUDE [iot-hub-cli-version-info](../../includes/iot-hub-cli-version-info.md)]
31+
* This tutorial uses the Azure CLI to create cloud resources. There are two ways to run CLI commands:
3232

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.
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).
3436

35-
You can download Node.js for multiple platforms from [nodejs.org](https://nodejs.org).
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).
3640

37-
You can verify the current version of Node.js on your development machine using the following command:
41+
* 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.
3842

39-
```cmd/sh
40-
node --version
41-
```
43+
* You can download Node.js for multiple platforms from [nodejs.org](https://nodejs.org).
44+
* You can verify the current version of Node.js on your development machine using the following command:
4245

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.
46+
```cmd/sh
47+
node --version
48+
```
4449
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).
50+
* Clone or download the sample Node.js project from [Azure IoT samples for Node.js](https://github.com/Azure-Samples/azure-iot-samples-node).
4651
47-
## Create an IoT hub
52+
* 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).
4853
49-
If you created a free or standard tier IoT hub in a previous quickstart or tutorial, you can skip this step.
50-
51-
[!INCLUDE [iot-hub-tutorials-create-free-hub](../../includes/iot-hub-tutorials-create-free-hub.md)]
54+
[!INCLUDE [iot-hub-create-hub-quickstart](../../includes/iot-hub-create-hub-quickstart.md)]
5255
5356
## Check device authentication
5457
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-
57-
Sign in to the portal and navigate to your IoT hub. Then navigate to the **IoT Devices** tool:
58+
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.
5859
59-
:::image type="content" source="media/tutorial-connectivity/iot-devices-tool.png" alt-text="IoT Devices tool":::
60+
### Register a device
6061
61-
To register a new device, click **+ New**, set **Device ID** to **MyTestDevice**, and click **Save**.
62+
[!INCLUDE [iot-hub-include-create-device-cli](../../includes/iot-hub-include-create-device-cli.md)]
6263
63-
:::image type="content" source="media/tutorial-connectivity/add-device.png" alt-text="Add new device":::
64+
### Simulate a test 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-
67-
:::image type="content" source="media/tutorial-connectivity/copy-connection-string.png" alt-text="Retrieve device connection string}":::
68-
69-
To simulate **MyTestDevice** sending telemetry to your IoT hub, run the Node.js simulated device application you downloaded previously.
66+
To simulate a device sending telemetry to your IoT hub, run the Node.js simulated device application you downloaded previously.
7067
7168
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.
7269
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.
70+
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.
7471
7572
```cmd/sh
7673
npm install
77-
node SimulatedDevice-1.js "{your device connection string}"
74+
node SimulatedDevice-1.js "{your_device_connection_string}"
7875
```
7976

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

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

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

9289
```azurecli-interactive
9390
# Generate a new Base64 encoded key using the current date
9491
read key < <(date +%s | sha256sum | base64 | head -c 32)
9592
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}
93+
# Reset the primary device key for test device
94+
az iot hub device-identity update --device-id {your_device_id} --set authentication.symmetricKey.primaryKey=$key --hub-name {your_iot_hub_name}
10195
```
10296

10397
In the terminal window on your development machine, run the simulated device application again:
10498

10599
```cmd/sh
106100
npm install
107-
node SimulatedDevice-1.js "{your device connection string}"
101+
node SimulatedDevice-1.js "{your_device_connection_string}"
108102
```
109103

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

125119
```azurecli-interactive
126-
az iot hub generate-sas-token --device-id MyTestDevice --hub-name {YourIoTHubName}
120+
az iot hub generate-sas-token --device-id {your_device_id} --hub-name {your_iot_hub_name}
127121
```
128122

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`
123+
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`
130124

131125
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.
132126

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

162156
## Check device-to-cloud connectivity
163157

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.
158+
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.
165159

166160
First, retrieve the current connection string for your simulated device using the following command:
167161

168162
```azurecli-interactive
169-
az iot hub device-identity connection-string show --device-id MyTestDevice --output table --hub-name {YourIoTHubName}
163+
az iot hub device-identity connection-string show --device-id {your_device_id} --output table --hub-name {your_iot_hub_name}
170164
```
171165

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

176170
```cmd/sh
177171
npm install
178-
node SimulatedDevice-3.js "{your device connection string}"
172+
node SimulatedDevice-3.js "{your_device_connection_string}"
179173
```
180174

181175
The terminal window displays information as it sends telemetry to your hub:
182176

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

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:
179+
You can use **Metrics** in the portal to verify that the telemetry messages are reaching your IoT hub.
180+
181+
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:
186182

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

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

197193
```cmd/sh
198-
node SimulatedDevice-3.js "{your device connection string}"
194+
node SimulatedDevice-3.js "{your_device_connection_string}"
199195
```
200196

201197
Use a CLI command to call a direct method on the device:
202198

203199
```azurecli-interactive
204-
az iot hub invoke-device-method --device-id MyTestDevice --method-name TestMethod --timeout 10 --method-payload '{"key":"value"}' --hub-name {YourIoTHubName}
200+
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}
205201
```
206202

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

215211
## Check twin synchronization
216212

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.
213+
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.
218214

219215
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.
220216

221217
In a terminal window, use the following command to run the simulated device application:
222218

223219
```cmd/sh
224-
node SimulatedDevice-3.js "{your device connection string}"
220+
node SimulatedDevice-3.js "{your_device_connection_string}"
225221
```
226222

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

229225
```azurecli-interactive
230-
az iot hub device-twin show --device-id MyTestDevice --hub-name {YourIoTHubName}
226+
az iot hub device-twin show --device-id {your_device_id} --hub-name {your_iot_hub_name}
231227
```
232228

233229
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 +233,7 @@ In the output from the command, you can see the **devicelaststarted** property i
237233
To verify that the hub can send desired property values to the device, use the following CLI command:
238234

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

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

249245
## Clean up resources
250246

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**.
247+
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**.
252248

253249
## Next steps
254250

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)