Post your best Lua script examples here

User avatar
By gerardwr
#3729 I’m going crazy!
I have been struggling all day with getting node.input and node.output working in my webserver code.

I give up, I need help from a REAL expert.

What do I want to do:
Build a webserver that can:
1. display html files in a browser (done)
2. execute Lua statements embedded in a html file and display the result (normally going to the serial console) in the browser (the problem).

This is the file htmllua.html. Note the <?lua and ?> lines aroound the lua statements
Code: Select all<html>
<body>
<BR>
    Hello. This HTML page contains a lua script<BR>
<?lua
print("GPIO0="..gpio.read(8))
print("Chip="..node.chipid())
print("Heap="..node.heap())
print("Lua executor : All lines in HTML file excuted")
?>
</body>
</html>


This is the interesting part of the code.
Code: Select all    53         repeat
    54           local line=file.readline()
    55           if line then
    56             -- strip the \n at the end of the line
    57             line = string.gsub(line,"\n","")
    58        if line == "<?lua" then
    59               node.output(s_output,1)
    60               while line ~= "?>" do
    61                 line=file.readline() line = string.gsub(line,"\n","")
    62                 node.input(line)
    63                 node.input(line.."\r\n")
    64                 print("Lua:"..line.."|")
    65               end
    66               -- read the next line to process
    67               line=file.readline()
    68               line = string.gsub(line,"\n","")
    69             end
    70             node.output(nil)
    71             print("Html:"..line) conn:send(line)
    72           end
    73         until not line


This the console output. Note that the Lua lines are recognised, BUT only the 1st Lua statement (the GPIO) is executed, and the result is displayed at the end of the output. Then a „>” follows, the prompt?
Code: Select allGET /htmllua.html HTTP/1.1
Host: 192.168.0.27
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/<snip>
Html:<html>
Html:<body>
Html:<BR>
Html:Hello. This HTML page contains a lua script<BR>
Lua:print("GPIO0="..gpio.read(8))|
Lua:print("Chip="..node.chipid())|
Lua:print("Heap="..node.heap())|
Lua:print("Lua executor : All lines in HTML file excuted")|
Lua:?>|
Html:</body>
Html:</html>
GPIO0=1
>


Here is the entire lua code
(3.01 KiB) Downloaded 700 times


My specific questions are:
- Why is just ONE lua statement correctly processed, and following ones NOT
- Why is the output of the processed statement at the END of the output, and not at the output position when the statement was recognized and executed.

I can imagine making a stupid mistake, my entire Lua experience is based on the working the last weeks with nodeMcu.

Help me find the „stupid mistake”. Please!
Attachments
(3.01 KiB) Downloaded 670 times
User avatar
By alonewolfx2
#3733 i think node.input and output working async. we have a delay for every executed lua lines in "<?lua ?> "
can you try delay in for loop?
edit: i dont try because my router reject my esp's and i dont know why :oops:
User avatar
By gerardwr
#3738
alonewolfx2 wrote:i think node.input and output working async. we have a delay for every executed lua lines in "<?lua ?> "
can you try delay in for loop?
edit: i dont try because my router reject my esp's and i dont know why :oops:


Maybe you are right, let's hope that someone can help us here.

BTW: I found a basic Lua way to execute a statement in a string and get the result in a string. I changed the execution of the string using node.input and node output to:
Code: Select allif line then
  linef=loadstring(line)
  print(linef())
end


See the result :shock:
All Lua statements embedded in my html are recognized AND executed AND the result is in a string variable.
Code: Select allHtml:<html>
Html:<body>
Html:<BR>
Html:Hello. This HTML page contains a lua script<BR>
GPIO0=1

Lua:print("GPIO0="..gpio.read(8))|
Chip=10116099

Lua:print("Chip="..node.chipid())|
Heap=7040

Lua:print("Heap="..node.heap())|
Lua executor : All lines in HTML file excuted

Lua:print("Lua executor : All lines in HTML file excuted")|


The Node.input/output took me the entire day, the solution above took me 15 minutes. Shorter = better.

Still want to know what is wrong with my code using node.input/output though!