|
|
@@ -21,9 +21,10 @@ local client_id = "abc"
|
|
|
local user_name = "user"
|
|
|
local password = "password"
|
|
|
|
|
|
-local topic1 = "/luatos/1"
|
|
|
-local topic2 = "/luatos/2"
|
|
|
-local topic3 = "/luatos/3"
|
|
|
+local pub_topic = "/luatos/pub/" .. (mcu.unique_id():toHex())
|
|
|
+local sub_topic = "/luatos/sub/" .. (mcu.unique_id():toHex())
|
|
|
+-- local topic2 = "/luatos/2"
|
|
|
+-- local topic3 = "/luatos/3"
|
|
|
|
|
|
local mqttc = nil
|
|
|
|
|
|
@@ -37,33 +38,45 @@ sys.taskInit(function()
|
|
|
local password = "12345678"
|
|
|
log.info("wifi", ssid, password)
|
|
|
-- TODO 改成esptouch配网
|
|
|
- LED = gpio.setup(12, 0, gpio.PULLUP)
|
|
|
+ -- LED = gpio.setup(12, 0, gpio.PULLUP)
|
|
|
wlan.init()
|
|
|
wlan.setMode(wlan.STATION)
|
|
|
wlan.connect(ssid, password, 1)
|
|
|
local result, data = sys.waitUntil("IP_READY", 30000)
|
|
|
log.info("wlan", "IP_READY", result, data)
|
|
|
device_id = wlan.getMac()
|
|
|
+ pub_topic = "/luatos/pub/" .. (wlan.getMac():toHex())
|
|
|
+ sub_topic = "/luatos/sub/" .. (wlan.getMac():toHex())
|
|
|
elseif rtos.bsp() == "AIR105" then
|
|
|
-- w5500 以太网, 当前仅Air105支持
|
|
|
w5500.init(spi.HSPI_0, 24000000, pin.PC14, pin.PC01, pin.PC00)
|
|
|
w5500.config() --默认是DHCP模式
|
|
|
w5500.bind(socket.ETH0)
|
|
|
- LED = gpio.setup(62, 0, gpio.PULLUP)
|
|
|
+ -- LED = gpio.setup(62, 0, gpio.PULLUP)
|
|
|
sys.wait(1000)
|
|
|
- -- TODO 获取mac地址作为device_id
|
|
|
+ pub_topic = "/luatos/pub/" .. (w5500.getMac():toHex())
|
|
|
+ sub_topic = "/luatos/sub/" .. (w5500.getMac():toHex())
|
|
|
elseif rtos.bsp() == "EC618" then
|
|
|
-- Air780E/Air600E系列
|
|
|
- --mobile.simid(2)
|
|
|
- LED = gpio.setup(27, 0, gpio.PULLUP)
|
|
|
+ --mobile.simid(2) -- 自动切换SIM卡
|
|
|
+ -- LED = gpio.setup(27, 0, gpio.PULLUP)
|
|
|
device_id = mobile.imei()
|
|
|
sys.waitUntil("IP_READY", 30000)
|
|
|
+ pub_topic = "/luatos/pub/" .. (mobile.imei())
|
|
|
+ sub_topic = "/luatos/sub/" .. (mobile.imei())
|
|
|
end
|
|
|
|
|
|
+ -- 打印一下上报(pub)和下发(sub)的topic名称
|
|
|
+ -- 上报: 设备 ---> 服务器
|
|
|
+ -- 下发: 设备 <--- 服务器
|
|
|
+ -- 可使用mqtt.x等客户端进行调试
|
|
|
+ log.info("mqtt", "pub", pub_topic)
|
|
|
+ log.info("mqtt", "sub", sub_topic)
|
|
|
+
|
|
|
-- 打印一下支持的加密套件, 通常来说, 固件已包含常见的99%的加密套件
|
|
|
- if crypto.cipher_suites then
|
|
|
- log.info("cipher", "suites", json.encode(crypto.cipher_suites()))
|
|
|
- end
|
|
|
+ -- if crypto.cipher_suites then
|
|
|
+ -- log.info("cipher", "suites", json.encode(crypto.cipher_suites()))
|
|
|
+ -- end
|
|
|
|
|
|
-------------------------------------
|
|
|
-------- MQTT 演示代码 --------------
|
|
|
@@ -80,8 +93,8 @@ sys.taskInit(function()
|
|
|
log.info("mqtt", "event", event, mqtt_client, data, payload)
|
|
|
if event == "conack" then
|
|
|
sys.publish("mqtt_conack")
|
|
|
- -- mqtt_client:subscribe(topic1)--单主题订阅
|
|
|
- mqtt_client:subscribe({[topic1]=1,[topic2]=1,[topic3]=1})--多主题订阅
|
|
|
+ mqtt_client:subscribe(sub_topic)--单主题订阅
|
|
|
+ -- mqtt_client:subscribe({[topic1]=1,[topic2]=1,[topic3]=1})--多主题订阅
|
|
|
elseif event == "recv" then
|
|
|
log.info("mqtt", "downlink", "topic", data, "payload", payload)
|
|
|
elseif event == "sent" then
|
|
|
@@ -106,18 +119,16 @@ sys.taskInit(function()
|
|
|
mqttc = nil
|
|
|
end)
|
|
|
|
|
|
+-- 这里演示在另一个task里上报数据
|
|
|
sys.taskInit(function()
|
|
|
- local data = "123"
|
|
|
- local qos = 1
|
|
|
+ local data = "123,"
|
|
|
+ local qos = 1 -- QOS0不带puback, QOS1是带puback的
|
|
|
while true do
|
|
|
sys.wait(5000)
|
|
|
if mqttc and mqttc:ready() then
|
|
|
- -- mqttc:subscribe(topic)
|
|
|
- local pkgid = mqttc:publish(topic1, data, qos)
|
|
|
- local pkgid = mqttc:publish(topic2, data, qos)
|
|
|
- local pkgid = mqttc:publish(topic3, data, qos)
|
|
|
- -- 也可以通过sys.publish发布到指定task去
|
|
|
- -- sys.publish("mqtt_pub", topic, data, qos)
|
|
|
+ local pkgid = mqttc:publish(pub_topic, data .. os.date(), qos)
|
|
|
+ -- local pkgid = mqttc:publish(topic2, data, qos)
|
|
|
+ -- local pkgid = mqttc:publish(topic3, data, qos)
|
|
|
end
|
|
|
end
|
|
|
end)
|