Post your best Lua script examples here

User avatar
By yoman91
#50253 Dear Community!

I'm trying to update a html page with the data (temperature) I receive from a uC. The uC sends the data every 2 seconds. I made a simple password page for some protection, and if the password is OK, then it pass to the temperature page. It works well for about 1 minute, then the ESP8266 freeze, and I can upload the firmware again to make it work. Can somebody help me what is wrong?
Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("default","default")
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,payload)
        gpio.write(led1,gpio.LOW);
        gpio.write(led2,gpio.LOW);

            uart.on("data", "\n",
            function(data)
                gpio.write(led1,gpio.HIGH)
               
                file.open("bob.htm","w")
                if (data~=nil)then
                file.write(data);
                end
                file.close()
                gpio.write(led1,gpio.LOW)
                     
            end, 0)
           
            tgtfile="index.htm"
            dotaz=nil
           
            kdesi={string.find(payload,"mypassword=")}
            if(kdesi[2]~=nil)then
                dotaz=string.sub(payload,kdesi[2]+1,#payload)   
            end

            if(dotaz ~= "1234" )then
           
            tgtfile="index.htm";
           
            elseif(dotaz == "1234")then
               
           
            tgtfile="bob.htm";
            end

            local f = file.open(tgtfile,"r")
            gpio.write(led2,gpio.HIGH);
            if (f ~= nil) then
                client:send(file.read())
                file.close()
            gpio.write(led2,gpio.LOW);
            end
       
      client:close();
      collectgarbage()
    end)
end)


Index.htm:
Code: Select all<html>
<head>
  <title>tomcsii home server</title>
</head>
<style>
a:link    {color:#ff0000; background-color:transparent; text-decoration:none}
a:visited {color:#ff0000; background-color:transparent; text-decoration:none}
a:hover   {color:#ff0000; background-color:transparent; text-decoration:underline}
a:active  {color:#ff0000; background-color:transparent; text-decoration:none}
h1 {color:red;}
h2 {color:blue;}
</style>
<Body style="background-color:lightgrey">
<h1>Home Server by tomcsii</h1>

<h2> Login page:</h2>

<FORM ACTION="" METHOD="POST">

<INPUT TYPE=PASSWORD NAME="mypassword">

<INPUT TYPE=SUBMIT VALUE="Login">
</FORM>

</body>
</html>


Bob.htm:
Code: Select all<html>
<head>
<title>MainPage</title>
</head>
<style>
a:link    {color:#ff0000; background-color:transparent; text-decoration:none}
a:visited {color:#ff0000; background-color:transparent; text-decoration:none}
a:hover   {color:#ff0000; background-color:transparent; text-decoration:underline}
a:active  {color:#ff0000; background-color:transparent; text-decoration:none}
</style>
<Body style="background-color:FAEBD7">
<p style="color:darkblue"><B>ESP8266 server </p></B>
<a href="index.htm">Home</a><BR>
<h2>Temperature: 23.2 C </h2>


</body>
</html>
User avatar
By jankop
#50295 Very bad idea.
1. Repeated frequent writing to flash memory destroy it.
2. For files bob.htm and index.htm you need to add the http header.