Post your best Lua script examples here

User avatar
By Brian
#10293 Ok sorry I am very new
I managed to load the nodemcu firmware and trying to write a simple program
I wrote config to connect to my wi-fi and it now does this on power up
trying to now write simple http client using the example...
conn=net.createConnection(net.TCP, false)
conn:on("receive", function(conn, pl) print(pl) end)
conn:connect(80,"121.41.33.127")
conn:send("GET / HTTP/1.1\r\nHost: www.nodemcu.com\r\n"
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n")

this don't appear to work or i don't get any output
i am entering code line by line in program mode

I tried the simple http server example and that works but what i did notice was it didn't store the program so I tried to write my program and store as a file like ...
file.remove("web.lua")
file.open("web.lua","w")
... my code
file.close()

What i notice in some of the examples when writing to a file is sometimes it says writeline and in other cases file.writeline so i am confused how to write my code and get the ESP to save it after power off

Any help very much appreciated
User avatar
By MK1888
#10302 Try this:
Code: Select allconn = nil
conn=net.createConnection(net.TCP, 0)

conn:on("receive", function(conn, pl) success=true print(pl) end)

conn:on("connection", function(conn, pl)
   conn:send("GET / HTTP/1.1\r\nHost: www.nodemcu.com\r\n"
   .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
end)
                                             
conn:connect(80,'www.nodemcu.com')


You don't show how you're writing to the file. You could be having problems with quotes.

I assumed your code didn't work, so I wrote the above. But I tested your code and it works too. Why can't you upload files with an application? There are Windows and Linux solutions available.
User avatar
By Brian
#10325 Thanks MK
I don't understand, writing to what file?
I was entering the commands line by line with a simple com port tool in windows.
I tried making it a file on the ESP using the file.open command.
I don't understand in what cases I use writeline and file.writeline.
I also don't understand why say the wi-fi connect code stays configured without writing the file to the ESP.
Your example doesn't use file.open.
I have the ESP firmware upload tool, can i use this to upload a pre written program file?
If I do at what address do i write the program too?
Also after writing the program if i take ESP out of command mode and reset i don't get anything back on the com
I'm sure i'm not far away but i'm struggling to understand the basic principles here.
In your example where is the variable that the web content is stored in?
I want to be able to program decisions depending on the content.
Like switch on red led if the page content says RED or blue led if page content is BLUE

Thanks
Brian
User avatar
By Brian
#10338 Ok so i made some progress..
I am now using the LUA Loader 0.83
I can only make programs using manual send, line by line.
If i try to upload file i get errors like...
> > > stdin:1: nesting of [[...]] is deprecated near '['
> stdin:1: nesting of [[...]] is deprecated near '['

I created two files...
file.open("web.lua","w")
file.writeline([[print("Connecting to server....")]])
file.writeline([[conn=net.createConnection(net.TCP, false) ]])
file.writeline([[conn:on("receive", function(conn, pl) print(pl) end)]])
file.writeline([[conn:connect(80,"50.116.105.225")]])
file.writeline([[conn:send("GET / HTTP/1.1\r\nHost: www.brianmoreau.com\r\n"]])
file.writeline([[.."Connection: keep-alive\r\nAccept: */*\r\n\r\n")]])
file.close()

file.open("init.lua","w")
file.writeline([[dofile("web.lua")]])
file.close()

Running web.lua on its own displays the HTML in the com monitor window
Restart ESP and i get message Connecting to server so i know the init is calling the web program but nothing is displayed.

How do i get the data in a variable?