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

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

看第一部分的源码

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

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

--[[
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)

说一下源码

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

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

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

现在贴上第二部分源码,也就是存在手机上的,手机上就是这样存的,前面一点是接收用户配置的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

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

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

3-51单片机WIFI学习(开发板8266底层源码介绍)的更多相关文章

  1. 1-51单片机WIFI学习(开发板介绍)

    源码链接都在后面 前面的都是介绍单独的WIFI,没有和单片机结合起来,因为做项目很少会只用WIFI模块.大多数都是WIFI模块作为中转数据的桥梁,单片机负责 数据采集,控制等等,所以自己准备出一套51 ...

  2. 4-51单片机WIFI学习(开发板51单片机自动冷启动下载原理)

    上一篇链接 http://www.cnblogs.com/yangfengwu/p/8743936.html 这一篇说一下自己板子的51单片机自动冷启动下载原理,我挥舞着键盘和鼠标,发誓要把世界写个明 ...

  3. 2-51单片机WIFI学习(开发板测试远程通信详细介绍)

    前一篇链接(最后有APP安装包下载地址) http://www.cnblogs.com/yangfengwu/p/8720148.html 由于自己别的贴片的板子还没到,所以用自己的8266最小系统板 ...

  4. 迅为4412开发板QtE系统源码-屏幕横竖屏切换修改方法

    迅为4412开发板QtE系统源码-屏幕横竖屏切换修改方法 详情了解:http://topeetboard.com 更多了解:https://arm-board.taobao.com/ 用户在开发板上运 ...

  5. GuavaCache学习笔记三:底层源码阅读

    申明:转载自 https://www.cnblogs.com/dennyzhangdd/p/8981982.html 感谢原博主的分享,看到这个写的真好,直接转载来,学习了. 另外也推荐另外一篇Gua ...

  6. 如何选择单片机和Android-LInux-ARM开发板?

    源: 如何选择单片机和Android-LInux-ARM开发板?

  7. Java学习-039-源码 jar 包的二次开发扩展实例(源码修改)

    最近在使用已有的一些 jar 包时,发现有些 jar 包中的一些方法无法满足自己的一些需求,例如返回固定的格式,字符串处理等等,因而需要对原有 jar 文件中对应的 class 文件进行二次开发扩展, ...

  8. Spring学习之——手写Spring源码V2.0(实现IOC、D、MVC、AOP)

    前言 在上一篇<Spring学习之——手写Spring源码(V1.0)>中,我实现了一个Mini版本的Spring框架,在这几天,博主又看了不少关于Spring源码解析的视频,受益匪浅,也 ...

  9. H5类似易企秀/编辑器/页面制作/开发/生成工具/软件/源码/授权

    代码地址如下:http://www.demodashi.com/demo/14960.html 项目简介 H5DS (HTML5 Design software) 这是一款基于WEB的 H5制作工具. ...

随机推荐

  1. error:Unterminated &lt;form:form tag

    问题:标签不对称 解决:<form:form></form> 改成 <form:form> </form:form> 虽然又是自动补全带来的bug,但还 ...

  2. Jexus 5.8.3正式发布:Asp.Net Core在Linux上最友好服务器平台

    Jexus Web Serever 是一款运行于 Linux 操作系统,以支持 ASP.NET.ASP.NET CORE.PHP 为特色的高性能 WEB 服务器和反向代理服务器.最新版 5.8.3 已 ...

  3. Spring MVC简单原理

    Spring MVC原理 针对有Java Web基础.Spring基础和Spring MVC使用经验者. 前言 目前基于Java的web后端,Spring生态应该是比较常见了.虽然现在流行前后端分离, ...

  4. libpqxx接口的在linux下的使用,解决psql:connections on Unix domain socket "/tmp/.s.PGSQL.5432"错误

    在项目中使用postgresql数据库时要求在windows和linux双平台兼容.于是在windows下使用的接口在linux下爆出异常: psql:connections on Unix doma ...

  5. 【Unity与23种设计模式】抽象工厂模式(Abstract Factory)

    GoF中定义: "提供一个能够建立整个类群组或有关联的对象,而不必指明它们的具体类." 意思就是 根据不同的执行环境产生不同的抽象类子类 抽象工厂模式经常在面试中会涉及到 下面的例 ...

  6. 深度剖析PHP序列化和反序列化

    序列化 序列化格式 在PHP中,序列化用于存储或传递 PHP 的值的过程中,同时不丢失其类型和结构. 序列化函数原型如下: string serialize ( mixed $value ) 先看下面 ...

  7. 5分钟学习spark streaming之 轻松在浏览器运行和修改Word Counts

    方案一:根据官方实例,下载预编译好的版本,执行以下步骤: nc -lk 9999 作为实时数据源 ./bin/run-example org.apache.spark.examples.sql.str ...

  8. kubernetes中kubeconfig的用法

    在开启了 TLS 的集群中,每当与集群交互的时候少不了的是身份认证,使用 kubeconfig(即证书) 和 token 两种认证方式是最简单也最通用的认证方式. 以kubectl为例介绍kubeco ...

  9. Mycat 分片规则详解--单月小时分片

    实现方式:单月内按照小时拆分,最小粒度是小时,一天最多可以有24个分片,最少1个分片,下个月从头开始循环 优点:使数据按照小时来进行分时存储,颗粒度比日期(天)分片要小,适用于数据采集类存储分片 缺点 ...

  10. 笔记:Maven 设置代理配置

    如果公司基于安全因素考虑,要求使用通过安全认证的代理服务器访问因特网,这种情况夏,需要为 Maven 配置HTTP代理,才能让他正常访问外部仓库,配置代理服务器需要在~/.ms2/settings.x ...