前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >13-编写WIFI模块连接MQTT程序,和调试助手测试通信

13-编写WIFI模块连接MQTT程序,和调试助手测试通信

作者头像
杨奉武
发布2018-12-12 16:12:48
2.2K0
发布2018-12-12 16:12:48
举报
文章被收录于专栏:知识分享知识分享

  直接上程序吧

代码语言:javascript
复制
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

代码语言:javascript
复制
--[[
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)

 自己的程序都写出模板来了,后期的所有程序都是基于这个模板来做的。。。先睡觉

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-11-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档