Skip to content

Latest commit

 

History

History
149 lines (90 loc) · 7.08 KB

quickstart-send-telemetry-ios.md

File metadata and controls

149 lines (90 loc) · 7.08 KB
title description author manager ms.author ms.service services ms.topic ms.custom ms.date
Send telemetry to Azure IoT Hub quickstart (iOS)
Run a sample iOS application to send simulated telemetry to an IoT hub and to read telemetry from the IoT hub for processing in the cloud.
wesmc7777
philmea
wesmc
iot-hub
iot-hub
quickstart
mvc
mqtt
04/03/2019

Quickstart: Send telemetry from a device to an IoT hub (iOS)

[!INCLUDE iot-hub-quickstarts-1-selector]

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.

[!INCLUDE cloud-shell-try-it.md]

If you don't have an Azure subscription, create a free 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).

  • 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-iot
    

    [!INCLUDE iot-hub-cli-version-info]

Create an IoT hub

[!INCLUDE iot-hub-include-create-hub]

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.

  1. 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
    
  2. Run the following command in Azure Cloud Shell 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 show-connection-string --hub-name {YourIoTHubName} --device-id myiOSdevice --output table
    

    Make a note of the device connection string, which looks like:

    HostName={YourIoTHubName}.azure-devices.net;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

  1. Open the sample workspace in XCode.

    open "MQTT Client Sample.xcworkspace"
  2. Expand the MQTT Client Sample project and then expand the folder of the same name.

  3. Open ViewController.swift for editing in XCode.

  4. Search for the connectionString variable and update the value with the device connection string that you made a note of earlier.

  5. Save your changes.

  6. Run the project in the device emulator with the Build and run button or the key combo command + r.

    Run the project

  7. 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:

Run the simulated device

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:

View telemetry

Clean up resources

[!INCLUDE iot-hub-quickstarts-clean-up-resources]

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.

[!div class="nextstepaction"] Quickstart: Control a device connected to an IoT hub