I want to receive about 5kb of data in Base64 format. The following code does not work. Only a few characters are received, sometimes CPU crashes.

String content = "";
char character;

while (Serial.available()) {
       character = Serial.read();

I do not care about ram usage since on ESP32 there is plenty. How can I get all the data? Example input: https://pastebin.com/wYG5AhHK

  • You need to signal the end of data by sending a code or a data length. Serial.available() may be receiving data by chunks, so it can be false at some moments. – user31481 Aug 9 '17 at 16:12
  • I can see no logical cause, other than maybe you want to yield() within your while loop. You can also pre-allocate the space in your string to reduce heap mangling. content.reserve(6000); Better would be to not use String at all but use a large statically allocated character array. – Majenko Aug 9 '17 at 16:16

I had to read until delimiter :D

String receive() {
        String content = "";
        content = Serial.readStringUntil('\r');
        return content;

Now it is fine. I will also change String to large char array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.