Advertisement
Guest User

ESP8266 WebServer

a guest
May 11th, 2015
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.13 KB | None | 0 0
  1. wifi.setmode(wifi.STATION)
  2. wifi.sta.config("YourSSID","YourPASSWORD")
  3. print(wifi.sta.getip())
  4. rele = 1;
  5. gpio.mode(rele, gpio.OUTPUT)
  6.  
  7. srv=net.createServer(net.TCP)
  8. srv:listen(80,function(conn)
  9.     conn:on("receive", function(client,request)
  10.         local buf = "";
  11.         local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
  12.         if(method == nil)then
  13.             _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
  14.         end
  15.         local _GET = {}
  16.         if (vars ~= nil)then
  17.             for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
  18.                 _GET[k] = v
  19.             end
  20.         end
  21.         buf = buf.."<h1> ESP8266 Web Server</h1>";
  22.         buf = buf.."<p><a href=\"?rele=ON\"><button>OPEN</button></a></p>";
  23.        
  24.         local _on,_off = "",""
  25.         if(_GET.rele == "ON")then
  26.               print("Gate Open");
  27.               gpio.write(rele, gpio.HIGH);
  28.              tmr.alarm(0, 400, 0, function() gpio.write(rele, gpio.LOW); print("Rele OFF"); end )
  29.         end
  30.         client:send(buf);
  31.         client:close();
  32.         collectgarbage();
  33.     end)
  34. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement