开发环境
ESP-01 wifi模块
LualLoader
NodeMCUnodemcu_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