Chat freely about anything...

User avatar
By dwhacks
#17421 So basically I want to serve up a simple page with two buttons to toggle a LED. I have it working as it should, but sometimes, randomly, it cannot connect to the server. If I leave it for a while, without resetting the module, it will randomly load the page again.

Maybe it's something simple, or maybe its not. Here's my code anyway:
Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("MyNetwork","MyPassword")
tmr.stop(1)
print(wifi.sta.getip())
led1 = 3
led2 = 4
--gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,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.."HTTP/1.1 200 OK\n\n";
        buf = buf.."\n";
        buf = buf.."<!DOCTYPE html>";
        buf = buf.."<html>";
        buf = buf.."<head>";
       buf = buf..'<meta  content="text/html; charset=utf-8">';
        buf = buf.."<title>Shower</title>";
        buf = buf.."</head>";
        buf = buf.."<body>";
        buf = buf.."<h1> Shower Web Server</h1>";
        buf = buf.."<p>Shower In Use <a href=\"?pin=ON2\"><button>YES</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>NO</button></a></p>";
        buf = buf.."</body>";
        buf = buf.."</html>";
        local _on,_off = "",""
        if(_GET.pin == "ON1")then
              gpio.write(led1, gpio.HIGH);
        elseif(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.LOW);
        elseif(_GET.pin == "ON2")then
              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "OFF2")then
              gpio.write(led2, gpio.LOW);
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)