一、簡(jiǎn)介
1、項(xiàng)目介紹
之前發(fā)布的文章有采用合宙的4G LTE Cat.1模塊,編程語(yǔ)言用的是lua,整體來(lái)說(shuō)代碼比較簡(jiǎn)潔,實(shí)現(xiàn)對(duì)華為云物聯(lián)網(wǎng)平臺(tái)的設(shè)備通信與控制,即采用一個(gè)變量作為模擬屬性定時(shí)上報(bào),并以一個(gè)LED燈作為受控設(shè)備進(jìn)行云端命令的控制,本期內(nèi)容為使用了阿里云物聯(lián)網(wǎng)平臺(tái)完成同樣上述功能。
準(zhǔn)備:
Air780e開(kāi)發(fā)板
usb-typec數(shù)據(jù)線
4G SIM手機(jī)卡(可上網(wǎng))
2、Air780E模組
Air780E 是合宙通信推出的 LTE Cat.1bis通信模塊,采用移芯EC618平臺(tái),支持4G全網(wǎng)通 支持雙卡單待、支持SPI LCD、支持USB 2.0, 僅CDC功能、支持I2S數(shù)字語(yǔ)音接口、支持?jǐn)z像頭等配置,支持AT指令開(kāi)發(fā)、CSDK開(kāi)發(fā)和luatos的lua腳本語(yǔ)言的多種開(kāi)發(fā),開(kāi)發(fā)板目前淘寶官網(wǎng)是39.9,性價(jià)比還是可以的
3、luatos
Lua可以說(shuō)是目前嵌入式方案中,資源占用最小、運(yùn)行效率最高、語(yǔ)法最簡(jiǎn)潔的一門腳本語(yǔ)言。對(duì)于編程小白來(lái)說(shuō),它適合作為你的編程入門語(yǔ)言,因?yàn)檎Z(yǔ)法簡(jiǎn)單。對(duì)于會(huì)c語(yǔ)言的老手來(lái)說(shuō),它與c可以完美契合,再加上LuatOS本身就是開(kāi)源,你可以輕松地使用c為其添加一套c庫(kù)接口,享受它的高效。在本次教程中,我們便使用lua語(yǔ)言跑luatos實(shí)現(xiàn)對(duì)阿里云物聯(lián)網(wǎng)平臺(tái)的設(shè)備通信與控制。
4、阿里云物聯(lián)網(wǎng)平臺(tái)
阿里云物聯(lián)網(wǎng)平臺(tái)的相關(guān)配置在這里就不和大家一一重復(fù)了,創(chuàng)建產(chǎn)品、設(shè)備、屬性等過(guò)程大家可以參考官方文檔或視頻,主要需要提前準(zhǔn)備的數(shù)據(jù)有阿里云物聯(lián)網(wǎng)平臺(tái)設(shè)備的MQTT連接參數(shù)、MQTT發(fā)布訂閱主題、設(shè)備屬性等,參考如下:
--根據(jù)自己阿里云物聯(lián)網(wǎng)平臺(tái)的配置修改以下參數(shù),下列參數(shù)僅作參考
local client_id = "a1ZR8uuCkfP.air780e_test|securemode=2,signmethod=hmacsha256,timestamp=1681048410186|"
local user_name = "air780e_test&a1ZR8u41341"
local password = "9b075c78b1a600065d28af9000www.funiot.xyz000www.funiot.xyz000"
local mqtt_host = "a1ZR8uuCkfP.iot-as-mqtt.cn-shanghai.aliyuncs.com"
local mqtt_port = 1883
訂閱主題并設(shè)置設(shè)備屬性:
local devdata_topic="/sys/a1ZR8uuCkfP/air780e_test/thing/event/property/post" --訂閱屬性上報(bào)主題
local cmdrec_topic="/sys/a1ZR8uuCkfP/air780e_test/thing/service/property/set" --訂閱屬性設(shè)置主題
local dev_control="thing.service.property.set" --訂閱屬性控制下發(fā)主題
local command_name="LED_Control" --控制命令
二、完整開(kāi)發(fā)流程
1. 下載軟件包與編譯燒錄工具
1) LuatOS軟件包:Air780e使用LuatOS-SoC@EC618
下載鏈接:LuatOS-SoC@EC618 V1103
2) 調(diào)試與燒錄工具:Luatools
下載鏈接:luatools調(diào)試與燒錄工具
2. 編寫luatos腳本
-- LuaTools需要PROJECT和VERSION這兩個(gè)信息
PROJECT = "led"
VERSION = "1.0.0"
-- 引入必要的庫(kù)文件(lua編寫), 內(nèi)部庫(kù)不需要require
sys = require("sys")
log.info("main", "led")
print(_VERSION)
if wdt then
--添加硬狗防止程序卡死,在支持的設(shè)備上啟用這個(gè)功能
wdt.init(9000)--初始化watchdog設(shè)置為9s
sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
end
--用戶代碼開(kāi)始---------------------------------------------------
--根據(jù)自己阿里云物聯(lián)網(wǎng)平臺(tái)的配置修改以下參數(shù),下列參數(shù)僅作參考
local mqtt_host = "a1ZR8uuCkfP.iot-as-mqtt.cn-shanghai.aliyuncs.com"
local mqtt_port = 1883
local mqtt_isssl = false
local client_id = "a1ZR8uuCkfP.air780e_test|securemode=2,signmethod=hmacsha256,timestamp=1681048410186|"
local user_name = "air780e_test&a1ZR81132134"
local password = "9b075c78b1a600065d28afa0d3b42www.funiot.xy&&www.funiot.xyz"
local mqtt_aliyun = nil
local devdata_topic="/sys/a1ZR8uuCkfP/air780e_test/thing/event/property/post" --訂閱屬性上報(bào)主題
local cmdrec_topic="/sys/a1ZR8uuCkfP/air780e_test/thing/service/property/set" --訂閱屬性設(shè)置主題
local dev_control="thing.service.property.set" --訂閱屬性控制下發(fā)主題
local command_name="LED_Control" --控制命令
local LED_PIN=27 --LED引腳編號(hào)
gpio.setup(LED_PIN,0, gpio.PULLUP) --設(shè)置LED上拉輸出
sys.taskInit(function()
print("connected to aliyun example\r\n")
while 1 do
--網(wǎng)絡(luò)相關(guān)
mobile.simid(2)
LED = gpio.setup(27, 0, gpio.PULLUP)
device_id = mobile.imei()
sys.waitUntil("IP_READY", 30000)
--mqtt客戶端創(chuàng)建
mqtt_aliyun = mqtt.create(nil,mqtt_host, mqtt_port, mqtt_isssl, ca_file)
mqtt_aliyun:auth(client_id,user_name,password)
mqtt_aliyun:keepalive(60) -- 默認(rèn)值240s
mqtt_aliyun:autoreconn(true, 3000) -- 自動(dòng)重連機(jī)制
--注冊(cè)mqtt回調(diào)
mqtt_aliyun:on(function(mqtt_client, event, data, payload)
-- 用戶自定義代碼
log.info("mqtt", "event", event, mqtt_client, data, payload)
if event == "conack" then --連接響應(yīng)成功
sys.publish("mqtt_conack")--訂閱主題
mqtt_client:subscribe(pub_devdata_topic)
mqtt_client:subscribe(pub_cmdrec_topic)
elseif event == "recv" then
log.info("mqtt", "downlink", "topic", data, "payload", payload)
print("payload:",payload)
--解析json
--例如:
--{"method":"thing.service.property.set","id":"273481693","params":{"LED_Control":1},"version":"1.0.0"}
local mycmd=json.decode(payload)
if mycmd then -- 若解碼失敗, 會(huì)返回nil
print("method :",mycmd["method"])
print("params is",mycmd["params"])
print("params->LED_Control is",mycmd["params"]["LED_Control"])
if mycmd["method"]==dev_control then
if mycmd["params"]["LED_Control"]==1 then
print("led turn on")
gpio.set(LED_PIN, gpio.HIGH)
elseif mycmd["params"]["LED_Control"]==0 then
print("led turn off")
gpio.set(LED_PIN, gpio.LOW)
end
end
end
elseif event == "sent" then
log.info("mqtt", "sent", "pkgid", data)
-- elseif event == "disconnect" then
-- 非自動(dòng)重連時(shí),按需重啟mqtt_aliyun
-- mqtt_client:connect()
end
end)
--連接mqtt
mqtt_aliyun:connect()
sys.waitUntil("mqtt_conack")
while true do
-- mqtt_aliyun自動(dòng)處理重連
local ret, topic, data, qos = sys.waitUntil("mqtt_pub", 30000)
if ret then
if topic == "close" then break end
mqtt_aliyun:publish(topic, data, qos)
end
end
mqtt_aliyun:close()
mqtt_aliyun = nil
end
end)
--定時(shí)上報(bào)屬性
sys.taskInit(function()
local topic = devdata_topic --上報(bào)的topic
local temp=0 --溫度屬性值
local data = "{\"method\":\"thing.service.property.set\",\"params\":{\"IndoorTemperature\":"..tostring(temp).."}}"
local qos = 1
local temp=0
while true do
sys.wait(5000)
if mqtt_aliyun and mqtt_aliyun:ready() then
-- mqtt_aliyun:subscribe(topic)
local pkgid = mqtt_aliyun:publish(topic, data, qos)
temp=temp+1
data = "{\"method\":\"thing.service.property.set\",\"params\":{\"IndoorTemperature\":"..tostring(temp).."}}"
print(data)
-- 也可以通過(guò)sys.publish發(fā)布到指定task去
-- sys.publish("mqtt_pub", topic, data, qos)
end
end
end)
-- 用戶代碼已結(jié)束---------------------------------------------
-- 結(jié)尾總是這一句
sys.run()
-- sys.run()之后后面不要加任何語(yǔ)句!!!!!
3. 編譯燒錄
打開(kāi)Luatools,點(diǎn)擊項(xiàng)目管理按鈕,選擇下載好的固件(后綴名為soc的文件)和編寫好的main.lua文件,勾選USB BOOT下載,點(diǎn)擊下載底層和腳本按鈕下載即可,具體操作流程如下圖所示:
4. 運(yùn)行結(jié)果
【注】命令下發(fā)通過(guò)設(shè)置設(shè)備屬性實(shí)現(xiàn),如下圖
命令接收的同時(shí)開(kāi)發(fā)板的LED燈會(huì)根據(jù)發(fā)送的對(duì)應(yīng)命令完成開(kāi)關(guān)亮滅
6.其他相關(guān)參考文章
【stm32+AT指令+ESP8266接入華為云物聯(lián)網(wǎng)平臺(tái)并完成屬性上報(bào)與下發(fā)的命令處理】文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-464064.html
【esp8266接入華為云物聯(lián)網(wǎng)平臺(tái)完成屬性上報(bào)、命令處理】
【合宙Air780e+luatos接入華為云物聯(lián)網(wǎng)平臺(tái)完成設(shè)備通信與控制】文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-464064.html
到了這里,關(guān)于合宙Air780e+luatos+阿里云物聯(lián)網(wǎng)平臺(tái)完成設(shè)備通信與控制(屬性上報(bào)+4G遠(yuǎn)程點(diǎn)燈)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!