云端可以控制另一台手机吗_从云端控制手机

云端可以控制另一台手机吗

As someone who’s interested in computers and coding you keep hearing about the Internet of Things (IoT) and how cool it is. But, what use cases are there for our day to day lives other than colorful light bulbs or fridges that update you that you finished your ice cream last night after your Netflix binge? How can you enable the really interesting use cases of IoT? This article will show you, using Google Cloud IoT Core, how you can connect and control your phone from the cloud and open up new possibilities for your IoT ideas.

作为对计算机和编码感兴趣的人,您会不断听到有关物联网(IoT)的知识,以及它有多酷。 但是,除了彩色灯泡或冰箱使您昨晚在Netflix暴饮暴食后昨晚吃完冰淇淋以外,我们每天的生活还有哪些用例? 您如何才能启用物联网真正有趣的用例? 本文将向您展示如何使用Google Cloud IoT Core从云端连接和控制手机,以及为IoT创意开辟新的可能性。

Image for post

目标 (Objectives)

  • Install Podfile and dependencies

    安装Podfile和依赖项
  • Connect iPhone to IoT Core

    将iPhone连接到IoT核心版
  • Receive Commands from IoT Core to the iPhone

    从IoT核心版接收命令到iPhone

在你开始之前 (Before you begin)

This tutorial assumes that you already have some background in iOS development and the basics of creating a registry, registering devices, and subscribing to a PubSub topic.

本教程假定您已经具有iOS开发的背景知识,并且具有创建注册表,注册设备和订阅PubSub主题的基础知识。

If you haven’t done this before, first go through the steps in IoT Core Getting Started to learn the basics of IoT Core.

如果您以前没有做过,请首先完成IoT Core入门中的步骤,以学习IoT Core的基础知识。

设置和安装Pod (Setting up and installing Pods)

Before connecting to IoT Core using MQTT, you need to first install CocoaPods. CocoaPods is an application-level dependency manager for Swift and many other languages that provides a standard format for managing external libraries. If you are using OSX, you should already have CocoaPods installed. To manually download or update CocoaPods use:

使用MQTT连接到IoT核心之前,您需要先安装CocoaPods。 CocoaPods是Swift和许多其他语言的应用程序级依赖性管理器,提供了用于管理外部库的标准格式。 如果您使用的是OSX,则应该已经安装了CocoaPods。 要手动下载或更新CocoaPods,请使用:

sudo gem install cocoapods

After verifying that CocoaPods is installed, install the dependencies. For this step, you need to initialize CocoaPods. Using Terminal or similar program, run the following command from the directory that contains the .xcodeproj file:

确认已安装CocoaPods之后,安装依赖项。 对于此步骤,您需要初始化CocoaPods。 使用终端或类似程序,从包含.xcodeproj文件的目录中运行以下命令:

pod init

This initializes the CocoaPods and creates a podfile where you can include the libraries you need.

这将初始化CocoaPods并创建一个podfile,您可以在其中包含所需的库。

After CocoaPods is successfully initialized, a podfile will be created. This podfile holds all of the dependencies needed for your project, let’s add SwiftJWT and CocoaMQTT to the podfile:

成功初始化CocoaPods之后,将创建一个podfile。 此Podfile包含项目所需的所有依赖项,让我们将SwiftJWT和CocoaMQTT添加到Podfile中:

target ‘iOS_MQTT’ do
pod ‘SwiftJWT’
use_frameworks! # Add this if you are targeting iOS 8+
pod ‘CocoaMQTT’
end

Next, install the libraries to your project:

接下来,将库安装到您的项目中:

pod install

After the pods are successfully installed, make sure that the SwiftJWT and CocoaMQTT are included in the Xcode project. Click the .xcworkspace file to open the project.

在成功安装Pod之后,请确保Xcode项目中包含SwiftJWT和CocoaMQTT。 单击.xcworkspace文件以打开项目。

If you’re getting an error that says “SwiftJWT or CocoaMQTT is not included” you will need to add them to the project dependencies.

如果遇到错误消息“不包含SwiftJWT或CocoaMQTT”,则需要将它们添加到项目依赖项中。

To add these libraries :

要添加这些库:

  1. Open the File > Swift Packages > Add Package Dependency menu

    打开文件> Swift软件包>添加软件包依赖项菜单
  2. Select the project name

    选择项目名称
  3. For SwiftJWT paste git@github.com:IBM-Swift/Swift-JWT.git, or for CocoaMQTT paste git@github.com:emqx/CocoaMQTT.git

    对于SwiftJWT粘贴git@github.com:IBM-Swift / Swift-JWT.git,或者对于CocoaMQTT粘贴git@github.com:emqx / CocoaMQTT.git

生成JSON Web令牌(JWT) (Generating the JSON Web Token (JWT))

Now that the library dependencies are installed, you can start creating the JWT.

现在已经安装了库依赖项,您可以开始创建JWT。

For the iPhone to be able to connect, you need to provide a JWT for the password which IoT Core uses.

为了使iPhone能够连接,您需要为IoT核心版使用的密码提供一个JWT。

The following code creates a JWT with an issued at (iat), expiration (exp), and audience (aud) fields:

以下代码创建一个JWT,并在(iat),到期(exp)和受众(aud)字段中发出:

If these values are not correct, the client will reject the connection.

如果这些值不正确,客户端将拒绝连接。

Note: The audience (aud field) should be set to the project ID from the Google Cloud Console

注意:受众群体(音频字段)应通过Google Cloud Console设置为项目ID

Image for post

After the JWT is created, it will need to be signed using the private key. Copy the private_key.pem file and pass it in the privateKeyPair variable making sure that the variable includes BEGIN EC PRIVATE KEY and END EC PRIVATE KEY.

创建JWT后,将需要使用私钥对其进行签名。 复制private_key.pem文件并将其传递到privateKeyPair变量中,确保该变量包括BEGIN EC PRIVATE KEYEND EC PRIVATE KEY。

Use the claim that was created above and sign it using the privateKeyPair that corresponds to the device’s public key:

使用上面创建的声明,并使用与设备的公钥相对应的privateKeyPair对其进行签名:

Now that the JWT is created, print it to see that all the information is correct. Go to JWT.io to check that all the information that was supplied matches.

现在已经创建了JWT,请打印它以查看所有信息是否正确。 转到JWT.io ,检查提供的所有信息是否匹配。

将iPhone连接到IoT核心版 (Connecting iPhone to IoT Core)

After the JWT is created, the cloudConnect function gets executed using the JWT; the cloudConnect function will create a CocoaMQTT client with the clientID, host, and port. You will need to provide the clientID which is the path of the device:

创建JWT后,将使用JWT执行cloudConnect函数。 cloudConnect函数将使用客户端ID,主机和端口创建一个CocoaMQTT客户端。 您将需要提供clientID,它是设备的路径:

Set up the MQTT client by supplying the JWT that was created earlier as the password, note that the username is an empty string because IoT Core does not need to specify the username to connect:

通过提供先前创建的JWT作为密码来设置MQTT客户端,请注意,用户名是空字符串,因为IoT核心无需指定用户名即可进行连接:

The keepAlive parameter makes sure that the connection between the device and IoT Core stays connected by sending a heartbeat operation to the MQTT server. After the MQTT client is set up, it will try to connect to IoT Core.

keepAlive参数通过将心跳操作发送到MQTT服务器来确保设备与IoT Core之间的连接保持连接状态。 设置MQTT客户端后,它将尝试连接到IoT核心版。

接收命令 (Receiving Commands)

After the mqtt.connect function has finished, the device should be connected. It may appear that the device is not connected because you don’t see any incoming messages, but this is expected because the MQTT client isn’t yet subscribed to the device’s command and configuration topics.

mqtt.connect函数完成后,应连接设备。 可能由于未看到任何传入消息而导致设备未连接,但这是可以预期的,因为MQTT客户端尚未订阅设备的命令和配置主题。

To subscribe to the command topic:

订阅命令主题:

To subscribe to the configuration topic:

订阅配置主题:

Note that for the command topic, you must include the wildcard(#) to match all subtopics. By subscribing to the wildcard topic, the device will receive commands sent to devices/{device-id}/commands, as well as commands sent to subtopics.

请注意,对于命令主题,必须包括通配符(#)以匹配所有子主题。 通过订阅通配符主题,设备将接收发送到devices / {device-id} / commands的命令以及发送到子主题的命令。

A callback function must be added to the connect function to print out the commands that are sent from IoT Core:

必须将回调函数添加到connect函数以打印出从IoT核心版发送的命令:

If you want to check for a specific message from IoT Core, you can add code that’s similar to the code below. The code below toggle’s the LED flash of the iPhone for commands with string payloads “on” or “off”.

如果要检查来自IoT Core的特定消息,则可以添加类似于以下代码的代码。 下面的代码将iPhone的LED闪光灯切换为字符串有效载荷为“ on”或“ off”的命令。

发布遥测数据 (Publishing Telemetry Data)

After connecting and subscribing to the device’s configuration and command topics, you can finally publish shared state (state) and telemetry event (event) data.

连接并订阅了设备的配置和命令主题之后,您最终可以发布共享状态(状态)和遥测事件(事件)数据。

Sending data is a matter of publishing to the topic for this device. To send data to the state topic, publish to /devices/{device-id}/state, and to send data to the event topic, publish to /devices/{device-id}/events.

发送数据是发布到此设备主题的问题。 要将数据发送到状态主题,请发布到/ devices / {device-id} / state ,并将数据发送到事件主题,请发布到/ devices / {device-id} / events

The device’s event topic is for frequent updates from the device that are handled by Cloud infrastructure. The device’s state topic is for less frequent updates that indicate a shared and persistent state between Google Cloud and the device.

设备的事件主题用于由云基础架构处理的设备中的频繁更新。 设备的状态主题用于不太频繁的更新,这些更新指示Google Cloud和设备之间的共享和持久状态。

To publish to the state topic:

要发布到状态主题:

To publish to the event topic:

要发布到事件主题:

You can simply change the publish code to send sensor data as you will see in the next article.

您可以简单地更改发布代码以发送传感器数据,如您在下一篇文章中将看到的那样。

运行样本 (Running the Sample)

To run the sample all you need to do is to clone the repo from GitHub by running the following command:

要运行该示例,您需要做的就是通过运行以下命令从GitHub复制该仓库:

git clone git@github.com:GoogleCloudPlatoform/google-cloud-iot-arduino.git

Then in your terminal go to google-cloud-iot arduino/examples/complex/esp32/ios-gateway/extras and run:

然后在您的终端中转到google-cloud-iot arduino / examples / complex / esp32 / ios-gateway / extras并运行:

pod install

This will install your dependencies if you’re getting an error that says “SwiftJWT or CocoaMQTT is not included” you will need to add them to the project dependencies.

如果出现错误消息“不包含SwiftJWT或CocoaMQTT”,这将安装您的依赖项,您需要将它们添加到项目依赖项中。

You can see how to do this in the Before You Begin section.

您可以在“开始之前”部分中查看如何执行此操作。

After the cocoapods are set up, you can run the project on your laptop or build it on your device.

设置cocoapods后,您可以在笔记本电脑上运行项目或在设备上构建项目。

After the application is running on your device you should be able to send “on” or “off” commands to toggle the flash, the application will not send event data because it’s expecting an ESP32 to connect.

在设备上运行应用程序后,您应该能够发送“ on”或“ off”命令来切换闪存,因此应用程序将不会发送事件数据,因为它希望连接ESP32。

If you want to send event data without connecting an ESP32 add the commands in the Publish Telemetry Data section into the publish function in the IoTBLEClient.swift file.

如果要发送事件数据而不连接ESP32,则将发布遥测数据部分中的命令添加到IoTBLEClient.swift文件中的发布功能中。

下一步 (Next Steps)

Now, this is a simple demonstration to show you how to connect your phone to IoT Core. IoT Core has access to all Google Cloud Services so from this point you can use any service, for example instead of uploading a message you can send an image to Cloud Vision API.

现在,这是一个简单的演示,向您展示如何将手机连接到IoT Core。 IoT核心版可以访问所有Google Cloud Services,因此从那时起您可以使用任何服务,例如,可以上传图像到Cloud Vision API而不是上传消息。

Follow my blog so you don’t miss part 2 of this article that will show you how to use this application to create an iPhone gateway that retrieves environmental data from an ESP32 using BLE.

请关注我的博客,以免您错过本文的第2部分,该部分将向您展示如何使用此应用程序创建iPhone网关,该网关使用BLE从ESP32检索环境数据。

翻译自: https://medium.com/swlh/controlling-your-phone-from-the-cloud-f075b75858c9

云端可以控制另一台手机吗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值