历经数月的困扰,至今总算一步步的实现了Google IOT Core上设备与移动端交互的过程与思路。因为我们国内对IOT的相关学习资料太少,况且还是Google IOT,文档描述的真是不敢去理解,太凌乱。在此,必须要分享下。
1.此篇博文主要讲解Google IOT Core设备,网关,与云函数的知识;
2.设备的创建,删除,更改,连接到Google IOT Core;
3.编写Function云函数,可选Pythone,Node.js,Go语言;
4.在gcloud sdk命令行,进行部署云函数;
5.调试云函数功能的代码,输出JSON返回值;
6.APP开发者进行相关的网络POST,GET等进行读取设备/网关等数据;
一:Google IOT Core的核心
Google Cloud IoT 是一套完整的工具,可连接、处理、存储和分析边缘和云端的数据。该平台包含多种可伸缩的全托管式云服务,以及具备机器学习功能、用于边缘/本地计算的集成式软件堆栈,可满足您的所有物联网需求。
Google Cloud IoT 物联网-API介绍
二:Google Cloud Function云函数
1.导入Google Iot Core程序包
"@google-cloud/iot": "^1.3.1",
2.package.json
3.index.js
'use strict';
const fs = require('fs');
// [START functions_helloworld_http]
const escapeHtml = require('escape-html');
//获取设备详细信息
exports.getadd= async (req, res) => {
const iot = require('@google-cloud/iot');
const client = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});
//res.send(client);
const formattedName = client.devicePath('riltmw025', 'us-central1', 'my-regiry', 'my-device01');
client.getDevice({name: formattedName})
.then(responses => {
const response = responses[0];
// doThingsWith(response)
res.send(response);
})
.catch(err => {
console.error(err);
res.send(err);
});
}
四:通过gcloud命令部署此函数到Google Cloud Function云函数;
gcloud functions deploy getadd --runtime nodejs8 --trigger-http
等待2分钟左右,会看到部署成功,此时看云函数的公开URL,如:
https://us-central1-riltmw0259.cloudfunctions.net/getadd
五:IOS 或者 Android APP 移动应用程序 通过网络获取到设备/网关等数据
此篇博文就讲述到此,下一章介绍如何运行IOS APP移动端访问Google IOT Core核心的设备数据,以及状态,身份验证等,希望能帮助到更多的Google Iot开发者。若有所帮助,请您是否可以给予点支持与关注,谢谢您的阅读!~