|
@@ -13,6 +13,10 @@ _G.sys = require("sys")
|
|
|
--[[特别注意, 使用mqtt库需要下列语句]]
|
|
--[[特别注意, 使用mqtt库需要下列语句]]
|
|
|
_G.sysplus = require("sysplus")
|
|
_G.sysplus = require("sysplus")
|
|
|
|
|
|
|
|
|
|
+-- 自动低功耗, 轻休眠模式
|
|
|
|
|
+-- Air780E支持uart唤醒和网络数据下发唤醒, 但需要断开USB,或者pm.power(pm.USB, false) 但这样也看不到日志了
|
|
|
|
|
+-- pm.request(pm.LIGHT)
|
|
|
|
|
+
|
|
|
--根据自己的服务器修改以下参数
|
|
--根据自己的服务器修改以下参数
|
|
|
local mqtt_host = "lbsmqtt.airm2m.com"
|
|
local mqtt_host = "lbsmqtt.airm2m.com"
|
|
|
local mqtt_port = 1884
|
|
local mqtt_port = 1884
|
|
@@ -47,6 +51,7 @@ sys.taskInit(function()
|
|
|
device_id = wlan.getMac()
|
|
device_id = wlan.getMac()
|
|
|
pub_topic = "/luatos/pub/" .. (wlan.getMac():toHex())
|
|
pub_topic = "/luatos/pub/" .. (wlan.getMac():toHex())
|
|
|
sub_topic = "/luatos/sub/" .. (wlan.getMac():toHex())
|
|
sub_topic = "/luatos/sub/" .. (wlan.getMac():toHex())
|
|
|
|
|
+ client_id = wlan.getMac():toHex()
|
|
|
elseif rtos.bsp() == "AIR105" then
|
|
elseif rtos.bsp() == "AIR105" then
|
|
|
-- w5500 以太网, 当前仅Air105支持
|
|
-- w5500 以太网, 当前仅Air105支持
|
|
|
w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00)
|
|
w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00)
|
|
@@ -64,6 +69,7 @@ sys.taskInit(function()
|
|
|
sys.waitUntil("IP_READY", 30000)
|
|
sys.waitUntil("IP_READY", 30000)
|
|
|
pub_topic = "/luatos/pub/" .. (mobile.imei())
|
|
pub_topic = "/luatos/pub/" .. (mobile.imei())
|
|
|
sub_topic = "/luatos/sub/" .. (mobile.imei())
|
|
sub_topic = "/luatos/sub/" .. (mobile.imei())
|
|
|
|
|
+ client_id = mobile.imei()
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
-- 打印一下上报(pub)和下发(sub)的topic名称
|
|
-- 打印一下上报(pub)和下发(sub)的topic名称
|
|
@@ -85,7 +91,7 @@ sys.taskInit(function()
|
|
|
mqttc = mqtt.create(nil,mqtt_host, mqtt_port, mqtt_isssl, ca_file)
|
|
mqttc = mqtt.create(nil,mqtt_host, mqtt_port, mqtt_isssl, ca_file)
|
|
|
|
|
|
|
|
mqttc:auth(client_id,user_name,password) -- client_id必填,其余选填
|
|
mqttc:auth(client_id,user_name,password) -- client_id必填,其余选填
|
|
|
- mqttc:keepalive(30) -- 默认值240s
|
|
|
|
|
|
|
+ -- mqttc:keepalive(240) -- 默认值240s
|
|
|
mqttc:autoreconn(true, 3000) -- 自动重连机制
|
|
mqttc:autoreconn(true, 3000) -- 自动重连机制
|
|
|
|
|
|
|
|
mqttc:on(function(mqtt_client, event, data, payload)
|
|
mqttc:on(function(mqtt_client, event, data, payload)
|
|
@@ -97,6 +103,7 @@ sys.taskInit(function()
|
|
|
-- mqtt_client:subscribe({[topic1]=1,[topic2]=1,[topic3]=1})--多主题订阅
|
|
-- mqtt_client:subscribe({[topic1]=1,[topic2]=1,[topic3]=1})--多主题订阅
|
|
|
elseif event == "recv" then
|
|
elseif event == "recv" then
|
|
|
log.info("mqtt", "downlink", "topic", data, "payload", payload)
|
|
log.info("mqtt", "downlink", "topic", data, "payload", payload)
|
|
|
|
|
+ sys.publish("mqtt_payload", data, payload)
|
|
|
elseif event == "sent" then
|
|
elseif event == "sent" then
|
|
|
log.info("mqtt", "sent", "pkgid", data)
|
|
log.info("mqtt", "sent", "pkgid", data)
|
|
|
-- elseif event == "disconnect" then
|
|
-- elseif event == "disconnect" then
|
|
@@ -109,7 +116,7 @@ sys.taskInit(function()
|
|
|
sys.waitUntil("mqtt_conack")
|
|
sys.waitUntil("mqtt_conack")
|
|
|
while true do
|
|
while true do
|
|
|
-- mqttc自动处理重连
|
|
-- mqttc自动处理重连
|
|
|
- local ret, topic, data, qos = sys.waitUntil("mqtt_pub", 30000)
|
|
|
|
|
|
|
+ local ret, topic, data, qos = sys.waitUntil("mqtt_pub", 300000)
|
|
|
if ret then
|
|
if ret then
|
|
|
if topic == "close" then break end
|
|
if topic == "close" then break end
|
|
|
mqttc:publish(topic, data, qos)
|
|
mqttc:publish(topic, data, qos)
|
|
@@ -119,12 +126,12 @@ sys.taskInit(function()
|
|
|
mqttc = nil
|
|
mqttc = nil
|
|
|
end)
|
|
end)
|
|
|
|
|
|
|
|
--- 这里演示在另一个task里上报数据
|
|
|
|
|
|
|
+-- 这里演示在另一个task里上报数据, 会定时上报数据,不需要就注释掉
|
|
|
sys.taskInit(function()
|
|
sys.taskInit(function()
|
|
|
local data = "123,"
|
|
local data = "123,"
|
|
|
local qos = 1 -- QOS0不带puback, QOS1是带puback的
|
|
local qos = 1 -- QOS0不带puback, QOS1是带puback的
|
|
|
while true do
|
|
while true do
|
|
|
- sys.wait(5000)
|
|
|
|
|
|
|
+ sys.wait(60000)
|
|
|
if mqttc and mqttc:ready() then
|
|
if mqttc and mqttc:ready() then
|
|
|
local pkgid = mqttc:publish(pub_topic, data .. os.date(), qos)
|
|
local pkgid = mqttc:publish(pub_topic, data .. os.date(), qos)
|
|
|
-- local pkgid = mqttc:publish(topic2, data, qos)
|
|
-- local pkgid = mqttc:publish(topic2, data, qos)
|
|
@@ -133,6 +140,26 @@ sys.taskInit(function()
|
|
|
end
|
|
end
|
|
|
end)
|
|
end)
|
|
|
|
|
|
|
|
|
|
+-- 以下是演示与uart结合, 简单的mqtt-uart透传实现,不需要就注释掉
|
|
|
|
|
+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)
|
|
|
|
|
+
|
|
|
|
|
|
|
|
-- 用户代码已结束---------------------------------------------
|
|
-- 用户代码已结束---------------------------------------------
|
|
|
-- 结尾总是这一句
|
|
-- 结尾总是这一句
|