ESP8266-01之NodeMCU(lua)实现远程控制LED灯

开发环境

ESP-01 wifi模块
LualLoader
NodeMCU

nodemcu_float_0.9.6-dev_20150704.bin
nodemcu_integer_0.9.6-dev_20150704.bin

将ESP-01刷写过NodeMCU固件之后,就可以用lua语言来玩wifi模块了:
这里写图片描述
NodeMCU固件中的GPIO引脚定义如下:
这里写图片描述

使用GPIO2控制LED灯:

wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PWD")
print(wifi.sta.getip())

led = 4
gpio.mode(led, 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> ESP8266 Web Server</h1>";
        buf = buf.."<p>LED <a href=\"?pin=ON\"><button>ON</button></a> <a href=\"?pin=OFF\"><button>OFF</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "ON")then
              gpio.write(led, gpio.HIGH);
        elseif(_GET.pin == "OFF")then
              gpio.write(led, gpio.LOW);
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)

这里写图片描述

参考:http://blog.csdn.net/dingzz/article/details/46876503
参考:http://blog.csdn.net/menghuanbeike/article/details/73504188

猜你喜欢

转载自blog.csdn.net/u011958166/article/details/79227401