As the title says... Chat on...

User avatar
By icsyzet
#60462 Hi, i`m having some trouble reading and displaying the gpio after changing the value 1/0 , I used an example found on internet and tried to modify for learning purposes.

Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("yyy","xxxxxxxxx")
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.."<html>";
        buf = buf.."<head><title>OxY`s ESP8266 Server</title></head>";
        buf = buf.."<body>";
        buf = buf.."<body style=background-color:powderblue>";
        buf = buf.."<center>";
        buf = buf.."<BR>";
        buf = buf.."<h1> ESP8266 Web Server by OxY</h1>";
        buf = buf.."<p>GPIO0 <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
        buf = buf.."<p>GPIO2 <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
        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
        ValGpio_0 = gpio.read(led1);
        ValGpio_1 = gpio.read(led2);
        if(ValGpio_0 == 0)then
              buf = buf.."<font color='red'>GPIO_0=OFF</font>";
              buf = buf.."<br>";
        elseif(ValGpio_0 == 1)then
              buf = buf.."<font color='green'>GPIO_0=ON</font>";
              buf = buf.."<br>";
        elseif(ValGpio_1 == 0)then
              buf = buf.."<font color='red'>GPIO_1=OFF</font>";
              buf = buf.."<br>";
        elseif(ValGpio_1 == 1)then
              buf = buf.."<font color='green'>GPIO_1=ON</font>";
              buf = buf.."<br>";
        end                           
        buf = buf.."</center>";
        buf = buf.."</body>";
        buf = buf.."</html>";
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)


What is displaying is this :

Image

Thanks.
User avatar
By Nadi
#64131 Hi, The script works for me. But your attachment is not available for me to look into. Let me know if even today you are struggling with this problem.