I am using an ESP-01 and just simply forwarding data from the serial port out to a connected socket. Since the socket.write methods do not packetize in the background and would send each character as separate packets causing 100ms or so delay between each character read it’s necessary to do a little buffering. I’m using the Serial.readBytes method. Creating a 64 byte buffer and reading until it’s full or until 65ms timeout like this:
Serial.setTimeout( 65)
while (Serial.available()){
//read for 65ms or until 64 characters are reached.
byte readBuffer[64];
byte buffLen = Serial.readBytes( readBuffer, 64);
dataSocket.write( readBuffer, buffLen);
}
The serial stream it’s reading is only 9600 baud so very slow and usually less than 64 bytes but I’ve tested with longer strings and it works great. The problem is that once in a while for no reason that I can see it chokes and sends some random junk characters. The program itself does not hang and the connections keep going and the next packet is entirely valid. I’m currently going through the program and doing some memory optimization but there should be plenty of memory at this point. I am in the process of testing that now to be sure. I am relatively new to low level C coding and directly managing things like arrays of bytes but that code just isn’t that complicated.
I dont believe that the serial input is a problem, I’ve had this same code running through an xBee radio interface for a year without any corrupt packets and I am trying to be able to use these wonderful new devices in the future.
Any suggestions or advice would be greatly appreciated thank you!