ESP8266 TCP Server gets 'weird' when Macbook TCP Client idle
Posted: Tue Oct 09, 2018 5:12 pm
Hello,
I have a relatively simple sketch, and relatively simple behavior. The sketch has a TCP server, and once connected, performs analogRead's every 100ms, and sends that data every 1second as a 28-byte packet to the remote TCP client.
For debug, I'm using Julia (programming language) on a Macbook to initiate a TCP connection with my ESP8266 server, and receive packets. For debug, I'm printing to the serial port every 2 seconds driven by the Ticker library.
Below is debug output and
* "Connected(x): 0" means "Connection 0 is active and millis() = x"
* "cix=0, afw=x" means "On connection 0, WifiClient.availableForWrite() = x"
Where things get 'weird' is when the Macbook goes to sleep. My theory is that it's related to the ESP8266 trying to send data on a TCP connection where the other side did not cleanly close the socket.
The 'Connected' message should print every 2 seconds, and it does until the macbook goes to sleep. When the macbook goes to sleep, 'availableForWrite' goes down by 28 bytes each packet, which makes sense. What doesn't make sense is that 'availableForWrite' gets 'stuck' at a number much greater than 0, and then the 2 second differential in millis becomes much greater than 2000 or 2 seconds. Here's where things start to go 'weird':
I haven't wiresharked exactly what's happening, but has this happened to others? I can side-step this problem, but I want to get to the bottom of it and practice 'defensive coding'. I can't submit my sketch as-is, but can make a version that replicates the problem if that's helpful.
Here's a longer section of the serial log:
I have a relatively simple sketch, and relatively simple behavior. The sketch has a TCP server, and once connected, performs analogRead's every 100ms, and sends that data every 1second as a 28-byte packet to the remote TCP client.
For debug, I'm using Julia (programming language) on a Macbook to initiate a TCP connection with my ESP8266 server, and receive packets. For debug, I'm printing to the serial port every 2 seconds driven by the Ticker library.
Below is debug output and
* "Connected(x): 0" means "Connection 0 is active and millis() = x"
* "cix=0, afw=x" means "On connection 0, WifiClient.availableForWrite() = x"
Where things get 'weird' is when the Macbook goes to sleep. My theory is that it's related to the ESP8266 trying to send data on a TCP connection where the other side did not cleanly close the socket.
The 'Connected' message should print every 2 seconds, and it does until the macbook goes to sleep. When the macbook goes to sleep, 'availableForWrite' goes down by 28 bytes each packet, which makes sense. What doesn't make sense is that 'availableForWrite' gets 'stuck' at a number much greater than 0, and then the 2 second differential in millis becomes much greater than 2000 or 2 seconds. Here's where things start to go 'weird':
Code: Select all
Connected(457377): 0
cix=0, afw=2696
cix=0, afw=2696
Connected(469677): 0
I haven't wiresharked exactly what's happening, but has this happened to others? I can side-step this problem, but I want to get to the bottom of it and practice 'defensive coding'. I can't submit my sketch as-is, but can make a version that replicates the problem if that's helpful.
Here's a longer section of the serial log:
Code: Select all
Connected(439377): 0
cix=0, afw=2892
cix=0, afw=2892
Connected(441377): 0
cix=0, afw=2892
cix=0, afw=2892
Connected(443377): 0
cix=0, afw=2892
cix=0, afw=2892
Connected(445377): 0
cix=0, afw=2892
cix=0, afw=2892
Connected(447377): 0
cix=0, afw=2892
cix=0, afw=2892
Connected(449377): 0
cix=0, afw=2892
cix=0, afw=2864
Connected(451377): 0
cix=0, afw=2836
cix=0, afw=2808
Connected(453377): 0
cix=0, afw=2780
cix=0, afw=2752
Connected(455377): 0
cix=0, afw=2724
cix=0, afw=2696
Connected(457377): 0
cix=0, afw=2696
cix=0, afw=2696
Connected(469677): 0
cix=0, afw=2696
cix=0, afw=2696
Connected(481877): 0
cix=0, afw=2696
cix=0, afw=2696
Connected(494077): 0
cix=0, afw=2696
cix=0, afw=2696
Connected(506077): 0
cix=0, afw=2696
cix=0, afw=2696
Connected(518377): 0
cix=0, afw=2696
cix=0, afw=2696
Connected(530577): 0
cix=0, afw=2696
cix=0, afw=2696
Connected(542677): 0
cix=0, afw=2696
cix=0, afw=2696
Connected(554877): 0
cix=0, afw=2696
cix=0, afw=2696
Connected(566877): 0
cix=0, afw=2696
cix=0, afw=2696