ESP8266 - Buffer Overflow?

Hi

I am just testing out a ESP8266 chip using a simple HTTP Request GET

I get a valid response but then I get corruption after about 60 characters.

At this point I am not sure of where the corruption is coming from? I am running from the Arduino Mega 3.3v line which may not be enough to drive the module properly or the corruption is because the serial buffer is full?

GET /HOME/TEST?x=741049 HTTP/1.0
Host: 192.168.2.124

This responds with

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=uet-e
r0rNr0 t
g
Y
atwnv/ll
  0pd>

Notice the response is good http response but failed around the charset=

The device is running firmware: 00160901 using the command AT+GMR

So this is not the latest firmware

I am hesitant to upgrade the firmware as some people seem to have an almost bricked device after upgrading to .992 where the device does not respond and may be a CR/LF issue or something else.

Chris

what does your code looks like?

Pretty basic simple example

unsigned long timer=0;
unsigned long sendtimer=0;
bool IsHigh = false;


#define SSID "CROWE" // insert your SSID
#define PASS "" // insert your password

#define LOCATIONID "2925533" // location id
#define DST_IP "192.168.2.124" //api.openweathermap.org


void setup()
{
  Serial.begin(9600);
  Serial.println("Starting....");

  Serial1.begin(115200);  
  pinMode(13, OUTPUT);   

  Serial1.println("AT+RST"); // restet and test if module is redy
  delay(1000);
  if(Serial1.find("ready")) {
    Serial.println("WiFi - Module is ready");
  }
  else{
    Serial.println("Module dosn't respond.");
    while(1);
  }
  delay(1000);
  // try to connect to wifi
  boolean connected=false;
  for(int i=0;i<5;i++){
    if(connectWiFi()){
      connected = true;
      Serial.println("Connected to WiFi...");
      break;
    }
  }
  if (!connected){
    Serial.println("Coudn't connect to WiFi.");
    while(1);
  }
  delay(5000);
  Serial1.println("AT+CIPMUX=0"); // set to single connection mode 
}

void loop()
{
  if ((millis() - timer) > 500)
  {
    if (IsHigh== false){
      digitalWrite(13, HIGH); 
      IsHigh = true;
    }
    else
    {
      digitalWrite(13, LOW); 
      IsHigh = false;
    }
    timer = millis();
  }
  while (Serial1.available())
  {
    char ch = Serial1.read();
    Serial.print(ch);
  }

  while (Serial.available())
  {
    char ch = Serial.read();
    Serial1.print(ch);
  }


  if ((millis() - sendtimer) > 20000)
  {
    sendtimer = millis();
    String cmd = "AT+CIPSTART=\"TCP\",\"";
    cmd += DST_IP;
    cmd += "\",80";
    Serial.println(cmd);
    Serial1.println(cmd);
    if(Serial1.find("Error")) return;
    cmd = "GET /HOME/TEST?x=";
    cmd += millis();
    cmd += " HTTP/1.0\r\nHost: 192.168.2.124\r\n\r\n";
    Serial1.print("AT+CIPSEND=");
    Serial1.println(cmd.length());
    if(Serial1.find(">")){
      Serial.print(">");
    }
    else{
      Serial1.println("AT+CIPCLOSE");
      Serial.println("connection timeout");
      delay(1000);
      return;
    }
    Serial1.print(cmd);
  }
}

boolean connectWiFi()
{
  Serial1.println("AT+CWMODE=1");
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  Serial.println(cmd);
  Serial1.println(cmd);
  delay(2000);
  if(Serial1.find("OK")){
    Serial.println("OK, Connected to WiFi.");
    return true;
  }
  else{
    Serial.println("Can not connect to the WiFi.");
    return false;
  }
}

You are using the String class which is known to use dynamic memory, something an Arduino is not good at.

  1. Please try to rewrite the code without Strings or
  2. initialize the String cmd with a size big enough to hold the longest String (in constructor IIRC)

You have sort of lost me Rob

The code that returns the data form the HTTP Web Server and sends to to the serial monitor is not using strings

while (Serial1.available())
{
char ch = Serial1.read();
Serial.print(ch);
}

Can you explain what you mean? IIRC?

I will change the code to use a fixed buffer for generating the HTTP Request and see what it does.

Hope to hear back from you.

Chris

you're not the first that lost me :wink:

This String I meant:

String cmd = "AT+CIPSTART=\"TCP\",\"";

I meant this - reserve() - Arduino Reference -

IIRC == If I Recall Correctly

Hi Again

I found a couple of interesting things:

  • I found that the main serial port I was using was at 9600 and the ESP8266 at 115200
  • I am using an arduino Mega 2560

So 9600/115200 - this seemed to be an issue.

The 9600 is 12 times slower than 115200, so this seemed to be an issue as I found when increasing the port speed it read more data..

I increased the speed of the 9600 port to 57600 and got more valid text but still some problems. I found that I could not get 115200 - everytime I restarted the script it would fail to connect to the wifi module.

So I changed from using the ESP8266 on Serial1 to Serial2 (Mega has 4 hardware serial)

This seems to fix things a lot.

I have not upgraded the firmware yet on the ESP8266 which allows you to use different serial speeds.

I think this is next.

Chris

  1. Upgrade the ESP8266 firmware to V0.922
    Follow directions from here
    With the new firmware the default baud rate is 9600, you can change it with AT+CIOBAUD=57600 or whatever rate you want. I find 57600 to be stable.
  2. Use a 3.3v regulator with a 1000uF or greater cap on the output to power the ESP8266, do not power it from the Mega 3.3V pin
  3. Connect the ESP8266 RST and CH_PD pins to 3.3V
  4. On the ESP8266 module connect a 10uf cap from VCC to GND
  5. You must use a level shifter like the CD4050BE for the RX TX lines, they will not work correctly with 5V.
    That will get your ESP8266 module setup correctly.