I have a peculiar issue with serial communication and/or strings. I made a small gadget that reads RFID badge, sends logging info to server with ESP8266 and then retrieves a log of previous reads to be displayed on a 1" OLED screen. Board is Pro Micro, Arduino 1.6.6. Everything works fine and dandy except reading the log back from net. I only get part of the data which seems to be quite random length from the start. Sometimes I get all, sometimes just 20% or so, and everything in between.
On server side I have made it simple for Arduino and send the log like this:
{08.11. 23:26 (92231460)#08.11. 23:09 (92231460)#08.11. 23:09 Sininen#08.11. 23:09 Valkoinen#08.11. 23:08 (92231460)#}
‘{’ to tell the response begins, then timestamp + card ID + ‘#’ for each log entry and ‘}’ to tell the response ends.
I have a serial adapter tapped between ESP8266 and Micro Serial port to listen to what ESP8266 is sending back to Arduino. Response for the GET call looks like this:
+IPD,209:HTTP/1.1 200 OK
Date: Sun, 08 Nov 2015 21:26:09 GMT
Server: Apache/2.4.10 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4
X-Powered-By: PHP/5.4.37
Transfer-Encoding: chunked
Content-Type: text/html
1
{
+IPD,75:45
08.11. 23:26 (92231460)#08.11. 23:09 (92231460)#08.11. 23:09 Sininen#
+IPD,54:30
08.11. 23:09 Valkoinen#08.11. 23:08 (92231460)#}
On Arduino side after making the GET call I read in the response to global String variable responseFromServer with:
Serial1.find("{");
responseFromServer = Serial1.readStringUntil('}');
First reading the Serial1 stream and omitting the content until ‘{’ is found, then reading in stuff to variable until end character ‘}’ is found.
However, what I get in responseFromServer string is this:
+IPD,75:45
08.11. 23:26 (92231460)#08.11. 23:09 (92231460)#08.11. 23:09 S
What is happening? Why is my responseFromServer string truncated? As said, the position it gets truncated is quite random, some times I even get the full response until ‘}’ character.
I have tried my everything to solve the issue but in avail. I build a custom “read until ‘}’ or timeout” method but it works exactly the same: it reads the input stream from Serial1 (ESP8266) for a brief time (definitely not until timeout), exits and in result string I get some random truncated part of the server response.