nodemcu 例程,

作者:shuxiansheng | 更新时间:2020-03-01 | 浏览量:841

--relayA-GPIO15-index8
--relayB-GPIO5- index1
gpio.OUTPUT)
gpio.mode(2,gpio.OUTPUT)
gpio.mode(4,gpio.OUTPUT)
gpio.write(1,gpio.LOW)
gpio.write(2,gpio.LOW)
gpio.write(4,gpio.LOW)

gpio.mode(5,gpio.OUTPUT)
gpio.mode(7,gpio.OUTPUT)
gpio.mode(8,gpio.OUTPUT)
gpio.write(5,gpio.LOW)
gpio.write(7,gpio.LOW)
gpio.write(8,gpio.LOW)
onOff_flag1 = "0"   
onOff_flag2 = "0"
device_id =
Product_id=

ServerAddr =       --192.168.0.1
port =       --6002
O7FpePAl=yKxzCQ0CWaWfZTkrZ8=
function on_off(index,level)
    gpio.write(index,level)
   
    if(index == 8) then
        if(level == gpio.HIGH) then
            onOff_flag1 = "1"
        else
            onOff_flag1 = "0"
      end
    else
        if(level == gpio.HIGH) then
            onOff_flag2 = "1"
        else
            onOff_flag2 = "0"
      end
    end
end
function implement(recData)
    CMD = string.sub(recData,18,22)
    if(CMD == "C1ON") then
        on_off(8,gpio.HIGH)
    elseif(CMD == "C2ON") then
        on_off(1,gpio.HIGH)
    elseif(CMD == "C1OF") then
        on_off(8,gpio.LOW)
    elseif(CMD == "C2OF") then
        on_off(1,gpio.LOW)
    end
end
function workStation()
    tmr.alarm(0,1000,tmr.ALARM_AUTO,function()
        if(wifi.sta.getip() == nil) then
            print("waiting...")
        else  
            tmr.stop(0)
            staMac = wifi.sta.getmac()
            print(staMac)
      
            ns = net.createServer(net.TCP,300) 
            ns:listen(12345,function(c)
                c:on("receive", function(c,instrction_wifi)
                    print("wifi:"..instrction_wifi)
                    if(string.sub(instrction_wifi,1,17) == staMac)  then 
                        if(string.sub(instrction_wifi,18,21) == "ENQU") then 
                            c:send("ILDRGBW"..staMac..RGBWhiteStateFlag..RGBRHhythmStateFlag..brightness..RGBDelayTime)
                        else
                            implement(instrction_wifi)
                        end
                    end                
                end)               
            end)
           
            http_table = {}
            http_table.sn = staMac
            http_table.title = "ILD"..staMac 
            ok,to_json = pcall(sjson.encode,http_table)
            http.post('http:/*********/?register_code= ******',
                    'Content-Type: application/json\r\n',
                    to_json,
                    function(code, data)
                        if (code < 0) then 
                        print("HTTP request failed")
                        else  
                            dataSjson = sjson.decode(data)
                            deviceId = dataSjson.data.device_id
                        
                            http.post('http:/*******/api.heclouds.com/mqtt/topic',
                               'api-key:O7FpePAl=*******\r\n',
                               '{"name":"ILDRGBW"}',
                               function(code, data)
                                   if (code < 0) then
                                        print("HTTP request failed")
                                   end
                                 end)
                        end
                    end)
       local ServerAddr = "*****"
       local port =       --6002
       tmr.alarm(1,1000,tmr.ALARM_SEMI,function()
               client = mqtt.Client(device_id,300,Product_id,AuthInfo,false)
               client:connect(ServerAddr,port,0,0,function(client)  
                    client:subscribe("ILDJACK",0,function(client) end)
                    client:on("message",function(client,topic,instruction_mqtt)
                        print("mqtt:"..instruction_mqtt)
                        if(string.sub(instruction_mqtt,1,17) == staMac)  then 
                                    if(string.sub(instruction_mqtt,18,21) == "ENQU")    then  
                                                                    client:publish("ILDJACK","ILDRGBW"..staMac..RGBWhiteStateFlag..RGBRHhythmStateFlag..brightness..RGBDelayTime,0,0)
                                    else
                                        implement(instruction_mqtt)
                                    end
                                end
                    end)
                    client:on("offline",function(client)  
                        tmr.start(1)
                    end)
               end)
           end)
        end
    end)
end
 

评论:共1条

aaronwang 评论于:2020-03-06 18:07:34
好长啊。。。没太看懂
返回顶部