Post your best Lua script examples here

User avatar
By Mmiscool
#22966 how can i terminate the task.lua web server and launch a different script that has its own server by sending a url.

The flowing modification dose not work for me.


when ever I run i receive an error.
only one tcp server allowed

How can i close the first server in my first script?

Code: Select allled1 = 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.."<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);
        elseif(_GET.pin == "configesp")then
              client:close();
              srv:close();
              dofile(myOtherScript);
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)
User avatar
By Vincent Lê
#30775 A general question is the persistency of the SSID+PWD credentials.

After the http post request, you set them with
Code: Select allwifi.sta.config(args.ssid, args.passwd)
then you restart the ESP.

Then the ESP knows its SSID+PWD and connects at startup.

My first question is: how are the ssid and password stored in the memory?

Then, second question: is this reliable? I don't want my user to re-enter setup mode if someting happended but the network hasn't changed. To be sure, I would store those in a file containing parameters, then load this file at startup.
User avatar
By Mauricio
#32203 hello

I tried to upload these scripts, using ESPlorer

but recieve some error message when uploading init.lua

stdin:1: ')' expected near ']'
>w([[cleanup()]]);

after clicking "Accept" to the message, the file uploaded but at the end I can see: "not enough memory"

can some one give me some idea on how to solve this ?

by the way, when the module restart, It serve the page asking for credentials to connect to my local AP, when I give them to it, I can see at ESPlorer the message:

"PANIC: unprotected error in call to Lua API (not enough memory)"

but this is strange to me because I just flashed the module an it has nothing but the three scripts you posted

thanks in advance