最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Luat Air780E/Air700E獲取設(shè)備經(jīng)緯度后使用MQTT協(xié)議發(fā)送至巴法云

2023-04-21 10:57 作者:TOP_SC  | 我要投稿

來自星星筆記(cxing.top):鏈接先左鍵選中再右鍵點擊打開(直接復(fù)制打開可能打不開)??https://cxing.top/archives/35.html

先來程序

PROJECT = "mqttdemo"

VERSION = "1.0.0"

_G.sys = require("sys")

_G.sysplus = require("sysplus")


local mqtt_host = "bemfa.com"

local mqtt_port = 9501

local mqtt_isssl = false

local client_id = "********"

local pub_topic = "mqtt/set"

local sub_topic = "mqtt"


local uart2_data = ""



if wdt then

? ? --添加硬狗防止程序卡死,在支持的設(shè)備上啟用這個功能

? ? wdt.init(9000)--初始化watchdog設(shè)置為9s

? ? sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗

end


local uartid = 2 -- 根據(jù)實際設(shè)備選取不同的uartid


--初始化

local result = uart.setup(

? ? uartid,--串口id

? ? 9600,--波特率

? ? 8,--數(shù)據(jù)位

? ? 1--停止位

)




-- 收取數(shù)據(jù)會觸發(fā)回調(diào), 這里的"receive" 是固定值

uart.on(uartid, "receive", function(id, len)

? ? local s = ""

? ? repeat

? ? ? ? -- 如果是air302, len不可信, 傳1024

? ? ? ? -- s = uart.read(id, 1024)

? ? ? ? s = uart.read(id, len)

? ? ? ? if #s > 0 then -- #s 是取字符串的長度

? ? ? ? ? ? -- log.info("uart", "receive", id, #s, s)

? ? ? ? ? ? local utc_time, lat, lat_ns, lon, lon_ew = string.match(s, "%$GNGGA,(%d+%.%d+),(%d+%.%d+),(%a),(%d+%.%d+),(%a),")

? ? ? ? ? ? if utc_time and lat and lat_ns and lon and lon_ew then

? ? ? ? ? ? ? ? log.info("uart", "UTC Time", utc_time)

? ? ? ? ? ? ? ? local hour = tonumber(utc_time:sub(1,2))

? ? ? ? ? ? ? ? local minute = tonumber(utc_time:sub(3,4))

? ? ? ? ? ? ? ? local second = tonumber(utc_time:sub(5,6))

? ? ? ? ? ? ? ? hour = hour +8

? ? ? ? ? ? ? ? if hour >=24 then

? ? ? ? ? ? ? ? ? ? hour = hour -24

? ? ? ? ? ? ? ? end

? ? ? ? ? ? ? ? local time_str = string.format("%02d:%02d:%02d", hour, minute, second) -- 轉(zhuǎn)換為時分秒格式

? ? ? ? ? ? ? ? log.info("uart", "Beijing Time", time_str)


? ? ? ? ? ? ? ? local lat_deg = math.floor(tonumber(lat) / 100)

? ? ? ? ? ? ? ? local lat_min = tonumber(lat) % 100

? ? ? ? ? ? ? ? local lat_wgs84 = lat_deg + lat_min / 60

? ? ? ? ? ? ? ? if lat_ns == "S" then

? ? ? ? ? ? ? ? ? ? lat_wgs84 = -lat_wgs84

? ? ? ? ? ? ? ? end

? ? ? ? ? ? ? ? log.info("uart", "Latitude (WGS84)", lat_wgs84)


? ? ? ? ? ? ? ? local lon_deg = math.floor(tonumber(lon) / 100)

? ? ? ? ? ? ? ? local lon_min = tonumber(lon) % 100

? ? ? ? ? ? ? ? local lon_wgs84 = lon_deg + lon_min / 60

? ? ? ? ? ? ? ? if lon_ew == "W" then

? ? ? ? ? ? ? ? ? ? lon_wgs84 = -lon_wgs84

? ? ? ? ? ? ? ? end

? ? ? ? ? ? ? ? log.info("uart", "Longitude (WGS84)", lon_wgs84)


? ? ? ? ? ? ? ? uart2_data = "#" .. time_str .. "#" .. "12".. "#" .. "24" .. "#" .. tostring(lat_wgs84) .. "#" .. tostring(lon_wgs84)

? ? ? ? ? ? end

? ? ? ? end

? ? ? ? if #s == len then

? ? ? ? ? ? break

? ? ? ? end

? ? until s == ""

end)



sys.taskInit(function()

? ? if rtos.bsp() == "AIR780E" then

? ? ? ? device_id = mobile.imei()

? ? ? ? sys.waitUntil("IP_READY", 30000)

? ? ? ? pub_topic = "mqtt"

? ? ? ? sub_topic = "mqtt"

? ? end


? ? log.info("mqtt", "pub", pub_topic)

? ? log.info("mqtt", "sub", sub_topic)


? ? local mqttc = mqtt.create(nil, mqtt_host, mqtt_port, mqtt_isssl, nil)


? ? mqttc:auth(client_id, nil, nil)

? ? mqttc:autoreconn(true, 3000)


? ? mqttc:on(function(mqtt_client, event, data, payload)

? ? ? ? if event == "conack" then

? ? ? ? ? ? sys.publish("mqtt_conack")

? ? ? ? ? ? mqtt_client:subscribe(sub_topic)

? ? ? ? elseif event == "recv" then

? ? ? ? ? ? log.info("mqtt", "received", "topic", data, "payload", payload)

? ? ? ? elseif event == "sent" then

? ? ? ? ? ? log.info("mqtt", "sent", "pkgid", data)

? ? ? ? end

? ? end)


? ? mqttc:connect()

? ? sys.waitUntil("mqtt_conack")


? ? while true do

? ? ? ? sys.wait(5000)

? ? ? ? local data = uart2_data

? ? ? ? uart2_data = "" -- 清空緩存

? ? ? ? -- 如果數(shù)據(jù)非空,則發(fā)布到MQTT服務(wù)器

? ? ? ? if data ~= "" then

? ? ? ? ? ? local pkgid = mqttc:publish(pub_topic, data, 1)

? ? ? ? ? ? log.info("mqtt", "published", pkgid, pub_topic, data)

? ? ? ? end

? ? end


? ? mqttc:disconnect()

? ? mqttc:close()

? ? mqttc = nil

end)

sys.run()


代碼解釋

這段代碼是一個基于 Lua 語言的 MQTT 客戶端示例,用于接收 UART(通用異步收發(fā)器)數(shù)據(jù),解析 GPS 信息,并將其通過 MQTT 發(fā)布到服務(wù)器。以下是代碼的詳細解釋:


1. 首先導(dǎo)入所需的模塊:

```lua

_G.sys = require("sys")

_G.sysplus = require("sysplus")

```


2. 定義 MQTT 服務(wù)器相關(guān)參數(shù):

```lua

local mqtt_host = "bemfa.com"

local mqtt_port = 9501

local mqtt_isssl = false

local client_id = "********"

local pub_topic = "mqtt/set"

local sub_topic = "mqtt"

```


3. 初始化一個空字符串用于存儲 UART2 數(shù)據(jù):

```lua

local uart2_data = ""

```


4. 對支持的設(shè)備啟用硬狗防止程序卡死功能:

```lua

if wdt then

? ? wdt.init(9000)

? ? sys.timerLoopStart(wdt.feed, 3000)

end

```


5. 根據(jù)實際設(shè)備選取不同的 UART ID:

```lua

local uartid = 2

```


6. 初始化 UART:

```lua

local result = uart.setup(

? ? uartid,

? ? 9600,

? ? 8,

? ? 1

)

```


7. 設(shè)置 UART 接收數(shù)據(jù)時的回調(diào)函數(shù),用于解析收到的 GPS 數(shù)據(jù):

```lua

uart.on(uartid, "receive", function(id, len)

? ? -- 處理收到的數(shù)據(jù),提取 GPS 信息

? ? -- ...

end)

```


8. 初始化一個任務(wù),用于連接 MQTT 服務(wù)器并定時發(fā)布 UART2 數(shù)據(jù):

```lua

sys.taskInit(function()

? ? -- 如果設(shè)備為 AIR780E,設(shè)置一些參數(shù)

? ? -- ...

? ??

? ? -- 顯示 MQTT 服務(wù)器的發(fā)布和訂閱主題

? ? log.info("mqtt", "pub", pub_topic)

? ? log.info("mqtt", "sub", sub_topic)


? ? -- 創(chuàng)建 MQTT 客戶端,并設(shè)置參數(shù)

? ? local mqttc = mqtt.create(nil, mqtt_host, mqtt_port, mqtt_isssl, nil)

? ? mqttc:auth(client_id, nil, nil)

? ? mqttc:autoreconn(true, 3000)


? ? -- 設(shè)置 MQTT 客戶端的回調(diào)函數(shù)

? ? mqttc:on(function(mqtt_client, event, data, payload)

? ? ? ? -- 處理 MQTT 事件,例如連接成功、接收到消息、發(fā)送消息成功等

? ? ? ? -- ...

? ? end)


? ? -- 連接 MQTT 服務(wù)器

? ? mqttc:connect()

? ? sys.waitUntil("mqtt_conack")


? ? -- 循環(huán)檢查 UART2 緩存數(shù)據(jù),并發(fā)布到 MQTT 服務(wù)器

? ? while true do

? ? ? ? sys.wait(5000)

? ? ? ? local data = uart2_data

? ? ? ? uart2_data = ""

? ? ? ? if data ~= "" then

? ? ? ? ? ? local pkgid = mqttc:publish(pub_topic, data, 1)

? ? ? ? ? ? log.info("mqtt", "published", pkgid, pub_topic, data)

? ? ? ? end

? ? end


? ? -- 斷開 MQTT 連接并釋放資源

? ? mqttc:disconnect()

? ? mqttc:close()

? ? mqttc = nil

end)

```


9. 最后執(zhí)行 `sys.run()` 運行整個程序。


Luat Air780E/Air700E獲取設(shè)備經(jīng)緯度后使用MQTT協(xié)議發(fā)送至巴法云的評論 (共 條)

分享到微博請遵守國家法律
灵宝市| 建德市| 吉安市| 海盐县| 安福县| 色达县| 松桃| 鱼台县| 林州市| 沐川县| 宜兰市| 新野县| 万载县| 北宁市| 平邑县| 依安县| 观塘区| 竹溪县| 芦山县| 邛崃市| 铁岭市| 德安县| 罗定市| 瑞金市| 炉霍县| 天峻县| 黔南| 高尔夫| 延吉市| 旌德县| 马龙县| 长治县| 梅州市| 当阳市| 利川市| 沙洋县| 孟州市| 济宁市| 奉化市| 西乡县| 南康市|