lua mqtt
PROJECT = "wx_lbs"
VERSION = "1.0.0"
_G.sys = require("sys")
_G.sysplus = require("sysplus")
local mqtt_host = "122.114.225.56"
local mqtt_port = 1884
local mqtt_isssl = false
local client_id = "abc"
local user_name = "admin"
local password = "123456"
local mqttc = nil
sys.taskInit(function()
local device_id = mcu.unique_id():toHex()
if wlan and wlan.connect then
local ssid = "TheGang"
local password = "jing&&2020"
log.info("wifi", ssid, password)
wlan.init()
wlan.setMode(wlan.STATION)
device_id = wlan.getMac()
wlan.connect(ssid, password, 1)
elseif mobile then
device_id = mobile.imei()
elseif w5500 then
w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00)
w5500.config()
w5500.bind(socket.ETH0)
elseif socket or mqtt then
else
while 1 do
sys.wait(1000)
log.info("bsp", "本bsp可能未适配网络层, 请查证")
end
end
sys.waitUntil("IP_READY")
sys.publish("net_ready", device_id)
end)
sys.taskInit(function()
local ret, device_id = sys.waitUntil("net_ready")
client_id = device_id
log.info("mqtt", "pub", pub_topic)
log.info("mqtt", "sub", sub_topic)
if mqtt == nil then
while 1 do
sys.wait(1000)
log.info("bsp", "本bsp未适配mqtt库, 请查证")
end
end
mqttc = mqtt.create(nil, mqtt_host, mqtt_port, mqtt_isssl, ca_file)
mqttc:auth(client_id,user_name,password)
mqttc:autoreconn(true, 3000)
mqttc:on(function(mqtt_client, event, data, payload)
log.info("mqtt", "event", event, mqtt_client, data, payload)
if event == "conack" then
sys.publish("mqtt_conack")
mqtt_client:subscribe(sub_topic)
elseif event == "recv" then
log.info("mqtt", "downlink", "topic", data, "payload", payload)
sys.publish("mqtt_payload", data, payload)
elseif event == "sent" then
end
end)
mqttc:connect()
sys.waitUntil("mqtt_conack")
while true do
local ret, topic, data, qos = sys.waitUntil("mqtt_pub", 300000)
if ret then
if topic == "close" then break end
mqttc:publish(topic, data, qos)
end
end
mqttc:close()
mqttc = nil
end)
sys.taskInit(function()
sys.wait(3000)
local data = "123,"
local qos = 1
while true do
sys.wait(30)
if mqttc and mqttc:ready() then
local pkgid = mqttc:publish(pub_topic, data .. os.date(), qos)
end
end
end)
local uart_id = 1
uart.setup(uart_id, 9600)
uart.on(uart_id, "receive", function(id, len)
local data = ""
while 1 do
local tmp = uart.read(uart_id)
if not tmp or #tmp == 0 then
break
end
data = data .. tmp
end
log.info("uart", "uart收到数据长度", #data)
sys.publish("mqtt_pub", pub_topic, data)
end)
sys.subscribe("mqtt_payload", function(topic, payload)
log.info("uart", "uart发送数据长度", #payload)
uart.write(1, payload)
end)
sys.taskInit(function ()
while true do
sys.wait(3000)
log.info("lua", rtos.meminfo())
log.info("sys", rtos.meminfo("sys"))
end
end)
sys.run()
MQTT与自建服务器通信成功例子
PROJECT = "wx_lbs"
VERSION = "1.0.0"
-- sys库是标配
_G.sys = require("sys")
--[[特别注意, 使用mqtt库需要下列语句]]
_G.sysplus = require("sysplus")
-- 自动低功耗, 轻休眠模式
-- Air780E支持uart唤醒和网络数据下发唤醒, 但需要断开USB,或者pm.power(pm.USB, false) 但这样也看不到日志了
-- pm.request(pm.LIGHT)
--根据自己的服务器修改以下参数
local mqtt_host = "122.114.225.56"
local mqtt_port = 1883
local mqtt_isssl = false
local client_id = "AIR780E001"
local user_name = "admin"
local password = "123456"
local pub_topic = "client_send"
local sub_topic = "service_send"
sys.taskInit(function()
if rtos.bsp() == "AIR780E" then
device_id = mobile.imei()
sys.waitUntil("IP_READY", 30000)
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, user_name, password)
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 = json.encode({action = "getMessage",msg = "hello world"})
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()