美文网首页
3-51单片机ESP8266学习-AT指令(开发板8266底层源

3-51单片机ESP8266学习-AT指令(开发板8266底层源

作者: 杨奉武 | 来源:发表于2018-04-26 18:54 被阅读23次

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

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

    看第一部分的源码

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

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

    --[[

    GPIO0--3GPIO1--10GPIO2--4GPIO3--9GPIO4--2GPIO5--1GPIO9--11GPIO10--12GPIO12--6GPIO13--7GPIO14--5GPIO15--8GPIO16--00--GPIO161--GPIO52--GPIO43--GPIO04--GPIO25--GPIO146--GPIO127--GPIO138--GPIO159--GPIO3    10--GPIO111--GPIO912--GPIO10

    ]]

    RelayPin=2;

    splitStart =1splitIdex=0;

    splitTable={}

    function split(s, delim)

        iftype(delim) ~="string"orstring.len(delim) <=0 then

            return    end

        splitStart =1    splitTable = {}

        whiletruedo        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

    wifi.setmode(wifi.STATION)

    local updataFlage =0YouInitData1 ="NULL";

    YouInitData2 ="NULL";

    YouInitData3 ="NULL";

    YouInitData4 ="NULL";

    MyInitData1 ="";

    MyInitData2 ="";

    MyInitData3 ="";

    MyInitData4 ="";

    NodeMcuTimeInSecond =0;

    NodeMcuTimeInUSecond =0;

    NodeMcuTimeInrate =0;

    NodeMcuYear =0;

    NodeMcuMonth =0;

    NodeMcuDay=0;

    NodeMcuHour=0;

    NodeMcuMinute=0;

    NodeMcuSecond=0;

    NodeMcuWeekday=0;

    NodeMcuRtctime={};

    NodeMcuTimer={};

    NodeMcuTimerList={};

    NodeMcuDataString="nil"NodeMcuTimeString="nil";

    NodeMcuWeekdayString="nil";

    mqttConnectedFlage =0;

    NodeMcuRequestTime=0sntpAddress="nil";

    updataCnt =0;

    linkcnt=0;

    printtimeCnt=0;

    TCPReadDataOverTime =0;

    tmr.softwd(60)--watchdog 60S

    clientid = wifi.sta.getmac()

    SubscribeTopic ="yang/sw/"..clientid

    PublishTopic ="feng/sw/"..clientidiffile.open("InitData.lua","r") then

        local MyInitData = file.read();

        local DataList = split(MyInitData,';');

        ifDataList[1] =="MyData" then

            MyInitData1 = DataList[2]

            MyInitData2 = DataList[3]

            MyInitData3 = DataList[4]

            MyInitData4 = DataList[5]

        endelse    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="11223344"wifi.sta.config(apcfg)

    wifi.sta.connect()--wifi.sta.autoconnect(1)--[[The connection serve phone]]

    connectIP="192.168.1.102"ClientConnectedFlage =0ReceiveData ="";

    ReceiveDataCopy ="";

    UpdataDataCopy="";

    ReceiveDataCnt =0;

    ReceiveDataCntCopy =0;

    function InitTimer0function()

        ifClientConnectedFlage ==0 then

            Client = net.createConnection(net.TCP,0)

            --Client:connect(8888,connectIP)

            Client:connect(8888,gateway)

            Client:on("receive", function(Client, data)

                ReceiveData = ReceiveData..data;

                ReceiveDataCnt = ReceiveDataCnt +1;

            end)

            Client:on("connection", function(sck, c)

                ClientConnectedFlage =1            TcpConnect = Client

                print("Link OK")

                tmr.stop(0)

                Client:on("disconnection", function(sck, c)

                    ClientConnectedFlage =0                TcpConnect = nil

                    tmr.start(0)

                end)

            end)

            ifClientConnectedFlage ==0 then

                print("Link Error")

                updataCnt =0;

                updataFlage =0;

                linkcnt = linkcnt+1iflinkcnt >=6 then

                    linkcnt =0ifTcpConnect ~= nil then

                        TcpConnect:close()

                    end

                    tmr.stop(0)

                    tmr.stop(1)

                    dofile("updata.lua");

                end

            end

        end

    end

    tmr.alarm(0,1000,1, InitTimer0function)

    function disposedata(data)

        local DataList = split(data,';');

        ifDataList[1] =="MyData" then

            iffile.open("InitData.lua","w+") then

                file.write(data)--MyInitData

                file.close()

                MyInitData1 = DataList[2]

                MyInitData2 = DataList[3]

                MyInitData3 = DataList[4]

                MyInitData4 = DataList[5]

                print("MyInitData1 = "..DataList[2])

                print("MyInitData2 = "..DataList[3])

                print("MyInitData3 = "..DataList[4])

                print("MyInitData4 = "..DataList[5])

                ifTcpConnect ~= nil then

                    TcpConnect:send(data);

                end

                print("write InitData success")   

            end

        elseif  DataList[1] =="YouData" then   

                ifTcpConnect ~= nil then

                    TcpConnect:send("YouData;"..clientid..";"..SubscribeTopic..";"..PublishTopic..";"..YouInitData1..";"..

                                    YouInitData2..";"..YouInitData3..";"..YouInitData4);

                end

                print("Send YouData success") 

        elseif  DataList[1] =="UpdataData" then 

                updataFlage =1;

                ifTcpConnect ~= nil then

                    TcpConnect:send("WaitUpdataData0;");

                end   

                print("WaitUpdataData......")

                file.open("updata.lua","w+")

        end

        ReceiveDataCopy ="";

    end

    function InitTimer1function()

      ifReceiveDataCnt ~=0 then

          ifReceiveDataCnt == ReceiveDataCntCopy then

                TCPReadDataOverTime =0;

                ReceiveDataCopy = ReceiveData;

                ReceiveData ="";

                ReceiveDataCnt =0;

                ReceiveDataCntCopy =0;

                ifupdataFlage ==0 then

                    disposedata(ReceiveDataCopy);

                elseifReceiveDataCopy ~="UpdataDataSuccess" then

                        file.write(ReceiveDataCopy)

                        updataCnt = updataCnt +1ifTcpConnect ~= nil then

                            TcpConnect:send("WaitUpdataData"..updataCnt..";");

                        end 

                        print("start down...") 

                    elseifTcpConnect ~= nil then

                            TcpConnect:send("UpdataDataSuccess");

                            TcpConnect:close()

                        end

                        file.close()

                        updataFlage =0 

                        print("UpdataDataSuccess") 

                        tmr.stop(0)

                        tmr.stop(1)

                        --dofile("main.lua");

                        node.compile("updata.lua")

                        dofile("updata.lua");

                        --dofile("main.lc");

                    end

                    ReceiveDataCopy ="";

                end

          else            ReceiveDataCntCopy = ReceiveDataCnt     

          end

      end

    end

    tmr.alarm(1,400,1, InitTimer1function)

    gpio.mode(4,gpio.OUTPUT)--输出模式 

    tmr.alarm(2,500,1, function()

        ifmqttConnectedFlage==1 then

            gpio.write(4,1-gpio.read(4))

            ifNodeMcuRequestTime ==0 then

                sntp.sync(sntpAddress,

                function()

                    print("sync succeeded:"..sntpAddress)

                    NodeMcuRequestTime =1;

                end,

                function(index)

                    print("failed : "..index)

                end)

            end

        end

        NodeMcurRtctime = rtctime.epoch2cal(rtctime.get()+28800)

        NodeMcuTimer =string.format("%04d;%02d;%02d;%02d;%02d;%02d;%01d",

                            NodeMcurRtctime["year"],

                            NodeMcurRtctime["mon"],

                            NodeMcurRtctime["day"],

                            NodeMcurRtctime["hour"]+8,

                            NodeMcurRtctime["min"],

                            NodeMcurRtctime["sec"],

                            NodeMcurRtctime["wday"])

        NodeMcuTimerList = split(NodeMcuTimer,';');

        NodeMcuYear = tonumber(NodeMcuTimerList[1]);

        NodeMcuMonth = tonumber(NodeMcuTimerList[2]);

        NodeMcuDay = tonumber(NodeMcuTimerList[3]);

        NodeMcuHour = tonumber(NodeMcuTimerList[4]);

        NodeMcuMinute = tonumber(NodeMcuTimerList[5]);

        NodeMcuSecond = tonumber(NodeMcuTimerList[6]);

        NodeMcuWeekday = tonumber(NodeMcuTimerList[7]);

        NodeMcuHour = NodeMcuHour -8; 

        NodeMcuDataString=NodeMcuYear.."/"..NodeMcuMonth.."/"..NodeMcuDay; 

        NodeMcuTimeString=NodeMcuHour..":"..NodeMcuMinute..":"..NodeMcuSecond; 

        NodeMcuWeekdayString = NodeMcuWeekday.."";

        NodeMcuTimeInSecond,NodeMcuTimeInUSecond,NodeMcuTimeInrate= rtctime.get()

        ifNodeMcuHour ==2and NodeMcuMinute ==2and NodeMcuSecond ==2 then

            NodeMcuRequestTime =0;

        end

        printtimeCnt = printtimeCnt +1;

        ifprinttimeCnt >20 then

          printtimeCnt =0;

          print("Year="..NodeMcuYear..";Month="..NodeMcuMonth..";Day="..NodeMcuDay..";Hour="..

                NodeMcuHour..";Minute="..NodeMcuMinute..";Second="..NodeMcuSecond..";Weekday="..

                NodeMcuWeekdayString..";TimeInSecond="..NodeMcuTimeInSecond)

        end

        ifTcpConnect ~= nil then

            TCPReadDataOverTime = TCPReadDataOverTime +1;

            ifTCPReadDataOverTime >20 then

              TCPReadDataOverTime =0;

              TcpConnect:close()

              dofile("updata.lua");

            end

        end

        tmr.softwd(60);

    end)

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

        printip =0end)

    wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)

      ifprintip ==0 then

          ip,netmask,gateway = wifi.sta.getip()

          print(ip)

      end

      printip =1end)

     说一下源码

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

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

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

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

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

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

            configwifissid = \"%1$s\";\nconfigwifipwd= \"%2$s\";\nMqttUserString = \"%3$s\";\nMqttPwdString = \"%4$s\";\nMqttIPString = \"%5$s\";\nMqttPort = %6$d;\n

            ifconfigwifipwd == \"nil\" then\nconfigwifipwd = nil\n

            end\n

            globalSendData =\"nil\"; \nglobalSendData1=\"nil\";\nglobalSendData2=\"nil\";\n       

            MemoryRelayState = \"3\";       

            regulartime={};\n

            regularoption={};\n

            regularrepetition={};\n

            DisposeOnceTime="nil";\n

            RegularTimeLabel=0;\n

            RegularTimeData=\"0\";\nRegularOptionLabel=0;\n

            RegularOptionData=\"0\";\nRegularRepetitionLabel=0;\n

            RegularRepetitionData=\"0\";\nRegularCustom={}\n

            RegularCustomLabel=0;\n

            RegularCustomData=\"0\";\n       

            DelayData={};\n

            DelayTime={};\n

            DelayOption={};\n

            DelayUnixTime={};\n

            DelayunixTime=0;\n

            DelayIndex=0;\n

            DelayTimeLabel=0;\n

            DelayTimeData=\"0\";\nDelayTimeDataNumber=0;\n

            RelayNowState=0\n

            RelayNowStateCopy=0\n

            UsartReceiveData=\"\";UsartReceiveDataCnt=0;

            UsartReceiveDataCntCopy=0;

            fori=0,7do\n

                iffile.open(\"regular=\"..i..\".lua\", \"r\") then\nlocal list = split(file.read(), \";\")\nfor_, sinipairs(list)do\n

                        local mlist = split(s, \"=\")\nifstring.find(mlist[1],\"time\") then\nregulartime[i] = mlist[2];print(regulartime[i])\n

                        elseif  string.find(mlist[1],\"option\") then \nregularoption[i] = mlist[2];print(regularoption[i])\n

                        elseif  string.find(mlist[1],\"repetition\") then \nregularrepetition[i] = mlist[2];print(regularrepetition[i])\n

                        end\n

                    end\n

                    file.close() \n

                end\n

                iffile.open(\"delay=\"..i..\".lua\", \"r\") then\nlocal list = split(file.read(), \";\")\nfor_, sinipairs(list)do\n

                      local mlist = split(s, \"=\")\nifstring.find(mlist[1],\"data\") then\nDelayData[i] = mlist[2];print(DelayData[i])\n

                      elseif  string.find(mlist[1],\"time\") then\nDelayTime[i] = mlist[2];print(DelayTime[i])\n

                      elseif  string.find(mlist[1],\"option\") then \nDelayOption[i] = mlist[2];print(DelayOption[i])  \n   

                      elseif  string.find(mlist[1],\"unixTime\") then \nDelayUnixTime[i] = mlist[2];print(DelayUnixTime[i])  \n 

                      end    \n

                    end\n

                    file.close() \n

                end\n

            end\n

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

            end\n

                        iffile.open(\"switch=1.lua\", \"r\") then\niffile.read() == \"relay=1\" then\ngpio.write(RelayPin,1)\n

                    gpio.mode(RelayPin,gpio.OUTPUT)\n

                    RelayNowState=1\n

                    RelayNowStateCopy=1\n

                    print(\"relay=1\")\n            end\n

                iffile.read() == \"relay=0\" then\ngpio.write(RelayPin,0)\n

                    gpio.mode(RelayPin,gpio.OUTPUT)\n

                    RelayNowState=0\n

                    RelayNowStateCopy=0\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(1)\n

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

            tmr.alarm(3,1000,1, function()\n

                Mymqtt:connect(MqttIPString, MqttPort, 0,ConnectSuccess,ConnectFailed)\n

            end)\n

            function ConnectSuccess(client)\n

                client:subscribe(SubscribeTopic, 0, subscribeSuccess)\n

                print(\"connected\")\nmqttClient = client;\n

                tmr.stop(3);\n

                ifsntpAddress == \"nil\"  then \nsntpAddress=\"ntp2.aliyun.com\"; \n            end \n

                mqttConnectedFlage =1;\n

            end\n

            function ConnectFailed(client,reason)\n

              mqttConnectedFlage =0;\n

              print(\"failed reason: \" .. reason)\ntmr.start(3)\n

            end\n

            function subscribeSuccess(client)\n

                print(\"subscribe success\") \n        end\n

                                      Mymqtt:on(\"message\", function(client, topic, data) \nlocal list = split(data, \";\")\nControlRelayFlage = -1;\n

            uart.write(0,data)\n

            ifstring.find(list[1],\"regular\") ~= nil then\niffile.open(list[1]..\".lua\", \"w+\") then\n                file.write(data)\n

                    file.close() \n

                    for_, sinipairs(list)do\n

                        local mlist = split(s, \"=\")\nlocal aa =string.match(mlist[1],\"%d\"); \nifstring.find(mlist[1],\"time\") then\nregulartime[tonumber(aa)] = mlist[2];\n

                        elseif  string.find(mlist[1],\"option\") then \nregularoption[tonumber(aa)] = mlist[2];\n

                        elseif  string.find(mlist[1],\"repetition\") then \nregularrepetition[tonumber(aa)] = mlist[2];\n

                        end\n

                    end\n

                    globalSendData1 = data\n

                end 

            elseif string.find(list[1],\"delay\") ~= nil then\niffile.open(list[1]..\".lua\", \"w+\") then\n                file.write(data)\n

                    file.close() \n

                    for_, sinipairs(list)do\n

                      local mlist = split(s, \"=\")\nlocal aa =string.match(mlist[1],\"%d\");\nifstring.find(mlist[1],\"data\") then\nDelayIndex = tonumber(aa);\n

                          DelayData[tonumber(aa)] = mlist[2];\n

                      elseif  string.find(mlist[1],\"time\") then\nDelayTime[tonumber(aa)] = mlist[2];\n

                      elseif  string.find(mlist[1],\"option\") then \nDelayOption[tonumber(aa)] = mlist[2]; \n   

                      elseif  string.find(mlist[1],\"unixTime\") then \nDelayUnixTime[tonumber(aa)] = mlist[2];\n   

                            DelayunixTime = tonumber(mlist[2]);\n

                      end \n   

                    end\n

                    ifDelayunixTime < NodeMcuTimeInSecond and DelayunixTime ~=0and DelayunixTime ~=1 then\n

                        globalSendData1 = \"delayerror=\"..DelayIndex..\";data\"..DelayIndex..\"=\"..\nNodeMcuDataString..\";\"..\"time\"..DelayIndex..\"=\"..NodeMcuTimeString\nelse\n

                        globalSendData1 = data;\n

                    end\n

                    DelayunixTime=0; \n

                end

            elseif string.find(list[1],\"switch\") ~= nil then \niffile.open(list[1]..\".lua\", \"w+\") then \nfile.write(list[2]) \n

                    file.close() \n

                    for_, sinipairs(list)do \n

                        ifstring.find(s,\"relay=0\") ~= nil then \ngpio.write(RelayPin,0) \n

                            gpio.mode(RelayPin,gpio.OUTPUT) \n

                            globalSendData1 = data \n

                        end\n

                        ifstring.find(s,\"relay=1\") ~= nil then \ngpio.write(RelayPin,1) \n

                            gpio.mode(RelayPin,gpio.OUTPUT) \n

                            globalSendData1 = data \n

                        end\n

                    end\n

                end  \n

            elseif string.find(list[1],\"initpower\") ~= nil then\niffile.open(list[1]..\".lua\", \"w+\") then\nfile.write(list[2])\n

                    file.close() \n

                    globalSendData1 = data\n

                end    \n

            elseif string.find(list[1],\"SWStatus\") ~= nil then \nglobalSendData1 = \"switch=1;relay=\"..RelayNowState \n          end \n

        end)    \n

                            tmr.alarm(4,10,1, function() \n

        ifmqttClient ~= nil and mqttConnectedFlage ==1 then \n

            RelayNowState = gpio.read(RelayPin) \n

            ifRelayNowState ~= RelayNowStateCopy then \n

                RelayNowStateCopy = RelayNowState \n

                globalSendData1 = \"switch=1;relay=\"..RelayNowState \n        end \n

            ifglobalSendData1~=\"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

            ifglobalSendData ~= \"nil\" then \n mqttClient:publish(PublishTopic,globalSendData,0,0, function(client)  \n

                end) \n

            end \n

        end      \n

        ifUsartReceiveDataCnt ~=0 then  \n

          ifUsartReceiveDataCntCopy == UsartReceiveDataCnt then  \n

              UsartReceiveDataCnt=0;  \n

              UsartReceiveDataCntCopy =0;  \n

              globalSendData2 = UsartReceiveData;  \n

              UsartReceiveData=\"\";  \n else  \n

              UsartReceiveDataCntCopy = UsartReceiveDataCnt;  \n

          end  \n

        end  \n

    end) \n

    uart.setup(0,115200,8, uart.PARITY_NONE, uart.STOPBITS_1)

    uart.on(\"data\",0,function(data) UsartReceiveData = UsartReceiveData..data;

        UsartReceiveDataCnt = UsartReceiveDataCnt +1;

    end, 0)

    tmr.alarm(0,500,1, function()  \n

        forRegularTimeLabel, RegularTimeDatainpairs (regulartime)do \n

            ifRegularTimeData == NodeMcuTimeString  and DisposeOnceTime ~= NodeMcuTimeString then \n

                DisposeOnceTime = NodeMcuTimeString; \n

                ifregularrepetition[RegularTimeLabel] ==  \"everyday\" then \n print(\"everyday\") \n                DisposeData(RegularTimeLabel); \n

                elseif  regularrepetition[RegularTimeLabel] ==  \"work\" then  \n ifNodeMcuWeekdayString==\"2\" or NodeMcuWeekdayString==\"3\" or NodeMcuWeekdayString==\"4\" or NodeMcuWeekdayString==\"5\" or NodeMcuWeekdayString==\"6\" thenprint(\"work\") \n                    DisposeData(RegularTimeLabel); \n

                    end \n

                else \n

                    RegularCustom={} \n

                    RegularCustom = split(regularrepetition[RegularTimeLabel], \",\");    \n forRegularCustomLabel, RegularCustomDatainipairs (RegularCustom)do \n

                        ifRegularCustomData == NodeMcuWeekdayString then \n

                            print(\"custom\") \n                        DisposeData(RegularTimeLabel); \n

                        end \n

                    end  \n

                end \n

            else \n

                negationOnceTime=\"nil\";    \n          end \n

        end \n

        forDelayTimeLabel, DelayTimeDatainpairs (DelayUnixTime)do \n

            --DelayTimeDataNumber = tonumber(DelayTimeData); \n

            ifDelayTimeData == NodeMcuTimeInSecond..\"\" then \n ifDelayOption[DelayTimeLabel] == \"on\" then \n gpio.write(RelayPin,1) \n

                    gpio.mode(RelayPin,gpio.OUTPUT) \n

                    print(\"on\") \n elseif  DelayOption[DelayTimeLabel] == \"off\" then \n gpio.write(RelayPin,0) \n

                    gpio.mode(RelayPin,gpio.OUTPUT)  \n

                    print(\"off\") \n elseif  DelayOption[DelayTimeLabel] == \"negation\" then \n gpio.write(RelayPin,1-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

      ifregularoption[label] == \"on\" then \n gpio.write(RelayPin,1) \n

          gpio.mode(RelayPin,gpio.OUTPUT) \n

          print(\"on\") \n elseif  regularoption[label] == \"off\" then \n gpio.write(RelayPin,0) \n

          gpio.mode(RelayPin,gpio.OUTPUT)  \n

          print(\"off\") \n elseif  regularoption[label] == \"negation\" then \n gpio.write(RelayPin,1-gpio.read(RelayPin)) \n

          gpio.mode(RelayPin,gpio.OUTPUT)    \n

          print(\"negation\") \n    end \n

      print(\"pin=\"..gpio.read(RelayPin))  \n  end \n

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

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

    --configwifissid ="";--configwifipwd="";--MqttUserString ="";--MqttPwdString ="";--MqttIPString ="";--MqttPort = ;ifconfigwifipwd =="nil" then

      configwifipwd = nil

    end

    globalSendData ="nil";

    globalSendData1="nil";

    globalSendData2="nil";

    MemoryRelayState ="3";

    regulartime={};

    regularoption={};

    regularrepetition={};

    DisposeOnceTime="";

    RegularTimeLabel=0;

    RegularTimeData="";

    RegularOptionLabel=0;

    RegularOptionData="";

    RegularRepetitionLabel=0;

    RegularRepetitionData="";

    RegularCustom={}

    RegularCustomLabel=0;

    RegularCustomData="";

    DelayData={};

    DelayTime={};

    DelayOption={};

    DelayUnixTime={};

    DelayunixTime=0;

    DelayIndex=0;

    DelayTimeLabel=0;

    DelayTimeData="";

    DelayTimeDataNumber=0;

    RelayNowState=0RelayNowStateCopy=0UsartReceiveData="";

    UsartReceiveDataCnt=0;

    UsartReceiveDataCntCopy=0;fori=0,7doiffile.open("regular="..i..".lua","r") then

            local list = split(file.read(),';')

            for_, sinipairs(list)do            local mlist = split(s,'=')

                ifstring.find(mlist[1],"time") then

                    regulartime[i] = mlist[2];print(regulartime[i])

                elseif  string.find(mlist[1],"option") then

                    regularoption[i] = mlist[2];print(regularoption[i])

                elseif  string.find(mlist[1],"repetition") then

                    regularrepetition[i] = mlist[2];print(regularrepetition[i])

                end

            end

            file.close()

        end

        iffile.open("delay="..i..".lua","r") then

            local list = split(file.read(),';')

            for_, sinipairs(list)do          local mlist = split(s,'=')

              ifstring.find(mlist[1],"data") then

                  DelayData[i] = mlist[2];print(DelayData[i])

              elseif  string.find(mlist[1],"time") then

                  DelayTime[i] = mlist[2];print(DelayTime[i])

              elseif  string.find(mlist[1],"option") then

                    DelayOption[i] = mlist[2];print(DelayOption[i])     

              elseif  string.find(mlist[1],"unixTime") then

                    DelayUnixTime[i] = mlist[2];print(DelayUnixTime[i])   

              end   

            end

            file.close()

        end

    endiffile.open("initpower.lua","r") then

    endiffile.open("switch=1.lua","r") then

        iffile.read() =="relay=1" then

            gpio.write(RelayPin,1)

            gpio.mode(RelayPin,gpio.OUTPUT)

            RelayNowState=1        RelayNowStateCopy=1        print("relay=1") 

        end

        iffile.read() =="relay=0" then

            gpio.write(RelayPin,0)

            gpio.mode(RelayPin,gpio.OUTPUT)

            RelayNowState=0        RelayNowStateCopy=0        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(1)

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

    tmr.alarm(3,1000,1, function()

        Mymqtt:connect(MqttIPString, MqttPort, 0,ConnectSuccess,ConnectFailed)

    end)

    function ConnectSuccess(client)

        client:subscribe(SubscribeTopic, 0, subscribeSuccess)

        print("connected")

        mqttClient = client;

        tmr.stop(3);

        ifsntpAddress =="nil"  then

            sntpAddress="ntp2.aliyun.com";

        end

        mqttConnectedFlage =1;

    end

    function ConnectFailed(client,reason)

      mqttConnectedFlage =0;

      print("failed reason: " .. reason)

      tmr.start(3)

    end

    function subscribeSuccess(client)

        print("subscribe success")

    end

    Mymqtt:on("message", function(client, topic, data)

        local list = split(data,';')

        ControlRelayFlage = -1;

        uart.write(0,data)

        ifstring.find(list[1],"regular") ~= nil then

            iffile.open(list[1]..".lua","w+") then

                file.write(data)

                file.close()

                for_, sinipairs(list)do                local mlist = split(s,'=')

                    local aa =string.match(mlist[1],"%d");

                    ifstring.find(mlist[1],"time") then

                        regulartime[tonumber(aa)] = mlist[2];print(regulartime[tonumber(aa)])

                    elseif  string.find(mlist[1],"option") then

                        regularoption[tonumber(aa)] = mlist[2];print(regularoption[tonumber(aa)])

                    elseif  string.find(mlist[1],"repetition") then

                        regularrepetition[tonumber(aa)] = mlist[2];print(regularrepetition[tonumber(aa)])

                    end

                end

                globalSendData1 = data

            end

        elseif string.find(list[1],"delay") ~= nil then

            iffile.open(list[1]..".lua","w+") then

                file.write(data)

                file.close()

                for_, sinipairs(list)do              local mlist = split(s,'=')

                  local aa =string.match(mlist[1],"%d");

                  ifstring.find(mlist[1],"data") then

                      DelayIndex = tonumber(aa);

                      DelayData[tonumber(aa)] = mlist[2];print(DelayData[tonumber(aa)])

                  elseif  string.find(mlist[1],"time") then

                      DelayTime[tonumber(aa)] = mlist[2];print(DelayTime[tonumber(aa)])

                  elseif  string.find(mlist[1],"option") then

                        DelayOption[tonumber(aa)] = mlist[2];print(DelayOption[tonumber(aa)])     

                  elseif  string.find(mlist[1],"unixTime") then

                        DelayUnixTime[tonumber(aa)] = mlist[2];print(DelayUnixTime[tonumber(aa)])   

                        DelayunixTime = tonumber(mlist[2]);

                  end   

                end

                ifDelayunixTime < NodeMcuTimeInSecond and DelayunixTime ~=0and DelayunixTime ~=1 then

                    globalSendData1 ="delayerror="..DelayIndex..";data"..DelayIndex.."="..

                    NodeMcuDataString..";".."time"..DelayIndex.."="..NodeMcuTimeString

                else                globalSendData1 = data;

                end

                DelayunixTime=0;

                print(globalSendData1)

            end

        elseif string.find(list[1],"switch") ~= nil then

            iffile.open(list[1]..".lua","w+") then

                file.write(list[2])

                file.close()

                for_, sinipairs(list)doifstring.find(s,"relay=0") ~= nil then

                        gpio.write(RelayPin,0)

                        gpio.mode(RelayPin,gpio.OUTPUT)

                        globalSendData1 = data

                    end

                    ifstring.find(s,"relay=1") ~= nil then

                        gpio.write(RelayPin,1)

                        gpio.mode(RelayPin,gpio.OUTPUT)

                        globalSendData1 = data

                    end

                end

            end 

        elseif string.find(list[1],"initpower") ~= nil then

            iffile.open(list[1]..".lua","w+") then

                file.write(list[2])

                file.close()

                globalSendData1 = data

            end

        elseif string.find(list[1],"SWStatus") ~= nil then

              globalSendData1 ="switch=1;relay="..RelayNowState     

        end

    end)--[[The Send Msg]]

    tmr.alarm(4,10,1, function()

        ifmqttClient ~= nil and mqttConnectedFlage ==1 then

            RelayNowState = gpio.read(RelayPin)

            ifRelayNowState ~= RelayNowStateCopy then

                RelayNowStateCopy = RelayNowState

                globalSendData1 ="switch=1;relay="..RelayNowState

            end

            ifglobalSendData1~="nil" then

                globalSendData=globalSendData1;

                globalSendData1="nil"        elseif globalSendData2 ~="nil" then

                globalSendData=globalSendData2;

                globalSendData2="nil"else            globalSendData="nil";   

            end

            ifglobalSendData ~="nil" then

                mqttClient:publish(PublishTopic,globalSendData, 0,0, function(client)

                end)

            end

        end

        ifUsartReceiveDataCnt ~=0 then

          ifUsartReceiveDataCntCopy == UsartReceiveDataCnt then

              UsartReceiveDataCnt=0;

              UsartReceiveDataCntCopy =0;

              globalSendData2 = UsartReceiveData;

              UsartReceiveData="";

          else          UsartReceiveDataCntCopy = UsartReceiveDataCnt;

          end

        end

    end)

    uart.setup(0,115200,8, uart.PARITY_NONE, uart.STOPBITS_1)

    uart.on("data",0,function(data)

        UsartReceiveData = UsartReceiveData..data;

        UsartReceiveDataCnt = UsartReceiveDataCnt +1;

    end, 0)

    tmr.alarm(0,500,1, function()

        forRegularTimeLabel, RegularTimeDatainpairs (regulartime)doifRegularTimeData == NodeMcuTimeString  and DisposeOnceTime ~= NodeMcuTimeString then

                DisposeOnceTime = NodeMcuTimeString;

                ifregularrepetition[RegularTimeLabel] =="everyday" then

                    print("everyday")

                    DisposeData(RegularTimeLabel);

                elseif  regularrepetition[RegularTimeLabel] =="work" then

                    ifNodeMcuWeekdayString=="2"or NodeMcuWeekdayString=="3"or NodeMcuWeekdayString=="4"or NodeMcuWeekdayString=="5"or NodeMcuWeekdayString=="6" then

                        print("work")

                        DisposeData(RegularTimeLabel);

                    end

                else                RegularCustom={}

                    RegularCustom = split(regularrepetition[RegularTimeLabel],','); 

                    forRegularCustomLabel, RegularCustomDatainipairs (RegularCustom)doifRegularCustomData == NodeMcuWeekdayString then

                            print("custom")

                            DisposeData(RegularTimeLabel);

                        end

                    end

                end

            else            negationOnceTime="";   

            end

        end

        forDelayTimeLabel, DelayTimeDatainpairs (DelayUnixTime)do--DelayTimeDataNumber = tonumber(DelayTimeData);

            ifDelayTimeData == NodeMcuTimeInSecond.."" then

                ifDelayOption[DelayTimeLabel] =="on" then

                    gpio.write(RelayPin,1)

                    gpio.mode(RelayPin,gpio.OUTPUT)

                    print("on")

                elseif  DelayOption[DelayTimeLabel] =="off" then

                    gpio.write(RelayPin,0)

                    gpio.mode(RelayPin,gpio.OUTPUT)

                    print("off")

                elseif  DelayOption[DelayTimeLabel] =="negation" then

                    gpio.write(RelayPin,1-gpio.read(RelayPin))

                    gpio.mode(RelayPin,gpio.OUTPUT)   

                    print("negation")

                end

            end

        end

    end)

    function DisposeData(label)

      ifregularoption[label] =="on" then

          gpio.write(RelayPin,1)

          gpio.mode(RelayPin,gpio.OUTPUT)

          print("on")

      elseif  regularoption[label] =="off" then

          gpio.write(RelayPin,0)

          gpio.mode(RelayPin,gpio.OUTPUT)

          print("off")

      elseif  regularoption[label] =="negation" then

          gpio.write(RelayPin,1-gpio.read(RelayPin))

          gpio.mode(RelayPin,gpio.OUTPUT)   

          print("negation")

      end

      print("pin="..gpio.read(RelayPin)) 

    end

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

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

    下一篇连接

    http://www.cnblogs.com/yangfengwu/p/8757036.html

    相关文章

      网友评论

          本文标题:3-51单片机ESP8266学习-AT指令(开发板8266底层源

          本文链接:https://www.haomeiwen.com/subject/fhvmlftx.html