wifi.setmode(wifi.STATION);
wifi.sta.config("ASUS","");
wifi.sta.autoconnect(1);
gpio.mode(4, gpio.OUTPUT);
gpio.write(4, gpio.LOW);
tmr.alarm(0, 1000, 1, function()
ip = wifi.sta.getip()
if ip~=nil then
print(ip)
tmr.stop(0)
HTTPd()
else
print("no connect")
end
end)
function HTTPd()
srv=net.createServer(net.TCP);
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
print(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> Hello, NodeMcu.</h1><form src=\"/\">Turn PIN1 <select name=\"pin\" onchange=\"form.submit()\">";
local _on,_off = "","";
if(_GET.pin == "ON")then
_on = " selected=true";
gpio.write(4, gpio.HIGH);
elseif(_GET.pin == "OFF")then
_off = " selected=\"true\"";
gpio.write(4, gpio.LOW);
end
buf = buf.."<option".._off..">OFF</option><option".._on..">ON</opton></select></form>";
client:send(buf);
client:close();
collectgarbage();
end)
end)
end