使用 IoT 中心发送云到设备消息 (iOS)Send cloud-to-device messages with IoT Hub (iOS)

Azure IoT 中心是一项完全托管的服务,有助于在数百万台设备和单个解决方案后端之间实现安全可靠的双向通信。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. 从设备将遥测数据发送到 IoT 中心快速入门介绍了如何创建 IoT 中心、在其中预配设备标识,以及编写模拟设备应用来发送设备到云的消息。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:

  • 通过 IoT 中心,将云到设备的消息从解决方案后端发送到单个设备。From your solution back end, send cloud-to-device messages to a single device through IoT Hub.

  • 在设备上接收云到设备的消息。Receive cloud-to-device messages on a device.

  • 通过解决方案后端,请求确认收到从 IoT 中心发送到设备的消息(反馈 )。From your solution back end, request delivery acknowledgment (feedback) for messages sent to a device from IoT Hub.

可以在 IoT 中心开发人员指南的消息发送部分中找到有关云到设备消息的详细信息。You can find more information on cloud-to-device messages in the messaging section of the IoT Hub developer guide.

在本文结束时,运行两个 Swift iOS 项目:At the end of this article, you run two Swift iOS projects:

  • sample-device:在将遥测数据从设备发送到 IoT 中心中创建的同一应用,可连接到 IoT 中心并接收云到设备的消息。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.

  • sample-service:通过 IoT 中心将云到设备的消息发送到模拟设备应用,然后接收中心的传送确认。sample-service, which sends a cloud-to-device message to the simulated device app through IoT Hub, and then receives its delivery acknowledgement.

备注

IoT 中心通过 Azure IoT 设备 SDK 对许多设备平台和语言(包括 C、Java、Python 和 Javascript)提供 SDK 支持。IoT Hub has SDK support for many device platforms and languages (including C, Java, Python, and Javascript) through Azure IoT device SDKs. 有关如何将设备连接到本教程中的代码(通常是连接到 Azure IoT 中心)的逐步说明,请参阅 Azure IoT 开发人员中心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.

必备条件Prerequisites

  • 有效的 Azure 帐户。An active Azure account. (如果没有帐户,只需几分钟即可创建一个试用帐户。)(If you don't have an account, you can create a trial account in just a couple of minutes.)
  • Azure 中的活动 IoT 中心。An active IoT hub in Azure.
  • Azure 示例中的代码示例。The code sample from Azure samples .
  • 最新版本的 XCode,运行最新版本的 iOS SDK。The latest version of XCode, running the latest version of the iOS SDK. 本快速入门已使用 XCode 9.3 和 iOS 11.3 测试过。This quickstart was tested with XCode 9.3 and iOS 11.3.
  • 最新版 CocoaPodsThe latest version of CocoaPods.
  • 确保已在防火墙中打开端口 8883。Make sure that port 8883 is open in your firewall. 本文中的设备示例使用 MQTT 协议,该协议通过端口 8883 进行通信。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. 有关解决此问题的更多信息和方法,请参阅连接到 IoT 中心(MQTT)For more information and ways to work around this issue, see Connecting to IoT Hub (MQTT).

模拟 IoT 设备Simulate an IoT device

在本部分,我们将模拟一个运行 Swift 应用程序的 iOS 设备,以从 IoT 中心接收云到设备的消息。In this section, you simulate an iOS device running a Swift application to receive cloud-to-device messages from the IoT hub.

这是在将遥测数据从设备发送到 IoT 中心一文中创建的示例设备。This is the sample device that you create in the article Send telemetry from a device to an IoT hub. 如果已运行该设备,则可跳过本部分。If you already have that running, you can skip this section.

安装 CocoaPodsInstall CocoaPods

CocoaPods 管理那些使用第三方库的 iOS 项目的依赖项。CocoaPods manage dependencies for iOS projects that use third-party libraries.

在终端窗口中,导航到在先决条件部分下载的 Azure-IoT-Samples-iOS 文件夹。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

确保 XCode 已关闭,运行以下命令,以便安装在 podfile 文件中声明的 CocoaPods:Make sure that XCode is closed, then run the following command to install the CocoaPods that are declared in the podfile file:

pod install

除了为项目安装所需的 Pod,安装命令还创建了一个 XCode 工作区文件,该文件已配置为对依赖项使用 Pod。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 device application

  1. 检索设备的连接字符串。Retrieve the connection string for your device. 可以从 Azure 门户的设备详细信息边栏选项卡复制此字符串,或者使用以下 CLI 命令检索它: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
    
  2. 在 XCode 中打开示例工作区。Open the sample workspace in XCode.

    open "MQTT Client Sample.xcworkspace"
    
  3. 展开“MQTT 客户端示例”项目,然后展开同名的文件夹。 Expand the MQTT Client Sample project and then folder of the same name.

  4. 打开 ViewController.swift,以便在 XCode 中进行编辑。Open ViewController.swift for editing in XCode.

  5. 搜索 connectionString 变量,并使用第一个步骤中复制的设备连接字符串更新其值。Search for the connectionString variable and update the value with the device connection string that you copied in the first step.

  6. 保存所做更改。Save your changes.

  7. 使用“生成并运行”按钮或“Command + R”组合键在设备模拟器中运行项目。 Run the project in the device emulator with the Build and run button or the key combo command + r.

    运行项目

获取 IoT 中心连接字符串Get the IoT hub connection string

在本文中,你将创建一项后端服务,用于通过你在将遥测数据从设备发送到 IoT 中心中创建的 IoT 中心发送云到设备消息。In this article you create a backend service to send cloud-to-device messages through the IoT hub you created in Send telemetry from a device to an IoT hub. 若要发送云到设备消息,服务需要“服务连接”权限。 To send cloud-to-device messages, your service needs the service connect permission. 默认情况下,每个 IoT 中心都使用名为 service 的共享访问策略创建,该策略授予此权限。By default, every IoT Hub is created with a shared access policy named service that grants this permission.

若要获取 service 策略的 IoT 中心连接字符串,请执行以下步骤:To get the IoT Hub connection string for the service policy, follow these steps:

  1. Azure 门户中,选择“资源组” 。In the Azure portal, select Resource groups. 选择中心所在的资源组,然后从资源列表中选择中心。Select the resource group where your hub is located, and then select your hub from the list of resources.

  2. 在 IoT 中心的左侧窗格中,选择“共享访问策略” 。On the left-side pane of your IoT hub, select Shared access policies.

  3. 从策略列表中选择“service” 策略。From the list of policies, select the service policy.

  4. 在“共享访问密钥” 下,选择“连接字符串 - 主密钥” 所对应的“复制”图标并保存该值。Under Shared access keys, select the copy icon for the Connection string -- primary key and save the value.

    显示如何检索连接字符串

有关 IoT 中心共享访问策略和权限的详细信息,请参阅访问控制和权限For more information about IoT Hub shared access policies and permissions, see Access control and permissions.

模拟服务设备Simulate a service device

在本部分,我们将模拟另一个 iOS 设备,该设备上安装了可以通过 IoT 中心发送云到设备的消息的 Swift 应用。In this section, you simulate a second iOS device with a Swift app that sends cloud-to-device messages through the IoT hub. 此配置可用于以下 IoT 场景:一台 iPhone 或 iPad 充当与 IoT 中心连接的其他 iOS 设备的控制器。This configuration is useful for IoT scenarios where there is one iPhone or iPad functioning as a controller for other iOS devices connected to an IoT hub.

安装 CocoaPodsInstall CocoaPods

CocoaPods 管理那些使用第三方库的 iOS 项目的依赖项。CocoaPods manage dependencies for iOS projects that use third-party libraries.

导航到在先决条件部分下载的 Azure IoT iOS Samples 文件夹。Navigate to the Azure IoT iOS Samples folder that you downloaded in the prerequisites. 然后导航到示例服务项目:Then, navigate to the sample service project:

cd quickstart/sample-service

确保 XCode 已关闭,运行以下命令,以便安装在 podfile 文件中声明的 CocoaPods:Make sure that XCode is closed, then run the following command to install the CocoaPods that are declared in the podfile file:

pod install

除了为项目安装所需的 Pod,安装命令还创建了一个 XCode 工作区文件,该文件已配置为对依赖项使用 Pod。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 service application

  1. 在 XCode 中打开示例工作区。Open the sample workspace in XCode.

    open AzureIoTServiceSample.xcworkspace
    
  2. 展开“AzureIoTServiceSample”项目,然后展开同名的文件夹。 Expand the AzureIoTServiceSample project and then expand the folder of the same name.

  3. 打开 ViewController.swift,以便在 XCode 中进行编辑。Open ViewController.swift for editing in XCode.

  4. 搜索 connectionString 变量,并使用先前在获取 IoT 中心连接字符串中复制的服务连接字符串更新其值。Search for the connectionString variable and update the value with the service connection string that you copied previously in Get the IoT hub connection string.

  5. 保存所做更改。Save your changes.

  6. 在 Xcode 中,将模拟器设置更改为其他 iOS 设备,而不是用来运行 IoT 设备的 iOS 设备。In Xcode, change the emulator settings to a different iOS device than you used to run the IoT device. XCode 无法运行相同类型的多个模拟器。XCode cannot run multiple emulators of the same type.

    更改模拟器设备

  7. 使用“生成并运行”按钮或“Command + R”组合键在设备模拟器中运行项目。 Run the project in the device emulator with the Build and run button or the key combo Command + r.

    运行项目

发送云到设备的消息Send a cloud-to-device message

现在,可以使用这两个应用程序发送和接收云到设备的消息。You are now ready to use the two applications to send and receive cloud-to-device messages.

  1. 在模拟的 IoT 设备上运行的“iOS 应用示例”应用中,单击“启动”。 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.

    查看示例 IoT 设备应用

  2. 在模拟的服务设备上运行的“IoT 中心服务客户端示例”应用中,输入要向其发送消息的 IoT 设备的 ID。 In the IoTHub Service Client Sample app running on the simulated service device, enter the ID for the IoT device that you want to send a message to.

  3. 编写纯文本消息,然后单击“发送”。 Write a plaintext message, then click Send.

单击“发送”后,系统会执行多个操作。Several actions happen as soon as you click send. 服务示例会将消息发送到 IoT 中心,由于前面提供了服务连接字符串,应用可以访问该 IoT 中心。The service sample sends the message to your IoT hub, which the app has access to because of the service connection string that you provided. IoT 中心会检查设备 ID,将消息发送到目标设备,并向源设备发送确认回执。Your IoT hub checks the device ID, sends the message to the destination device, and sends a confirmation receipt to the source device. 在模拟的 IoT 设备上运行的应用会检查来自 IoT 中心的消息,并在屏幕上列显最新消息的文本。The app running on your simulated IoT device 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:

查看云到设备的消息

后续步骤Next steps

在本教程中,已学习如何发送和接收云到设备的消息。In this tutorial, you learned how to send and receive cloud-to-device messages.

若要了解有关使用 IoT 中心开发解决方案的详细信息,请参阅 IoT 中心开发人员指南To learn more about developing solutions with IoT Hub, see the IoT Hub developer guide.