1. 程式人生 > >13-編寫WIFI模組連線MQTT程式,和除錯助手測試通訊

13-編寫WIFI模組連線MQTT程式,和除錯助手測試通訊

  直接上程式吧

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)

 

 

 

 

 

 

 

 

 

    

 

 

 自己的程式都寫出模板來了,後期的所有程式都是基於這個模板來做的。。。先睡覺