You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
34
32
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
+
[](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).
36
36
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).
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.
44
44
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:
46
47
47
-
## Create an IoT hub
48
+
```cmd/sh
49
+
node --version
50
+
```
48
51
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).
* 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).
52
55
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
56
57
57
-
Sign in to the portal and navigate to your IoT hub. Then navigate to the **IoT Devices** tool:
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.
62
63
63
-
:::image type="content" source="media/tutorial-connectivity/add-device.png" alt-text="Add new device":::
64
+
### Register a device
64
65
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.
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.
70
71
71
72
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.
72
73
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.
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
123
121
To generate a known-good SAS token using the CLI, run the following command:
124
122
125
123
```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}
127
125
```
128
126
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`
130
128
131
129
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.
132
130
@@ -161,12 +159,12 @@ If the outbound port is blocked by a firewall, the device can't connect:
161
159
162
160
## Check device-to-cloud connectivity
163
161
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.
165
163
166
164
First, retrieve the current connection string for your simulated device using the following command:
167
165
168
166
```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}
170
168
```
171
169
172
170
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
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:
186
186
187
187
:::image type="content" source="media/tutorial-connectivity/metrics-portal.png" alt-text="Screenshot showing left pane metrics." border="true":::
188
188
@@ -195,13 +195,13 @@ This section shows how you can make a test direct method call to a device to che
195
195
In a terminal window, use the following command to run the simulated device application:
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
214
214
215
215
## Check twin synchronization
216
216
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.
218
218
219
219
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.
220
220
221
221
In a terminal window, use the following command to run the simulated device application:
To verify that the hub received the reported properties from the device, use the following CLI command:
228
228
229
229
```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}
231
231
```
232
232
233
233
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
237
237
To verify that the hub can send desired property values to the device, use the following CLI command:
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
248
248
249
249
## Clean up resources
250
250
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**.
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:
0 commit comments