欢迎光临散文网 会员登陆 & 注册

Luat Air780E/Air700E获取设备经纬度后使用MQTT协议发送至巴法云

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

来自星星笔记(cxing.top):链接先左键选中再右键点击打开(直接复制打开可能打不开)➡️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

    --添加硬狗防止程序卡死,在支持的设备上启用这个功能

    wdt.init(9000)--初始化watchdog设置为9s

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

end


local uartid = 2 -- 根据实际设备选取不同的uartid


--初始化

local result = uart.setup(

    uartid,--串口id

    9600,--波特率

    8,--数据位

    1--停止位

)




-- 收取数据会触发回调, 这里的"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) -- 转换为时分秒格式

                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 = "" -- 清空缓存

        -- 如果数据非空,则发布到MQTT服务器

        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(通用异步收发器)数据,解析 GPS 信息,并将其通过 MQTT 发布到服务器。以下是代码的详细解释:


1. 首先导入所需的模块:

```lua

_G.sys = require("sys")

_G.sysplus = require("sysplus")

```


2. 定义 MQTT 服务器相关参数:

```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 数据:

```lua

local uart2_data = ""

```


4. 对支持的设备启用硬狗防止程序卡死功能:

```lua

if wdt then

    wdt.init(9000)

    sys.timerLoopStart(wdt.feed, 3000)

end

```


5. 根据实际设备选取不同的 UART ID:

```lua

local uartid = 2

```


6. 初始化 UART:

```lua

local result = uart.setup(

    uartid,

    9600,

    8,

    1

)

```


7. 设置 UART 接收数据时的回调函数,用于解析收到的 GPS 数据:

```lua

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

    -- 处理收到的数据,提取 GPS 信息

    -- ...

end)

```


8. 初始化一个任务,用于连接 MQTT 服务器并定时发布 UART2 数据:

```lua

sys.taskInit(function()

    -- 如果设备为 AIR780E,设置一些参数

    -- ...

    

    -- 显示 MQTT 服务器的发布和订阅主题

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

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


    -- 创建 MQTT 客户端,并设置参数

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

    mqttc:auth(client_id, nil, nil)

    mqttc:autoreconn(true, 3000)


    -- 设置 MQTT 客户端的回调函数

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

        -- 处理 MQTT 事件,例如连接成功、接收到消息、发送消息成功等

        -- ...

    end)


    -- 连接 MQTT 服务器

    mqttc:connect()

    sys.waitUntil("mqtt_conack")


    -- 循环检查 UART2 缓存数据,并发布到 MQTT 服务器

    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. 最后执行 `sys.run()` 运行整个程序。


Luat Air780E/Air700E获取设备经纬度后使用MQTT协议发送至巴法云的评论 (共 条)

分享到微博请遵守国家法律