Quickstart: Send telemetry from a device to an IoT hub (iOS)
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 article, you send telemetry from a simulated device application to IoT Hub. Then you can view the data from a back-end application.
This article uses a pre-written Swift application to send the telemetry and a CLI utility to read the telemetry from IoT Hub.
Use Azure Cloud Shell
Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. Cloud Shell lets you use either
PowerShell to work with Azure services. You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.
To launch Azure Cloud Shell:
|Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell.|
|Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser.|
|Select the Cloud Shell button on the top-right menu bar in the Azure portal.|
To run the code in this article in Azure Cloud Shell:
- Launch Cloud Shell.
- Select the Copy button on a code block to copy the code.
- Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.
- Press Enter to run the code.
If you don’t have an Azure subscription, create a free account before you begin.
Download the code sample from Azure samples
The latest version of XCode, running the latest version of the iOS SDK. This quickstart was tested with XCode 10.2 and iOS 12.2.
The latest version of CocoaPods.
Run the following command to add the Microsoft Azure IoT Extension for Azure CLI to your Cloud Shell instance. The IOT Extension adds IoT Hub, IoT Edge, and IoT Device Provisioning Service (DPS) specific commands to Azure CLI.
az extension add --name azure-cli-iot-ext
Create an IoT hub
This section describes how to create an IoT hub using the Azure portal.
Log in to the Azure portal.
Choose +Create a resource, then Search the Marketplace for the IoT Hub.
Select IoT Hub and click the Create button. You see the first screen for creating an IoT hub.
Fill in the fields.
Subscription: Select the subscription to use for your IoT hub.
Resource Group: You can create a new resource group or use an existing one. To create a new one, click Create new and fill in the name you want to use. To use an existing resource group, click Use existing and select the resource group from the dropdown list. For more information, see Manage Azure Resource Manager resource groups.
Region: This is the region in which you want your hub to be located. Select the location closest to you from the dropdown list.
IoT Hub Name: Put in the name for your IoT Hub. This name must be globally unique. If the name you enter is available, a green check mark appears.
Because the IoT hub will be publicly discoverable as a DNS endpoint, be sure to avoid entering any sensitive or personally identifiable information when you name it.
Click Next: Size and scale to continue creating your IoT hub.
On this screen, you can take the defaults and just click Review + create at the bottom.
Pricing and scale tier: You can choose from several tiers depending on how many features you want and how many messages you send through your solution per day. The free tier is intended for testing and evaluation. It allows 500 devices to be connected to the IoT hub and up to 8,000 messages per day. Each Azure subscription can create one IoT Hub in the free tier.
IoT Hub units: The number of messages allowed per unit per day depends on your hub's pricing tier. For example, if you want the IoT hub to support ingress of 700,000 messages, you choose two S1 tier units.
For details about the other tier options, see Choosing the right IoT Hub tier.
Advanced / Device-to-cloud partitions: This property relates the device-to-cloud messages to the number of simultaneous readers of the messages. Most IoT hubs only need four partitions.
Click Review + create to review your choices. You see something similar to this screen.
Click Create to create your new IoT hub. Creating the hub takes a few minutes.
Register a device
A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure Cloud Shell to register a simulated device.
Run the following command in Azure Cloud Shell to create the device identity.
YourIoTHubName : Replace this placeholder below with the name you choose for your IoT hub.
myiOSdevice : This is the name given for the registered device. Use myiOSdevice as shown. If you choose a different name for your device, you will also need to use that name throughout this article, and update the device name in the sample applications before you run them.
az iot hub device-identity create --hub-name YourIoTHubName --device-id myiOSdevice
Run the following command to get the device connection string for the device you just registered:
az iot hub device-identity show-connection-string --hub-name YourIoTHubName --device-id myiOSdevice --output table
Make a note of the device connection string, which looks like:
You use this value later in the quickstart.
Send simulated telemetry
The sample application runs on an iOS device, which connects to a device-specific endpoint on your IoT hub and sends simulated temperature and humidity telemetry.
CocoaPods manage dependencies for iOS projects that use third-party libraries.
In a local terminal window, navigate to the Azure-IoT-Samples-iOS folder that you downloaded in the prerequisites. Then, navigate to the sample project:
Make sure that XCode is closed, then run the following command to install the CocoaPods that are declared in the podfile file:
Along with installing the pods required for your project, the installation command also created an XCode workspace file that is already configured to use the pods for dependencies.
Run the sample application
Open the sample workspace in XCode.
open "MQTT Client Sample.xcworkspace"
Expand the MQTT Client Sample project and then expand the folder of the same name.
Open ViewController.swift for editing in XCode.
Search for the connectionString variable and update the value with the device connection string that you made a note of previously.
Save your changes.
Run the project in the device emulator with the Build and run button or the key combo command + r.
When the emulator opens, select Start in the sample app.
The following screenshot shows some example output as the application sends simulated telemetry to your IoT hub:
Read the telemetry from your hub
The sample app that you ran on the XCode emulator shows data about messages sent from the device. You can also view the data through your IoT hub as it is received. The IoT Hub CLI extension can connect to the service-side Events endpoint on your IoT Hub. The extension receives the device-to-cloud messages sent from your simulated device. An IoT Hub back-end application typically runs in the cloud to receive and process device-to-cloud messages.
Run the following commands in Azure Cloud Shell, replacing
YourIoTHubName with the name of your IoT hub:
az iot hub monitor-events --device-id myiOSdevice --hub-name YourIoTHubName
The following screenshot shows the output as the extension receives telemetry sent by the simulated device to the hub:
The following screenshot shows the type of telemetry that you see in your local terminal window:
Clean up resources
If you will be continuing to the next recommended article, you can keep the resources you've already created and reuse them.
Otherwise, you can delete the Azure resources created in this article to avoid charges.
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. If you created the IoT Hub inside an existing resource group that contains resources you want to keep, only delete the IoT Hub resource itself instead of deleting the resource group.
To delete a resource group by name:
Sign in to the Azure portal and click Resource groups.
In the Filter by name... textbox, type the name of the resource group containing your IoT Hub.
To the right of your resource group in the result list, click ... then Delete resource group.
You will be asked to confirm the deletion of the resource group. Type the name of your resource group again to confirm, and then click Delete. After a few moments, the resource group and all of its contained resources are deleted.
In this article, you set up an IoT hub, registered a device, sent simulated telemetry to the hub from an iOS device, and read the telemetry from the hub.
To learn how to control your simulated device from a back-end application, continue to the next quickstart.