Post your best Lua script examples here

User avatar
By ChrixXenon
#6668
lightbulb wrote: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.


Thanks lightbulb, but I'm not getting them confused. All of the lines I quoted all come from the simple HTTP client example on the NodeMCU website.
User avatar
By Pigs Fly
#6745 On the restart problem it's because the watchdog timer must be cleared during long loops. tmr.wdclr() I think is the command.

I have also read somewhere that network traffic stops flowing during loops (so you will never receive your response), and that matches my own experience as well.

Sorry, but I don't have references on either item, only that I've seen the same thing happen, have the same frustration, and have to find ways around it.
User avatar
By ChrixXenon
#6782
Pigs Fly wrote:On the restart problem it's because the watchdog timer must be cleared during long loops. tmr.wdclr() I think is the command.

I have also read somewhere that network traffic stops flowing during loops (so you will never receive your response), and that matches my own experience as well.

Sorry, but I don't have references on either item, only that I've seen the same thing happen, have the same frustration, and have to find ways around it.


Thanks for your helpful comments, Pigs Fly. I'm thinking perhaps it would be better to bit the bullet and learn the C++ Windows SDK. I was hpoing to avoid that learning curve, but it's looking like Lua is a bit of a lash-up.
User avatar
By lightbulb
#6788 ChrixXenon,

ChrixXenon wrote:
lightbulb wrote:Hi ChrixXenon,
Thanks lightbulb, but I'm not getting them confused. All of the lines I quoted all come from the simple HTTP client example on the NodeMCU website.


.


Your original request only had snippets of code, but your latter post showed what you really wanted to do...."get data from a remote server"

Your getpage() function is perhaps a little wrong.
You don't need to poll like that as the socket services are largely event driven, however the "order" in which you define/execute code within nodemcu IS important.
nodemcu is an interpreter, not a "compiler", and so it pretty much executes code as codeblocks are read in (dofile is little different).

simple rule: define all your event listeners before you execute code that uses them.
In your case, its the connect that kicks everything off, so you want your listeners defined before its used.
"receive" may be called many times for a single "page", so the page has not been technically "retrieved" after the first call, you'll likely truncate data doing it the way you have.

Finally, HTTP is a complex beast at heart, and the servers to which you want to talk may want to adhere to standards more than others, so check the type and order of the headers you need to send, coupled with the protocol version you want to use.

Here is a simple GET using HTTP 1.0. I dont have a module available where i am here at work, so this was done from memory, using my good friend wget. So it "may" not work off the bat.

Note: Many HTTP servers will NOT reply to an IP without a Host: header, as the IP to which you connect may be a proxy/firewall that simply send the request on, but those environments are simply too complex to discuss here.


Code: Select allconn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)
    print(payload)
    end)
conn:on("disconnection", function(conn,payload)
     print("disconnect")
     conn:close()
end)

conn:on("connection", function(conn,payload)
     print("sending...")
     conn:send("HEAD / HTTP/1.0\r\n")
     conn:send("Accept: */*\r\n")
     conn:send("User-Agent: Mozilla/4.0 (compatible; ESP8266;)\r\n")
     conn:send("\r\n")

end)

conn:dns('google.com',function(conn,ip) ipaddr=ip;
     --print(ipaddr)
     conn:connect(80,ipaddr)
     end)