2019-12-31 00:53:54 +01:00
|
|
|
do
|
2016-01-07 23:08:44 +01:00
|
|
|
wifi.setmode(wifi.SOFTAP)
|
2018-06-04 13:16:20 +02:00
|
|
|
wifi.ap.config({ ssid = "test", pwd = "12345678" })
|
2015-01-08 10:06:50 +01:00
|
|
|
gpio.mode(1, gpio.OUTPUT)
|
2019-12-31 00:53:54 +01:00
|
|
|
local srv = net.createServer(net.TCP)
|
2018-06-04 13:16:20 +02:00
|
|
|
srv:listen(80, function(conn)
|
|
|
|
conn:on("receive", function(client, request)
|
|
|
|
local buf = ""
|
2019-12-31 00:53:54 +01:00
|
|
|
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP") -- luacheck: no unused
|
2018-06-04 13:16:20 +02:00
|
|
|
if (method == nil) then
|
2019-12-31 00:53:54 +01:00
|
|
|
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP") -- luacheck: no unused
|
2018-06-04 13:16:20 +02:00
|
|
|
end
|
2019-12-31 00:53:54 +01:00
|
|
|
|
2018-06-04 13:16:20 +02:00
|
|
|
local _GET = {}
|
|
|
|
if (vars ~= nil) then
|
|
|
|
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
|
|
|
|
_GET[k] = v
|
|
|
|
end
|
|
|
|
end
|
2019-12-31 00:53:54 +01:00
|
|
|
buf = buf .. "<!DOCTYPE html><html><body><h1>Hello, this is NodeMCU.</h1>"
|
|
|
|
.. "<form src=\"/\">Turn PIN1 <select name=\"pin\" onchange=\"form.submit()\">"
|
2018-06-04 13:16:20 +02:00
|
|
|
local _on, _off = "", ""
|
|
|
|
if (_GET.pin == "ON") then
|
|
|
|
_on = " selected=true"
|
|
|
|
gpio.write(1, gpio.HIGH)
|
|
|
|
elseif (_GET.pin == "OFF") then
|
|
|
|
_off = " selected=\"true\""
|
|
|
|
gpio.write(1, gpio.LOW)
|
|
|
|
end
|
|
|
|
buf = buf .. "<option" .. _on .. ">ON</option><option" .. _off .. ">OFF</option></select></form></body></html>"
|
|
|
|
client:send(buf)
|
|
|
|
end)
|
|
|
|
conn:on("sent", function(c) c:close() end)
|
2015-01-08 10:06:50 +01:00
|
|
|
end)
|
2019-12-31 00:53:54 +01:00
|
|
|
end
|