-->
Page 1 of 1

When can I power OFF WIFI after TCP transaction ?!

PostPosted: Sat Jun 17, 2017 2:39 pm
by Seb.26
Hi world,

I have an ESP-12 that stay awake to do some background jobs, but I turn WIFI OFF when not needed.

Periodically I turn WIFI ON, connect to WIFI AP, open TCP socket and send/receive some data from a server, and when this is done, I turn WIFI OFF.

My problem is : how do I know that all data I've sent have been received by the server ?

For exemple, if I turn WIFI OFF just after the data sending, the server don't receive last bytes, looks like I've switch OFF WIFI before the TCP job is done.

So I've add a "delay(5000)" and all is fine, but this is not a great solution :
> 5000ms is probably far too long 95% of time
> 5000ms will be too short a day with bad WIFI or busy server or ... ?!

So : how can I check that TCP socket have finish to send data ?

Thanks.

Re: When can I power OFF WIFI after TCP transaction ?!

PostPosted: Sat Jun 17, 2017 7:36 pm
by gbafamily1
Try something like this.

client.stop(); // Close socket
// Wait for close handshake to complete. Needs timeout.
while (client.connected()) delay(10);
// Now power off or go to deepSleep.

Re: When can I power OFF WIFI after TCP transaction ?!

PostPosted: Sun Jun 18, 2017 4:02 am
by Seb.26
Thanks for your answer.

The Arduino doc says :
Whether or not the client is connected. Note that a client is considered connected if the connection has been closed but there is still unread data.


So the .connected() flag will wait the socket is "really" close before go false, this sounds great ! ;)

I will try that asap, thanks !

Re: When can I power OFF WIFI after TCP transaction ?!

PostPosted: Mon Jun 19, 2017 2:12 pm
by Seb.26
looks like this is working, thanks !