Post your best Lua script examples here

User avatar
By gerardwr
#4147
Erni wrote: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.


I see no possibility in the current code to print a variable to the console.

PRINT statements in embedded Lua scripts are now "printed" to the browser page only. For "printing" to console, you would have to change the server code.

For instance: If you want EVERYTHING that is "printed" to the browser ALSO to print on the console, add "print" lines after lines 48 and 50 in the adamjp source of init.lua, something like this:
Code: Select all                         if type == "pht" then
                                conn:send(line:gsub(tags, docode));
                                print(line:gsub(tags, docode));
                         else
                                conn:send(line);
                                print(line);
                         end


NOTE 1 : This might give you more output to the console than you would like!!.

NOTE 2 : Now you will have your own version of init.lua, when adamjp publishes new versions, you will have to adapt to your local changes again.

NOTE 3 : This is GREAT for debugging output to the browser!
User avatar
By gerardwr
#4175 I have been studying the source from adamjp in more detail. I've learned:
- many new tricks in Lua (having a 1-week experience in Lua, everything is "New")
- adamjp knows his coding, excellent.

Being curious and impatient I made some (small) changes to the source code, to eliminate some unwanted behavior (at least for me!).

Below the changes I made to the source code, and the changes I am intending to make, unless adamjp beats me to it (please do Adam ;) )
I have not uploaded the changed source code, I think that having only 1 baseline (adamjp's GITHUB) serves the community better. OK?

Code: Select allCHANGES made in init.lua (version dec 14, 2014)

[reason] luatool uploader tool raises an error when uploading init.lua, on the 1st char (space?) in the file
[change] deleted 1st char in file init.lua

[reason] want to use function docode also in the console, for testing, but it is defined as LOCAL
[action] removed LOCAL from function docode

[reason] need more debugging info while ongoin test
[action] added printing of full HTTP request
[action] added print statement in function sendfilecontents to print output to console too.

[reason] execution of .lua file is not implemented, I believe it was implemented in the 1st version?
[action] code added for execution code in .lua file, output of every line is going to the browser



Code: Select allINTENDED CHANGES

[reason] requests with params (like /GET test.pht?var1=„hello”&var2=„gerard”) are not supported, code is in source, but commented out]
[intended action] full request including params to be made available to .pht files for further processing in HTML/embedded-Lua. Not sure how I'm going to approach this, give me some inspiration, please!

[reason] multi-line embedded lua scripts in .pht files not supported
[intended action] support embedded multiline lua scripts in .pht files by offering docode NOT just 1 line but multiple lines. The idea is to replace the READLINE (in case of a <?lua line) by a series of READLINES to a string, and then pass this string to docode.

[reason] execution of .lua file and embedded Lua code in .pht files result in correct output, but is concluded with a CRLF? This becomes a blank line in the HTML to the browser, and this sometimes causes problems.]
[intended action] Changed the line in the code to suppress the \n at the end of the output.. Commented it out for now, need some further testing to ensure that it creates no other problems.
User avatar
By Erni
#4181 Thanks gerardwr
That works, but as you say I get more console output than I want, but good for debugging.

I had a look at the newest (I wish there was a version number ) init.lua
If you put this code after line 81
Code: Select allfor k,v in pairs(reqdata) do
print (k,v)
end

You get a nice output.
If for example i put in
Code: Select allhttp://192.168.0.107/?led3=1


I get this

Code: Select allindex.pht
led3   1
favicon.ico


And that would be easy to interprete for an extaernal mcu
User avatar
By yes8s
#4184 I've been testing this implementation for a few days now and for the most part it works great. I did make a couple of little modifications to better handle post requests and I'm also running a few additional functions for my applications.

When I embed lua code into the pht file I get a reset when I request the page from a browser. I've narrowed it down to the docode function being the cause. If I replace the function to just print heap to the console and return back the non decoded text, it is ok. I'm suspecting it may be a memory issue.

The heap is at about 5kb at the docode function that has been stripped back (as explained above). Obviously, when the original function is used the heap usage would be greater due to additional code and variables.

I'm curious, what is your heap at when you get to this function? Can somebody do a print heap inside the function and report?

Memory is starting to become an issue when you try to build an application that has a server and other functions and variables. The server part alone on my esp consumes over half the heap. I'm worried we've hit the wall too early.