3-51单片机WIFI学习(开发板8266底层源码介绍)

时间:2023-03-09 01:32:44
3-51单片机WIFI学习(开发板8266底层源码介绍)

上一篇链接  http://www.cnblogs.com/yangfengwu/p/8743502.html

直接上源码:注意源码有两部分,第一部分是一开始的时候写在模块内部的,另一部分是存在手机内部的,方便后期更新功能

看第一部分的源码

亲们可以直接把下面的第一部分(注意只下载第一部分就行)的程序下载到8266里面就可以用自己的APP实现远程控制

怎么下载看我以前的文章,LUA开发的

3-51单片机WIFI学习(开发板8266底层源码介绍)

--[[
GPIO0--
GPIO1--
GPIO2--
GPIO3--
GPIO4--
GPIO5--
GPIO9--
GPIO10--
GPIO12--
GPIO13--
GPIO14--
GPIO15--
GPIO16--
--GPIO16   --GPIO5   --GPIO4   --GPIO0   --GPIO2
--GPIO14   --GPIO12  --GPIO13  --GPIO15  --GPIO3
--GPIO1   --GPIO9  --GPIO10
]]

RelayPin=;

splitStart =
splitIdex=;
splitTable={}
function split(s, delim)
     then
        return
    end
    splitStart =
    splitTable = {}
    while true do
        splitIdex = string.find (s, delim, splitStart, true) -- plain find
        if not splitIdex then
          break
        end
        table.insert (splitTable, ))
        splitStart = splitIdex + string.len (delim)
    end
    table.insert (splitTable, string.sub (s, splitStart))

    return splitTable
end

wifi.setmode(wifi.STATION)

local updataFlage = 

YouInitData1 = "NULL";
YouInitData2 = "NULL";
YouInitData3 = "NULL";
YouInitData4 = "NULL";

MyInitData1 = "";
MyInitData2 = "";
MyInitData3 = "";
MyInitData4 = "";

NodeMcuTimeInSecond = ;
NodeMcuTimeInUSecond = ;
NodeMcuTimeInrate = ;
NodeMcuYear = ;
NodeMcuMonth = ;
NodeMcuDay= ;
NodeMcuHour= ;
NodeMcuMinute= ;
NodeMcuSecond= ;
NodeMcuWeekday= ;
NodeMcuRtctime={};
NodeMcuTimer={};
NodeMcuTimerList={};
NodeMcuDataString="nil"
NodeMcuTimeString="nil";
NodeMcuWeekdayString="nil";

mqttConnectedFlage = ;
NodeMcuRequestTime=

sntpAddress="nil";

updataCnt = ;
linkcnt=;
printtimeCnt=;
TCPReadDataOverTime = ;

tmr.softwd()--watchdog 60S

clientid = wifi.sta.getmac()
SubscribeTopic = "yang/sw/"..clientid
PublishTopic = "feng/sw/"..clientid

if  file.open("InitData.lua", "r") then
    local MyInitData = file.read();
    local DataList = split(MyInitData, ';');
    ] == "MyData" then
        MyInitData1 = DataList[]
        MyInitData2 = DataList[]
        MyInitData3 = DataList[]
        MyInitData4 = DataList[]
    end
else
    print("no InitData")
end

print("Topic = switch/"..clientid)
print("MyInitData1 = "..MyInitData1)
print("MyInitData2 = "..MyInitData2)
print("MyInitData3 = "..MyInitData3)
print("MyInitData4 = "..MyInitData4)

--[[Connect the phone to the ap]]
apcfg={}
apcfg.ssid="qqqqqq"
apcfg.pwd="
wifi.sta.config(apcfg)
wifi.sta.connect()
--wifi.sta.autoconnect()

--[[The connection serve phone]]
connectIP="192.168.1.102"
ClientConnectedFlage = 

ReceiveData = "";
ReceiveDataCopy = "";
UpdataDataCopy="";
ReceiveDataCnt = ;
ReceiveDataCntCopy = ;

function InitTimer0function()
     then
        Client = net.createConnection(net.TCP, )
        --Client:connect(,connectIP)
        Client:connect(,gateway)

        Client:on("receive", function(Client, data)
            ReceiveData = ReceiveData..data;
            ReceiveDataCnt = ReceiveDataCnt + ;
        end)
        Client:on("connection", function(sck, c)
            ClientConnectedFlage =
            TcpConnect = Client
            print("Link OK")
            tmr.stop()
            Client:on("disconnection", function(sck, c)
                ClientConnectedFlage =
                TcpConnect = nil
                tmr.start()
            end)
        end)

         then
            print("Link Error")
            updataCnt = ;
            updataFlage = ;
            linkcnt = linkcnt+
             then
                linkcnt =
                if  TcpConnect ~= nil then
                    TcpConnect:close()
                end
                tmr.stop()
                tmr.stop()
                dofile("updata.lua");
            end
        end
    end
end
tmr.alarm(, , , InitTimer0function)

function disposedata(data)
    local DataList = split(data, ';');
    ] == "MyData" then
        if  file.open("InitData.lua", "w+") then
            file.write(data)--MyInitData
            file.close()

            MyInitData1 = DataList[]
            MyInitData2 = DataList[]
            MyInitData3 = DataList[]
            MyInitData4 = DataList[]

            print(])
            print(])
            print(])
            print(])

            if  TcpConnect ~= nil then
                TcpConnect:send(data);
            end

            print("write InitData success")
        end
    elseif  DataList[] == "YouData" then
            if  TcpConnect ~= nil then
                TcpConnect:send("YouData;"..clientid..";"..SubscribeTopic..";"..PublishTopic..";"..YouInitData1..";"..
                                YouInitData2..";"..YouInitData3..";"..YouInitData4);
            end
            print("Send YouData success")
    elseif  DataList[] == "UpdataData" then
            updataFlage = ;
            if  TcpConnect ~= nil then
                TcpConnect:send("WaitUpdataData0;");
            end
            print("WaitUpdataData......")
            file.open("updata.lua", "w+")
    end
    ReceiveDataCopy = "";
end

function InitTimer1function()
    then
       if   ReceiveDataCnt == ReceiveDataCntCopy then
            TCPReadDataOverTime = ;

            ReceiveDataCopy = ReceiveData;
            ReceiveData = "";
            ReceiveDataCnt = ;
            ReceiveDataCntCopy = ;

             then
                disposedata(ReceiveDataCopy);
            else
                if  ReceiveDataCopy ~= "UpdataDataSuccess" then
                    file.write(ReceiveDataCopy)
                    updataCnt = updataCnt +
                    if  TcpConnect ~= nil then
                        TcpConnect:send("WaitUpdataData"..updataCnt..";");
                    end
                    print("start down...")
                else
                    if  TcpConnect ~= nil then
                        TcpConnect:send("UpdataDataSuccess");
                        TcpConnect:close()
                    end
                    file.close()
                    updataFlage =
                    print("UpdataDataSuccess")  

                    tmr.stop()
                    tmr.stop()
                    --dofile("main.lua");
                    node.compile("updata.lua")
                    dofile("updata.lua");
                    --dofile("main.lc");
                end
                ReceiveDataCopy = "";
            end
       else
            ReceiveDataCntCopy = ReceiveDataCnt
       end
   end
end
tmr.alarm(, , , InitTimer1function)

gpio.mode(,gpio.OUTPUT)--输出模式
tmr.alarm(, , , function()
     then
        gpio.write(,-gpio.read())
         then
            sntp.sync(sntpAddress,
            function()
                print("sync succeeded:"..sntpAddress)
                NodeMcuRequestTime = ;
            end,
            function(index)
                print("failed : "..index)
            end)
        end
    end

    NodeMcurRtctime = rtctime.epoch2cal(rtctime.)
    NodeMcuTimer = string.format("%04d;%02d;%02d;%02d;%02d;%02d;%01d",
                        NodeMcurRtctime["year"],
                        NodeMcurRtctime["mon"],
                        NodeMcurRtctime["day"],
                        NodeMcurRtctime[,
                        NodeMcurRtctime["min"],
                        NodeMcurRtctime["sec"],
                        NodeMcurRtctime["wday"])

    NodeMcuTimerList = split(NodeMcuTimer, ';');
    NodeMcuYear = tonumber(NodeMcuTimerList[]);
    NodeMcuMonth = tonumber(NodeMcuTimerList[]);
    NodeMcuDay = tonumber(NodeMcuTimerList[]);
    NodeMcuHour = tonumber(NodeMcuTimerList[]);
    NodeMcuMinute = tonumber(NodeMcuTimerList[]);
    NodeMcuSecond = tonumber(NodeMcuTimerList[]);
    NodeMcuWeekday = tonumber(NodeMcuTimerList[]);
    NodeMcuHour = NodeMcuHour - ;
    NodeMcuDataString=NodeMcuYear.."/"..NodeMcuMonth.."/"..NodeMcuDay;
    NodeMcuTimeString=NodeMcuHour..":"..NodeMcuMinute..":"..NodeMcuSecond;
    NodeMcuWeekdayString = NodeMcuWeekday.."";

    NodeMcuTimeInSecond,NodeMcuTimeInUSecond,NodeMcuTimeInrate= rtctime.get()

     and NodeMcuMinute ==  and NodeMcuSecond ==  then
        NodeMcuRequestTime = ;
    end
    printtimeCnt = printtimeCnt +;
     then
       printtimeCnt = ;
       print("Year="..NodeMcuYear..";Month="..NodeMcuMonth..";Day="..NodeMcuDay..";Hour="..
            NodeMcuHour..";Minute="..NodeMcuMinute..";Second="..NodeMcuSecond..";Weekday="..
            NodeMcuWeekdayString..";TimeInSecond="..NodeMcuTimeInSecond)
    end

    if  TcpConnect ~= nil then
        TCPReadDataOverTime = TCPReadDataOverTime +;
         then
           TCPReadDataOverTime = ;
           TcpConnect:close()
           dofile("updata.lua");
        end
    end

    tmr.softwd();
end)

printip =
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
    printip =
end)

wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
    then
      ip,netmask,gateway = wifi.sta.getip()
      print(ip)
   end
   printip =
end)

说一下源码

3-51单片机WIFI学习(开发板8266底层源码介绍)

3-51单片机WIFI学习(开发板8266底层源码介绍)

3-51单片机WIFI学习(开发板8266底层源码介绍)

3-51单片机WIFI学习(开发板8266底层源码介绍)

3-51单片机WIFI学习(开发板8266底层源码介绍)

关于SNTP获取网络时间的问题,用阿里的博客写的

https://yq.aliyun.com/articles/511601?spm=a2c4e.11155435.0.0.15873312ZAcLKJ

3-51单片机WIFI学习(开发板8266底层源码介绍)

直接把源码一贴,不好讲了,因为涉及的多...所以还是看后期自己一点一点的写源码吧

现在贴上第二部分源码,也就是存在手机上的,手机上就是这样存的,前面一点是接收用户配置的MQTT和WIFI的一些信息,所以做成这样子了

\n是换行  \" 的意思就是"  是因为特殊的字符在手机APP源码上要这样写

还有 \"&#037  其实就是 "%   也是因为特殊字符的问题

<string name="initupdata0">
        configwifissid = \"%1$s\";\n
        configwifipwd= \"%2$s\";\n
        MqttUserString = \"%3$s\";\n
        MqttPwdString = \"%4$s\";\n
        MqttIPString = \"%5$s\";\n
        MqttPort = %$d;\n

        if configwifipwd == \"nil\" then\n
           configwifipwd = nil\n
        end\n

        globalSendData =\"nil\"; \n
        globalSendData1=\"nil\";\n
        globalSendData2=\"nil\";\n

        MemoryRelayState = \"3\";

        regulartime={};\n
        regularoption={};\n
        regularrepetition={};\n
        DisposeOnceTime="nil";\n

        RegularTimeLabel=;\n
        RegularTimeData=\"0\";\n
        RegularOptionLabel=;\n
        RegularOptionData=\"0\";\n
        RegularRepetitionLabel=;\n
        RegularRepetitionData=\"0\";\n
        RegularCustom={}\n
        RegularCustomLabel=;\n
        RegularCustomData=\"0\";\n

        DelayData={};\n
        DelayTime={};\n
        DelayOption={};\n
        DelayUnixTime={};\n
        DelayunixTime=;\n
        DelayIndex=;\n

        DelayTimeLabel=;\n
        DelayTimeData=\"0\";\n
        DelayTimeDataNumber=;\n

        RelayNowState=\n
        RelayNowStateCopy=\n

        UsartReceiveData=\"\";
        UsartReceiveDataCnt=;
        UsartReceiveDataCntCopy=;

        , do\n
            if  file.open(\"regular=\"..i..\".lua\", \"r\") then\n
                local list = split(file.read(), \";\")\n
                for _, s in ipairs(list) do\n
                    local mlist = split(s, \"=\")\n
                    ],\"time\") then\n
                        regulartime[i] = mlist[];print(regulartime[i])\n
                    elseif  ],\"option\") then \n
                        regularoption[i] = mlist[];print(regularoption[i])\n
                    elseif  ],\"repetition\") then \n
                        regularrepetition[i] = mlist[];print(regularrepetition[i])\n
                    end\n
                end\n
                file.close() \n
            end\n
            if  file.open(\"delay=\"..i..\".lua\", \"r\") then\n
                local list = split(file.read(), \";\")\n
                for _, s in ipairs(list) do\n
                   local mlist = split(s, \"=\")\n
                   ],\"data\") then\n
                       DelayData[i] = mlist[];print(DelayData[i])\n
                   elseif  ],\"time\") then\n
                       DelayTime[i] = mlist[];print(DelayTime[i])\n
                   elseif  ],\"option\") then \n
                        DelayOption[i] = mlist[];print(DelayOption[i])  \n
                   elseif  ],\"unixTime\") then \n
                        DelayUnixTime[i] = mlist[];print(DelayUnixTime[i])  \n
                   end     \n
                end\n
                file.close() \n
            end\n

        end\n

        if  file.open(\"initpower.lua\",\"r\") then\n

        end\n

        </string>

    <string name="initupdata0_0" formatted="false">
        if  file.open(\"switch=1.lua\", \"r\") then\n
            if  file.read() == \"relay=1\" then\n
                gpio.write(RelayPin,)\n
                gpio.mode(RelayPin,gpio.OUTPUT)\n
                RelayNowState=\n
                RelayNowStateCopy=\n
                print(\"relay=1\")\n
            end\n
            if  file.read() == \"relay=0\" then\n
                gpio.write(RelayPin,)\n
                gpio.mode(RelayPin,gpio.OUTPUT)\n
                RelayNowState=\n
                RelayNowStateCopy=\n
                print(\"relay=0\")\n
            end\n
            file.close() \n
        else\n
            print(\"open RelayState.lua faild\")  \n
        end\n

        apcfg={}\n
        apcfg.ssid=configwifissid\n
        apcfg.pwd=configwifipwd\n
        wifi.sta.config(apcfg)\n
        --wifi.sta.connect()\n
        wifi.sta.autoconnect()\n

        Mymqtt = mqtt.Client(clientid, ,MqttUserString, MqttPwdString);\n

        tmr.alarm(, , , function()\n
            Mymqtt:connect(MqttIPString, MqttPort, ,ConnectSuccess,ConnectFailed)\n
        end)\n

        function ConnectSuccess(client)\n
             client:subscribe(SubscribeTopic, , subscribeSuccess)\n
             print(\"connected\")\n
             mqttClient = client;\n
             tmr.stop();\n
             if sntpAddress == \"nil\"  then \n
                sntpAddress=\"ntp2.aliyun.com\"; \n
             end \n
             mqttConnectedFlage = ;\n
        end\n
        function ConnectFailed(client,reason)\n
           mqttConnectedFlage = ;\n
           print(\"failed reason: \" .. reason)\n
           tmr.start()\n
        end\n
        function subscribeSuccess(client)\n
            print(\"subscribe success\") \n
        end\n
        </string>

       <string name="initupdata0_1" formatted="false">
            Mymqtt:on(\"message\", function(client, topic, data) \n
        local list = split(data, \";\")\n
        ControlRelayFlage = -;\n
        uart.write(,data)\n
        ],\"regular\") ~= nil then\n
            ]..\".lua\", \"w+\") then\n
                file.write(data)\n
                file.close() \n
                for _, s in ipairs(list) do\n
                    local mlist = split(s, \"=\")\n
                    local aa = ],\"%d\"); \n
                    ],\"time\") then\n
                        regulartime[tonumber(aa)] = mlist[];\n
                    elseif  ],\"option\") then \n
                        regularoption[tonumber(aa)] = mlist[];\n
                    elseif  ],\"repetition\") then \n
                        regularrepetition[tonumber(aa)] = mlist[];\n
                    end\n
                end\n
                globalSendData1 = data\n
            end
        elseif ],\"delay\") ~= nil then\n
            ]..\".lua\", \"w+\") then\n
                file.write(data)\n
                file.close() \n
                for _, s in ipairs(list) do\n
                   local mlist = split(s, \"=\")\n
                   local aa = ],\"%d\");\n
                   ],\"data\") then\n
                       DelayIndex = tonumber(aa);\n
                       DelayData[tonumber(aa)] = mlist[];\n
                   elseif  ],\"time\") then\n
                       DelayTime[tonumber(aa)] = mlist[];\n
                   elseif  ],\"option\") then \n
                        DelayOption[tonumber(aa)] = mlist[]; \n
                   elseif  ],\"unixTime\") then \n
                        DelayUnixTime[tonumber(aa)] = mlist[];\n
                        DelayunixTime = tonumber(mlist[]);\n
                   end \n
                end\n
                ; NodeMcuTimeInSecond and DelayunixTime ~=  and DelayunixTime ~=  then\n
                    globalSendData1 = \"delayerror=\"..DelayIndex..\";data\"..DelayIndex..\"=\"..\n
                    NodeMcuDataString..\";\"..\"time\"..DelayIndex..\"=\"..NodeMcuTimeString\n
                else\n
                    globalSendData1 = data;\n
                end\n
                DelayunixTime=; \n

            end
        elseif ],\"switch\") ~= nil then \n
            ]..\".lua\", \"w+\") then \n
                file.write(list[]) \n
                file.close() \n
                for _, s in ipairs(list) do \n
                    if  string.find(s,\"relay=0\") ~= nil then \n
                        gpio.write(RelayPin,) \n
                        gpio.mode(RelayPin,gpio.OUTPUT) \n
                        globalSendData1 = data \n
                    end\n
                    if  string.find(s,\"relay=1\") ~= nil then \n
                        gpio.write(RelayPin,) \n
                        gpio.mode(RelayPin,gpio.OUTPUT) \n
                        globalSendData1 = data \n
                    end\n
                end\n
            end  \n
        elseif ],\"initpower\") ~= nil then\n
            ]..\".lua\", \"w+\") then\n
                file.write(list[])\n
                file.close() \n
                globalSendData1 = data\n
            end    \n
        elseif ],\"SWStatus\") ~= nil then \n
               globalSendData1 = \"switch=1;relay=\"..RelayNowState \n
        end \n
    end)    \n 

         </string>

       <string name="initupdata0_2" formatted="false">

tmr.alarm(, , , function() \n
     then \n
        RelayNowState = gpio.read(RelayPin) \n
        if  RelayNowState ~= RelayNowStateCopy then \n
            RelayNowStateCopy = RelayNowState \n
            globalSendData1 = \"switch=1;relay=\"..RelayNowState \n
        end \n 

        if  globalSendData1~=\"nil\" then \n
            globalSendData=globalSendData1; \n
            globalSendData1=\"nil\" \n
        elseif globalSendData2 ~=\"nil\" then \n
            globalSendData=globalSendData2; \n
            globalSendData2=\"nil\" \n
        else \n
            globalSendData=\"nil\";     \n
        end \n
        if  globalSendData ~= \"nil\" then \n
            mqttClient:publish(PublishTopic,globalSendData, , , function(client)  \n
            end) \n
        end \n
    end       \n 

     then  \n
       if UsartReceiveDataCntCopy == UsartReceiveDataCnt then  \n
          UsartReceiveDataCnt=;  \n
          UsartReceiveDataCntCopy = ;  \n
          globalSendData2 = UsartReceiveData;  \n
          UsartReceiveData=\"\";  \n
       else  \n
          UsartReceiveDataCntCopy = UsartReceiveDataCnt;  \n
       end  \n
    end  \n
end) \n 

uart.setup(, , , uart.PARITY_NONE, uart.STOPBITS_1)
uart.on(\"data\",0,function(data)
    UsartReceiveData = UsartReceiveData..data;
    UsartReceiveDataCnt = UsartReceiveDataCnt + ;
end, )

tmr.alarm(, , , function()  \n
    for  RegularTimeLabel, RegularTimeData in pairs (regulartime)  do \n
         if  RegularTimeData == NodeMcuTimeString  and DisposeOnceTime ~= NodeMcuTimeString then \n
             DisposeOnceTime = NodeMcuTimeString; \n
             if regularrepetition[RegularTimeLabel] ==  \"everyday\" then \n
                print(\"everyday\") \n
                DisposeData(RegularTimeLabel); \n
             elseif  regularrepetition[RegularTimeLabel] ==  \"work\" then  \n
                if  NodeMcuWeekdayString==\"2\" or NodeMcuWeekdayString==\"3\" or NodeMcuWeekdayString==\"4\" or NodeMcuWeekdayString==\"5\" or NodeMcuWeekdayString==\"6\" then
                    print(\"work\") \n
                    DisposeData(RegularTimeLabel); \n
                 end \n
             else \n
                 RegularCustom={} \n
                 RegularCustom = split(regularrepetition[RegularTimeLabel], \",\");    \n
                 for RegularCustomLabel, RegularCustomData in ipairs (RegularCustom)  do \n
                     if RegularCustomData == NodeMcuWeekdayString then \n
                        print(\"custom\") \n
                        DisposeData(RegularTimeLabel); \n
                     end \n
                 end  \n
             end \n
         else \n
            negationOnceTime=\"nil\";    \n
         end \n
    end \n 

    for  DelayTimeLabel, DelayTimeData in pairs (DelayUnixTime)  do \n
         --DelayTimeDataNumber = tonumber(DelayTimeData); \n
         if DelayTimeData == NodeMcuTimeInSecond..\"\" then \n
            if  DelayOption[DelayTimeLabel] == \"on\" then \n
                gpio.write(RelayPin,) \n
                gpio.mode(RelayPin,gpio.OUTPUT) \n
                print(\"on\") \n
             elseif  DelayOption[DelayTimeLabel] == \"off\" then \n
                gpio.write(RelayPin,) \n
                gpio.mode(RelayPin,gpio.OUTPUT)  \n
                print(\"off\") \n
             elseif  DelayOption[DelayTimeLabel] == \"negation\" then \n
                gpio.write(RelayPin,-gpio.read(RelayPin)) \n
                gpio.mode(RelayPin,gpio.OUTPUT)     \n
                print(\"negation\") \n
             end \n
         end \n
    end \n
end) \n 

function DisposeData(label) \n
   if  regularoption[label] == \"on\" then \n
       gpio.write(RelayPin,) \n
       gpio.mode(RelayPin,gpio.OUTPUT) \n
       print(\"on\") \n
   elseif  regularoption[label] == \"off\" then \n
       gpio.write(RelayPin,) \n
       gpio.mode(RelayPin,gpio.OUTPUT)  \n
       print(\"off\") \n
   elseif  regularoption[label] == \"negation\" then \n
       gpio.write(RelayPin,-gpio.read(RelayPin)) \n
       gpio.mode(RelayPin,gpio.OUTPUT)     \n
       print(\"negation\") \n
   end \n
   print(\"pin=\"..gpio.read(RelayPin))  \n
end \n 

           </string>

上面的不方便看,给大家一个直接可以下载用的

前面是路由器的和MQTT的信息,屏蔽了

--configwifissid = "";
--configwifipwd="";
--MqttUserString = "";
--MqttPwdString = "";
--MqttIPString = "";
--MqttPort = ;

if configwifipwd == "nil" then
   configwifipwd = nil
end

globalSendData ="nil";
globalSendData1="nil";
globalSendData2="nil";

MemoryRelayState = ";

regulartime={};
regularoption={};
regularrepetition={};
DisposeOnceTime="";

RegularTimeLabel=;
RegularTimeData="";
RegularOptionLabel=;
RegularOptionData="";
RegularRepetitionLabel=;
RegularRepetitionData="";
RegularCustom={}
RegularCustomLabel=;
RegularCustomData="";

DelayData={};
DelayTime={};
DelayOption={};
DelayUnixTime={};
DelayunixTime=;
DelayIndex=;

DelayTimeLabel=;
DelayTimeData="";
DelayTimeDataNumber=;

RelayNowState=
RelayNowStateCopy=

UsartReceiveData="";
UsartReceiveDataCnt=;
UsartReceiveDataCntCopy=;

, do
    if  file.open("regular="..i..".lua", "r") then
        local list = split(file.read(), ';')
        for _, s in ipairs(list) do
            local mlist = split(s, '=')
            ],"time") then
                regulartime[i] = mlist[];print(regulartime[i])
            elseif  ],"option") then
                regularoption[i] = mlist[];print(regularoption[i])
            elseif  ],"repetition") then
                regularrepetition[i] = mlist[];print(regularrepetition[i])
            end
        end
        file.close()
    end
    if  file.open("delay="..i..".lua", "r") then
        local list = split(file.read(), ';')
        for _, s in ipairs(list) do
           local mlist = split(s, '=')
           ],"data") then
               DelayData[i] = mlist[];print(DelayData[i])
           elseif  ],"time") then
               DelayTime[i] = mlist[];print(DelayTime[i])
           elseif  ],"option") then
                DelayOption[i] = mlist[];print(DelayOption[i])
           elseif  ],"unixTime") then
                DelayUnixTime[i] = mlist[];print(DelayUnixTime[i])
           end
        end
        file.close()
    end

end

if  file.open("initpower.lua","r") then

end

if  file.open("switch=1.lua", "r") then
    if  file.read() == "relay=1" then
        gpio.write(RelayPin,)
        gpio.mode(RelayPin,gpio.OUTPUT)
        RelayNowState=
        RelayNowStateCopy=
        print("relay=1")
    end
    if  file.read() == "relay=0" then
        gpio.write(RelayPin,)
        gpio.mode(RelayPin,gpio.OUTPUT)
        RelayNowState=
        RelayNowStateCopy=
        print("relay=0")
    end
    file.close()
else
    print("open RelayState.lua faild")
end

apcfg={}
apcfg.ssid=configwifissid
apcfg.pwd=configwifipwd
wifi.sta.config(apcfg)
--wifi.sta.connect()
wifi.sta.autoconnect()

Mymqtt = mqtt.Client(clientid, ,MqttUserString, MqttPwdString);

tmr.alarm(, , , function()
    Mymqtt:connect(MqttIPString, MqttPort, ,ConnectSuccess,ConnectFailed)
end)

function ConnectSuccess(client)
     client:subscribe(SubscribeTopic, , subscribeSuccess)
     print("connected")
     mqttClient = client;
     tmr.stop();
     if sntpAddress == "nil"  then
        sntpAddress="ntp2.aliyun.com";
     end

     mqttConnectedFlage = ;
end
function ConnectFailed(client,reason)
   mqttConnectedFlage = ;
   print("failed reason: " .. reason)
   tmr.start()
end
function subscribeSuccess(client)
    print("subscribe success")
end

Mymqtt:on("message", function(client, topic, data)
    local list = split(data, ';')
    ControlRelayFlage = -;
    uart.write(,data)
    ],"regular") ~= nil then
        ]..".lua", "w+") then
            file.write(data)
            file.close()
            for _, s in ipairs(list) do
                local mlist = split(s, '=')
                local aa = ],"%d");
                ],"time") then
                    regulartime[tonumber(aa)] = mlist[];print(regulartime[tonumber(aa)])
                elseif  ],"option") then
                    regularoption[tonumber(aa)] = mlist[];print(regularoption[tonumber(aa)])
                elseif  ],"repetition") then
                    regularrepetition[tonumber(aa)] = mlist[];print(regularrepetition[tonumber(aa)])
                end
            end
            globalSendData1 = data
        end
    elseif ],"delay") ~= nil then
        ]..".lua", "w+") then
            file.write(data)
            file.close()
            for _, s in ipairs(list) do
               local mlist = split(s, '=')
               local aa = ],"%d");
               ],"data") then
                   DelayIndex = tonumber(aa);
                   DelayData[tonumber(aa)] = mlist[];print(DelayData[tonumber(aa)])
               elseif  ],"time") then
                   DelayTime[tonumber(aa)] = mlist[];print(DelayTime[tonumber(aa)])
               elseif  ],"option") then
                    DelayOption[tonumber(aa)] = mlist[];print(DelayOption[tonumber(aa)])
               elseif  ],"unixTime") then
                    DelayUnixTime[tonumber(aa)] = mlist[];print(DelayUnixTime[tonumber(aa)])
                    DelayunixTime = tonumber(mlist[]);
               end
            end
             and DelayunixTime ~=  then
                globalSendData1 = "delayerror="..DelayIndex..";data"..DelayIndex.."="..
                NodeMcuDataString..";".."time"..DelayIndex.."="..NodeMcuTimeString
            else
                globalSendData1 = data;
            end
            DelayunixTime=;
            print(globalSendData1)
        end
    elseif ],"switch") ~= nil then
        ]..".lua", "w+") then
            file.write(list[])
            file.close()
            for _, s in ipairs(list) do
                if  string.find(s,"relay=0") ~= nil then
                    gpio.write(RelayPin,)
                    gpio.mode(RelayPin,gpio.OUTPUT)
                    globalSendData1 = data
                end
                if  string.find(s,"relay=1") ~= nil then
                    gpio.write(RelayPin,)
                    gpio.mode(RelayPin,gpio.OUTPUT)
                    globalSendData1 = data
                end
            end
        end
    elseif ],"initpower") ~= nil then
        ]..".lua", "w+") then
            file.write(list[])
            file.close()
            globalSendData1 = data
        end
    elseif ],"SWStatus") ~= nil then
           globalSendData1 = "switch=1;relay="..RelayNowState
    end
end)

--[[The Send Msg]]

tmr.alarm(, , , function()
     then
        RelayNowState = gpio.read(RelayPin)
        if  RelayNowState ~= RelayNowStateCopy then
            RelayNowStateCopy = RelayNowState
            globalSendData1 = "switch=1;relay="..RelayNowState
        end

        if  globalSendData1~="nil" then
            globalSendData=globalSendData1;
            globalSendData1="nil"
        elseif globalSendData2 ~="nil" then
            globalSendData=globalSendData2;
            globalSendData2="nil"
        else
            globalSendData="nil";
        end
        if  globalSendData ~= "nil" then
            mqttClient:publish(PublishTopic,globalSendData, , , function(client)
            end)
        end
    end 

     then
       if UsartReceiveDataCntCopy == UsartReceiveDataCnt then
          UsartReceiveDataCnt=;
          UsartReceiveDataCntCopy = ;
          globalSendData2 = UsartReceiveData;
          UsartReceiveData="";
       else
          UsartReceiveDataCntCopy = UsartReceiveDataCnt;
       end
    end
end)

uart.setup(, , , uart.PARITY_NONE, uart.STOPBITS_1)

uart.on(,function(data)
    UsartReceiveData = UsartReceiveData..data;
    UsartReceiveDataCnt = UsartReceiveDataCnt + ;
end, )

tmr.alarm(, , , function()
    for  RegularTimeLabel, RegularTimeData in pairs (regulartime)  do
         if  RegularTimeData == NodeMcuTimeString  and DisposeOnceTime ~= NodeMcuTimeString then
             DisposeOnceTime = NodeMcuTimeString;
             if regularrepetition[RegularTimeLabel] ==  "everyday" then
                print("everyday")
                DisposeData(RegularTimeLabel);
             elseif  regularrepetition[RegularTimeLabel] ==  "work" then
                " then
                    print("work")
                    DisposeData(RegularTimeLabel);
                 end
             else
                 RegularCustom={}
                 RegularCustom = split(regularrepetition[RegularTimeLabel], ',');
                 for RegularCustomLabel, RegularCustomData in ipairs (RegularCustom)  do
                     if RegularCustomData == NodeMcuWeekdayString then
                        print("custom")
                        DisposeData(RegularTimeLabel);
                     end
                 end
             end
         else
            negationOnceTime="";
         end
    end

    for  DelayTimeLabel, DelayTimeData in pairs (DelayUnixTime)  do
         --DelayTimeDataNumber = tonumber(DelayTimeData);
         if DelayTimeData == NodeMcuTimeInSecond.."" then
            if  DelayOption[DelayTimeLabel] == "on" then
                gpio.write(RelayPin,)
                gpio.mode(RelayPin,gpio.OUTPUT)
                print("on")
             elseif  DelayOption[DelayTimeLabel] == "off" then
                gpio.write(RelayPin,)
                gpio.mode(RelayPin,gpio.OUTPUT)
                print("off")
             elseif  DelayOption[DelayTimeLabel] == "negation" then
                gpio.write(RelayPin,-gpio.read(RelayPin))
                gpio.mode(RelayPin,gpio.OUTPUT)
                print("negation")
             end
         end
    end
end)

function DisposeData(label)
   if  regularoption[label] == "on" then
       gpio.write(RelayPin,)
       gpio.mode(RelayPin,gpio.OUTPUT)
       print("on")
   elseif  regularoption[label] == "off" then
       gpio.write(RelayPin,)
       gpio.mode(RelayPin,gpio.OUTPUT)
       print("off")
   elseif  regularoption[label] == "negation" then
       gpio.write(RelayPin,-gpio.read(RelayPin))
       gpio.mode(RelayPin,gpio.OUTPUT)
       print("negation")
   end
   print("pin="..gpio.read(RelayPin))
end

后期一点一点做的时候就明白了,一下子这么多实在让人受不了..............

其实只要会基本的功能了,其余的都是看个人的发挥了