直接上程序吧
local SubscribeTopic = "wifi/user".."/"..clientid
PublishTopic = "wifi/device".."/"..clientid
local UsartReceiveData="";
local UsartReceiveDataCnt=0;
local UsartReceiveDataCntCopy=0;
local globalSendData=""
local MqttConnectTimer = tmr.create()
Mymqtt = mqtt.Client(clientid,3,"yang", "11223344");
Mymqtt:lwt(PublishTopic, "{".."state:".."offline".."}", 1, 1)
Mymqtt:on("offline", function(client)
print("connect offline")
MqttConnectTimer:start();
if PubilcTimerState==5 then
PubilcTimerState=0;
end
end)
local function subscribeSuccess(client)
print("subscribe success")
end
local function ConnectSuccess(client)
print("connected")
MqttConnectTimer:stop();
gpio.write(IndicateLed, 0)
MqttConnectedFlage = true;
Mymqtt:subscribe(SubscribeTopic, 0,subscribeSuccess)
PubilcTimerState = 5;
end
local function ConnectFailed(client,reason)
MqttConnectedFlage = false;
MqttConnectTimer:start();
gpio.write(IndicateLed, 0)
print("failed reason: " .. reason)
if PubilcTimerState==5 then
PubilcTimerState=0;
end
end
MqttConnectTimer:register(5000, 1, function()
if ConnectApFalge == true then
if Mymqtt ~= nil then
Mymqtt:close()
end
Mymqtt:connect("47.93.14.37", 1883, 0,ConnectSuccess,ConnectFailed)
end
end)
MqttConnectTimer:start();
Mymqtt:on("message", function(client, topic, data)
uart.write(0,data)
end)
local DataDisposeTimer = tmr.create()
DataDisposeTimer:register(10, 1, function()
if UsartReceiveDataCnt ~= 0 then
DataDisposeTimerCnt=DataDisposeTimerCnt + 1;
if DataDisposeTimerCnt>=10 then
UsartReceiveDataCnt = 0;
DataDisposeTimerCnt=0;
globalSendData = UsartReceiveData;
UsartReceiveData="";
end
end
if Mymqtt ~= nil and MqttConnectedFlage == true then
if globalSendData ~= "nil" then
Mymqtt:publish(PublishTopic,globalSendData, 0, 0, function(client)
end)
globalSendData = "nil"
end
end
end)
DataDisposeTimer:start()
uart.setup(0, 115200, 8, uart.PARITY_NONE, uart.STOPBITS_1)
uart.on("data",0,function(data)
UsartReceiveData = UsartReceiveData..data;
UsartReceiveDataCnt = UsartReceiveDataCnt + 1;
DataDisposeTimerCnt = 0;
end,0)
上面的程序是透传程序---MQTT接收的数据直接转发到串口,串口接收的直接转发给MQTT服务器
其它说明:其实MQTT规定每一个设备都必须有个唯一的码,在连接的时候给MQTT服务器,我用的WIFI模块的MAC地址作为这个码
MQTT服务器检测设备如果3S(根据自己的设备)*1.5 倍 时间没有接收到设备发送的心跳包,就会默认设备掉线了
自己设置的掉线信息,让其它设备知道设备掉线了,一般都是硬件设备设置遗嘱,APP或者上位机通过这条信息知道设备掉线了
再看init.lua
--[[
GPIO0--3
GPIO1--10
GPIO2--4
GPIO3--9
GPIO4--2
GPIO5--1
GPIO9--11
GPIO10--12
GPIO12--6
GPIO13--7
GPIO14--5
GPIO15--8
GPIO16--0
0--GPIO16 1--GPIO5 2--GPIO4 3--GPIO0 4--GPIO2
5--GPIO14 6--GPIO12 7--GPIO13 8--GPIO15 9--GPIO3
10--GPIO1 11--GPIO9 12--GPIO10
]]
---------------------------------------split-----------------------------------
splitStart = 1;
splitIdex=0;
splitTable={};
function split(s, delim)
if type(delim) ~= "string" or string.len(delim) <= 0 then
return
end
splitStart = 1
splitTable = {}
while true do
splitIdex = string.find (s, delim, splitStart, true) -- plain find
if not splitIdex then
break
end
table.insert (splitTable, string.sub (s, splitStart, splitIdex - 1))
splitStart = splitIdex + string.len (delim)
end
table.insert (splitTable, string.sub (s, splitStart))
return splitTable
end
--------------------Config Pin---------------------------
local InputKeyPin = 3;
IndicateLed = 4;
gpio.mode(InputKeyPin, gpio.OUTPUT)
gpio.write(InputKeyPin, 1)
gpio.mode(IndicateLed, gpio.OUTPUT)
gpio.write(IndicateLed, 0)
node.stripdebug(3)
--------------------public.lua---------------------------
local i=0;
local PubilcTimerOut=0;
local PubilcTimerCnt = 0;
PubilcTimerState = 0;
local PubilcTimer = tmr.create()
PubilcTimer:register(100, 1, function()
if gpio.read(InputKeyPin) == 0 then
PubilcTimerCnt = PubilcTimerCnt + 1;
if PubilcTimerCnt == 30 then
PubilcTimerState = 1;
PubilcTimerOut=0;
print("startsmart")
wifi.setmode(wifi.STATION)
wifi.stopsmart()
wifi.startsmart(0,function(ssid, password)
print(string.format("Success. SSID:%s ; PASSWORD:%s", ssid, password))
PubilcTimerOut=0;
PubilcTimerState=2;
end)
end
print("PubilcTimerState="..PubilcTimerState)
else
PubilcTimerCnt=0;
end
if PubilcTimerState==1 then
PubilcTimerOut=PubilcTimerOut+1;
if PubilcTimerOut == 600 then node.restart();end
gpio.write(IndicateLed, 1-gpio.read(IndicateLed));
elseif PubilcTimerState ==2 then
PubilcTimerOut=PubilcTimerOut+1;
if PubilcTimerOut==50 then node.restart();end
elseif PubilcTimerState ==5 then
i=i+1;
if i>10 then
i=0;
gpio.write(IndicateLed, 1-gpio.read(IndicateLed));
end
end
end)
PubilcTimer:start()
--------------------Variable---------------------------
MqttConnectedFlage = false;
clientid = wifi.sta.getmac()
print("MAC: "..clientid)
--------------------Rst MCU---------------------------
local InputKeyCnt = 0
local InputKeyDownFlage = false
local function InputKeyTrig()
if InputKeyDownFlage==false then
InputKeyDownFlage=true
InputKeyCnt = tmr.now()
gpio.trig(InputKeyPin,"high",InputKeyTrig)
else
InputKeyDownFlage = false;
if tmr.now() - InputKeyCnt <1000000 and (PubilcTimerState==5 or PubilcTimerState==0) then
node.restart();
end
gpio.trig(InputKeyPin,"low",InputKeyTrig)
end
end
gpio.trig(InputKeyPin,"low",InputKeyTrig)
if file.open("mqttconfig.lua", "r") then
file.close();
dofile("mqttconfig.lua")
end
wifi.setmode(wifi.STATIONAP)
ConnectApFalge = false;
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
ConnectApFalge = false;
MqttConnectedFlage = false;
end)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
if ConnectApFalge == false then
print(wifi.sta.getip())
end
ConnectApFalge = true;
end)
自己的程序都写出模板来了,后期的所有程序都是基于这个模板来做的。。。先睡觉