Arduino won't accept data longer than 140 characters from esp8266?

Hello people, can anyone help me, the problem is the program doesn't work for me if i send over 140 characters from esp8266 to arduino uno. If characters are under 140 , it works and it separates data. But add at first place in char array random character.
So my questions are:
1 whay arduino won't work if input data over Serial is over 140. Arduino just stops.
2 whay when characters are under 140 and code is "working" add at first place in array random character.
Thank you all in advance :slight_smile: .

ESP8266 send data like _t1DATA##_d1DATA##_d2DATA##
Data between d1 and ## must be for me over 300 characters.

#include <SoftwareSerial.h>
#define ESP8266_rxPin 2
#define ESP8266_txPin 3
SoftwareSerial ESP8266(ESP8266_rxPin, ESP8266_txPin);// rx tx
char keyword_d1[]="d1";
char keyword_doublehash[]="##";
unsigned long timeout_start_val;
char scratch_data_from_ESP[20];//first byte is the length of bytes
char d1_from_ESP[400];
boolean read_until_ESP(const char keyword1[], int key_size, int timeout_val, byte mode);


void setup() {
  // put your setup code here, to run once:
  pinMode(ESP8266_rxPin, INPUT);
  pinMode(ESP8266_txPin, OUTPUT);
  
  ESP8266.begin(9600);//default baudrate for ESP
  ESP8266.listen();//not needed unless using other software serial instances
  Serial.begin(9600);
}

void loop() {
 


     //we still have more data to get out of this stream, now we want d1
        if(read_until_ESP(keyword_d1,sizeof(keyword_d1),5000,0)){//same as before - first d1
          if(read_until_ESP(keyword_doublehash,sizeof(keyword_doublehash),5000,1)){//now ## and mode=1
            for(int i=1; i<=(scratch_data_from_ESP[0]-sizeof(keyword_doublehash)+1); i++)
              d1_from_ESP[i] = scratch_data_from_ESP[i];
             d1_from_ESP[0] = (scratch_data_from_ESP[0]-sizeof(keyword_doublehash)+1);    
                 

          
        }
  
  
  
 
  }
 Serial.println(d1_from_ESP);
 delay(100);
  

}
//This function goes and finds a keyword received from the ESP
// const char keyword1[] = the keyword that this function will look for and return '1' when found
// int key_size = size of that keyword - sizeof()
// int timeout_val - timeout if the keword is not found (in milliseconds 5000=5sec)
// byte mode - if '1', this function will save every byte received to 'scratch_data_from_ESP[]' with length of 'scratch_length' - useful for parsing between keywords
boolean read_until_ESP(const char keyword1[], int key_size, int timeout_val, byte mode){
  timeout_start_val=millis();//for the timeout
  char data_in[5];//this is the buffer - if keyword is longer than 20, then increase this
  int scratch_length=1;//the length of the scratch data array
  key_size--;//since we're going to get an extra charachter from the sizeof()
 
 //FILL UP THE BUFFER
 for(byte i=0; i<key_size; i++){//we only need a buffer as long as the keyword
  
            //timing control
            while(!ESP8266.available()){//wait until a new byte is sent down from the ESP - good way to keep in lock-step with the serial port
              if((millis()-timeout_start_val)>timeout_val){//if nothing happens within the timeout period, get out of here
                Serial.println("timeout");
                return 0;//this will end the function
              }//timeout
            }// while !avail
   
    data_in[i]=ESP8266.read();// save the byte to the buffer 'data_in[]
  // Serial.print(data_in[i]);
    if(mode==1){//this will save all of the data to the scratch_data_from
      scratch_data_from_ESP[scratch_length]=data_in[i];//starts at 1
      scratch_data_from_ESP[0]=scratch_length;// [0] is used to hold the length of the array
      scratch_length++;//increment the length
       
    }//mode 1
    
  }//for i
  
//THE BUFFER IS FULL, SO START ROLLING NEW DATA IN AND OLD DATA OUT
  while(1){//stay in here until the keyword found or a timeout occurs

     //run through the entire buffer and look for the keyword
     //this check is here, just in case the first thing out of the ESP was the keyword, meaning the buffer was actually filled with the keyword
     for(byte i=0; i<key_size; i++){
       if(keyword1[i]!=data_in[i])//if it doesn't match, break out of the search now
       break;//get outta here
       if(i==(key_size-1)){//we got all the way through the keyword without breaking, must be a match!
       return 1; //return a 1 and get outta here!
       }//if
     }//for byte i
     

    //start rolling the buffer
    for(byte i=0; i<(key_size-1); i++){// keysize-1 because everthing is shifted over - see next line
      data_in[i]=data_in[i+1];// so the data at 0 becomes the data at 1, and so on.... the last value is where we'll put the new data
     
  }//for
 
         
           //timing control
            while(!ESP8266.available()){// same thing as done in the buffer
              if((millis()-timeout_start_val)>timeout_val){
                Serial.println("timeout");
                return 0;
              }//timeout
            }// while !avail
    
   
   
    data_in[key_size-1]=ESP8266.read();//save the new data in the last position in the buffer
   //  Serial.print(data_in[key_size-1]);
      if(mode==1){//continue to save everything if thsi is set
      scratch_data_from_ESP[scratch_length]=data_in[key_size-1];
      scratch_data_from_ESP[0]=scratch_length;
      scratch_length++;
    }//mode 1
    

    
    
  }//while 1

  
  
  
}//read until ESP

What is the capacity of the buffer that you are receiving data into on the arduino?

hi , it is > char d1_from_ESP[400]; ,

Is this the same problem you asked about in your last post:
https://forum.arduino.cc/index.php?topic=652689

If so, what happened to change the limit from 125 to 140 characters?

The Serial Input Buffer on the Uno is 64 bytes long. If you empty the buffer sufficiently quickly there is no upper limit to the size of message that can be received. My guess is that your program is not taking the bytes from the Input Buffer fast enough.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

Note that the examples assume the biggest message will be 32 characters. That can be changed by editing the value of numChars.

You could also consider changing the ESP8266 code so it doesn't send more than 64 bytes in a single message and waits for a "request" from the Uno before sending more data.

...R