Post your best Lua script examples here

User avatar
By gerardwr
#3992 Will study your previous message on multiline Lua processing later.

Tried your new code, and it works fine, thanks for the update. Hope more will follow!
- the error message in case of an error in a Lua statement is very useful. Had the pleasure of getting some informative error messages while testing ;)
- html and TXT handled nicely
- printing fo GET /filename is nice for debugging (ref. local requestToConsole = true;)
BTW : In your previous version I added "print(payload" in the beginning of the processing function. That's VERY informative for debugging. Would you consider adding a "payloadToConsole" too?

Played with the new code, making a framework for my own, it consists of 4 pages. Below I have added sreendumps of the 4 pages and a ZIP, possibly someone else can benefit from it. I Intend to extend the (now) basic pages further.
- indexgwr.pht to be copied to index.pht, with links to the other pages (inspired by your index,pht)
- files.pht, for file matters
- io.pht, for GPIO/ADC matters
- network.pht, for network matters.

NOTE When the # of files in the files page gets too long (above 7-8 files) the ESP throws the following error:
Code: Select allPANIC: unprotected error in call to Lua API (init.lua:98: need <256 payload)

I imagine this is caused by limitations of the API conn:send command, can't imagine it is in webserver code. Raised a Problem, in the Problem section, but no answer yet.

Schermafbeelding 2014-12-04 om 21.34.53.png

Schermafbeelding 2014-12-04 om 21.35.50.png

Schermafbeelding 2014-12-04 om 21.35.36.png

Schermafbeelding 2014-12-04 om 21.35.07.png


Sources for the files:
(2.25 KiB) Downloaded 338 times
User avatar
By gerardwr
#4007
adamjp wrote:In Elua webserver the files are read fully before processing, we on the esp lua implimentation can only read files in a line at at time.


Hi Adam,

In a previous life (about 1 week ago) when you published your webserver I was happily working on my own webserver code that had the ability to process multiline lua statements. When I read the elua page, I saw that they had implemented my idea EXACTLY. I'm sure they had now knowledge on my ideas, as I had no idea on theirs.

Please find below the code I used, that worked, perhaps you can use it for some inspiration. Here's a message that shows that the multiline is executed OK, and the result of each line copied to a string.
http://www.esp8266.com/viewtopic.php?f=19&t=611&start=60#p3760

The "last" remaining problem I had was sending this string to the browser too. I messed about with node.input and node.output but could not get this working. Then I had a look at the elua webserver code and saw the do code function, that solved this "last" problem. I was just starting to add the docode function to my webserver, and then I wondered " What if, if we migrate the elua example". When pondering about that : YOU PUBLISHED THE MIGRATED CODE! The rest is history ;)

The code that processes the .html file with possibly multiline lua scripts in it. I had no support for 1-liners.
Code: Select all      file.open(s,"r")

      repeat
        local line=file.readline()
        if line then
          -- strip the \n at the end of the line
          line = string.gsub(line,"\n","")
          if line == "<?lua" then
            -- skip to the 1st Lua line
            line=file.readline() line = string.gsub(line,"\n","")

            while line ~= "?>" do
            if line then
              print("[Lua-in]"..line)
              linef=loadstring(line)
              ln=linef()
              if ln then ln = string.gsub(ln,"\n","") print("[Lua-out]") print(ln) conn:send("Lua-output<BR>") conn:send(ln) end
            end -- if
              line=file.readline() line = string.gsub(line,"\n","")

            end -- while
            -- read the next line to process
            line=file.readline()
            line = string.gsub(line,"\n","")
          end -- if
          print([Html]"..line) conn:send(line)
        end -- if
      until not line
      file.close()
User avatar
By Erni
#4128 I am wondering it it would be possible to output a variable to serial.
As it is I get the name of the requested page on serial.

The reason I ask is taht this feature would be nice if I wanted to control a external mcu by sending commands and variables over serial.
I could propably do this by using I2C, but I think a serial communication is simpler, atleast from my point of view.