synchronization problem between Arduino Mega and ESP8266 [Solved]

my Project about sending about sending data to Firebase from Arduino Mega
Heres detailed how: Senseors ⤏ Mega ⤏ Esp8266 ⤏ Wi-fi ⤏Firebase but i have a proplem of getting the data in the same order on Mega i tried putting some intializng charachter between data but i get no result thats the arduino code side : (this is just the test code not actually the data)

 void loop() {
    Serial1.println(35);
    Serial1.println(80);
    Serial1.println("Mai");
}

that`s the Esp8266 code :

#include <SoftwareSerial.h>

SoftwareSerial swSer(14, 12, false, 256); //d5,d6
void setup() {
  Serial.begin(9600);
  swSer.begin(9600);
}
void loop() {
  static char buffer[10];
  if (readline(swSer.read(), buffer, 3) > 0)
  {
    Serial.print("Humidity is:");
    Serial.println(buffer);
  }
  if (readline(swSer.read(), buffer, 3) > 0) 
    {
    Serial.print( "Temp is:");
    Serial.println(buffer);   
    }

  if (readline(swSer.read(), buffer, 4) > 0) 
    {
    Serial.print("String is: ");
    Serial.println(buffer);   
    }
}

int readline(int readch, char *buffer, int len)
{
  static int pos = 0;
  int rpos;

  if (readch > 0) {
    switch (readch) {
      case '\n': // Ignore new-lines
        break;
      case '\r': // Return on CR
        rpos = pos;
        pos = 0;  // Reset position index ready for next time
        return rpos;
      default:
        if(pos < len-1) {
          buffer[pos++] = readch;
          buffer[pos] = 0;
        }
     }
    } 
  else{
   return readline(swSer.read(),buffer, len);
    }
  // No end of line has been found, so return -1.
  return -1;
}

this was working fine with me but without receiving data on the same order
can YOU help me to find what is the proplem with my approach
this is what i tried :
arduino code:

 void loop() {
     Serial1.println('H');
    Serial1.println(35);
    Serial1.println('T');
    Serial1.println(80);
    Serial1.println('S');
    Serial1.println("Mai")
}

the esp code:

void loop() {
  static char buffer[10];
  if (readline('H', swSer.read(), buffer, 3) > 0)
  {
    Serial.print("Humidity is:");
    Serial.println(buffer);
  }
  if (readline('T',swSer.read(), buffer, 3) > 0) 
    {
    Serial.print( "Temp is:");
    Serial.println(buffer);   
    }

  if (readline('S',swSer.read(), buffer, 4) > 0) 
    {
    Serial.print("String is: ");
    Serial.println(buffer);   
    }
}
int readline(char initial , int readch, char *buffer, int len)
{
  static int pos = 0;
  int rpos;

  if (readch > 0) {
    switch (readch) {
      case '\n': // Ignore new-lines
        break;
      case '\r': // Return on CR
        rpos = pos;
        pos = 0;  // Reset position index ready for next time
        return rpos;
      default:
       if(readchar==initial){
        while(pos < len-1) {
          buffer[pos++] = readch;
          buffer[pos] = 0;
        }
}
     }
    } 
  else{
   return readline(initial, swSer.read(),buffer, len);
    }
  // No end of line has been found, so return -1.
  return -1;
}

Have a look at Serial input basics - updated

UKHeliBob: Have a look at Serial input basics - updated

Thank you that helped!

In a PM you asked why numChars is 32. That is just a value I chose for the demo code. You can change it to any number you require. Just make sure it is at least one character longer than the longest message you need to receive - to leave room for the terminating '\0' character.

...R