Post your best Lua script examples here

User avatar
By kamal sonani
#53681 In 8051 and some of other controller , i able to toggle pin with single button like
Code: Select allbit Button = P0^0;  // define port 0 >> 0 pin
bit  Led     = P0^1;  // define output pin for led

void main (void)
{
Led = 0;
  while(1)
  {
     if (Button == 0 )  // check that button is pressed or not
       {
         Led = ~Led;
       }
  }
}


using this code toggle led on and off with single button.
how to write same as in LUA?

what i actually want to do >>

Code: Select allled1 = 4
led2 = 6
led3 = 1
led4 = 2
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
gpio.mode(led3, gpio.OUTPUT)
gpio.mode(led4, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(5555,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.."<h1>Relé WIFI </h1>";
        buf = buf.."<p>PORTÃO <a href=\"?pin=ON1\"><button>ON</button></a> <a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
        buf = buf.."<p>LUZES 1 <a href=\"?pin=ON2\"><button>ON</button></a> <a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
        buf = buf.."<p>LUZES 2 <a href=\"?pin=ON3\"><button>ON</button></a> <a href=\"?pin=OFF3\"><button>OFF</button></a></p>";
        buf = buf.."<p>LUZES 3 <a href=\"?pin=ON4\"><button>ON</button></a> <a href=\"?pin=OFF4\"><button>OFF</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.HIGH);
        elseif(_GET.pin == "ON1")then
              gpio.write(led1, gpio.LOW);
               if(_GET.pin == "ON1")then
                  tmr.delay(500000);
                   gpio.write(led1, gpio.HIGH);
               end
        elseif(_GET.pin == "OFF2")then
              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "ON2")then
              gpio.write(led2, gpio.LOW);
        elseif(_GET.pin == "OFF3")then
              gpio.write(led3, gpio.HIGH);
        elseif(_GET.pin == "ON3")then
              gpio.write(led3, gpio.LOW);
        elseif(_GET.pin == "OFF4")then
              gpio.write(led4, gpio.HIGH);
        elseif(_GET.pin == "ON4")then
              gpio.write(led4, gpio.LOW);     
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)



using this code when i want to change the status of pin , press OFF or ON from browser ,
but using invert logic, payload of server is same but work is changed.
so how its done? any idea about invert logic in LUA....


other thing that i want to know that where i found conn:on srv:listen , client:send , client:close(); , collectgarbage(); GET{} POST - method

this type of documentation? i mean what parameters is used for call function for esp8266 web-server?