@mdma Sorry for replying a bit late, but here are the details.
I have been trying to send 20K data samples using Client.print(), to a predefined server (or about 100Kbytes after print() converts them to a string, 4 digit number each and a delimiter, per sample). I have tried multiple approaches
- Tried sending all the data in a single for loop. Works well most of the times. Sometimes I only see ~19K samples reaching the server, and the number varies.
- Tried grouping the data in packets of 1K bytes, and do a single client.write() - Same observation where all the data does not reach my server side script. Varied the size from 500
Correct me if I am wrong, but I believe the status returned by client.write() isn’t handled properly. write() uses size_t as the return type, which is unsigned, but returns -1 if status() returns a 0. Why size_t and why not an int?
size_t TCPClient::write(const uint8_t *buffer, size_t size)
{
return status() ? socket_send(_sock, buffer, size) : -1;
}
Also, what is behavior of the firmware if the write fails, for whatever reason? I do not see any retry being handled here.
Owing to this, I changed my code to look at the return value from write() and if it is -1, then try re-sending the data. I also started to log the number of retires that was happening. I have seen the retry value to range from 0 to 10K counts, and as I said earlier, it varies (guess depending on the network load, etc…). After these changes, I see that the server sometimes receives more than 20K samlples sometimes, and works well most of the times.
I then added delay(100) in between multiple sends, and the data consistency has significantly improved, but I don’t think using delays() is the right thing to do. I also have made sure to use SYSTEM_THREAD(ENABLED). I still am looking at making it robust to be able to deliver all the samples, all the time.
Any idea what could be causing the data loss?