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
<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.
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?
GET /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
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!