Post your best Lua script examples here

User avatar
By ChrixXenon
#6587 Hi, I can copy an paste like most, and it works, but I don't really understand how it works. Take this example:

- A simple http client
conn=net.createConnection(net.TCP, false)

- OK - make a variable called CONN and create a connection using the TCP protocol and not secure. Clear enough.

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

- OK - define a receiver function to be called when the connection has a RECEIVE event.
So the ":on" syntax is how you made a receiver for an event, I guess.

conn:connect(80,"121.41.33.127")

- now here I get fuzzy. Is "connect" a method telling it to connect? Surely "createConnection() is doing that? And if so, why aren't he port number and IP address parameters of that call?

conn:send("GET / HTTP/1.1\r\nHost: www.nodemcu.com\r\n"
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n")

- and this one baffles me too. Is "send" a method of the connection object "conn"?
Is it called inline, at this point in the code, to implement the GET request?
Surely, it would have to wait until the connection is made (asynchronously).
So what causes this line of code to run?

I have similar issues with other examples. I have reviewed several node.js videos, and they make perfect sens, but the syntax is critically different, implementing a different hierarchy. I've looked at some Lua tutorials too but no joy.

I'm an experienced programmer, but I'm knocking on a bit and may be getting senile, but if anyone can shed some light, or point me to the light switch, I'd be grateful.
User avatar
By picstart
#6599 I agree it is an uneasy situation.....the code when it works can be a surprise since the underlying structure is confusing.
Often all you can do is copy something that works and hope the next sample of code you see sheds some light on the structure.
Nodemcu at first seems a huge improvement over the AT command forest where commands are echoed by the trees but later you find Nodemcu ialso
is a forest of code. The Nodemcu wiki helps somewhat but important calls like srv=net.createServer(net.TCP) are given in examples
but not well explained. Now the code is open source some hardy enthusiasts are finding answers in the code as to which lua lib type features are actually
implemented. Others just throw a command at the wall and if it sticks we assume it was implemented.
The situation could get better in the future.
User avatar
By lightbulb
#6622 Hi ChrixXenon,

I am pretty new to Lua also, having used it a little many many years ago.... :|

You maybe are getting outbound vs inbound connectivity confused.

You can create Protocol Servers (TCP/UDP) , for other clients to connect into, or you may create connections outbound to other services.
For example, you could create a HTTP Server on TCP:80, or you can connect and retrieve data from a remote web server (using TCP:80) etc.

The wiki and examples in this forum have examples of both. If you'd like to make a specific request I could point you either to an example, or supply one as a reply, or I am sure someone else could also. Being specific helps us to help you.
User avatar
By ChrixXenon
#6667
picstart wrote:I agree it is an uneasy situation.....the code when it works can be a surprise since the underlying structure is confusing.
Often all you can do is copy something that works and hope the next sample of code you see sheds some light on the structure.
Nodemcu at first seems a huge improvement over the AT command forest where commands are echoed by the trees but later you find Nodemcu ialso
is a forest of code. The Nodemcu wiki helps somewhat but important calls like srv=net.createServer(net.TCP) are given in examples
but not well explained. Now the code is open source some hardy enthusiasts are finding answers in the code as to which lua lib type features are actually
implemented. Others just throw a command at the wall and if it sticks we assume it was implemented.
The situation could get better in the future.


Thanks picstart. I'll loiter here a while - perhaps others have a clearer understanding of exactly how these examples work and will be kind enough to share.

UPDATE:

I think I have a slightly clearer understanding now.

conn:on("....

- invokes the ON method, which is used to register event listening functions.

conn:connect and conn:send are other methods of the connection object, and I assume they run in order after each of the previous methods completes - in other words, these are blocking methods.

The RECEIVE event is triggered when the data comes in from the internet, which might be quite some time, and the function defined there is run.

So far so good.

But I want to write a getPage(url) routine, and I want it to return the page contents as a string. My problem is getting that getPage routine to wait until the page is available before it returns, rather than returning with an empty page because the internet has not delivered it yet. I guess that's in effect, making a non-blocking action into a blocking action.

I tried this:

function getPage()
pageRetrieved = false
conn=net.createConnection(net.TCP, false)
conn:on("receive", function(conn, pl) print(pl) pageRetrieved = true return pl end)
conn:connect(80,"[ip address]")
conn:send("GET / HTTP/1.1\r\nHost: [url]"
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
while (not pageRetrieved) do
-- print("waiting for page")
end
end

getPage()

- but it doesn't work. I see lots of "waiting for page" messages in the console, then the chip resets, and I never see the page returned.

Perhaps I'm trying to force my own way of thinking onto a new programming paradigm. Can anyone tell me how would I write getPage() and then use the results elsewhere in some arbitrary programme?
Last edited by ChrixXenon on Thu Jan 08, 2015 11:54 am, edited 1 time in total.