I have a problem with larger messages received from ESP-01 on Serial1 on Arduino Mega 2560. For communication with ESP-01 I use bportaluri/WiFiEsp library.
I changed ESPLOGLEVEL to 4 in utility/debug.h to see more debug messages.
For example, when communication is establishing there is AT command (AT+GMR) to get version.
AT+GMR
22:08:40.683 -> AT version:1.2.0.0(Jul 1 2016 20:04:45)
22:08:40.683 -> SDK version:1.5.4.1(39cb9a32. 3[WiFiEsp] >>> TIMEOUT >>>
22:08:42.197 -> [WiFiEsp] End tag not found
When I use FTDI converter to use ESP-01 and I type AT+GMR in Arduino IDE Serial monitor I get this answer:
23:40:38.346 -> AT version:1.2.0.0(Jul 1 2016 20:04:45)
23:40:38.346 -> SDK version:1.5.4.1(39cb9a32)
23:40:38.346 -> v1.0.0
23:40:38.346 -> Mar 11 2018 18:27:31
23:40:38.346 -> OK
There is problem with received messages with content which throw TIMEOUT (as version example) because end tag was not found.
Do you have any ideas what can caused this?
After carefully considering the code you forgot to include, I’ll take a SWAG (Scientific Wild-Assuming Guess) and suggest that the issue might be related to the serial buffers. Serial devices use software buffers, and if these are overrun, it could result in the behavior you’re describing.
I cannot include whole code because it has more than 1000 rows. Sorry for that but it is not school project or something like this but it is my hobby project which is more larger and EPS-01 communication is only one part of it.
Can you provide me more about information about software buffers and how to set it to solve this behavior?
I found out that problem with debug which write to Serial is that Serial baud is less than Serial1 baud. If both Serials have same baud or Serial has higher baud than Serial1 then it work without problem.
Try this link. What is the Arduino Uno Serial buffer size? Think of serial communication like a bucket: if you pour out data faster than it’s added, the bucket will never overflow. However, if you add more data than you remove, it will eventually overflow. Software buffers work in a similar way.
I think I found out real problem. In one loop cycle I have multiple Subscription and Publish messages fired to MQTT. Problem was when there were some data received between these messages. I have loop method call but it was first line of arduino loop method. Buffer position was set to data position but second or another publish read serial stream and data was not available in next loop call.
Solution is call loop method before and after publish or subscribe method call to ensure each received data were read.