ESP8266 is not giving correct serial output all the time.

Hi...
I'm working on esp8266 with Arduino Uno to get data from the server (from SQL database through PHP) and print on the serial monitor. I'm using AT commands, and everything is working fine. But it doesn't give correct output data all the time.

Suppose I've echoed '135' from the PHP code to print on the serial monitor, then the output on serial comes like this: (if the code is running in an infinite loop)

135
135
035
13?
135
1?^
1q5
135
135...... like this.

I've tried Serial.flush() as well, in the hope that the problem may vanish. But I didn't work.
Please help as I need 100% accuracy in the serial output.

Here is the code on Arduino:

#include <SoftwareSerial.h>
#define RX 10
#define TX 11
SoftwareSerial esp8266(RX, TX);

String wifi = "wifi_name;
String pass = "password";

//String API = "";   //used for security purposes
String server_host = "localhost";
String port = "80";

int countTrueCommand;
String RFID_value = "";

void setup() {
  Serial.begin(115200);
  esp8266.begin(115200);
  sendCommand("AT", 5, "OK");
  sendCommand("AT+CWMODE=1", 5, "OK");
  sendCommand("AT+CWJAP=\"" + wifi + "\",\"" + pass + "\"", 20, "OK");
}

void loop() {
  RFID_value = getRFID_data();
  sendCommand("AT+CIPMUX=1", 5, "OK");
  sendCommand("AT+CIPSTART=0,\"TCP\",\"" + server_host + "\"," + port, 15, "OK");

  String getData = "GET /Test_site/RFID.php?tagID="+RFID_value;     //This is local site that gives the output data.

  sendCommand("AT+CIPSEND=0," + String(getData.length() + 4), 4, ">");
  esp8266.println(getData);
  delay(1000);
  countTrueCommand++;
  sendCommand("AT+CIPCLOSE=0", 10, "OK");
  
  if (esp8266.available() > 0) {
    String message = readWifiSerialMessage();
    Serial.println(message);
  }

}

String getRFID_data() {
  return "498191616678";  //Replace with RFID Functionality.
}

String  readWifiSerialMessage() {
  char value[20];
  int index_count = 0;
  while (esp8266.available() > 0) {
    //esp8266.readStringUntil('\0');
    //Serial.flush();
    value[index_count] = esp8266.read();
    index_count++;
    value[index_count] = '\0'; // Null terminate the string
  }
  String str(value);
  str.trim();
  return str;
}


void sendCommand(String command, int maxTime, char readReplay[]) {
  int countTimeCommand;
  boolean found = false;
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while (countTimeCommand < (maxTime * 1))
  {
    esp8266.println(command);//at+cipsend
    if (esp8266.find(readReplay)) //ok
    {
      found = true;
      break;
    }

    countTimeCommand++;
  }

  if (found == true)
  {
    Serial.println("Success");
    countTrueCommand++;
    countTimeCommand = 0;
  }

  if (found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }

  found = false;
}

Duplicate topics moved and merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Reduce the baudrate on the SoftwareSerial port to 57600 or 38400. I have found that SoftwareSerial struggles at 115200. The hardware port should be fine at this speed though.

Maximum of 38400 baud rate for SoftwareSerial.

When I decrease the baud rate of serial, it causes failure to AT Commands. At 9600, AT-commands work quite well, and at 115200 it works fine. At other than these two baud-rates, the transmission gets slower or either failed.

  1. at command => AT+CIPSTART=0,"TCP","localhost",80 -----Success
  2. at command => AT+CIPSEND=0,46 ----------Success
  3. at command => AT+CIPCLOSE=0 -------------Fail

I think the problem is not with the serial monitor. Isn't it possible that data transmission from esp8266 to Arduino is creating a mess? But we can't change the baud rate of esp8266.

I think I should mention one more thing.
I've seen on the internet that people use some resistors and level shifters in Rx and Tx wires. I've not added such circuits in my connections. Can this be a possible cause of the error?

Also, I've tried operating the esp8266 at 5v as well as 3.3v (both provided in Arduino Uno) to resolve the issue, nothing worked; but the esp8266 datasheet suggests 2.5V to 3.6V input. Are all these can be possible causes of the error?

Thanks in advance!