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.
If you don't have an Azure subscription, create a trial account before you begin.
Prerequisites
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.
Make sure that port 8883 is open in your firewall. The device sample in this quickstart 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).
You can use the local Azure CLI.
If you prefer, install the Azure CLI to run CLI reference commands.
Local Azure CLI, see how to install 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.
Sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
Note
This article uses the newest version of the Azure IoT extension, called azure-iot
. The legacy version is called azure-cli-iot-ext
.You should only have one version installed at a time. You can use the command az extension list
to validate the currently installed extensions.
Use az extension remove --name azure-cli-iot-ext
to remove the legacy version of the extension.
Use az extension add --name azure-iot
to add the new version of the extension.
To see what extensions you have installed, use az extension list
.
Create an IoT hub
This section describes how to create an IoT hub using the Azure portal.
Sign in to the Azure portal.
On the Azure homepage, select the + Create a resource button.
From the Categories menu, select Internet of Things, and then select IoT Hub.
On the Basics tab, complete the fields as follows:
Important
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.
Property Value Subscription Select the subscription to use for your hub. Resource group Select a resource group or create a new one. To create a new one, select Create new and fill in the name you want to use. IoT hub name Enter a name for your hub. This name must be globally unique, with a length between 3 and 50 alphanumeric characters. The name can also include the dash ( '-'
) character.Region Select the region, closest to you, where you want your hub to be located. Some features, such as IoT Hub device streams, are only available in specific regions. For these limited features, you must select one of the supported regions. Tier Select the tier that you want to use for your hub. Tier selection depends 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. The free tier allows 500 devices to be connected to the hub and up to 8,000 messages per day. Each Azure subscription can create one IoT hub in the free tier.
To compare the features available to each tier, select Compare tiers. For more information, see Choose the right IoT Hub tier for your solution.Daily message limit Select the maximum daily quota of messages for your hub. The available options depend on the tier you've selected for your hub. To see the available messaging and pricing options, select See all options and select the option that best matches the needs of your hub. For more information, see IoT Hub quotas and throttling. Select Next: Networking to continue creating your hub.
On the Networking tab, complete the fields as follows:
Property Value Connectivity configuration Choose the endpoints that devices can use to connect to your IoT hub. Accept the default setting, Public endpoint, for this example. You can change this setting after the IoT hub is created. For more information, see Managing public network access for your IoT hub. Select Next: Management to continue creating your hub.
Accept the default settings here. If desired, you can modify any of the following fields:
Pricing and scale tier: Tier selection depends 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. The free tier allows 500 devices to be connected to the hub and up to 8,000 messages per day. Each Azure subscription can create one IoT hub in the free tier. For details about other tier options, see Choosing the right IoT Hub tier.
If you're working through a quickstart, select 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 hub to support ingress of 700,000 messages, choose two S1 tier units.
Role-based access control: This property decides how you manage access to your IoT hub. Allow shared access policies or choose only role-based access control. For more information, see Control access to IoT Hub by using Azure Active Directory.
Device-to-cloud partitions: This property relates the device-to-cloud messages to the number of simultaneous readers of the messages. Most hubs need only four partitions.
Note
Prices shown are for example purposes only.
Select Next: Tags to continue to the next screen.
Tags are name/value pairs. You can assign the same tag to multiple resources and resource groups to categorize resources and consolidate billing. In this document, you won't be adding any tags. For more information, see Use tags to organize your Azure resources.
Select Next: Review + create to review your choices.
Select Create to start the deployment of your new hub. Your deployment will be in progress a few minutes while the hub is being created. Once the deployment is complete, select Go to resource to open the new hub.
Register a device
A device must be registered with your IoT hub before it can connect. In this quickstart, you use the Azure CLI 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 chose for your IoT hub.
myiOSdevice: This is the name of the device you're registering. It's recommended to use myiOSdevice as shown. If you choose a different name for your device, you'll 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:
YourIoTHubName: Replace this placeholder below with the name you chose for your IoT hub.
az iot hub device-identity connection-string show --hub-name {YourIoTHubName} --device-id myiOSdevice --output table
Make a note of the device connection string, which looks like:
HostName={YourIoTHubName}.azure-devices.cn;DeviceId=myiOSdevice;SharedAccessKey={YourSharedAccessKey}
You'll 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.
Install CocoaPods
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:
cd quickstart/sample-device
Make sure that XCode is closed, then run the following command to install the CocoaPods that are declared in the podfile file:
pod install
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 earlier.
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, 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.
Important
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 select 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, select ... 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 select Delete. After a few moments, the resource group and all of its contained resources are deleted.
Next steps
In this quickstart, 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.