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
# Quickstart: Send telemetry from a device to an IoT hub and monitor it with the Azure CLI
13
13
14
14
IoT Hub is an Azure service that enables you to ingest high volumes of telemetry from your IoT devices into the cloud for storage or processing. In this quickstart, you use the Azure CLI to create an IoT Hub and a simulated device, send device telemetry to the hub, and send a cloud-to-device message. You also use the Azure portal to visualize device metrics. This is a basic workflow for developers who use the CLI to interact with an IoT Hub application.
15
15
16
16
## Prerequisites
17
+
17
18
- 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
-
- Azure CLI. You can run all commands in this quickstart using the Azure Cloud Shell, an interactive CLI shell that runs in your browser. If you use the Cloud Shell, you don't need to install anything. If you prefer to use the CLI locally, this quickstart requires Azure CLI version 2.0.76 or later. Run az --version to find the version. To install or upgrade, see [Install Azure CLI](/cli/azure/install-azure-cli).
19
+
- Azure CLI. You can run all commands in this quickstart using the Azure Cloud Shell, an interactive CLI shell that runs in your browser. If you use the Cloud Shell, you don't need to install anything. If you prefer to use the CLI locally, this quickstart requires Azure CLI version 2.0.76 or later. Run `az --version` to find the version. To install or upgrade, see [Install Azure CLI](/cli/azure/install-azure-cli).
19
20
20
21
## Sign in to the Azure portal
21
-
Sign in to the Azure portal at https://portal.azure.com.
22
22
23
-
Regardless whether you run the CLI locally or in the Cloud Shell, keep the portal open in your browser. You use it later in this quickstart.
23
+
Sign in to the [Azure portal](https://portal.azure.com).
24
+
25
+
Regardless of whether you run the CLI locally or in the Cloud Shell, keep the portal open in your browser. You use it later in this quickstart.
24
26
25
27
## Launch the Cloud Shell
28
+
26
29
In this section, you launch an instance of the Azure Cloud Shell. If you use the CLI locally, skip to the section [Prepare two CLI sessions](#prepare-two-cli-sessions).
27
30
28
31
To launch the Cloud Shell:
29
32
30
-
1. Select the **Cloud Shell** button on the top-right menu bar in the Azure portal.
33
+
1. Select the **Cloud Shell** button on the top-right menu bar in the Azure portal.
> If this is the first time you've used the Cloud Shell, it prompts you to create storage, which is required to use the Cloud Shell. Select a subscription to create a storage account and Microsoft Azure Files share.
38
+
> If this is the first time you've used the Cloud Shell, it prompts you to create storage, which is required to use the Cloud Shell. Select a subscription to create a storage account and Microsoft Azure Files share.
36
39
37
-
2. Select your preferred CLI environment in the **Select environment** dropdown. This quickstart uses the **Bash** environment. All the following CLI commands work in the PowerShell environment too.
40
+
2. Select your preferred CLI environment in the **Select environment** dropdown. This quickstart uses the **Bash** environment. All the following CLI commands work in the PowerShell environment too.
@@ -44,147 +47,159 @@ In this section, you prepare two Azure CLI sessions. If you're using the Cloud S
44
47
45
48
Azure CLI requires you to be logged into your Azure account. All communication between your Azure CLI shell session and your IoT hub is authenticated and encrypted. As a result, this quickstart does not need additional authentication that you'd use with a real device, such as a connection string.
46
49
47
-
* Run the [az extension add](/cli/azure/extension#az_extension_add) command to add the Microsoft Azure IoT Extension for Azure CLI to your CLI shell. The IOT Extension adds IoT Hub, IoT Edge, and IoT Device Provisioning Service (DPS) specific commands to Azure CLI.
50
+
- Run the [az extension add](/cli/azure/extension#az_extension_add) command to add the Microsoft Azure IoT Extension for Azure CLI to your CLI shell. The IOT Extension adds IoT Hub, IoT Edge, and IoT Device Provisioning Service (DPS) specific commands to Azure CLI.
48
51
49
52
```azurecli
50
53
az extension add --name azure-iot
51
54
```
52
-
53
-
After you install the Azure IOT extension, you don't need to install it again in any Cloud Shell session.
55
+
56
+
After you install the Azure IOT extension, you don't need to install it again in any Cloud Shell session.
*Open a second CLI session. If you're using the Cloud Shell, select **Open new session**. If you're using the CLI locally, open a second instance.
60
+
-Open a second CLI session. If you're using the Cloud Shell, select **Open new session**. If you're using the CLI locally, open a second instance.
58
61
59
62
>[!div class="mx-imgBorder"]
60
63
>
61
64
62
-
## Create an IoT Hub
63
-
In this section, you use the Azure CLI to create a resource group and an IoT Hub. An Azure resource group is a logical container into which Azure resources are deployed and managed. An IoT Hub acts as a central message hub for bi-directional communication between your IoT application and the devices.
65
+
## Create an IoT hub
66
+
67
+
In this section, you use the Azure CLI to create a resource group and an IoT hub. An Azure resource group is a logical container into which Azure resources are deployed and managed. An IoT hub acts as a central message hub for bi-directional communication between your IoT application and the devices.
64
68
65
69
> [!TIP]
66
-
> Optionally, you can create an Azure resource group, an IoT Hub, and other resources by using the [Azure portal](iot-hub-create-through-portal.md), [Visual Studio Code](iot-hub-create-use-iot-toolkit.md), or other programmatic methods.
70
+
> Optionally, you can create an Azure resource group, an IoT hub, and other resources by using the [Azure portal](iot-hub-create-through-portal.md), [Visual Studio Code](iot-hub-create-use-iot-toolkit.md), or other programmatic methods.
67
71
68
-
1. Run the [az group create](/cli/azure/group#az_group_create) command to create a resource group. The following command creates a resource group named *MyResourceGroup* in the *eastus* location.
72
+
1. Run the [az group create](/cli/azure/group#az_group_create) command to create a resource group. The following command creates a resource group named *MyResourceGroup* in the *eastus* location.
69
73
70
74
```azurecli
71
75
az group create --name MyResourceGroup --location eastus
72
76
```
73
77
74
-
1. Run the [az iot hub create](/cli/azure/iot/hub#az_iot_hub_create) command to create an IoT hub. It might take a few minutes to create an IoT hub.
78
+
1. Run the [az iot hub create](/cli/azure/iot/hub#az_iot_hub_create) command to create an IoT hub. It might take a few minutes to create an IoT hub.
75
79
76
-
*YourIotHubName*. Replace this placeholder name and the curly brackets with the name you chose for your IoT hub. An IoT hub name must be globally unique in Azure. This placeholder is used in the rest of this quickstart to represent your IoT hub name.
80
+
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub. An IoT hub name must be globally unique in Azure. This placeholder is used in the rest of this quickstart to represent your IoT hub name.
77
81
78
82
```azurecli
79
83
az iot hub create --resource-group MyResourceGroup --name {YourIoTHubName}
80
84
```
81
85
82
86
## Create and monitor a device
87
+
83
88
In this section, you create a simulated device in the first CLI session. The simulated device sends device telemetry to your IoT hub. In the second CLI session, you monitor events and telemetry, and send a cloud-to-device message to the simulated device.
84
89
85
90
To create and start a simulated device:
86
-
1. Run the [az iot hub device-identity create](/cli/azure/iot/hub/device-identity#az_iot_hub_device_identity_create) command in the first CLI session. This creates the simulated device identity.
87
91
88
-
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
92
+
1. Run the [az iot hub device-identity create](/cli/azure/iot/hub/device-identity#az_iot_hub_device_identity_create) command in the first CLI session. This creates the simulated device identity.
89
93
90
-
*simDevice*. You can use this name directly for the simulated device in the rest of this quickstart. Optionally, use a different name.
94
+
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
95
+
96
+
*simDevice*. You can use this name directly for the simulated device in the rest of this quickstart. Optionally, use a different name.
91
97
92
98
```azurecli
93
99
az iot hub device-identity create --device-id simDevice --hub-name {YourIoTHubName}
94
100
```
95
101
96
102
1. Run the [az iot device simulate](/cli/azure/iot/device#az_iot_device_simulate) command in the first CLI session. This starts the simulated device. The device sends telemetry to your IoT hub and receives messages from it.
97
103
98
-
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
104
+
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
99
105
100
106
```azurecli
101
107
az iot device simulate -d simDevice -n {YourIoTHubName}
102
108
```
103
109
104
110
To monitor a device:
111
+
105
112
1. In the second CLI session, run the [az iot hub monitor-events](/cli/azure/iot/hub#az_iot_hub_monitor_events) command. This starts monitoring the simulated device. The output shows telemetry that the simulated device sends to the IoT hub.
106
113
107
-
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
114
+
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
108
115
109
116
```azurecli
110
117
az iot hub monitor-events --output table --hub-name {YourIoTHubName}
1. After you monitor the simulated device in the second CLI session, press Ctrl+C to stop monitoring.
122
+
1. After you monitor the simulated device in the second CLI session, press Ctrl+C to stop monitoring.
116
123
117
124
## Use the CLI to send a message
125
+
118
126
In this section, you use the second CLI session to send a message to the simulated device.
119
127
120
128
1. In the first CLI session, confirm that the simulated device is running. If the device has stopped, run the following command to start it:
121
129
122
-
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
130
+
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
123
131
124
132
```azurecli
125
133
az iot device simulate -d simDevice -n {YourIoTHubName}
126
134
```
127
135
128
136
1. In the second CLI session, run the [az iot device c2d-message send](/cli/azure/iot/device/c2d-message#az_iot_device_c2d-message-send) command. This sends a cloud-to-device message from your IoT hub to the simulated device. The message includes a string and two key-value pairs.
129
137
130
-
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
138
+
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
Optionally, you can send cloud-to-device messages by using the Azure portal. To do this, browse to the overview page for your IoT Hub, select **IoT Devices**, select the simulated device, and select **Message to Device**.
136
143
137
-
1. In the first CLI session, confirm that the simulated device received the message.
144
+
Optionally, you can send cloud-to-device messages by using the Azure portal. To do this, browse to the overview page for your IoT Hub, select **IoT Devices**, select the simulated device, and select **Message to Device**.
145
+
146
+
1. In the first CLI session, confirm that the simulated device received the message.
1. After you view the message, close the second CLI session. Keep the first CLI session open. You use it to clean up resources in a later step.
142
151
143
152
## View messaging metrics in the portal
144
-
The Azure portal enables you to manage all aspects of your IoT Hub and devices. In a typical IoT Hub application that ingests telemetry from devices, you might want to monitor devices or view metrics on device telemetry.
153
+
154
+
The Azure portal enables you to manage all aspects of your IoT hub and devices. In a typical IoT Hub application that ingests telemetry from devices, you might want to monitor devices or view metrics on device telemetry.
145
155
146
156
To visualize messaging metrics in the Azure portal:
147
-
1. In the left navigation menu on the portal, select **All Resources**. This lists all resources in your subscription, including the IoT hub you created.
157
+
158
+
1. In the left navigation menu on the portal, select **All Resources**. This lists all resources in your subscription, including the IoT hub you created.
148
159
149
160
1. Select the link on the IoT hub you created. The portal displays the overview page for the hub.
150
161
151
-
1. Select **Metrics** in the left pane of your IoT Hub.
162
+
1. Select **Metrics** in the left pane of your IoT Hub.
1. In the **Scope** field, enter your IoT hub name.
156
167
157
-
2. Select *Iot Hub Standard Metrics* in **Metric Namespace**.
168
+
1. In the **Metric Namespace** field, select *Iot Hub Standard Metrics*.
158
169
159
-
3. Select *Total number of messages used* in **Metric**.
170
+
1. In the **Metric** field, select *Total number of messages used*.
160
171
161
-
4. Hover your mouse pointer over the area of the timeline in which your device sent messages. The total number of messages at a point in time appears in the lower left corner of the timeline.
172
+
1. Hover your mouse pointer over the area of the timeline in which your device sent messages. The total number of messages at a point in time appears in the lower left corner of the timeline.
5. Optionally, use the **Metric** dropdown to display other metrics on your simulated device. For example, *C2d message deliveries completed* or *Total devices (preview)*.
176
+
1. Optionally, use the **Metric** dropdown to display other metrics on your simulated device. For example, *C2d message deliveries completed* or *Total devices (preview)*.
166
177
167
178
## Clean up resources
179
+
168
180
If you no longer need the Azure resources created in this quickstart, you can use the Azure CLI to delete them.
169
181
170
-
If you continue to the next recommended article, you can keep the resources you've already created and reuse them.
182
+
If you continue to the next recommended article, you can keep the resources you've already created and reuse them.
171
183
172
184
> [!IMPORTANT]
173
-
> Deleting a resource group is irreversible. The resource group and all the resources contained in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources.
185
+
> Deleting a resource group is irreversible. The resource group and all the resources contained in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources.
174
186
175
187
To delete a resource group by name:
188
+
176
189
1. Run the [az group delete](/cli/azure/group#az_group_delete) command. This removes the resource group, the IoT Hub, and the device registration you created.
177
190
178
191
```azurecli
179
192
az group delete --name MyResourceGroup
180
193
```
194
+
181
195
1. Run the [az group list](/cli/azure/group#az_group_list) command to confirm the resource group is deleted.
182
196
183
197
```azurecli
184
198
az group list
185
199
```
186
200
187
201
## Next steps
202
+
188
203
In this quickstart, you used the Azure CLI to create an IoT hub, create a simulated device, send telemetry, monitor telemetry, send a cloud-to-device message, and clean up resources. You used the Azure portal to visualize messaging metrics on your device.
189
204
190
205
If you are a device developer, the suggested next step is to see the telemetry quickstart that uses the Azure IoT Device SDK for C. Optionally, see one of the available Azure IoT Hub telemetry quickstart articles in your preferred language or SDK.
0 commit comments