IOT Projects

  • Control GPIO

    Controlling the GPIO cross-web server which is from the things esp8266 had a design for
    so in this project, we learn how to make LUA script and control GPIO number 4.
    Code Demonstration
    setup the module to wifi mode for connect it to your router and print the IP of the esp after the connection.
    setmode(wifi.STATION)
    wifi.sta.config("your_router", "your_password")
    print("Server IP Address:", wifi.sta.getip())
    								

    make the GPIO 4 as OUTPUT PIN
    led1 = 4
    gpio.mode(led1, gpio.OUTPUT)
    								

    this function for creating a web page on port 80, you can change the web page port by changing the number
    for example 8080 in case you want to make a forward port for your esp8266.
    srv=net.createServer(net.TCP)
    srv:listen(80,function(conn)
        conn:on("receive", function(client,request)
    								

    I named the variable "buf" for putting the string command of the HTML script which will see it later.
    after that, I put everything after the address of my esp in the variable for example 192.168.1.5/?led=on
    so led=on it will be saved in "vars" variable then compare it in the if loop condition.
    local buf = "";
            local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
            print (vars)
            if(vars == "led1=on")then
                  gpio.write(led1, gpio.HIGH);
            elseif(vars == "led1=off")then
                  gpio.write(led1, gpio.LOW);
                  end
    								

    so after I finish from the if loop, I write the web page script in HTML language.
    		buf = buf..('<html lang="en">'); 
            buf=buf..('</html>'); 
            buf=buf..('<head><titel>Electro-think.de</titel></head>');
            buf=buf..('<body><h1> GPIO control</h1><br>');
            buf=buf..('<p> LIGHT 1 <a href="?led1=on"><button>ON</button></a>');
            buf=buf..(' <a href="?led1=off"><button>OFF</button></a></p>');
            buf=buf..('</boady>');
            buf=buf..('</html>');    
            client:send(buf);
            client:close();
            collectgarbage();
        end)
    end)
    
    								

    the web page took like this Post
    Full LUA code
    						
    setmode(wifi.STATION)
    wifi.sta.config("your_router", "your_password")
    print("Server IP Address:", wifi.sta.getip())
    led1 = 4
    gpio.mode(led1, 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");
            print (vars)
            if(vars == "led1=on")then
                  gpio.write(led1, gpio.HIGH);
            elseif(vars == "led1=off")then
                  gpio.write(led1, gpio.LOW);
                  end
            buf = buf..('<html lang="en">'); 
            buf=buf..('</html>'); 
            buf=buf..('<head><titel>Electro-think.de</titel></head>');
            buf=buf..('<body><h1> GPIO control</h1><br>');
            buf=buf..('<p> LIGHT 1 <a href="?led1=on"><button>ON</button></a>');
            buf=buf..(' <a href="?led1=off"><button>OFF</button></a></p>');
            buf=buf..('</boady>');
            buf=buf..('</html>');    
            client:send(buf);
            client:close();
            collectgarbage();
        end)
    end)