Skip to content

Commit c0b0546

Browse files
authored
Merge pull request #95353 from timlt/iothub-qs-cli
Publish new CLI quickstart
2 parents 1043a1d + ef6185e commit c0b0546

10 files changed

+640
-449
lines changed

articles/iot-hub/TOC.yml

Lines changed: 451 additions & 449 deletions
Large diffs are not rendered by default.
1.39 KB
Loading
12.8 KB
Loading
36.6 KB
Loading
9.35 KB
Loading
12.1 KB
Loading
4.37 KB
Loading
36.4 KB
Loading
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
---
2+
title: Send telemetry to Azure IoT Hub (CLI) quickstart
3+
description: This quickstart shows developers new to IoT Hub how to get started by using the Azure CLI to create an IoT hub, send telemetry, and view messages between a device and the hub.
4+
ms.service: iot-hub
5+
ms.topic: quickstart
6+
ms.custom: [iot-send-telemetry-cli, iot-p0-scenario]
7+
ms.author: timlt
8+
author: timlt
9+
ms.date: 11/06/2019
10+
---
11+
# Quickstart: Send telemetry from a device to an IoT hub and monitor it with the Azure CLI
12+
13+
[!INCLUDE [iot-hub-quickstarts-1-selector](../../includes/iot-hub-quickstarts-1-selector.md)]
14+
15+
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.
16+
17+
## Prerequisites
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.
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).
20+
21+
## Sign in to the Azure portal
22+
Sign in to the Azure portal at https://portal.azure.com.
23+
24+
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.
25+
26+
## Launch the Cloud Shell
27+
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).
28+
29+
To launch the Cloud Shell:
30+
1. Select the **Cloud Shell** button on the top-right menu bar in the Azure portal.
31+
32+
![Azure portal Cloud Shell button](media/quickstart-send-telemetry-cli/cloud-shell-button.png)
33+
34+
> [!NOTE]
35+
> 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+
37+
1. 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.
38+
39+
![Select CLI environment](media/quickstart-send-telemetry-cli/cloud-shell-environment.png)
40+
41+
## Prepare two CLI sessions
42+
In this section, you prepare two Azure CLI sessions. In the Cloud Shell, you will run the two sessions in separate browser tabs. In a local CLI client, you will run two separate CLI instances. You'll use the first session as a simulated device, and the second session to monitor and send messages. To run a command, select **Copy** to copy a block of code in this quickstart, paste it into your shell session, and run it.
43+
44+
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.
45+
46+
1. Run the [az extension add](https://docs.microsoft.com/cli/azure/extension?view=azure-cli-latest#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.
47+
48+
```azurecli
49+
az extension add --name azure-cli-iot-ext
50+
```
51+
After you install the Azure IOT extension, you don't need to install it again in any Cloud Shell session.
52+
53+
1. 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.
54+
55+
![Open new Cloud Shell session](media/quickstart-send-telemetry-cli/cloud-shell-new-session.png)
56+
57+
## Create an IoT Hub
58+
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.
59+
60+
> [!TIP]
61+
> 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.
62+
63+
1. Run the [az group create](https://docs.microsoft.com/cli/azure/group?view=azure-cli-latest#az-group-create) command to create a resource group. The following command creates a resource group named *MyResourceGroup* in the *eastus* location.
64+
65+
```azurecli
66+
az group create --name MyResourceGroup --location eastus
67+
```
68+
69+
1. Run the [az iot hub create](https://docs.microsoft.com/cli/azure/iot/hub?view=azure-cli-latest#az-iot-hub-create) command to create an IoT hub.
70+
71+
*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.
72+
73+
```azurecli
74+
az iot hub create --resource-group MyResourceGroup --name {YourIoTHubName}
75+
```
76+
77+
## Create and monitor a device
78+
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.
79+
80+
To create and start a simulated device:
81+
1. Run the [az iot hub device-identity create](https://docs.microsoft.com/cli/azure/ext/azure-cli-iot-ext/iot/hub/device-identity?view=azure-cli-latest#ext-azure-cli-iot-ext-az-iot-hub-device-identity-create) command in the first CLI session. This creates the simulated device identity.
82+
83+
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
84+
85+
*simDevice*. You can use this name directly for the simulated device in the rest of this quickstart. Optionally, use a different name.
86+
87+
```azurecli
88+
az iot hub device-identity create --device-id simDevice --hub-name {YourIoTHubName}
89+
```
90+
91+
1. Run the [az iot device simulate](https://docs.microsoft.com/cli/azure/ext/azure-cli-iot-ext/iot/device?view=azure-cli-latest#ext-azure-cli-iot-ext-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.
92+
93+
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
94+
95+
```azurecli
96+
az iot device simulate -d simDevice -n {YourIoTHubName}
97+
```
98+
99+
To monitor a device:
100+
1. In the second CLI session, run the [az iot hub monitor-events](https://docs.microsoft.com/cli/azure/ext/azure-cli-iot-ext/iot/hub?view=azure-cli-latest#ext-azure-cli-iot-ext-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.
101+
102+
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
103+
104+
```azurecli
105+
az iot hub monitor-events --output table --hub-name {YourIoTHubName}
106+
```
107+
108+
![Cloud Shell monitor events](media/quickstart-send-telemetry-cli/cloud-shell-monitor.png)
109+
110+
1. After you monitor the simulated device in the second CLI session, press Ctrl+C to stop monitoring.
111+
112+
## Use the CLI to send a message
113+
In this section, you use the second CLI session to send a message to the simulated device.
114+
115+
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:
116+
117+
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
118+
119+
```azurecli
120+
az iot device simulate -d simDevice -n {YourIoTHubName}
121+
```
122+
123+
1. In the second CLI session, run the [az iot device c2d-message send](https://docs.microsoft.com/cli/azure/ext/azure-cli-iot-ext/iot/device/c2d-message?view=azure-cli-latest#ext-azure-cli-iot-ext-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.
124+
125+
*YourIotHubName*. Replace this placeholder below with the name you chose for your IoT hub.
126+
127+
```azurecli
128+
az iot device c2d-message send -d simDevice --data "Hello World" --props "key0=value0;key1=value1" -n {YourIoTHubName}
129+
```
130+
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**.
131+
132+
1. In the first CLI session, confirm that the simulated device received the message.
133+
134+
![Cloud Shell cloud-to-device message](media/quickstart-send-telemetry-cli/cloud-shell-receive-message.png)
135+
136+
1. After you view the message, close both CLI sessions.
137+
138+
## View messaging metrics in the portal
139+
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.
140+
141+
To visualize messaging metrics in the Azure portal:
142+
1. On the **Home** page in the portal, select **All Resources**.
143+
144+
1. In the list of resource groups, select the IoT hub you created.
145+
146+
1. Select **Metrics** in the left pane of your IoT Hub.
147+
148+
![IoT Hub messaging metrics](media/quickstart-send-telemetry-cli/iot-hub-portal-metrics.png)
149+
150+
1. Enter your IoT hub name in **Scope**.
151+
152+
2. Select *Iot Hub Standard Metrics* in **Metric Namespace**.
153+
154+
3. Select *Total number of messages used* in **Metric**.
155+
156+
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.
157+
158+
![View Azure IoT Hub metrics](media/quickstart-send-telemetry-cli/iot-hub-portal-view-metrics.png)
159+
160+
5. Optionally, use the **Metric** dropdown to display other metrics on your simulated device. For example, *C2d message deliveries completed* or *Total devices (preview)*.
161+
162+
## Clean up resources
163+
If you no longer need the Azure resources created in this quickstart, you can use the Azure CLI to delete them.
164+
165+
If you continue to the next recommended article, you can keep the resources you've already created and reuse them.
166+
167+
> [!IMPORTANT]
168+
> 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.
169+
170+
To delete a resource group by name:
171+
1. Run the [az group delete](https://docs.microsoft.com/cli/azure/group?view=azure-cli-latest#az-group-delete) command. This removes the resource group, the IoT Hub, and the device registration you created.
172+
173+
```azurecli
174+
az group delete --name MyResourceGroup
175+
```
176+
1. Run the [az group list](https://docs.microsoft.com/cli/azure/group?view=azure-cli-latest#az-group-list) command to confirm the resource group is deleted.
177+
178+
```azurecli
179+
az group list
180+
```
181+
182+
## Next steps
183+
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.
184+
185+
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.
186+
187+
> [!div class="nextstepaction"]
188+
> [Quickstart: Send telemetry from a device to an IoT hub (C)](quickstart-send-telemetry-c.md)

includes/iot-hub-quickstarts-1-selector.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
---
1212

1313
> [!div class="op_single_selector"]
14+
> * [CLI](../articles/iot-hub/quickstart-send-telemetry-cli.md)
1415
> * [C](../articles/iot-hub/quickstart-send-telemetry-c.md)
1516
> * [.NET](../articles/iot-hub/quickstart-send-telemetry-dotnet.md)
1617
> * [Java](../articles/iot-hub/quickstart-send-telemetry-java.md)

0 commit comments

Comments
 (0)