Home Article Node Mcu esp8266 远程控制

Node Mcu esp8266 远程控制

Release time:2020-07-24 20:23:37 Author:admin Reading volume:59

今天闲着没事(呸呸呸),为了完成综合设计大作业,又改了套方案,实现了nodemcu的远程控制

lua代码如下:

wifi.setmode(wifi.STATION) 
    wifi.sta.config("你家的WiFi名称","你家的WiFi密码"
    print(wifi.sta.getip()) 
    led1 = 3 
    led2 = 4 
    gpio.mode(led1, gpio.OUTPUT) 
    gpio.mode(led2, gpio.OUTPUT) 
    srv=net.createServer(net.TCP) 
    srv:listen(80,function(conn) 
        conn:on("receive", function(client,request) 
            local buf = ""; 
            local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP"); 
            if(method == nil)then 
                _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP"); 
            end 
            local _GET = {} 
            if (vars ~= nil)then 
                for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do 
                    _GET[k] = v 
                end 
            end 
            buf = buf.."<h1> 远程控制系统 </h1>"; 
            buf = buf.."<p>GPIO0 <a href="?pin=ON1"><button>ON</button></a> <a href="?pin=OFF1"><button>OFF</button></a></p>"; 
            buf = buf.."<p>GPIO2 <a href="?pin=ON2"><button>ON</button></a> <a href="?pin=OFF2"><button>OFF</button></a></p>"; 
            local _on,_off = "","" 
            if(_GET.pin == "ON1")then 
                  gpio.write(led1, gpio.HIGH); 
            elseif(_GET.pin == "OFF1")then 
                  gpio.write(led1, gpio.LOW); 
            elseif(_GET.pin == "ON2")then 
                  gpio.write(led2, gpio.HIGH); 
            elseif(_GET.pin == "OFF2")then 
                  gpio.write(led2, gpio.LOW); 
            end 
            client:send(buf); 
            client:close(); 
            collectgarbage(); 
        end) 
    end) 

我在引脚上面加了个继电器来控制一个插座,效果还挺不错的。

支付宝打赏 微信打赏
  
I want to comment

Search

Leave a message
http://blog.rjxj513.com/
User login
You have not written any reviews yet!
You have commented!
Can only praise once!
You have a collection!