前面发了腾讯云点灯以后,有朋友提到了点灯科技,人家都叫点灯科技了,那咱们学点灯必须连一下试试呀。本文将记录如何将设备连接点灯云平台,并通过手机进行远程控制
先上成果演示
下载点灯科技的app-blinker,注册账号,这部分就不用写了
点击右上角添加设备,设备类型选独立设备
右下角选择网络接入
然后复制得到的key
点击我们刚添加的设备,根据提示,我们直接点载入示例
思路一样,首先找文档,看看如何接入。可以看到有http和mqtt方式接入,那我们肯定是mqtt呀,因为我们要长连接才能保证设备实时被控制。
所以查找文档点灯科技 (diandeng.tech)可以看到mqtt的鉴权需要先使用http请求获得连接参数
找一下luatos的http的库http2 - http2客户端 - LuatOS 文档可看到很简单,就一行代码,而且接口使用和其他语言基本一致
所以直接上代码,我们直接封装一个函数,传入authKey就可以获得连接参数
local function getDeviceInfo(authKey)
local code, headers, data = http2.request("GET", serverAdress .. authKey .. "&protocol=mqtt").wait()
if data and json.decode(data) then
local authdata = json.decode(data)
if authdata.message == 1000 then
log.info(
"获取设备信息成功",
authdata.detail.host:sub(8, -1),
authdata.detail.deviceName,
authdata.detail.iotId,
authdata.detail.iotToken,
authdata.detail.uuid
)
return true, authdata.detail.host:sub(8, -1), authdata.detail.deviceName, authdata.detail.iotId, authdata.detail.iotToken, authdata.detail.uuid
end
end
return false
end
连接部分的代码就和昨天连腾讯云没啥两样了,都是连mqtt,然后订阅数据,收到数据以后根据文档进行处理
直接上完整代码吧,一样是Air780E,Air105,ESP32C3通用代码
PROJECT = "gpiodemo"
VERSION = "1.0.0"
local sys = require "sys"
require("sysplus")
--{"deviceType": "OwnApp", "fromDevice": "856ED5F216FO23SKEEDQJ8LL", "toDevice": "0451f7667f1c11ec94755254", "data": {"state": "online"}}
local wifi_ssid = "NAS"
local wifi_password = "Air123456"
--如果是wifi设备,这里改成你的wifi账号和密码
local authKey = "" --点灯云申请的设备authKey
local serverAdress = "http://iot.diandeng.tech/api/v1/user/device/diy/auth?authKey=" --http鉴权地址
local mqttc = nil
local led_state = 0
local num = 0
local function getDeviceInfo(authKey)
local code, headers, data = http2.request("GET", serverAdress .. authKey .. "&protocol=mqtt").wait()
if data and json.decode(data) then
local authdata = json.decode(data)
if authdata.message == 1000 then
log.info(
"获取设备信息成功",
authdata.detail.host:sub(8, -1),
authdata.detail.deviceName,
authdata.detail.iotId,
authdata.detail.iotToken,
authdata.detail.uuid
)
return true, authdata.detail.host:sub(8, -1), authdata.detail.deviceName, authdata.detail.iotId, authdata.detail.iotToken, authdata.detail.uuid
end
end
return false
end
sys.taskInit(
function()
log.info("BSP", rtos.bsp())
if rtos.bsp() == "ESP32C3" then
LED = gpio.setup(12, 0, gpio.PULLUP)
wlan.init()
wlan.setMode(wlan.STATION)
wlan.connect(ssid, password, 1)
local result, data = sys.waitUntil("IP_READY")
log.info("wlan", "IP_READY", result, data)
elseif rtos.bsp() == "AIR105" then
w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00)
w5500.config() --默认是DHCP模式
w5500.bind(socket.ETH0)
LED = gpio.setup(62, 0, gpio.PULLUP)
sys.wait(1000)
elseif rtos.bsp() == "EC618" then
mobile.simid(2)
LED = gpio.setup(27, 0, gpio.PULLUP)
end
result, host, deviceName, iotId, iotToken, uuid = getDeviceInfo(authKey)
if not result then
log.info("获取设备信息失败")
return
end
log.info("result", host, deviceName, iotId, iotToken)
mqttc = mqtt.create(nil, host, 1883)
-- mqttc:debug(true)
mqttc:auth(deviceName, iotId, iotToken)
mqttc:keepalive(30) -- 默认值240s
mqttc:autoreconn(true, 3000) -- 自动重连机制
local mqtt_topic = "/device/" .. deviceName .. "/r"
mqtt_topic_pub = "/device/" .. deviceName .. "/s"
local send_date = {}
mqttc:on(
function(mqtt_client, event, data, payload)
if event == "conack" then
sys.publish("mqtt_conack")
log.info("mqtt", "mqtt已连接")
mqtt_client:subscribe(mqtt_topic)
send_date.dedeviceType = "OwnApp"
send_date.fromDevice = deviceName
send_date.toDevice = uuid
send_date.data = {}
send_date.data.state = "online"
mqttc:publish(mqtt_topic_pub, json.encode(send_date), 0)
elseif event == "recv" then
log.info("mqtt", "收到消息", data, payload)
local data = json.decode(payload)
if data.data["btn-abc"] == "tap" then
led_state = (~led_state) & 1
LED(led_state)
send_date.data = {}
send_date.data["btn-abc"] = "tap"
mqttc:publish(mqtt_topic_pub, json.encode(send_date), 0)
elseif data.data.get == "state" then
send_date.data = {}
send_date.data.state = "online"
log.info("mqtt", "回复状态为在线", json.encode(send_date))
mqttc:publish(mqtt_topic_pub, json.encode(send_date), 0)
elseif data.data["btn-123"] == "tap" then
log.info("mqtt", "数字")
num = num + 1
send_date.data = {}
send_date.data["num-abc"] = {}
send_date.data["num-abc"].tex = "num"
send_date.data["num-abc"].val = num
mqttc:publish(mqtt_topic_pub, json.encode(send_date), 0)
end
elseif event == "sent" then
log.info("mqtt", "sent", "pkgid", data)
end
end
)
mqttc:connect()
sys.wait(10000)
sys.waitUntil("mqtt_conack")
while true do
-- mqttc自动处理重连
local ret, topic, data, qos = sys.waitUntil("mqtt_pub", 30000)
if ret then
if topic == "close" then
break
end
mqttc:publish(mqtt_topic_pub, data, qos)
end
end
mqttc:close()
mqttc = nil
end
)
-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!
把完整代码烧录进设备,不会烧录的可以看 AIR780E二次开发点灯(LuatOS)
底层固件要用最新的,可以自行编译CSDK里的luatos项目,也可以用我这里编译好的LuatOS-SoC_V1001_EC618.soc
烧录好以后,打开点灯app,然后就可以开始测试了。点击开灯按钮可以翻转开发板上带的灯的状态;点击计数按钮,设备端会累加次数并上报,在app可以看到次数不断增加。