|
|
@@ -32,45 +32,56 @@ local sub_topic = "/luatos/sub/" .. (mcu.unique_id():toHex())
|
|
|
|
|
|
local mqttc = nil
|
|
|
|
|
|
+-- 统一联网函数
|
|
|
sys.taskInit(function()
|
|
|
+ local device_id = mcu.unique_id():toHex()
|
|
|
-----------------------------
|
|
|
-- 统一联网函数, 可自行删减
|
|
|
----------------------------
|
|
|
- if rtos.bsp():startsWith("ESP32") then
|
|
|
+ if wlan and wlan.connect then
|
|
|
-- wifi 联网, ESP32系列均支持
|
|
|
local ssid = "uiot"
|
|
|
local password = "12345678"
|
|
|
log.info("wifi", ssid, password)
|
|
|
- -- TODO 改成esptouch配网
|
|
|
+ -- TODO 改成自动配网
|
|
|
-- LED = gpio.setup(12, 0, gpio.PULLUP)
|
|
|
wlan.init()
|
|
|
- wlan.setMode(wlan.STATION)
|
|
|
+ wlan.setMode(wlan.STATION) -- 默认也是这个模式,不调用也可以
|
|
|
+ device_id = wlan.getMac():toHex()
|
|
|
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())
|
|
|
- client_id = wlan.getMac():toHex()
|
|
|
- elseif rtos.bsp() == "AIR105" then
|
|
|
+ elseif mobile then
|
|
|
+ -- Air780E/Air600E系列
|
|
|
+ --mobile.simid(2) -- 自动切换SIM卡
|
|
|
+ -- LED = gpio.setup(27, 0, gpio.PULLUP)
|
|
|
+ device_id = mobile.imei()
|
|
|
+ elseif w5500 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)
|
|
|
- sys.wait(1000)
|
|
|
- pub_topic = "/luatos/pub/" .. (w5500.getMac():toHex())
|
|
|
- sub_topic = "/luatos/sub/" .. (w5500.getMac():toHex())
|
|
|
- elseif rtos.bsp() == "EC618" then
|
|
|
- -- Air780E/Air600E系列
|
|
|
- --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())
|
|
|
- client_id = mobile.imei()
|
|
|
+ elseif socket or mqtt then
|
|
|
+ -- 适配的socket库也OK
|
|
|
+ -- 没有其他操作, 单纯给个注释说明
|
|
|
+ else
|
|
|
+ -- 其他不认识的bsp, 循环提示一下吧
|
|
|
+ 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")
|
|
|
+ -- 下面的是mqtt的参数均可自行修改
|
|
|
+ client_id = device_id
|
|
|
+ pub_topic = "/luatos/pub/" .. device_id
|
|
|
+ sub_topic = "/luatos/sub/" .. device_id
|
|
|
|
|
|
-- 打印一下上报(pub)和下发(sub)的topic名称
|
|
|
-- 上报: 设备 ---> 服务器
|
|
|
@@ -83,12 +94,18 @@ sys.taskInit(function()
|
|
|
-- if crypto.cipher_suites then
|
|
|
-- log.info("cipher", "suites", json.encode(crypto.cipher_suites()))
|
|
|
-- end
|
|
|
+ if mqtt == nil then
|
|
|
+ while 1 do
|
|
|
+ sys.wait(1000)
|
|
|
+ log.info("bsp", "本bsp未适配mqtt库, 请查证")
|
|
|
+ end
|
|
|
+ end
|
|
|
|
|
|
-------------------------------------
|
|
|
-------- MQTT 演示代码 --------------
|
|
|
-------------------------------------
|
|
|
|
|
|
- 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:keepalive(240) -- 默认值240s
|
|
|
@@ -98,6 +115,7 @@ sys.taskInit(function()
|
|
|
-- 用户自定义代码
|
|
|
log.info("mqtt", "event", event, mqtt_client, data, payload)
|
|
|
if event == "conack" then
|
|
|
+ -- 联上了
|
|
|
sys.publish("mqtt_conack")
|
|
|
mqtt_client:subscribe(sub_topic)--单主题订阅
|
|
|
-- mqtt_client:subscribe({[topic1]=1,[topic2]=1,[topic3]=1})--多主题订阅
|
|
|
@@ -112,15 +130,19 @@ sys.taskInit(function()
|
|
|
end
|
|
|
end)
|
|
|
|
|
|
+ -- mqttc自动处理重连, 除非自行关闭
|
|
|
mqttc:connect()
|
|
|
sys.waitUntil("mqtt_conack")
|
|
|
while true do
|
|
|
- -- mqttc自动处理重连
|
|
|
+ -- 演示等待其他task发送过来的上报信息
|
|
|
local ret, topic, data, qos = sys.waitUntil("mqtt_pub", 300000)
|
|
|
if ret then
|
|
|
+ -- 提供关闭本while循环的途径, 不需要可以注释掉
|
|
|
if topic == "close" then break end
|
|
|
mqttc:publish(topic, data, qos)
|
|
|
end
|
|
|
+ -- 如果没有其他task上报, 可以写个空等待
|
|
|
+ --sys.wait(60000000)
|
|
|
end
|
|
|
mqttc:close()
|
|
|
mqttc = nil
|