Post your best Lua script examples here

User avatar
By gerardwr
#3617
alonewolfx2 wrote:i agree. i attached my version of webserver.lua. it can execute lua command. i think you can merge with your version.


I want to have look to your solution of the embedded Lua code, but when downloaded the ZIP file is NOT recognized as a valid ZIP file. Can you check yourself, please?
User avatar
By alonewolfx2
#3620 i checked zip file on my computer and i downloaded on attachment. both worked.
i changed this line.
Code: Select allline = string.gsub(line,"\n","")
          if line == "<?lua" then  node.input(file.readline().."\r\n") function s_output(str) if (conn~=nil) then conn:send(str) end end node.output(s_output,1) end
          if line == "?>" then end
          conn:send(line)



gerardwr wrote:
alonewolfx2 wrote:i agree. i attached my version of webserver.lua. it can execute lua command. i think you can merge with your version.


I want to have look to your solution of the embedded Lua code, but when downloaded the ZIP file is NOT recognized as a valid ZIP file. Can you check yourself, please?
User avatar
By gerardwr
#3621 [quote="alonewolfx2"]i checked zip file on my computer and i downloaded on attachment. both worked.
i changed this line.
Code: Select allline = string.gsub(line,"\n","")
          if line == "<?lua" then  node.input(file.readline().."\r\n") function s_output(str) if (conn~=nil) then conn:send(str) end end node.output(s_output,1) end
          if line == "?>" then end
          conn:send(line)


My mistake. I expected a file webserver.zip, but my Mac automagically unzips the downloaded file and presto : webserver.lua appears. My bad!

Your solution for execution an embedded Lua line looks amazingly simple, thanks for that!

I will inject it into my code. After testing I will upload the new version here. When I get some confirmation that it (more or less) is OK I will post it in a [RELEASE] topic.

Appreciate your help!
User avatar
By gerardwr
#3660
alonewolfx2 wrote:i changed this line.
Code: Select allline = string.gsub(line,"\n","")
          if line == "<?lua" then  node.input(file.readline().."\r\n") function s_output(str) if (conn~=nil) then conn:send(str) end end node.output(s_output,1) end
          if line == "?>" then end
          conn:send(line)



I added your code to mine. Looks simple but I did not understand it at first. I have split the "one-liner" to separate lines for better understanding.
Code: Select all      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
            node.input(file.readline().."\r\n")
            function s_output(str)
              if (conn~=nil) then conn:send(str) end
            end
           node.output(s_output,1)
          end
          if line == "?>" then end
        end
      until not line


1.It seems that in your code the 1st line AFTER the "<?lua" line is executed, but the remaining lines are just printed, is that correct?
2.It seems that result from the executed line is just printed to the console, not to send to the browser.
Can you confirm my assumptions 1. and 2.?

I want all of the lua lines between "<?lua" and "?>" executed and the result of each executed line printed to the serial console and to the browser.

For testing I now use the following htmllua.html file
Code: Select all<html>
<body>
<BR>
    Hello. This HTML page contains a lua script<BR>
<?lua
print("Lua line in HTML file excuted")
print("GPIO0="..gpio.read(8))
print("Chip="..node.chipid())
print("Heap="..node.heap())
?>
</body>
</html>


Then I would expect as output in the browser:
Code: Select all    Hello. This HTML page contains a lua script
Lua line in HTML executed
GPIO0=1
Chip=123456
Heap=1234


If you have any time, would you have look at it too, together we can crack this egg!

Almost there……..but not quite yet!