title | description | author | ms.service | services | ms.topic | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|---|
Cloud-to-device messages with Azure IoT Hub (iOS) | Microsoft Docs |
How to send cloud-to-device messages to a device from an Azure IoT hub using the Azure IoT SDKs for iOS. |
kgremban |
iot-hub |
iot-hub |
conceptual |
04/19/2018 |
kgremban |
mqtt |
[!INCLUDE iot-hub-selector-c2d]
Azure IoT Hub is a fully managed service that helps enable reliable and secure bi-directional communications between millions of devices and a solution back end. The Send telemetry from a device to an IoT hub quickstart shows how to create an IoT hub, provision a device identity in it, and code a simulated device app that sends device-to-cloud messages.
This tutorial shows you how to:
- Receive cloud-to-device messages on a device.
You can find more information on cloud-to-device messages in the messaging section of the IoT Hub developer guide.
At the end of this article, you run the following Swift iOS project:
- sample-device, the same app created in Send telemetry from a device to an IoT hub, which connects to your IoT hub and receives cloud-to-device messages.
Note
IoT Hub has SDK support for many device platforms and languages (including C, Java, Python, and JavaScript) through Azure IoT device SDKs. For step-by-step instructions on how to connect your device to this tutorial's code, and generally to Azure IoT Hub, see the Azure IoT Developer Center.
-
An active Azure account. (If you don't have an account, you can create a free account in just a couple of minutes.)
-
An active IoT hub in Azure.
-
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 9.3 and iOS 11.3.
-
The latest version of CocoaPods.
-
Make sure that port 8883 is open in your firewall. The device sample in this article 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).
In this section, you simulate an iOS device running a Swift application to receive cloud-to-device messages from the IoT hub.
CocoaPods manages dependencies for iOS projects that use third-party libraries.
In a 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.
-
Retrieve the connection string for your device. You can copy this string from the Azure portal in the device details blade, or retrieve it with the following CLI command:
az iot hub device-identity show-connection-string --hub-name {YourIoTHubName} --device-id {YourDeviceID} --output table
-
Open the sample workspace in XCode.
open "MQTT Client Sample.xcworkspace"
-
Expand the MQTT Client Sample project and then 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 copied in the first step.
-
Save your changes.
-
Run the project in the device emulator with the Build and run button or the key combo command + r.
You are now ready to receive cloud-to-device messages. Use the Azure portal to send a test cloud-to-device message to your simulated IoT device.
-
In the iOS App Sample app running on the simulated IoT device, click Start. The application starts sending device-to-cloud messages, but also starts listening for cloud-to-device messages.
-
In the Azure portal, navigate to your IoT hub.
-
On the Devices page, select the device ID for your simulated IoT device.
-
Select Message to Device to open the cloud-to-device message interface.
-
Write a plaintext message in the Message body text box, then select Send message.
-
Watch the app running on your simulated IoT device. It checks for messages from IoT Hub and prints the text from the most recent one on the screen. Your output should look like the following example:
In this tutorial, you learned how to send and receive cloud-to-device messages.
To learn more about developing solutions with IoT Hub, see the IoT Hub developer guide.