ESP8266 RX buffer overflow

Hi;

I have Arduino uno and ESP8266-01 module. I connect ESP to hardware serials pins.

I'm send HTTP request with mozilla browser (http://ESP_IP/?password=1234&command=test)
I see first time request data correct:

0,CONNECT

+IPD,0,370:GET /?password=1234&command=test HTTP/1.1
Host: 192.168.1.14
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1

But second request always lost some lines EX:

0,CONNECT

+IPD,0,370:GET /?password=1234&command=test HTTP/1.1
Host: 192.168.1.14
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.

maybe hardware serial RX buffer overflow. How can I fix this?
NOTE: I use external good power supply for ESP8266. Problem not cause of power.

#include <SoftwareSerial.h>
#include <string.h>

SoftwareSerial terminalSerial(10, 11);      //RX, TX for Monitor

String ssid = "MYSSID";
String ssidPassword = "MYPASSWORD";
String internalIp = "192.168.1.14";
String connectionpassword = "1234"; // Please change do not use this default

int answerWaitTime = 100;
int relayNumber;

char delimiterSpace[] = " ";
char delimiterN[] = "\n";
char delimiterAnd[] = "&";
char *splitWithN;
char *splitWithSpace;
char *splitWithAnd;
char *splitWithHttp;
int count;

String dataFromEsp = "";
String espResponse = "";
String answer = "";

String cmd = "";
boolean wifiConnectionStatus = false;

String requestID = "";
String httpGetData;
String password;
String command;

String messageOk = "HTTP/1.1 200 OK\r\n\r\nOK";
String messagePasswordWrong = "HTTP/1.1 200 OK\r\n\r\npassword_wrong";
String messageCmdCldntUnderstand = "HTTP/1.1 200 OK\r\n\r\ncommand could not understood";

// Relays
int relays[] = {12, 2, 3, 4, 5, 6, 7, 8, 9};

void setup() {
  // put your setup code here, to run once:

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  relay(2, "HIGH");
  relay(3, "HIGH");
  relay(4, "HIGH");
  relay(5, "HIGH");
  relay(6, "HIGH");
  relay(7, "HIGH");
  relay(8, "HIGH");
  relay(9, "HIGH");

  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }



  terminalSerial.begin(115200);

  espResponse = sendToWifi("AT", true); //Send AT to Esp

  if (find(espResponse, "OK")) { // if get OK
    terminalSerial.println("--> AT answer success");
    espResponse = sendToWifi("AT+CWMODE=3", true); //Set Esp CWMODE to 3
    if (find(espResponse, "OK")) { //if set to CWMODE
      terminalSerial.println("--> AT+CWMODE=3 Success");
      cmd = "connectToWifi";
      espResponse = sendToWifi("AT+CWJAP=\"" + ssid + "\",\"" + ssidPassword + "\"", false); // Try connect to SSID
    }
  } else {
    terminalSerial.println("--> AT NO ANSWER");
  }

}

void loop() {
  // put your main code here, to run repeatedly:
  if ((Serial.available() > 0)) {

    dataFromEsp = "";

      //terminalSerial.println("--| HAVE  COM DATA |--");
      dataFromEsp = Serial.readString();
      //terminalSerial.println(dataFromEsp);

    terminalSerial.println("--| dataFromEsp |--");
    terminalSerial.println(dataFromEsp);
    terminalSerial.flush();
    Serial.flush();

  } else {

  }

  if (cmd == "connectToWifi") {
    if (find(dataFromEsp, "OK")) {
      terminalSerial.println("--> Wifi Connection success");
      cmd = "";
      wifiConnectionStatus = true; // connection to WiFi Success
      cmd = "setCipsta";
      espResponse = sendToWifi("AT+CIPSTA=\"" + internalIp + "\"", false); //Sets the MAC Address of the ESP8266 Station
    }
  }

  if (cmd == "setCipsta") {
    if (find(dataFromEsp, "OK")) {
      terminalSerial.println("--> AT+CIPSTA success");
      cmd = "";
      cmd = "setCipmux";
      espResponse = sendToWifi("AT+CIPMUX=1", false); // configure for multiple connections
    }
  }

  if (cmd == "setCipmux") {
    if (find(dataFromEsp, "OK")) {
      terminalSerial.println("--> AT+CIPMUX=1 Success");
      cmd = "";
      cmd = "startCipserver";
      espResponse = sendToWifi("AT+CIPSERVER=1,80", false); // turn on server on port 80
    }
  }

  if (cmd == "startCipserver") {
    if (find(dataFromEsp, "OK")) {
      terminalSerial.println("--> AT+CIPSERVER=1,80 Success");
      cmd = "";
    }
  }




 

}


/*
  Name: sendToWifi
  Description: Function used to send data to ESP8266.
  Params: command - the data/command to send; getAnswer - get return data from ESP8266 (true = yes, false = no)
  Returns: The response from the esp8266 (if there is a reponse and if getAnswer = true)
*/
String sendToWifi(String atCommand, boolean getAnswer) {
  answer = "";
  espResponse = "";
  terminalSerial.println("-- SEND TO WiFi --");
  terminalSerial.println(atCommand);
  Serial.println(atCommand);
  if (getAnswer == true) {
    delay(answerWaitTime);
    answer = "";
    if (Serial.available() > 0) {
      while (Serial.available()) {
        answer = Serial.readString();
      }
    }
  }
  return answer;
}

/*
  Name: find
  Description: Function used to match two string
  Params:
  Returns: true if match else false
*/
boolean find(String s, String value) {
  if (s.indexOf(value) >= 0) {
    return true;
  } else {
    return false;
  }
}

void relay(int pinNumber, String position) {
  if (position == "ON") {
    digitalWrite(pinNumber, LOW);
  } else {
    digitalWrite(pinNumber, HIGH);
  }

}

void sendData(String reqID, String dataStr) {
  int len = dataStr.length();
  sendToWifi("AT+CIPSEND=" + reqID + "," + len, false);
  delay(1200);
  sendToWifi(dataStr, false);
  delay(300);
  sendToWifi("AT+CIPCLOSE=" + reqID, false);
}
  terminalSerial.begin(115200);

SoftwareSerial is not so stable at that baud rate... try to go slower start at 9600

and you have so many Strings instances in your code that my eyes hurt... :slight_smile: can't read the code further...

J-M-L:

  terminalSerial.begin(115200);

SoftwareSerial is not so stable at that baud rate... try to go slower start at 9600

and you have so many Strings instances in your code that my eyes hurt... :slight_smile: can't read the code further...

I'm test it with 9600 baudrate. Result is same. And I use Putty (Not use Arduino serial monitor) for serial monitor.

J-M-L:
and you have so many Strings instances in your code that my eyes hurt... :slight_smile: can't read the code further...

You are correct but I not need all. I'm delete next time :slight_smile:

The problem is not buffer overrun but the opposite, you start printing the buffer before you've received the whole message and then you flush the rest ! I am not in favor of using the ESP with AT-commands from an Arduino as a master since you can program the ESP directly using the Arduino IDE. But ... how about you read the bytes 1 by 1 and check if there is another byte available, and if there isn't do a delay of the time it takes to send a byte and try again, sort of like this :

String dataFromEsp = "";
while (Serial.available()) {
char c=Serial.read();
dataFromEsp+=c;
if (!Serial.available()) delayMicroseconds(200);
}

(typos may exist, untested code)
SoftwareSerial, it is actually reliable for transmission at that rate, and the hwSerial is used for communication with the ESP so there is no issue there.

Problem cause of SRAM overflow.. I need minimize SRAM use. :slight_smile:

makarna35:
Problem cause of SRAM overflow.. I need minimize SRAM use. :slight_smile:

No it isn't You need to make sure you read all the bytes that are being sent.

Deva_Rishi:
No it isn't You need to make sure you read all the bytes that are being sent.

I'm convert some Strings = "" to char. and I'm add F macro for some println (For using Flash memory)

Ex:

terminalSerial.println(F("-- We have a HTTP request --"));

After recompile I see reduce ram using.
I'm test it again and now I sure read all the bytes that are being sent