Advertisement
Guest User

Untitled

a guest
Jan 7th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. wifi.setmode(wifi.STATION)
  2. wifi.sta.config("MOTO","nina")
  3. print(wifi.sta.getip())
  4. led1 = 3
  5. led2 = 4
  6. gpio.mode(led1, gpio.OUTPUT)
  7. gpio.mode(led2, gpio.OUTPUT)
  8. srv=net.createServer(net.TCP)
  9. srv:listen(80,function(conn)
  10. conn:on("receive", function(client,request)
  11. local buf = "";
  12. local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
  13. if(method == nil)then
  14. _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
  15. end
  16. local _GET = {}
  17. if (vars ~= nil)then
  18. for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
  19. _GET[k] = v
  20. end
  21. end
  22. buf = buf.."<h1> ESP8266 Web Server</h1>";
  23. buf = buf.."<p>GPIO0 <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
  24. buf = buf.."<p>GPIO2 <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
  25. local _on,_off = "",""
  26. if(_GET.pin == "ON1")then
  27. gpio.write(led1, gpio.HIGH);
  28. elseif(_GET.pin == "OFF1")then
  29. gpio.write(led1, gpio.LOW);
  30. elseif(_GET.pin == "ON2")then
  31. gpio.write(led2, gpio.HIGH);
  32. elseif(_GET.pin == "OFF2")then
  33. gpio.write(led2, gpio.LOW);
  34. end
  35. client:send(buf);
  36. client:close();
  37. collectgarbage();
  38. end)
  39. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement