├── LICENSE ├── README.md ├── core └── LuatOS-SoC_V1103_EC618_TEMP_20230204_192000.soc ├── examples ├── 01.ThingsCloud_Connect │ ├── basic_connect │ │ ├── demo.lua │ │ └── main.lua │ ├── fetch_certificate │ │ ├── demo.lua │ │ └── main.lua │ └── fetch_certificate_auto_create │ │ ├── demo.lua │ │ └── main.lua ├── 02.ThingsCloud_Communicate │ ├── attributes_get │ │ ├── demo.lua │ │ └── main.lua │ ├── attributes_push │ │ ├── demo.lua │ │ └── main.lua │ ├── attributes_report │ │ ├── demo.lua │ │ └── main.lua │ ├── command_send │ │ ├── demo.lua │ │ └── main.lua │ ├── custom_data │ │ ├── demo.lua │ │ └── main.lua │ └── event_report │ │ ├── demo.lua │ │ └── main.lua └── 10.IoT_Tutorials │ ├── adc_sensor │ ├── demo.lua │ └── main.lua │ ├── air780eg_dtu_json │ ├── demo.lua │ └── main.lua │ ├── air780eg_gps_tracker │ ├── demo.lua │ └── main.lua │ ├── gpio_out │ ├── demo.lua │ └── main.lua │ ├── ota_check │ ├── demo.lua │ └── main.lua │ ├── ota_check_uart_mcu │ ├── demo.lua │ └── main.lua │ ├── ota_push │ ├── demo.lua │ └── main.lua │ ├── pwm_out │ ├── demo.lua │ └── main.lua │ ├── sht30_sensor │ ├── demo.lua │ └── main.lua │ ├── uart_dtu │ ├── demo.lua │ └── main.lua │ └── uart_json │ ├── demo.lua │ └── main.lua └── libs ├── README.md └── ThingsCloud.lua /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 ThingsCloud 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # luatos-thingscloud-sdk 2 | 3 | 这里是基于合宙模组 [LuatOS](https://wiki.luatos.com/) 快速接入 [ThingsCloud](https://www.thingscloud.xyz) 物联网平台的 SDK ,帮你 10 分钟完成模组到云平台的双向通信,一键生成物联网 SaaS 后台,以及用户 App,快速落地物联网项目和产品。 4 | 5 | ## 特点 6 | 7 | - 简单,简单,简单,填写几个参数就可以烧录运行。 8 | - 可实现传感器数据上报和控制下发,利用各种外设发挥你的想象空间。 9 | - 封装了 ThingsCloud 接入协议,只需调用函数和绑定事件,就可以实现设备和云平台双向通信。 10 | - 支持自定义Topic,通过云平台设备类型的自定义数据流。 11 | - 支持一型一密/自动注册设备,用于量产设备。 12 | - 享受 ThingsCloud 云平台所有特性,一键生成物联网 SaaS 后台,以及用户 App,快速落地物联网项目和产品。 13 | 14 | ## 支持模组 15 | 16 | - Air780E 17 | - Air780EG 18 | - Air700E 19 | - Air600E 20 | 21 | 对于 Air724/820 系列模组,请移步到 [luat-thingscloud-libs](https://github.com/IoT-ThingsCloud/luat-thingscloud-libs) 22 | 23 | ## ThingsCloud 准备工作 24 | 25 | - [快速上手控制台](https://www.thingscloud.xyz/docs/guide/quickstart/signin-console.html) 26 | - [如何获得设备证书?](https://www.thingscloud.xyz/docs/guide/connect-device/device-certificate.html) 27 | - [ThingsCloud MQTT 接入文档](https://docs.thingscloud.xyz/guide/connect-device/mqtt.html) 28 | 29 | 30 | ## 使用方法 31 | 32 | ### 引用库文件 33 | 34 | 在主程序中引入库文件: 35 | 36 | ```lua 37 | local ThingsCloud = require "ThingsCloud" 38 | ``` 39 | 40 | 然后将 `libs/` 下的库文件添加到项目的脚本列表中。 41 | 42 | 43 | ### 定义设备证书和连接参数 44 | 45 | - 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 46 | - 创建项目,可选择免费版。 47 | - 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 48 | 49 | ![articles/2023/20230308225114_a177c9cd9a38216ca9872f51c8c31f9f.png](https://img-1300291923.cos.ap-beijing.myqcloud.com/articles/2023/20230308225114_a177c9cd9a38216ca9872f51c8c31f9f.png) 50 | 51 | ```lua 52 | -- 一机一密方式 53 | -- ProjectKey 54 | local projectKey = "" 55 | -- AccessToken 56 | local accessToken = "" 57 | -- MQTT 接入点,只需主机名部分 58 | local host = "" 59 | ``` 60 | 61 | ### 连接 ThingsCloud 62 | 63 | ```lua 64 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 65 | sys.taskInit(function() 66 | -- 连接云平台,支持判断网络可用性、MQTT自动重连 67 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 68 | ThingsCloud.connect({ 69 | host = host, 70 | projectKey = projectKey, 71 | accessToken = accessToken, 72 | }) 73 | end) 74 | ``` 75 | 76 | ### 烧录固件 77 | 78 | 使用 Luatools 烧录底层固件和脚本,底层固件在 core/ 目录下。 79 | 80 | ![articles/2023/20230309183140_8791097d56f2594bb981b675615b3842.png](https://img-1300291923.cos.ap-beijing.myqcloud.com/articles/2023/20230309183140_8791097d56f2594bb981b675615b3842.png) 81 | 82 | 83 | ## 示例项目 84 | 85 | [点此进入示例目录](https://github.com/IoT-ThingsCloud/luatos-thingscloud-sdk/tree/main/examples) 86 | 87 | 88 | ### 连接云平台 89 | 90 | #### 普通连接(一机一密) 91 | 92 | [01.ThingsCloud_Connect/basic_connect](examples/01.ThingsCloud_Connect/basic_connect) 93 | 94 | #### 动态获取证书(一型一密) 95 | 96 | [01.ThingsCloud_Connect/fetch_certificate](examples/01.ThingsCloud_Connect/fetch_certificate) 97 | 98 | #### 自动创建设备+动态获取证书(一型一密) 99 | 100 | [01.ThingsCloud_Connect/fetch_certificate_auto_create](examples/01.ThingsCloud_Connect/fetch_certificate_auto_create) 101 | 102 | ### 和云平台双向通信 103 | 104 | #### 属性上报 105 | 106 | [02.ThingsCloud_Communicate/attributes_report](examples/02.ThingsCloud_Communicate/attributes_report) 107 | 108 | #### 接收下发属性 109 | 110 | [02.ThingsCloud_Communicate/attributes_push](examples/02.ThingsCloud_Communicate/attributes_push) 111 | 112 | #### 获取云端属性 113 | 114 | [02.ThingsCloud_Communicate/attributes_get](examples/02.ThingsCloud_Communicate/attributes_get) 115 | 116 | #### 事件上报 117 | 118 | [02.ThingsCloud_Communicate/event_report](examples/02.ThingsCloud_Communicate/event_report) 119 | 120 | #### 接收下发命令 121 | 122 | [02.ThingsCloud_Communicate/command_send](examples/02.ThingsCloud_Communicate/command_send) 123 | 124 | #### 自定义数据流 125 | 126 | [02.ThingsCloud_Communicate/custom_data](examples/02.ThingsCloud_Communicate/custom_data) 127 | 128 | ### 综合示例 129 | 130 | #### 透传 DTU(支持二进制通信,也可用于 RS485 透传) 131 | 132 | [10.IoT_Tutorials/uart_dtu](examples/10.IoT_Tutorials/uart_dtu) 133 | 134 | #### 透传 DTU(JSON 透传) 135 | 136 | [10.IoT_Tutorials/uart_json](examples/10.IoT_Tutorials/uart_json) 137 | 138 | #### 透传 DTU(支持 GPS 模块) 139 | 140 | [10.IoT_Tutorials/air780eg_dtu_json](examples/10.IoT_Tutorials/air780eg_dtu_json) 141 | 142 | #### GPS 上报 143 | 144 | [10.IoT_Tutorials/air780eg_gps_tracker](examples/10.IoT_Tutorials/air780eg_gps_tracker) 145 | 146 | #### 云平台控制 GPIO 输出 —— 网络继电器 147 | 148 | [10.IoT_Tutorials/gpio_out](examples/10.IoT_Tutorials/gpio_out) 149 | 150 | #### SHT30 传感器上报 151 | 152 | [10.IoT_Tutorials/sht30_sensor](examples/10.IoT_Tutorials/sht30_sensor) 153 | 154 | #### 云平台控制 PWM 输出 —— 电机调速 155 | 156 | [10.IoT_Tutorials/pwm_out](examples/10.IoT_Tutorials/pwm_out) 157 | 158 | #### ADC 模拟量采集 159 | 160 | [10.IoT_Tutorials/adc_sensor](examples/10.IoT_Tutorials/adc_sensor) 161 | 162 | #### OTA 升级 - 云平台主动推送模式 163 | 164 | [10.IoT_Tutorials/ota_push](examples/10.IoT_Tutorials/ota_push) 165 | 166 | #### OTA 升级 - 设备检查新版本 167 | 168 | [10.IoT_Tutorials/ota_check](examples/10.IoT_Tutorials/ota_check) 169 | 170 | #### OTA 升级 - 主控 MCU 通过 Air780E 检查新版本并下载固件 171 | 172 | [10.IoT_Tutorials/ota_check_uart_mcu](examples/10.IoT_Tutorials/ota_check_uart_mcu) 173 | 174 | 175 | 更多示例代码完善中,欢迎你的建议,也欢迎提交贡献。 176 | 177 | 178 | 179 | ## 关于 ThingsCloud 180 | 181 | ThingsCloud 是物联网设备统一接入平台和低代码应用开发平台。可以帮助任何需要数字化改造的行业客户,在极短的时间内搭建物联网应用,并适应不断变化的发展需求。ThingsCloud 支持智能传感器、执行器、控制器、智能硬件等设备接入,支持 MQTT/HTTP/TCP/Modbus/LoRa/Zigbee/WiFi/BLE 等通信协议,实现数据采集、分析、监控,还可以灵活配置各种规则,生成项目应用 SaaS 和用户应用 App,这一切无需任何云端代码开发。 182 | 183 | - 官网:https://www.thingscloud.xyz/ 184 | - 控制台:https://console.thingscloud.xyz/ 185 | - 教程:https://docs.thingscloud.xyz/tutorials/ 186 | - 使用文档:https://docs.thingscloud.xyz 187 | - 设备接入:https://docs.thingscloud.xyz/guide/connect-device/ 188 | - 博客:https://www.thingscloud.xyz/blog/ 189 | - B站:https://space.bilibili.com/1953347444 190 | 191 | 192 | ![articles/2023/20230112114634_afd61232cd029fca77eaebe67e12beaf.png](https://img-1300291923.cos.ap-beijing.myqcloud.com/articles/2023/20230112114634_afd61232cd029fca77eaebe67e12beaf.png) 193 | 194 | ![](https://img-1300291923.cos.ap-beijing.myqcloud.com/articles/2023/20230303162529_7d47018b2466053ef3af13dcfd23b703.png) 195 | 196 | ![](https://img-1300291923.cos.ap-beijing.myqcloud.com/articles/2023/20230303194054_fe9320028f7b499a18893b7a0d25b3c7.png) 197 | 198 | ![](https://img-1300291923.cos.ap-beijing.myqcloud.com/articles/2023/20230303163508_4b2e3b2052e282bcf2e36143fe90d101.png) 199 | 200 | ![](https://img-1300291923.cos.ap-beijing.myqcloud.com/articles/2023/20230303164617_c0f98e1ae66b5987aba3408faf86ac1d.png) 201 | 202 | ![](https://img-1300291923.cos.ap-beijing.myqcloud.com/articles/2023/20230303163103_40fe1d013e8d1d665bdd3cd0ae42adc0.png) 203 | 204 | ### 技术支持 205 | 206 | 联系 ThingsCloud 技术支持 207 | 208 | ![](https://img-1300291923.cos.ap-beijing.myqcloud.com/service/support-qrcode-wlww-1208.png) -------------------------------------------------------------------------------- /core/LuatOS-SoC_V1103_EC618_TEMP_20230204_192000.soc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IoT-ThingsCloud/luatos-thingscloud-sdk/cf791279959accf4742237ef485ace70aad2dbad/core/LuatOS-SoC_V1103_EC618_TEMP_20230204_192000.soc -------------------------------------------------------------------------------- /examples/01.ThingsCloud_Connect/basic_connect/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | -- 设备成功连接云平台后,触发该函数 15 | local function onConnect(result) 16 | if result then 17 | -- 当设备连接成功后 18 | 19 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 20 | 21 | end 22 | end 23 | 24 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 25 | sys.taskInit(function() 26 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 27 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 28 | ThingsCloud.connect({ 29 | host = host, 30 | projectKey = projectKey, 31 | accessToken = accessToken, 32 | }) 33 | 34 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 35 | ThingsCloud.on("connect", onConnect) 36 | 37 | end) 38 | 39 | -------------------------------------------------------------------------------- /examples/01.ThingsCloud_Connect/basic_connect/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/01.ThingsCloud_Connect/fetch_certificate/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- MQTT 接入点,只需主机名部分 10 | local host = "" 11 | -- HTTP 接入点,为设备提供证书获取服务。设备通过 DeviceKey 获取 AccessToken 12 | local apiEndpoint = "" 13 | 14 | -- 设备成功连接云平台后,触发该函数 15 | local function onConnect(result) 16 | if result then 17 | -- 当设备连接成功后 18 | 19 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 20 | 21 | end 22 | end 23 | 24 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 25 | sys.taskInit(function() 26 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 27 | -- 这里采用了设备一型一密方式,为项目下所有设备烧录相同的固件。 28 | ThingsCloud.connect({ 29 | host = host, 30 | projectKey = projectKey, 31 | apiEndpoint = apiEndpoint, 32 | }) 33 | 34 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 35 | ThingsCloud.on("connect", onConnect) 36 | 37 | end) 38 | 39 | -------------------------------------------------------------------------------- /examples/01.ThingsCloud_Connect/fetch_certificate/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/01.ThingsCloud_Connect/fetch_certificate_auto_create/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- MQTT 接入点,只需主机名部分 10 | local host = "" 11 | -- HTTP 接入点,为设备提供证书获取服务。设备通过 DeviceKey 获取 AccessToken 12 | local apiEndpoint = "" 13 | -- TypeKey,自动创建设备必须指定设备类型,在设备类型的设置中开启允许自动创建设备,并复制 TypeKey 14 | local typeKey = "" 15 | 16 | -- 设备成功连接云平台后,触发该函数 17 | local function onConnect(result) 18 | if result then 19 | -- 当设备连接成功后 20 | 21 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 22 | 23 | end 24 | end 25 | 26 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 27 | sys.taskInit(function() 28 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 29 | -- 这里采用了设备一型一密方式,为项目下所有设备烧录相同的固件。 30 | ThingsCloud.connect({ 31 | host = host, 32 | projectKey = projectKey, 33 | apiEndpoint = apiEndpoint, 34 | typeKey = typeKey, 35 | }) 36 | 37 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 38 | ThingsCloud.on("connect", onConnect) 39 | 40 | end) 41 | 42 | -------------------------------------------------------------------------------- /examples/01.ThingsCloud_Connect/fetch_certificate_auto_create/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/02.ThingsCloud_Communicate/attributes_get/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | -- 设备成功连接云平台后,触发该函数 15 | local function onConnect(result) 16 | if result then 17 | -- 当设备连接成功后 18 | 19 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 20 | -- TODO 21 | 22 | -- 向云平台请求设备属性,例如读取配置信息。 23 | -- 参数是 table 数组,用来指定希望读取的属性名称,如果数组为空,可请求所有属性 24 | -- 云平台回复属性值,在事件 attributes_get_response 的回调函数中接收 25 | ThingsCloud.getAttributes({}) 26 | end 27 | end 28 | 29 | -- 设备向云平台发送读取云端属性后,接收到云平台下发的命令时,触发该函数 30 | -- response 是table结构的返回数据 31 | -- responseId 作为请求标识,通常可以不使用 32 | local function onAttributesGetResponse(response, responseId) 33 | log.info("attributes get response", json.encode(response), responseId) 34 | 35 | -- 获得云平台回复的属性值,实现相应的自定义逻辑 36 | if response.result == 1 then 37 | local attributes = response.attributes or {} 38 | if attributes.relay == true then 39 | -- TODO 例如开灯 40 | 41 | elseif attributes.relay == false then 42 | -- TODO 例如关灯 43 | 44 | end 45 | end 46 | 47 | end 48 | 49 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 50 | sys.taskInit(function() 51 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 52 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 53 | ThingsCloud.connect({ 54 | host = host, 55 | projectKey = projectKey, 56 | accessToken = accessToken 57 | }) 58 | 59 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 60 | ThingsCloud.on("connect", onConnect) 61 | ThingsCloud.on("attributes_get_response", onAttributesGetResponse) 62 | 63 | end) 64 | 65 | -------------------------------------------------------------------------------- /examples/02.ThingsCloud_Communicate/attributes_get/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/02.ThingsCloud_Communicate/attributes_push/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | -- 设备成功连接云平台后,触发该函数 15 | local function onConnect(result) 16 | if result then 17 | -- 当设备连接成功后 18 | 19 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 20 | 21 | end 22 | end 23 | 24 | -- 设备接收到云平台下发的属性时,触发该函数 25 | local function onAttributesPush(attributes) 26 | log.info("recv attributes push", json.encode(attributes)) 27 | 28 | -- 实现相应的自定义逻辑 29 | if attributes.relay == true then 30 | -- TODO 例如开灯 31 | 32 | elseif attributes.relay == false then 33 | -- TODO 例如关灯 34 | 35 | end 36 | end 37 | 38 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 39 | sys.taskInit(function() 40 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 41 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 42 | ThingsCloud.connect({ 43 | host = host, 44 | projectKey = projectKey, 45 | accessToken = accessToken, 46 | }) 47 | 48 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 49 | ThingsCloud.on("connect", onConnect) 50 | ThingsCloud.on("attributes_push", onAttributesPush) 51 | 52 | end) 53 | 54 | -------------------------------------------------------------------------------- /examples/02.ThingsCloud_Communicate/attributes_push/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/02.ThingsCloud_Communicate/attributes_report/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | -- 设备成功连接云平台后,触发该函数 15 | local function onConnect(result) 16 | if result then 17 | -- 当设备连接成功后 18 | 19 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 20 | 21 | end 22 | end 23 | 24 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 25 | sys.taskInit(function() 26 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 27 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 28 | ThingsCloud.connect({ 29 | host = host, 30 | projectKey = projectKey, 31 | accessToken = accessToken 32 | }) 33 | 34 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 35 | ThingsCloud.on("connect", onConnect) 36 | 37 | end) 38 | 39 | -- 在独立的协程中上报数据到云平台,可实现固定时间间隔的上报。 40 | -- 上报的数据,可在其它协程中读取,例如读取串口传感器数据 41 | sys.taskInit(function() 42 | while true do 43 | -- 此处要判断是否已连接成功 44 | if ThingsCloud.isConnected() then 45 | 46 | -- 上报属性,这里举例模拟一些数据 47 | ThingsCloud.reportAttributes({ 48 | imei = mobile.imei(), 49 | imsi = mobile.imsi(), 50 | csq = mobile.csq(), 51 | iccid = mobile.iccid(), 52 | rssi = mobile.rssi(), 53 | rsrq = mobile.rsrq(), 54 | rsrp = mobile.rsrp(), 55 | snr = mobile.snr(), 56 | simid = mobile.simid() 57 | }) 58 | end 59 | -- 上报间隔时间为60秒 60 | -- 使用 ThingsCloud 免费版时,数据上报频率不要低于1分钟,否则可能会被断开连接,造成设备通信不稳定 61 | sys.wait(1000 * 60) 62 | end 63 | end) 64 | -------------------------------------------------------------------------------- /examples/02.ThingsCloud_Communicate/attributes_report/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/02.ThingsCloud_Communicate/command_send/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | -- 设备成功连接云平台后,触发该函数 15 | local function onConnect(result) 16 | if result then 17 | -- 当设备连接成功后 18 | 19 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 20 | 21 | end 22 | end 23 | 24 | -- 设备接收到云平台下发的命令时,触发该函数 25 | local function onCommandSend(command) 26 | log.info("recv command send", json.encode(command)) 27 | if command.method == "ota" then 28 | sys.taskInit(function() 29 | -- 执行在线升级 30 | -- TODO 31 | 32 | sys.wait(5000) 33 | 34 | -- 完成任务后,回复平台 35 | ThingsCloud.replyCommand({ 36 | method = "ota", 37 | params = { 38 | status = true 39 | } 40 | }) 41 | end) 42 | elseif command.method == "restart" then 43 | -- 调用模组的重启指令 44 | rtos.reboot() 45 | elseif command.method == "other_name" then 46 | -- 执行其它自定义命令,触发硬件执行一些操作 47 | -- TODO 48 | end 49 | end 50 | 51 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 52 | sys.taskInit(function() 53 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 54 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 55 | ThingsCloud.connect({ 56 | host = host, 57 | projectKey = projectKey, 58 | accessToken = accessToken, 59 | }) 60 | 61 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 62 | ThingsCloud.on("connect", onConnect) 63 | ThingsCloud.on("command_send", onCommandSend) 64 | 65 | end) 66 | 67 | -------------------------------------------------------------------------------- /examples/02.ThingsCloud_Communicate/command_send/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/02.ThingsCloud_Communicate/custom_data/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | -- 设备成功连接云平台后,触发该函数 15 | local function onConnect(result) 16 | if result then 17 | -- 当设备连接成功后 18 | 19 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 20 | 21 | -- 订阅自定义数据流。必须先在设备类型中创建自定义数据流,才可以使用。 22 | ThingsCloud.subscribe("data/stream/set") 23 | 24 | end 25 | end 26 | 27 | -- 设备接收到云平台下发的属性时,触发该函数 28 | local function onDataSet(data) 29 | log.info("recv data set", data, string.toHex(data)) 30 | end 31 | 32 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 33 | sys.taskInit(function() 34 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 35 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 36 | ThingsCloud.connect({ 37 | host = host, 38 | projectKey = projectKey, 39 | accessToken = accessToken 40 | }) 41 | 42 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 43 | ThingsCloud.on("connect", onConnect) 44 | ThingsCloud.on("data_set", onDataSet) 45 | 46 | end) 47 | 48 | -- 在独立的协程中上报数据到云平台,可实现固定时间间隔的上报。 49 | -- 上报的数据,可在其它协程中读取,例如读取串口传感器数据 50 | sys.taskInit(function() 51 | while true do 52 | -- 此处要判断是否已连接成功 53 | if ThingsCloud.isConnected() then 54 | 55 | -- 使用自定义 topic 上报数据,必须在设备类型中创建自定义数据流,并设置数据类型,支持 JSON/Bin/PlainText 56 | -- 这里上报一个二进制数据流到云平台 57 | ThingsCloud.publishCustomTopic("data/stream", string.fromHex("010304003289A0F900")) 58 | end 59 | -- 上报间隔时间为60秒 60 | -- 使用 ThingsCloud 免费版时,数据上报频率不要低于1分钟,否则可能会被断开连接,造成设备通信不稳定 61 | sys.wait(1000 * 60) 62 | end 63 | end) 64 | -------------------------------------------------------------------------------- /examples/02.ThingsCloud_Communicate/custom_data/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/02.ThingsCloud_Communicate/event_report/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | -- 设备成功连接云平台后,触发该函数 15 | local function onConnect(result) 16 | if result then 17 | -- 当设备连接成功后 18 | 19 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 20 | 21 | end 22 | end 23 | 24 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 25 | sys.taskInit(function() 26 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 27 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 28 | ThingsCloud.connect({ 29 | host = host, 30 | projectKey = projectKey, 31 | accessToken = accessToken, 32 | }) 33 | 34 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 35 | ThingsCloud.on("connect", onConnect) 36 | 37 | end) 38 | 39 | 40 | -- 在独立的协程中上报数据到云平台,可实现固定时间间隔的上报。 41 | -- 上报的数据,可在其它协程中读取,例如读取串口传感器数据 42 | sys.taskInit( 43 | function() 44 | while true do 45 | -- 此处要判断是否已连接成功 46 | if ThingsCloud.isConnected() then 47 | 48 | -- 上报事件,可携带参数,参数不会写入设备属性 49 | ThingsCloud.reportEvent( 50 | { 51 | method = "alarm", 52 | params = { 53 | msg = "temperature sensor fault", 54 | code = 1390 55 | } 56 | } 57 | ) 58 | 59 | end 60 | -- 上报间隔时间为60秒 61 | -- 使用 ThingsCloud 免费版时,数据上报频率不要低于1分钟,否则可能会被断开连接,造成设备通信不稳定 62 | sys.wait(1000 * 60) 63 | end 64 | end 65 | ) 66 | -------------------------------------------------------------------------------- /examples/02.ThingsCloud_Communicate/event_report/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/adc_sensor/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | --[[ 15 | 8. Air780E内部ADC接口为12bits 外部直流分压为0-3.4V 16 | 9. Air780E内部具有2个ADC接口,ADC0 -- AIO3 ADC1 -- AIO4 17 | ]] 18 | -- 初始化 ADC 引脚 19 | local ADC_PIN = 0 20 | adc.open(ADC_PIN) 21 | 22 | -- 设备成功连接云平台后,触发该函数 23 | local function onConnect(result) 24 | if result then 25 | -- 当设备连接成功后 26 | 27 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 28 | 29 | end 30 | end 31 | 32 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 33 | sys.taskInit(function() 34 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 35 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 36 | ThingsCloud.connect({ 37 | host = host, 38 | projectKey = projectKey, 39 | accessToken = accessToken 40 | }) 41 | 42 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 43 | ThingsCloud.on("connect", onConnect) 44 | 45 | end) 46 | 47 | -- 读取的数据全局定义 48 | local adc_value = nil 49 | -- 在独立的协程中不断读取最新的 ADC 模拟量 50 | sys.taskInit(function() 51 | 52 | while true do 53 | adc_value = adc.get(ADC_PIN) 54 | -- 这里省去对模拟量的计算逻辑 55 | -- TODO 56 | 57 | sys.wait(1000) 58 | end 59 | 60 | end) 61 | 62 | -- 在独立的协程中上报数据到云平台,可实现固定时间间隔的上报。 63 | -- 上报的数据,可在其它协程中读取,例如读取串口传感器数据 64 | sys.taskInit(function() 65 | while true do 66 | -- 此处要判断是否已连接成功 67 | if ThingsCloud.isConnected() then 68 | 69 | if adc_value ~= nil then 70 | ThingsCloud.reportAttributes({ 71 | adc_value = adc_value 72 | }) 73 | end 74 | 75 | end 76 | -- 上报间隔时间为60秒 77 | -- 使用 ThingsCloud 免费版时,数据上报频率不要低于1分钟,否则可能会被断开连接,造成设备通信不稳定 78 | sys.wait(1000 * 60) 79 | end 80 | end) 81 | 82 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/adc_sensor/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/air780eg_dtu_json/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | -- 如果采用一型一密方式,参考示例 01.ThingsCloud_Connect/fetch_certificate 14 | 15 | -- 初始化和主控通信的 UART1 16 | local UART_MCU_ID = 1 17 | uart.setup(UART_MCU_ID, -- 串口id 18 | 115200, -- 波特率 19 | 8, -- 数据位 20 | 1 -- 停止位 21 | ) 22 | 23 | uart.on(UART_MCU_ID, "receive", function(id, len) 24 | local data = "" 25 | repeat 26 | data = uart.read(id, len) 27 | if #data > 0 then 28 | log.info("uart", "receive", id, #data, data) 29 | -- 将串口收到的 JSON 数据,作为属性上报到云平台。注意 data 必须是 JSON 格式文本。 30 | ThingsCloud.publish("attributes", data) 31 | end 32 | until data == "" 33 | end) 34 | 35 | -- 初始化 GPS UART2,适用于 Air780EG,或 Air780E 连接独立的 GPS 模块,都使用 UART2 连接 GPS 36 | local UART_GPS_ID = 2 37 | sys.taskInit(function() 38 | uart.setup(UART_GPS_ID, 9600) 39 | 40 | log.info("GPS", "start") 41 | pm.power(pm.GPS, true) 42 | 43 | -- 读取 UART 数据后调用解析函数 44 | uart.on(UART_GPS_ID, "recv", function(id, len) 45 | while 1 do 46 | local data = uart.read(id, 1024) 47 | if data and #data > 1 then 48 | -- log.info(data) 49 | libgnss.parse(data) 50 | else 51 | break 52 | end 53 | end 54 | end) 55 | 56 | sys.subscribe("GNSS_STATE", function(event, ticks) 57 | -- event取值有 58 | -- FIXED 定位成功 59 | -- LOSE 定位丢失 60 | -- ticks是事件发生的时间,一般可以忽略 61 | log.info("gnss", "state", event, ticks) 62 | end) 63 | 64 | while true do 65 | log.info("GPS", "isFix", libgnss.isFix(), "loc", libgnss.getIntLocation(), "rmc", json.encode(libgnss.getRmc(2))) 66 | sys.wait(1000 * 2) 67 | end 68 | 69 | end) 70 | 71 | -- 设备成功连接云平台后,触发该函数 72 | local function onConnect(result) 73 | if result then 74 | -- 当设备连接成功后 75 | 76 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 77 | 78 | end 79 | end 80 | 81 | -- 设备接收到云平台下发的属性时,触发该函数 82 | local function onAttributesPush(attributes) 83 | log.info("recv attributes push", json.encode(attributes)) 84 | 85 | -- 将云平台下发的属性 JSON,发送到主控 86 | uart.write(UART_MCU_ID, json.encode(attributes)) 87 | end 88 | 89 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 90 | sys.taskInit(function() 91 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 92 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 93 | ThingsCloud.connect({ 94 | host = host, 95 | projectKey = projectKey, 96 | accessToken = accessToken 97 | }) 98 | 99 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 100 | ThingsCloud.on("connect", onConnect) 101 | ThingsCloud.on("attributes_push", onAttributesPush) 102 | 103 | end) 104 | 105 | -- 在独立的协程中上报数据到云平台,可实现固定时间间隔的上报。 106 | -- 上报的数据,可在其它协程中读取,例如读取串口传感器数据 107 | sys.taskInit(function() 108 | 109 | while true do 110 | -- 此处要判断是否已连接成功 111 | if ThingsCloud.isConnected() then 112 | 113 | local attributes = {} 114 | attributes["gps_connected"] = libgnss.isFix() 115 | attributes["gps_rmc"] = libgnss.getRmc(2) 116 | attributes["gps_gsv"] = libgnss.getGsv() 117 | attributes["gps_gsa"] = libgnss.getGsa(2) 118 | attributes["gps_vtg"] = libgnss.getVtg(2) 119 | attributes["gps_zda"] = libgnss.getZda() 120 | attributes["gps_gga"] = libgnss.getGga(2) 121 | attributes["gps_gll"] = libgnss.getGll(2) 122 | 123 | ThingsCloud.reportAttributes(attributes) 124 | 125 | end 126 | -- 上报间隔时间为60秒 127 | -- 使用 ThingsCloud 免费版时,数据上报频率不要低于1分钟,否则可能会被断开连接,造成设备通信不稳定 128 | sys.wait(1000 * 60) 129 | end 130 | 131 | end) 132 | 133 | sys.taskInit(function() 134 | 135 | while true do 136 | -- 此处要判断是否已连接成功 137 | if ThingsCloud.isConnected() then 138 | 139 | ThingsCloud.reportAttributes({ 140 | imei = mobile.imei(), 141 | imsi = mobile.imsi(), 142 | csq = mobile.csq(), 143 | iccid = mobile.iccid(), 144 | rssi = mobile.rssi(), 145 | rsrq = mobile.rsrq(), 146 | rsrp = mobile.rsrp(), 147 | snr = mobile.snr(), 148 | simid = mobile.simid() 149 | }) 150 | 151 | end 152 | -- 上报间隔时间为5分钟 153 | -- 使用 ThingsCloud 免费版时,数据上报频率不要低于1分钟,否则可能会被断开连接,造成设备通信不稳定 154 | sys.wait(1000 * 60 * 5) 155 | end 156 | 157 | end) 158 | 159 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/air780eg_dtu_json/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/air780eg_gps_tracker/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | -- 初始化 GPS UART,适用于 Air780EG,或 Air780E 连接独立的 GPS 模块 15 | local UART_ID = 2 16 | sys.taskInit(function() 17 | uart.setup(UART_ID, 115200) 18 | 19 | log.info("GPS", "start") 20 | pm.power(pm.GPS, true) 21 | 22 | -- 方式1,读取 UART 数据后调用解析函数 23 | -- uart.on(2, "recv", function(id, len) 24 | -- while 1 do 25 | -- local data = uart.read(id, 1024) 26 | -- if data and #data > 1 then 27 | -- log.info(data) 28 | -- libgnss.parse(data) 29 | -- else 30 | -- break 31 | -- end 32 | -- end 33 | -- end) 34 | -- 方式1 结束 35 | 36 | -- 方式2,使用 libgnss 库绑定 UART 自动读取并解析 37 | -- 使用自动解析 38 | libgnss.bind(UART_ID) 39 | -- 清空数据兼初始化 40 | libgnss.clear() 41 | -- 可选调试模式 42 | libgnss.debug(true) 43 | -- 定位成功后自动RTC 44 | libgnss.rtcAuto(true) 45 | -- 方式2 结束 46 | 47 | sys.subscribe("GNSS_STATE", function(event, ticks) 48 | -- event取值有 49 | -- FIXED 定位成功 50 | -- LOSE 定位丢失 51 | -- ticks是事件发生的时间,一般可以忽略 52 | log.info("gnss", "state", event, ticks) 53 | end) 54 | 55 | while true do 56 | log.info("GPS", "isFix", libgnss.isFix(), "loc", libgnss.getIntLocation(), "rmc", json.encode(libgnss.getRmc(2))) 57 | sys.wait(1000 * 2) 58 | end 59 | 60 | end) 61 | 62 | -- 设备成功连接云平台后,触发该函数 63 | local function onConnect(result) 64 | if result then 65 | -- 当设备连接成功后 66 | 67 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 68 | 69 | end 70 | end 71 | 72 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 73 | sys.taskInit(function() 74 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 75 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 76 | ThingsCloud.connect({ 77 | host = host, 78 | projectKey = projectKey, 79 | accessToken = accessToken 80 | }) 81 | 82 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 83 | ThingsCloud.on("connect", onConnect) 84 | 85 | end) 86 | 87 | -- 在独立的协程中上报数据到云平台,可实现固定时间间隔的上报。 88 | -- 上报的数据,可在其它协程中读取,例如读取串口传感器数据 89 | sys.taskInit(function() 90 | 91 | while true do 92 | -- 此处要判断是否已连接成功 93 | if ThingsCloud.isConnected() then 94 | 95 | local attributes = {} 96 | attributes["gps_connected"] = libgnss.isFix() 97 | attributes["gps_rmc"] = libgnss.getRmc(2) 98 | attributes["gps_gsv"] = libgnss.getGsv() 99 | attributes["gps_gsa"] = libgnss.getGsa(2) 100 | attributes["gps_vtg"] = libgnss.getVtg(2) 101 | attributes["gps_zda"] = libgnss.getZda() 102 | attributes["gps_gga"] = libgnss.getGga(2) 103 | attributes["gps_gll"] = libgnss.getGll(2) 104 | 105 | ThingsCloud.reportAttributes(attributes) 106 | 107 | end 108 | -- 上报间隔时间为60秒 109 | -- 使用 ThingsCloud 免费版时,数据上报频率不要低于1分钟,否则可能会被断开连接,造成设备通信不稳定 110 | sys.wait(1000 * 60) 111 | end 112 | 113 | end) 114 | 115 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/air780eg_gps_tracker/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/gpio_out/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | -- GPIO 15 | local GPIO_PIN = 8 16 | gpio.setup(GPIO_PIN, 0, gpio.PULLUP) 17 | 18 | -- 设备成功连接云平台后,触发该函数 19 | local function onConnect(result) 20 | if result then 21 | -- 当设备连接成功后 22 | 23 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 24 | 25 | 26 | sys.wait(1000) 27 | -- 向云平台请求设备属性,例如读取配置信息。 28 | -- 参数是 table 数组,用来指定希望读取的属性名称,如果数组为空,可请求所有属性 29 | -- 云平台回复属性值,在事件 attributes_get_response 的回调函数中接收 30 | ThingsCloud.getAttributes({}) 31 | 32 | end 33 | end 34 | 35 | -- 设备开机时获取最新的状态,用来初始化设备。 36 | -- 设备向云平台发送读取云端属性后,接收到云平台下发的命令时,触发该函数 37 | -- response 是table结构的返回数据 38 | -- responseId 作为请求标识,通常可以不使用 39 | local function onAttributesGetResponse(response, responseId) 40 | log.info("attributes get response", json.encode(response), responseId) 41 | 42 | -- 获得云平台回复的属性值,实现相应的自定义逻辑 43 | if response.result == 1 then 44 | local attributes = response.attributes or {} 45 | if attributes.relay == true then 46 | -- TODO 例如开灯 47 | gpio.set(GPIO_PIN, 1) 48 | 49 | elseif attributes.relay == false then 50 | -- TODO 例如关灯 51 | gpio.set(GPIO_PIN, 0) 52 | 53 | end 54 | end 55 | 56 | end 57 | 58 | -- 设备接收到云平台下发的属性时,触发该函数 59 | local function onAttributesPush(attributes) 60 | log.info("recv attributes push", json.encode(attributes)) 61 | 62 | -- 实现相应的自定义逻辑 63 | if attributes.relay == true then 64 | -- TODO 例如开灯 65 | gpio.set(GPIO_PIN, 1) 66 | 67 | elseif attributes.relay == false then 68 | -- TODO 例如关灯 69 | gpio.set(GPIO_PIN, 0) 70 | 71 | end 72 | end 73 | 74 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 75 | sys.taskInit(function() 76 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 77 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 78 | ThingsCloud.connect({ 79 | host = host, 80 | projectKey = projectKey, 81 | accessToken = accessToken, 82 | }) 83 | 84 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 85 | ThingsCloud.on("connect", onConnect) 86 | ThingsCloud.on("attributes_push", onAttributesPush) 87 | ThingsCloud.on("attributes_get_response", onAttributesGetResponse) 88 | 89 | end) 90 | 91 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/gpio_out/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/ota_check/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | local function updateOTA(ota) 15 | log.info("Download and update OTA...") 16 | local code, headers, body = http.request("GET", ota.url, nil, nil, { 17 | fota = true, 18 | timeout = 3000 19 | }).wait() 20 | log.info("update OTA resp -> ", code, headers, body) 21 | local ret 22 | if code == 200 or code == 206 then 23 | if body == 0 then 24 | ret = 4 25 | else 26 | ret = 0 27 | end 28 | elseif code == -4 then 29 | ret = 1 30 | elseif code == -5 then 31 | ret = 3 32 | else 33 | ret = 4 34 | end 35 | return ret 36 | end 37 | 38 | -- 设备成功连接云平台后,触发该函数 39 | local function onConnect(result) 40 | if result then 41 | -- 当设备连接成功后 42 | 43 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 44 | 45 | -- 连接后立即上报当前固件版本 46 | ThingsCloud.reportAttributes({ 47 | version = _G.VERSION 48 | }) 49 | 50 | end 51 | end 52 | 53 | -- 设备接收到云平台下发的命令时,触发该函数 54 | local function onCommandSend(command) 55 | log.info("recv command send", json.encode(command)) 56 | if command.method == "otaUpgrade" then 57 | -- 参考 ThingsCloud OTA 文档:https://www.thingscloud.xyz/docs/guide/maintain/ota.html 58 | local params = command.params or {} 59 | if params.upgrade then 60 | 61 | sys.taskInit(function() 62 | local ota_result = updateOTA(params) 63 | -- 完成任务后,回复平台,上报OTA升级结果 64 | ThingsCloud.replyCommand({ 65 | method = "otaUpgrade", 66 | params = { 67 | ota_result = ota_result 68 | }, 69 | id = command.id 70 | }) 71 | if ota_result == 0 then 72 | -- 升级成功,等待片刻,重启模组 73 | sys.wait(3000) 74 | ThingsCloud.disconnect() 75 | rtos.reboot() 76 | end 77 | end) 78 | end 79 | 80 | elseif command.method == "restart" then 81 | -- 调用模组的重启指令 82 | ThingsCloud.disconnect() 83 | rtos.reboot() 84 | end 85 | end 86 | 87 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 88 | sys.taskInit(function() 89 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 90 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 91 | ThingsCloud.connect({ 92 | host = host, 93 | projectKey = projectKey, 94 | accessToken = accessToken 95 | }) 96 | 97 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 98 | ThingsCloud.on("connect", onConnect) 99 | ThingsCloud.on("command_send", onCommandSend) 100 | 101 | end) 102 | 103 | -- 定时主动向云平台检查是否有新版本固件 104 | -- 文档参考:https://www.thingscloud.xyz/docs/guide/maintain/ota.html 105 | sys.taskInit(function() 106 | while true do 107 | -- 此处要判断是否已连接成功 108 | if ThingsCloud.isConnected() then 109 | 110 | -- 定期上报 OTA 新版本检查事件 111 | ThingsCloud.reportEvent({ 112 | method = "otaCheck", 113 | params = {} 114 | }) 115 | 116 | -- 如果存在新版本,云平台会下发 otaUpgrade 命令,在上边的函数中处理 117 | 118 | end 119 | -- 12 个小时检查一次新版本 120 | sys.wait(1000 * 3600 * 12) 121 | end 122 | end) 123 | 124 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/ota_check/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/ota_check_uart_mcu/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | 15 | -- UART 初始化 16 | -- 也可以用于 RS485 透传,只需要在外部电路实现 UART 转 RS485 17 | local UART_ID = 1 18 | uart.setup(UART_ID, -- 串口id 19 | 115200, -- 波特率 20 | 8, -- 数据位 21 | 1 -- 停止位 22 | ) 23 | 24 | uart.on(UART_ID, "receive", function(id, len) 25 | local data = "" 26 | repeat 27 | -- 如果是air302, len不可信, 传1024 28 | -- data = uart.read(id, 1024) 29 | data = uart.read(id, len) 30 | if #data > 0 then 31 | log.info("uart", "receive", id, #data, data) 32 | -- MCU 通过串口向模组发送以下指令,用于检查固件新版本 33 | if data == "ota_check" then 34 | ThingsCloud.reportEvent({ 35 | method = "otaCheck", 36 | params = {} 37 | }) 38 | end 39 | end 40 | until data == "" 41 | end) 42 | 43 | local function updateOTA(ota) 44 | log.info("OTA start download firmware...") 45 | local code, headers, body = http.request("GET", ota.url, nil, nil, { 46 | timeout = 3000 47 | }).wait() 48 | log.info("OTA response -> ", code, headers, body) 49 | local ret 50 | if code == 200 or code == 206 then 51 | if body ~= nil then 52 | ret = 0 53 | log.info("OTA download success, start sending to MCU") 54 | -- 将下载的固件二进制流,通过串口发送给MCU 55 | uart.write(UART_ID, "ota_start") 56 | sys.wait(1000) 57 | uart.write(UART_ID, body) 58 | else 59 | ret = 4 60 | end 61 | elseif code == -4 then 62 | ret = 1 63 | elseif code == -5 then 64 | ret = 3 65 | else 66 | ret = 4 67 | end 68 | return ret 69 | end 70 | 71 | 72 | -- 设备成功连接云平台后,触发该函数 73 | local function onConnect(result) 74 | if result then 75 | -- 当设备连接成功后 76 | 77 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 78 | 79 | end 80 | end 81 | 82 | -- 设备接收到云平台下发的命令时,触发该函数 83 | local function onCommandSend(command) 84 | log.info("recv command send", json.encode(command)) 85 | if command.method == "otaUpgrade" then 86 | -- 参考 ThingsCloud OTA 文档:https://www.thingscloud.xyz/docs/guide/maintain/ota.html 87 | local params = command.params or {} 88 | if params.upgrade then 89 | sys.taskInit(function() 90 | local ota_dl_result = updateOTA(params) 91 | -- 完成任务后,回复平台,上报OTA升级结果 92 | ThingsCloud.replyCommand({ 93 | method = "otaUpgrade", 94 | params = { 95 | ota_dl_result = ota_dl_result 96 | }, 97 | id = command.id 98 | }) 99 | end) 100 | end 101 | 102 | elseif command.method == "restart" then 103 | -- 调用模组的重启指令 104 | ThingsCloud.disconnect() 105 | rtos.reboot() 106 | end 107 | end 108 | 109 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 110 | sys.taskInit(function() 111 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 112 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 113 | ThingsCloud.connect({ 114 | host = host, 115 | projectKey = projectKey, 116 | accessToken = accessToken 117 | }) 118 | 119 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 120 | ThingsCloud.on("connect", onConnect) 121 | ThingsCloud.on("command_send", onCommandSend) 122 | 123 | end) 124 | 125 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/ota_check_uart_mcu/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/ota_push/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | local function updateOTA(ota) 15 | log.info("Download and update OTA...") 16 | local code, headers, body = http.request("GET", ota.url, nil, nil, { 17 | fota = true, 18 | timeout = 3000 19 | }).wait() 20 | log.info("update OTA resp -> ", code, headers, body) 21 | local ret 22 | if code == 200 or code == 206 then 23 | if body == 0 then 24 | ret = 4 25 | else 26 | ret = 0 27 | end 28 | elseif code == -4 then 29 | ret = 1 30 | elseif code == -5 then 31 | ret = 3 32 | else 33 | ret = 4 34 | end 35 | return ret 36 | end 37 | 38 | -- 设备成功连接云平台后,触发该函数 39 | local function onConnect(result) 40 | if result then 41 | -- 当设备连接成功后 42 | 43 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 44 | 45 | -- 连接后立即上报当前固件版本 46 | ThingsCloud.reportAttributes({ 47 | version = _G.VERSION 48 | }) 49 | 50 | end 51 | end 52 | 53 | -- 设备接收到云平台下发的命令时,触发该函数 54 | local function onCommandSend(command) 55 | log.info("recv command send", json.encode(command)) 56 | if command.method == "otaUpgrade" then 57 | -- 参考 ThingsCloud OTA 文档:https://www.thingscloud.xyz/docs/guide/maintain/ota.html 58 | local params = command.params or {} 59 | if params.upgrade then 60 | local ota_result = updateOTA(params) 61 | -- 完成任务后,回复平台,上报OTA升级结果 62 | ThingsCloud.replyCommand({ 63 | method = "otaFinished", 64 | params = { 65 | ota_result = ota_result 66 | } 67 | }) 68 | if ota_result == 0 then 69 | -- 升级成功 70 | sys.taskInit(function() 71 | -- 等待片刻,重启模组 72 | sys.wait(3000) 73 | ThingsCloud.disconnect() 74 | rtos.reboot() 75 | end) 76 | end 77 | end 78 | 79 | elseif command.method == "restart" then 80 | -- 调用模组的重启指令 81 | ThingsCloud.disconnect() 82 | rtos.reboot() 83 | end 84 | end 85 | 86 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 87 | sys.taskInit(function() 88 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 89 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 90 | ThingsCloud.connect({ 91 | host = host, 92 | projectKey = projectKey, 93 | accessToken = accessToken 94 | }) 95 | 96 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 97 | ThingsCloud.on("connect", onConnect) 98 | ThingsCloud.on("command_send", onCommandSend) 99 | 100 | end) 101 | 102 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/ota_push/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/pwm_out/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | -- PWM 引脚定义 15 | local PWM_ID = 1 16 | -- PWM 状态全局变量 17 | local pwm_state = false -- 是否开启 18 | local pwm_freq = 1000 -- 频率(Hz) 19 | local pwm_duty = 100 -- 占空比0-100 20 | 21 | -- 设备成功连接云平台后,触发该函数 22 | local function onConnect(result) 23 | if result then 24 | -- 当设备连接成功后 25 | 26 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 27 | 28 | sys.wait(1000) 29 | -- 向云平台请求设备属性,例如读取配置信息。 30 | -- 参数是 table 数组,用来指定希望读取的属性名称,如果数组为空,可请求所有属性 31 | -- 云平台回复属性值,在事件 attributes_get_response 的回调函数中接收 32 | ThingsCloud.getAttributes({}) 33 | 34 | end 35 | end 36 | 37 | -- 设备开机时获取最新的状态,用来初始化设备。 38 | -- 设备向云平台发送读取云端属性后,接收到云平台下发的命令时,触发该函数 39 | -- response 是table结构的返回数据 40 | -- responseId 作为请求标识,通常可以不使用 41 | local function onAttributesGetResponse(response, responseId) 42 | log.info("attributes get response", json.encode(response), responseId) 43 | 44 | -- 获得云平台回复的属性值,实现相应的自定义逻辑 45 | if response.result == 1 then 46 | local attributes = response.attributes or {} 47 | pwm_state = attributes.pwm_state or false 48 | pwm_freq = attributes.pwm_freq or 1000 49 | pwm_duty = attributes.pwm_duty or 100 50 | if pwm_state == true then 51 | -- 启动 PWM 52 | pwm.open(PWM_ID, pwm_freq, pwm_duty) 53 | end 54 | end 55 | 56 | end 57 | 58 | -- 设备接收到云平台下发的属性时,触发该函数 59 | local function onAttributesPush(attributes) 60 | log.info("recv attributes push", json.encode(attributes)) 61 | 62 | pwm_state = attributes.pwm_state or false 63 | pwm_freq = attributes.pwm_freq or 1000 64 | pwm_duty = attributes.pwm_duty or 100 65 | if pwm_state == true then 66 | -- 启动 PWM 67 | pwm.open(PWM_ID, pwm_freq, pwm_duty) 68 | else 69 | pwm.close(PWM_ID) 70 | end 71 | 72 | end 73 | 74 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 75 | sys.taskInit(function() 76 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 77 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 78 | ThingsCloud.connect({ 79 | host = host, 80 | projectKey = projectKey, 81 | accessToken = accessToken 82 | }) 83 | 84 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 85 | ThingsCloud.on("connect", onConnect) 86 | ThingsCloud.on("attributes_push", onAttributesPush) 87 | ThingsCloud.on("attributes_get_response", onAttributesGetResponse) 88 | 89 | end) 90 | 91 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/pwm_out/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/sht30_sensor/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | -- 设备成功连接云平台后,触发该函数 15 | local function onConnect(result) 16 | if result then 17 | -- 当设备连接成功后 18 | 19 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 20 | 21 | end 22 | end 23 | 24 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 25 | sys.taskInit(function() 26 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 27 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 28 | ThingsCloud.connect({ 29 | host = host, 30 | projectKey = projectKey, 31 | accessToken = accessToken 32 | }) 33 | 34 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 35 | ThingsCloud.on("connect", onConnect) 36 | 37 | end) 38 | 39 | -- 传感器读取的数据全局定义 40 | 41 | local temperature = nil 42 | local humidity = nil 43 | 44 | -- 接线 45 | --[[ 46 | SHT30 --- Air302 47 | SDA - I2C_SDA 48 | SCL - I2C_SCL 49 | VCC - VDDIO 50 | GND - GND 51 | ]] 52 | -- 提示, 老板子上的I2C丝印可能是反的, 如果读取失败请调换一下SDA和SLA 53 | -- 启动个task, 定时查询SHT20的数据 54 | sys.taskInit(function() 55 | 56 | -- sht30的默认i2c地址 57 | local addr = 0x44 58 | -- 按实际修改哦 59 | local id = 0 60 | 61 | log.info("i2c", "initial", i2c.setup(0)) 62 | 63 | while true do 64 | -- 第一种方式 65 | i2c.send(id, addr, string.char(0x2C, 0x06)) 66 | sys.wait(5) -- 5ms 67 | local data = i2c.recv(id, addr, 6) 68 | log.info("sht30", data:toHex()) 69 | if #data == 6 then 70 | local _, tval, ccrc, hval, hcrc = pack.unpack(data, ">HbHb") 71 | temperature = ((tval * 175) / 65535.0) - 45 72 | humidity = ((hval * 100) / 65535.0) 73 | log.info("sht30", cTemp, humidity) 74 | end 75 | sys.wait(2000) 76 | end 77 | 78 | end) 79 | 80 | -- 在独立的协程中上报数据到云平台,可实现固定时间间隔的上报。 81 | -- 上报的数据,可在其它协程中读取,例如读取串口传感器数据 82 | sys.taskInit(function() 83 | while true do 84 | -- 此处要判断是否已连接成功 85 | if ThingsCloud.isConnected() then 86 | 87 | -- 上报属性,这里举例模拟一些数据 88 | ThingsCloud.reportAttributes({ 89 | temperature = temperature, 90 | humidity = humidity 91 | }) 92 | end 93 | -- 上报间隔时间为60秒 94 | -- 使用 ThingsCloud 免费版时,数据上报频率不要低于1分钟,否则可能会被断开连接,造成设备通信不稳定 95 | sys.wait(1000 * 60) 96 | end 97 | end) 98 | 99 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/sht30_sensor/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/uart_dtu/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | -- UART 初始化 15 | -- 也可以用于 RS485 透传,只需要在外部电路实现 UART 转 RS485 16 | local UART_ID = 1 17 | uart.setup(UART_ID, -- 串口id 18 | 115200, -- 波特率 19 | 8, -- 数据位 20 | 1 -- 停止位 21 | ) 22 | 23 | uart.on(UART_ID, "receive", function(id, len) 24 | local data = "" 25 | repeat 26 | -- 如果是air302, len不可信, 传1024 27 | -- data = uart.read(id, 1024) 28 | data = uart.read(id, len) 29 | if #data > 0 then 30 | log.info("uart", "receive", id, #data, data) 31 | ThingsCloud.publishCustomTopic("data/stream", data) 32 | end 33 | until data == "" 34 | end) 35 | 36 | 37 | -- 设备成功连接云平台后,触发该函数 38 | local function onConnect(result) 39 | if result then 40 | -- 当设备连接成功后 41 | 42 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 43 | 44 | -- 订阅自定义数据流。必须先在设备类型中创建自定义数据流,才可以使用。 45 | ThingsCloud.subscribe("data/stream/set") 46 | 47 | end 48 | end 49 | 50 | -- 设备接收到云平台下发的属性时,触发该函数 51 | local function onDataSet(data) 52 | log.info("recv data set", data, string.toHex(data)) 53 | -- 透传到 UART 54 | uart.write(UART_ID, data) 55 | end 56 | 57 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 58 | sys.taskInit(function() 59 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 60 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 61 | ThingsCloud.connect({ 62 | host = host, 63 | projectKey = projectKey, 64 | accessToken = accessToken 65 | }) 66 | 67 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 68 | ThingsCloud.on("connect", onConnect) 69 | ThingsCloud.on("data_set", onDataSet) 70 | 71 | end) 72 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/uart_dtu/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/uart_json/demo.lua: -------------------------------------------------------------------------------- 1 | -- 引入 ThingsCloud 接入库 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = require "ThingsCloud" 4 | 5 | -- 进入 ThingsCloud 控制台:https://www.thingscloud.xyz 6 | -- 创建设备,进入设备详情页的【连接】页面,复制设备证书和MQTT接入点地址。请勿泄露你的设备证书。 7 | -- ProjectKey 8 | local projectKey = "" 9 | -- AccessToken 10 | local accessToken = "" 11 | -- MQTT 接入点,只需主机名部分 12 | local host = "" 13 | 14 | -- UART 初始化,用于和主控MCU通信,使用JSON格式 15 | local UART_ID = 1 16 | uart.setup(UART_ID, -- 串口id 17 | 115200, -- 波特率 18 | 8, -- 数据位 19 | 1 -- 停止位 20 | ) 21 | 22 | uart.on(UART_ID, "receive", function(id, len) 23 | local data = "" 24 | repeat 25 | -- 如果是air302, len不可信, 传1024 26 | -- data = uart.read(id, 1024) 27 | data = uart.read(id, len) 28 | if #data > 0 then 29 | log.info("uart", "receive", id, #data, data) 30 | -- 将串口收到的 JSON 数据,作为属性上报到云平台。注意 data 必须是 JSON 格式文本。 31 | ThingsCloud.publish("attributes", data) 32 | end 33 | until data == "" 34 | end) 35 | 36 | 37 | -- 设备成功连接云平台后,触发该函数 38 | local function onConnect(result) 39 | if result then 40 | -- 当设备连接成功后 41 | 42 | -- 例如:切换设备的LED闪烁模式,提示用户设备已正常连接。 43 | 44 | end 45 | end 46 | 47 | -- 设备接收到云平台下发的属性时,触发该函数 48 | local function onAttributesPush(attributes) 49 | log.info("recv attributes push", json.encode(attributes)) 50 | 51 | -- 将云平台下发的属性 JSON,发送到串口 52 | uart.write(UART_ID, json.encode(attributes)) 53 | end 54 | 55 | -- 设备接入云平台的初始化逻辑,在独立协程中完成 56 | sys.taskInit(function() 57 | -- 连接云平台,内部支持判断网络可用性、MQTT自动重连 58 | -- 这里采用了设备一机一密方式,需要为每个设备固件单独写入证书。另外也支持一型一密,相同设备类型下的所有设备使用相同固件。 59 | ThingsCloud.connect({ 60 | host = host, 61 | projectKey = projectKey, 62 | accessToken = accessToken 63 | }) 64 | 65 | -- 注册各类事件的回调函数,在回调函数中编写所需的硬件端操作逻辑 66 | ThingsCloud.on("connect", onConnect) 67 | ThingsCloud.on("attributes_push", onAttributesPush) 68 | 69 | end) 70 | -------------------------------------------------------------------------------- /examples/10.IoT_Tutorials/uart_json/main.lua: -------------------------------------------------------------------------------- 1 | -- LuaTools需要PROJECT和VERSION这两个信息 2 | PROJECT = "thingscloud_example" 3 | VERSION = "1.0" 4 | PRODUCT_KEY = "s1uUnY6KA06ifIjcutm5oNbG3MZf5aUv" --换成自己的 5 | 6 | -- sys库是标配 7 | _G.sys = require("sys") 8 | _G.sysplus = require("sysplus") 9 | log.style(1) 10 | 11 | require "demo" 12 | 13 | sys.run() 14 | -------------------------------------------------------------------------------- /libs/README.md: -------------------------------------------------------------------------------- 1 | # ThingsCloud 接入库 2 | 3 | 所有示例项目都依赖接入库文件,将 `ThingsCloud.lua` 加入到示例项目工程中。 -------------------------------------------------------------------------------- /libs/ThingsCloud.lua: -------------------------------------------------------------------------------- 1 | -- 合宙模组 LUATOS 接入 ThingsCloud 物联网平台 2 | -- 接入协议参考:ThingsCloud MQTT 接入文档 https://docs.thingscloud.xyz/guide/connect-device/mqtt.html 3 | local ThingsCloud = {} 4 | 5 | local projectKey = "" -- project_key 6 | local accessToken = "" -- access_token 7 | local typeKey = "" -- type_key 8 | local host = "" 9 | local port = 1883 10 | local apiEndpoint = "" -- api endpoint 11 | local mqttc = nil 12 | local connected = false 13 | local deviceInfo = {} 14 | 15 | local certFetchRetryMax = 5 16 | local certFetchRetryCnt = 0 17 | 18 | local SUBSCRIBE_PREFIX = { 19 | ATTRIBUTES_GET_REPONSE = "attributes/get/response/", 20 | ATTRIBUTES_PUSH = "attributes/push", 21 | COMMAND_SEND = "command/send/", 22 | COMMAND_REPLY_RESPONSE = "command/reply/response/", 23 | DATA_SET = "data/", 24 | GW_ATTRIBUTES_PUSH = "gateway/attributes/push", 25 | GW_COMMAND_SEND = "gateway/command/send" 26 | } 27 | local EVENT_TYPES = { 28 | fetch_cert = true, 29 | connect = true, 30 | attributes_report_response = true, 31 | attributes_get_response = true, 32 | attributes_push = true, 33 | command_send = true, 34 | command_reply_response = true, 35 | data_set = true, 36 | gw_attributes_push = true, 37 | gw_command_send = true 38 | } 39 | local CALLBACK = {} 40 | local QUEUE = { 41 | PUBLISH = {} 42 | } 43 | local logger = {} 44 | function logger.info(...) 45 | log.info("ThingsCloud", ...) 46 | end 47 | 48 | function ThingsCloud.on(eType, cb) 49 | if not eType or not EVENT_TYPES[eType] or type(cb) ~= "function" then 50 | return 51 | end 52 | CALLBACK[eType] = cb 53 | logger.info("on", eType) 54 | end 55 | 56 | local function cb(eType, ...) 57 | if not eType or not EVENT_TYPES[eType] or not CALLBACK[eType] then 58 | return 59 | end 60 | CALLBACK[eType](...) 61 | logger.info("event", eType, ...) 62 | end 63 | 64 | local function mqttConnect() 65 | local retryCount = 0 66 | logger.info("ThingsCloud connecting...") 67 | 68 | mqttc = mqtt.create(nil, host, port, false) 69 | mqttc:auth(mobile.imei(), accessToken, projectKey) 70 | mqttc:keepalive(300) 71 | mqttc:autoreconn(true, 10000) 72 | mqttc:connect() 73 | 74 | mqttc:on(function(mqtt_client, event, data, payload) 75 | 76 | if event == "conack" then 77 | connected = true 78 | logger.info("ThingsCloud connected") 79 | cb("connect", true) 80 | sys.publish("mqtt_conack") 81 | ThingsCloud.subscribe("attributes/push") 82 | ThingsCloud.subscribe("attributes/get/response/+") 83 | ThingsCloud.subscribe("command/send/+") 84 | ThingsCloud.subscribe("command/reply/response/+") 85 | 86 | elseif event == "recv" then 87 | logger.info("receive from cloud", data or nil, payload or "nil") 88 | if (data:sub(1, SUBSCRIBE_PREFIX.ATTRIBUTES_GET_REPONSE:len()) == SUBSCRIBE_PREFIX.ATTRIBUTES_GET_REPONSE) then 89 | local response = json.decode(payload) 90 | local responseId = tonumber(data:sub(SUBSCRIBE_PREFIX.ATTRIBUTES_GET_REPONSE:len() + 1)) 91 | cb("attributes_get_response", response, responseId) 92 | elseif (data == SUBSCRIBE_PREFIX.ATTRIBUTES_PUSH) then 93 | local response = json.decode(payload) 94 | cb("attributes_push", response) 95 | elseif (data:sub(1, SUBSCRIBE_PREFIX.COMMAND_SEND:len()) == SUBSCRIBE_PREFIX.COMMAND_SEND) then 96 | local response = json.decode(payload) 97 | if response.method and response.params then 98 | cb("command_send", response) 99 | end 100 | elseif (data:sub(1, SUBSCRIBE_PREFIX.COMMAND_REPLY_RESPONSE:len()) == 101 | SUBSCRIBE_PREFIX.COMMAND_REPLY_RESPONSE) then 102 | local response = json.decode(payload) 103 | local replyId = tonumber(data:sub(SUBSCRIBE_PREFIX.COMMAND_REPLY_RESPONSE:len() + 1)) 104 | cb("command_reply_response", response, replyId) 105 | elseif (data:sub(1, SUBSCRIBE_PREFIX.DATA_SET:len()) == SUBSCRIBE_PREFIX.DATA_SET) then 106 | local tmp = split(data, "/") 107 | if #tmp == 3 and tmp[3] == "set" then 108 | local identifier = tmp[2] 109 | cb("data_set", payload) 110 | end 111 | elseif (data == SUBSCRIBE_PREFIX.GW_ATTRIBUTES_PUSH) then 112 | local response = json.decode(payload) 113 | cb("gw_attributes_push", response) 114 | elseif (data == SUBSCRIBE_PREFIX.GW_COMMAND_SEND) then 115 | local response = json.decode(payload) 116 | cb("gw_command_send", response) 117 | end 118 | 119 | elseif event == "sent" then 120 | log.info("mqtt", "sent", data) 121 | end 122 | end) 123 | 124 | end 125 | 126 | function ThingsCloud.disconnect() 127 | if not connected then 128 | return 129 | end 130 | mqttc:close() 131 | mqttc = nil 132 | end 133 | 134 | function ThingsCloud.connect(param) 135 | if not param.host or not param.projectKey then 136 | logger.info("host or projectKey not found") 137 | return 138 | end 139 | host = param.host 140 | projectKey = param.projectKey 141 | 142 | if param.accessToken then 143 | accessToken = param.accessToken 144 | sys.waitUntil("IP_READY", 30000) 145 | sys.taskInit(function() 146 | sys.taskInit(procConnect) 147 | end) 148 | 149 | else 150 | if not param.apiEndpoint then 151 | logger.info("apiEndpoint not found") 152 | return 153 | end 154 | apiEndpoint = param.apiEndpoint 155 | if param.typeKey ~= "" or param.typeKey ~= nil then 156 | typeKey = param.typeKey 157 | end 158 | sys.waitUntil("IP_READY", 30000) 159 | sys.taskInit(function() 160 | sys.taskInit(fetchDeviceCert) 161 | end) 162 | 163 | end 164 | end 165 | 166 | -- 一型一密,使用IMEI作为DeviceKey,领取设备证书AccessToken 167 | function fetchDeviceCert() 168 | local headers = {} 169 | headers["Project-Key"] = projectKey 170 | headers["Content-Type"] = "application/json" 171 | local url = apiEndpoint .. "/device/v1/certificate" 172 | local deviceKey = mobile.imei() 173 | local code, headers, body = http.request("POST", url, headers, json.encode({ 174 | device_key = deviceKey, 175 | type_key = typeKey 176 | }), { 177 | timeout = 5000 178 | }).wait() 179 | log.info("http fetch cert:", deviceKey, code, headers, body) 180 | if code == 200 then 181 | local data = json.decode(body) 182 | if data.result == 1 then 183 | sys.taskInit(function() 184 | cb("fetch_cert", true) 185 | end) 186 | deviceInfo = data.device 187 | accessToken = deviceInfo.access_token 188 | procConnect() 189 | return 190 | end 191 | end 192 | if certFetchRetryCnt < certFetchRetryMax then 193 | -- 重试 194 | certFetchRetryCnt = certFetchRetryCnt + 1 195 | sys.wait(1000 * 10) 196 | fetchDeviceCert() 197 | else 198 | cb("fetch_cert", false) 199 | end 200 | end 201 | 202 | function procConnect() 203 | mqttConnect() 204 | sys.waitUntil("mqtt_conack") 205 | sys.taskInit(function() 206 | while true do 207 | if #QUEUE.PUBLISH > 0 then 208 | local item = table.remove(QUEUE.PUBLISH, 1) 209 | logger.info("publish", item.topic, item.data) 210 | if mqttc:publish(item.topic, item.data) then 211 | -- 212 | end 213 | end 214 | sys.wait(100) 215 | end 216 | end) 217 | end 218 | 219 | function ThingsCloud.isConnected() 220 | return connected 221 | end 222 | 223 | local function insertPublishQueue(topic, data) 224 | if not connected then 225 | return 226 | end 227 | table.insert(QUEUE.PUBLISH, { 228 | topic = topic, 229 | data = data 230 | }) 231 | end 232 | 233 | function ThingsCloud.subscribe(topic) 234 | if not connected then 235 | return 236 | end 237 | logger.info("subscribe", topic) 238 | mqttc:subscribe(topic) 239 | end 240 | 241 | function ThingsCloud.publish(topic, data) 242 | insertPublishQueue(topic, data) 243 | end 244 | 245 | function ThingsCloud.reportAttributes(tableData) 246 | insertPublishQueue("attributes", json.encode(tableData)) 247 | sys.publish("QUEUE_PUBLISH", "ATTRIBUTES") 248 | end 249 | 250 | function ThingsCloud.getAttributes(attrsList, options) 251 | options = options or {} 252 | options.getId = options.getId or 1000 253 | local data = { 254 | keys = attrsList 255 | } 256 | if #attrsList == 0 then 257 | data = {} 258 | end 259 | insertPublishQueue("attributes/get/" .. tostring(options.getId), json.encode(data)) 260 | end 261 | 262 | function ThingsCloud.reportEvent(event, options) 263 | options = options or {} 264 | options.eventId = options.eventId or 1000 265 | insertPublishQueue("event/report/" .. tostring(options.eventId), json.encode(event)) 266 | end 267 | 268 | function ThingsCloud.replyCommand(commandReply, options) 269 | options = options or {} 270 | options.replyId = options.replyId or 1000 271 | insertPublishQueue("command/reply/" .. tostring(options.replyId), json.encode(commandReply)) 272 | end 273 | 274 | function ThingsCloud.publishCustomTopic(topic, payload, options) 275 | insertPublishQueue(topic, payload) 276 | end 277 | 278 | function getAccessToken() 279 | return accessToken 280 | end 281 | 282 | function isGateway() 283 | if deviceInfo.conn_type == "3" then 284 | return true 285 | end 286 | return false 287 | end 288 | 289 | function split(str, sep) 290 | local sep, fields = sep or ":", {} 291 | local pattern = string.format("([^%s]+)", sep) 292 | str:gsub(pattern, function(c) 293 | fields[#fields + 1] = c 294 | end) 295 | return fields 296 | end 297 | 298 | return ThingsCloud 299 | --------------------------------------------------------------------------------