Post your best Lua script examples here

User avatar
By gerardwr
#3947
gerardwr wrote:I have had a pleasant few hours testing the migrated elua webserver. I REALLY, REALLY like it. It’s much more robust and complete as my own developed webbrowser (in the Examples section). Will probably put my own code to "rest" and continue with this as baseline.

It basically works very smoothly. More specific:
- nice and stable
- .htm pages display fine
- .pht files with single-line lua statements embedded in HTML work fine
- .lua files seems to be executed
- .pht files display fine
- new features is a matter of writing HTML/PHT and uploading. No reboot required! Great for prototyping.

What I would like having improved:
- .html files lead to a PANIC reset, both .htm and .html should be accepted
- multiline Lua code is not working now. It's listed on the elua web as a feature, and is used in the example.pht. Probably a bug.
- larger print output (>256 bytes) of an embedded Lua lines leads to a PANIC reset.
- when .lua page is requested it seesm to be executed, but results are not printed to the serial console, as expected. Returned page to the browser is empy.
- when embedded Lua is faulty in browser is printed :>>> Invalid Lua code <<< 1. Please add the line that was faulty both in the browser and console.- provide some debugging in the serial console, like http request. Now testing more or less a shot in the dark.
- if file exists, but NOT .lua or .htm or .html, display the content in the browser. Great to check log files, if you have any.

The list of improvements seems longer than the „like” list. Don’t let this discourage you, is my enthusiasm on the usability of this code for "real world" web based applications. The combination of HTML and LUA is "golden" for that. The pro’s outweigh the cons easily.

Adamjp has done excellent work here, thanks for that!

For reference my updated index.pht and the browser screen it produces. Notice the last "funny" line, it's output from a multiline Lua script.

Schermafbeelding 2014-12-03 om 15.50.08.png


index.pht.zip
User avatar
By adamjp
#3956 Well I've hacked a few off the list:

Done
post works (except for safari mobile, odd esp reboot), still problem with original query string parsing code hogging memory, commented out.
Memory Usage quite stable.
Add bool for console output on request (output on eg: GET:index.pht, MemUsage:2504 (5880))
Supported files by default:
txt = "text/plain",
htm = "text/html",
pht = "text/html",
lua = "text/html",
html = "text/html"
(Script type is pht)
Error in lua script tag code with give some indication of the error, may be a better way to do this.

TODO
multi line script tags.
Update parsing query string and post data parameters.

Suggestions welcome.

Cheers.
Attachments
2014-12-04 webserver
(2.25 KiB) Downloaded 315 times
User avatar
By gerardwr
#3966 Thanks Adam, will have fun tonight ;)

Did some tests with multiline Lua code, but had no luck (see below).

On the Elua lhttpd page they state that multiline is a feature, and the example .pht on the same page has this code in it:
Code: Select all  32 <?lua
  33   pio.pin.setdir( pio.OUTPUT, pio.PF_0 )
  34   if reqdata['ledon'] then
  35     pio.pin.sethigh( pio.PF_0 )
  36     print '<br><font color="blue"><b>The LED is now ON</b></font><br>'
  37   elseif reqdata['ledoff'] then
  38     pio.pin.setlow( pio.PF_0 )
  39     print '<br><font color="blue"><b>The LED is now OFF</b></font><br>'   
  40   elseif next(reqdata) ~= nil then
  41     print '<br><font color="red"><b>Invalid CGI request!</b></font><br>'   
  42   end
  43 ?>


But, after I quick check of the code I could not find any proof in the code that it is handled this way. Will have a better look tonight.

Some test I did, and failed
Code: Select all:===================
This results in error:
<?lua print("Lua is counting from 1 to 3 in a 3-line for loop :<BR>")
<?lua  for i=1,3 do ?>
<?lua    print(i.." ") ?>
<?lua  end ?>

PANIC: unprotected error in call to Lua API ([string "    print(i.." ") "]:1: attempt to concatenate global 'i' (a nil value))
===================

This is really annoying. Splitting a multiline lua script in multiple one-liners is not a good workaround. Each one-liner seems to be executed in it's own environment, hiding variables from the next one-liner. Could this be caused by the "local" docode function?

Code: Select all<?lua
 print("Free Ram: "..node.heap().."<br>")
 print("Chip Id : "..node.chipid().."<br>")
 print("Heap    : "..node.heap().."<br>")
?>

But it leads to console message:
htmllua.html
PANIC: unprotected error in call to Lua API (init.lua:87: attempt to concatenate field '?' (a nil value))
PANIC: unprotected error in call to Lua API (attempt to call a nil value)
._..RS..FjS.fJS.f.
NodeMcu 0.9.2 build 20141202  powered by Lua 5.1.4
==========================
>


Man!, your code is NOT " a few steps in the right direction", it's a giant leap!

Greetings, Gerard.
User avatar
By adamjp
#3987 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.

the Elua docode function works as expected: test in http://www.lua.org/cgi-bin/demo

Code: Select allfilecontents = [[
<h1>Hello</h1>
<div>single</div>
<?lua print("world") ?>
<?lua
  pio.pin.setdir( pio.OUTPUT, pio.PF_0 )
  if reqdata['ledon'] then
    pio.pin.sethigh( pio.PF_0 )
    print '<br><font color="blue"><b>The LED is now ON</b></font><br>'
  elseif reqdata['ledoff'] then
    pio.pin.setlow( pio.PF_0 )
    print '<br><font color="blue"><b>The LED is now OFF</b></font><br>'   
  elseif next(reqdata) ~= nil then
    print '<br><font color="red"><b>Invalid CGI request!</b></font><br>'   
  end
?>
<br>
]]

for code in string.gmatch(filecontents,"<%?lua(.-)%?>") do
 print('Found code block');
 print(code)
end


the code above finds single and multi line statements and is how the docode functions on the whole file content. In our case because of the constraints of the module cannot read the full file before sending.

On a side note, there is a function in lua which is dofile:

try this in your pht script and see how it works:

<?lua dofile('myfile.lua') ?>


myfile.lua could contain multiple lines I believe.

Cheers.