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

Luat Air780E/Air700E使用MQTT协议接入巴法云物联网平台

2023-04-01 00:23 作者:TOP_SC  | 我要投稿

来自星星笔记(cxing.top):链接➡️https://cxing.top/archives/33.html

###### 程序源码

1、先来代码:

```lua

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 = "****/set"

local sub_topic = "****"


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 = "OFF"

        if math.random(0, 1) == 1 then

            data = "ON"

        end

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

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

    end


    mqttc:disconnect()

    mqttc:close()

    mqttc = nil

end)


sys.run()

```

2、需要修改的地方:

```lua

local mqtt_host = "bemfa.com"

local mqtt_port = 9501

local mqtt_isssl = false

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

local pub_topic = "****/set"

local sub_topic = "****"

```

(1)把 `client_id` 换成巴法云MQTT接入的私钥。

 ![巴法云mqtt私钥](https://png.cxing.top/i/2023/03/31/12yt1uo.png) 

(2)把 `pub_topic` 换成自己的mqtt主题,之所以加 `/set`是为了防止自己接收到自己推送的信息。具体可以参考 [巴法云MQTT接入协议](https://cloud.bemfa.com/docs/#/?id=_43-mqtt%e8%ae%be%e5%a4%87%e4%ba%91) 。

(3)把 `sub_topic` 换成自己想要订阅的主题。

###### 源码解释

1、这段程序使用 Lua 编写,主要功能是连接 MQTT 服务器并定期向指定的主题(topic)发布消息。以下是程序的具体运行流程:


2、首先定义了一些常量和变量,包括项目名 PROJECT、版本号 VERSION、MQTT 服务器的地址、端口号、是否启用 SSL 等信息。其中 client_id 是一个唯一的客户端标识符,用于连接 MQTT 服务器时进行认证。pub_topic 和 sub_topic 分别是发布和订阅的主题。


3、创建一个任务,在该任务中连接 MQTT 服务器,并定期向指定的主题发布消息。如果设备是 AIR780E,则使用设备的 IMEI 作为客户端标识符,且发布和订阅的主题改为 "mqtt"。


4、在任务中,首先等待 IP_READY 事件发生(只有 AIR780E 才需要等待),等待时间为 30 秒。然后打印发布和订阅的主题。


5、创建一个 MQTT 客户端 mqttc,并设置认证信息、自动重连等属性。然后注册一个回调函数,处理 MQTT 事件,包括连接成功(conack)、接收消息(recv)和发送消息(sent)。


6、连接 MQTT 服务器,等待连接成功的事件 mqtt_conack 发生,然后订阅指定的主题。


7、进入一个无限循环,每隔 5 秒向指定的主题发布一条消息,消息内容为 "ON" 或 "OFF"。使用 mqttc:publish 方法发布消息,并打印发布的主题和消息内容。


8、循环结束后,断开 MQTT 连接,并关闭客户端。


 总体来说,这段程序的功能是连接 MQTT 服务器并定期向指定的主题发布消息。如果设备是 AIR780E,则使用设备的 IMEI 作为客户端标识符,且发布和订阅的主题改为 "mqtt"。程序的运行流程包括连接 MQTT 服务器、注册回调函数、订阅主题、发布消息等。

###### 程序烧录

 使用Luatools烧录程序,具体烧录教程看官网教程: [LuaTools 上手教程-看这篇就够了](https://doc.openluat.com/article/1719/0?from=luatools) 

 ![luatools烧录截图](https://png.cxing.top/i/2023/04/01/7zieq.png) 

{message type="success" content="注:此教程适用于Air780E和Air700E,其他板子和其他MQTT服务器可参考修改。"/}

###### 部分引用:

> 巴法云物联网平台:https://cloud.bemfa.com/

> Air700E资料:https://air700e.cn/

Luat Air780E/Air700E使用MQTT协议接入巴法云物联网平台的评论 (共 条)

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