-->
Page 1 of 1

How fast can i call nodemcu api via UART

PostPosted: Sat Jun 20, 2015 2:08 pm
by littledino2112
Hi all,
I'm writing a firmware on a MCU and want to use ESP8266 as a Wifi bridge. In my main application, i would call some nodemcu api to create connections and send POST requests to a server. So far so good, however, i have problem calling 2 nodemcu commands continuously.
For example,
printf("create_conn=function() conn=net.createConnection(net.TCP,0)"
"conn:on(\"receive\",function(conn_s,pl) print(\"Sent\") end)"
"conn:on(\"disconnection\",function(conn_s,pl) create_conn() end)"
"conn:connect(7777,\"192.168.0.103\") return conn end;\n");
printf("conn=create_conn();\n");

Calling these 2 functions without a delay between them would sometimes confuse nodemcu and it can't recognize my commands appropriately.
Adding a delay of 500ms between them will help solve the problem. But i want to send POST data continuously to a server and adding a delay between requests does really slow the whole thing down a lot.
My question is how fast can i send 2 continuous commands without having nodemcu unable to recognize them properly?

Regards,
Hoang

Re: How fast can i call nodemcu api via UART

PostPosted: Sat Jun 20, 2015 8:49 pm
by TerryE
The SDK is non-preemptive and these network functions are async to the Lua exeution. You have to return control back to the C code RTS fr this to work. See my FAQ below for more details..

Re: How fast can i call nodemcu api via UART

PostPosted: Sat Jun 20, 2015 9:43 pm
by littledino2112
Hi Terry,
I understand the async nature of nodemcu. The problem i'm having is when i start sending commands to nodemcu, the nodemcu firmware seems not being able to recognize them correctly if i don't put a delay btw 2 commands.
For example: i'm sending these 2 post requests at the same time
conn:send("POST /upload HTTP/1.1\r\n".."Host:192.168.0.103\r\nContent-Length:69\r\n\r\n".."{\"manu_id\":\"0058\",\"major_num\":\"0204\",\"minor_num\":\"0608\",\"rssi\":\"c3\"}\r\n\r\n");
conn:send("POST >> /upload HTTP/1.1\r\n".."Host:192.168.0.103\r\nContent-Length:69\r\n\r\n".."{\"manu_id\":\"0059\",\"major_num\":\"1234\",\"minor_num\":\"5678\",\"rssi\":\"c3\"}\r\n\r\n");

You see, the nodemcu firmware automatically split my second commands (this could be due to its UART RX buffer size). This results in my command not being recognized properly with the following error: stdin:1: unfinished string near '"POST '
Is there anyway to fix this issue?

Hoang

Re: How fast can i call nodemcu api via UART

PostPosted: Sun Jun 21, 2015 4:32 pm
by TerryE
A post is a form of RPC. you connect; send a request; get a response; then if its a 200 OK then you can send another. a yes with HTTP 1.1 you can batch these but things will get complicated. Why not just set up a global message and have a separate event handler to process it. e.g.[code]
_Update[#_Update+1] ={ manu_id = 58, major = 204, minor = 608, rssi = "c3"}
-- ...
-- and the min instead of doing a simple return tailcall into post_update()
if not _Update or #_Update == 0 return
local post_update = loadfile('post_update.lc')
return post_update()
end[/code]
post_update formats and sends the first entry in the _Update array, and the on ("data") callback looks for the "200 OK" and if it finds it tailcalls the post_update to pick off the next, ....

You don't wait explicitly just ping them out as fast as your get the 200 OK's. You've got to think event-driven [i]all he time[/i] when you are using Lua on the ESP8266.

PS. sorry for the crappy formatting but you turned off BBcodes in your OP. Your bad :-)