-Basic HTTP Authentication
-A button to switch GPIO2
-SOFTAP network mode
Thanks In advance.
Explore... Chat... Share...
function setState(state)
if(state == "ON")then
gpio.write(4, gpio.HIGH);
elseif(state == "OFF")then
gpio.write(4, gpio.LOW);
end
end
pin = 4;
gpio.mode(pin, gpio.OUTPUT);
file.open("pin", "r");
setState(file.readline());
file.close();
wifi.setmode(wifi.SOFTAP);
wifi.ap.config({ssid="test",pwd="12345678"});
srv=net.createServer(net.TCP);
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local buf = "HTTP/1.0 200 OK\r\n\r\n";
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)%?(.-) HTTP");
if(method == nil)then _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP"); end
local _, _, auth = string.find(request, "%cAuthorization: Basic ([%w=\+\/]+)");--Authorization:
if (auth == nil or auth ~= "dXNlcjoxMjM0")then --user:1234
client:send("HTTP/1.0 401 Authorization Required\r\nWWW-Authenticate: Basic realm=\"NodeMCU\"\r\n\r\n<h1>Access DENIDED</h1>");
client:close();
return;
end
local _GET = {};
if (vars ~= nil)then for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do _GET[k] = v end end
buf = buf.."<meta name=\"viewport\" content=\"initial-scale=1.5\"/><h3>NodeMcu Тест.</h3><form src=/>Состояние GPIO2 <select name=pin onchange=\"form.submit()\">";
local _on,_off = "","";
if(_GET.pin == nil)then
if(gpio.read(pin) == gpio.HIGH)then
_GET.pin = "ON";
else
_GET.pin = "OFF";
end
end
local tr = " selected=true";
if(_GET.pin == "ON")then
_on = tr;
elseif(_GET.pin == "OFF")then
_off = tr;
end
setState(_GET.pin);
file.open("pin", "w+");
file.write(_GET.pin);
file.close();
buf = buf.."<option value=\"ON\"".._on..">ВКЛ</opton><option value=\"OFF\"".._off..">ОТКЛ</option></select></form>";
client:send(buf);
client:close();
collectgarbage();
end)
end)
It takes about 20-25 seconds for home assistant c[…]
I tried to upgrade tof my sonoff basic R2 with the[…]
a problem Perhaps you want to define "Probl[…]
Rebooting your router will not give you a faster I[…]
There are no other notifications from esptool.py i[…]
Using the Arduino IDE, you'll learn how to set up […]
In this project, you will post to Twitter using an[…]
In this project, we will build a water level contr[…]
I guess I'm late, but I had the same problem and f[…]
Last night I received my first D1 Minis for a lear[…]
Although I am aware that this is an old post, I fe[…]