As the title says... Chat on...

User avatar
By jimlyke
#60099 My greatest barrier to using the NodeMCUs is in getting a clear understanding of the "net" module described

https://nodemcu.readthedocs.io/en/lates ... etsocketon

I guess I am not a good programmer, but I tried hard to study the page, the FAQ at

wiki/doku.php?id=nodemcu-unofficial-faq

and looking at many examples of code from here and elsewhere. It seems magical and I can get some examples to work, but I really need a deeper understanding. Can anyone recommend another source or provide some better insight for these functions?
User avatar
By marcelstoer
#60108
jimlyke wrote:https://nodemcu.readthedocs.io/en/latest/en/modules/net/#netsocketon

I guess I am not a good programmer


It might also be that we're not explaining things well enough ;) The thing tough is that our documentation is an API reference and not a tutorial. As the net module offers a low level operations a solid foundation of networking know-how is indispensable I guess.

What exactly is it that you'd like to know? What challenge would you like to solve using the net module?
User avatar
By jimlyke
#60265
marcelstoer wrote:
jimlyke wrote:https://nodemcu.readthedocs.io/en/latest/en/modules/net/#netsocketon

I guess I am not a good programmer


It might also be that we're not explaining things well enough ;) The thing tough is that our documentation is an API reference and not a tutorial. As the net module offers a low level operations a solid foundation of networking know-how is indispensable I guess.

What exactly is it that you'd like to know? What challenge would you like to solve using the net module?


Thank you, I am mostly trying to parse returns from GET methods (more ambitiously I am trying to orchestrate a distributed network of ESPs through REST-like API calls, but one must walk before running, and this means I need to nail a GET request before trying anything more elaborate). In the many examples and snippets I have tried to follow, I see a sequence of invocations of the form:

1. conn=net.createConnection(net.TCP, 0)

2. conn:on("receive", function(conn, payload)
<stuff> end)

3. conn:connect(80,s.host)

4. conn:on("connection",function(conn, payload)

5. conn:send(<stuff>)
end
)

6. conn:on("disconnection", function(conn)


Obviously, #1 is necessary to establish a client connection "object", for lack of a better term. The next thing I would think necessary is to actually perform a connection with a website, which is done by #3. I don't understand why this is not immediately next, but in most examples, it seems to be done in this particular order. To perform a GET request, we would send an appropriately formatted string, which is done in #4,#5 (i guess once we have a connection, then we should send something...). But then we expect to get something back (i.e., we receive a string/file back from the host), which is done in #2. And sometimes I see the disconnection statement (#6) which I guess seems to signify the end of this set of actions.

But this ordering seems to not follow the sequence of what happens. In the FAQ, I remember something about how the nodeMCU does things in terms of events and that statements either do not block execution or that they TOTALLY block execution (like the tmr.delay, which actually disables interrupts and appears to stop the CPU at a machine level). I am interpretting this to maybe mean we can order the statements non-sequentially. But if so, why not order them logically just to improve understanding, e.g. #1->#3->#4->#5->#2->#6.?

I also do not understand the whole notion of a callback. I am mimicking what others do and I more-or-less get things to sometimes work. Doesn't instill confidence.

I am also having name server problems and have to use ip #'s instead of names. I saw comments in the past about this, but I have so far been able to only get things to work using numbers for ip instead of host/domain names.
User avatar
By marcelstoer
#60276 I can see you did some reading in our documentation, well done!

Let me start by saying that if you're mainly interested in sending data over HTTP our dedicated HTTP module would be a more convenient fit. However, I believe it's essential to understand what goes on at a lower level - and that is the net module.
I'll dissect the net.socket:on() example from the docs below. I added Lua comments so that the code should be executable as-is:

Code: Select all-- Creates a connection client object. I guess it was named 'srv' to imply that it represents an abstraction for the server (or so).
srv = net.createConnection(net.TCP, 0)
-- Registers an (unnamed) inline function that is executed when the 'receive' event is fired.
-- That event is fired once data was received *from the server* i.e. the function will execute sometime in the future.
srv:on("receive", function(sck, c) print(c) end)
-- Registers an (unnamed) inline function that is executed when the 'connection' event is fired.
-- That event is fired once the connection *to the server* is established i.e. the function will execute sometime in the future.
srv:on("connection", function(sck, c)
  -- At this point the actual socket connection is established and represented by the 'sck' variable.
  -- Only now can "data" be sent *to the server*. The data is a HTTP GET request in this case. Ideally the server will
  -- receive the request and send some data back in which case the 'receive' event will fire.
  sck:send("GET /get HTTP/1.1\r\nHost: httpbin.org\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
end)
-- The client should now connect to the host known by "httpbin.org" on port 80 as all event handlers (i.e. callback
-- functions for events we're interested in) are in place. They wait to spring into action when their time has come.
srv:connect(80,"httpbin.org")


Unfortunately this forum doesn't seem to support proper syntax highlighting :(