ESP8266 stop working after several connections

Hi community,

I have been trying all the forums to find the solution to my problem with no success. I created a class to use AT commands to send a POST to a webapi using an Arduino Mega and a ES8266 WIFI module.

Everything works perfect at the beginning, but after 30 minutes, the AT+CIPSTART starts failing. I added an AT+CLOSE command after each AT+CIPSEND.

I have to reset the WIFI module to get it to work again.

Does anybody have faced the same issue? Any ideas how to fix it? memory issues?

Thanks

Any ideas how to fix it? memory issues?

Without having seen your code?

I have to reset the WIFI module to get it to work again.

So resetting the WiFi module is enough, you don't have to reset the Arduino?

Hi, sorry, I am attaching the code.

This is the version of the firmware:

AT version:1.7.4.0(May 11 2020 19:13:04)
SDK version:3.0.4(9532ceb)
compile time:May 27 2020 10:12:17
Bin version(Wroom)

Thanks,

#define _SSID "My Network"
#define _PASS "My Password"

#define _HOST "ebrechabf-001-site2.itempurl.com"
#define _HTTP_PORT 80

#define _AUTHENTICATE_URL "/api/device/authenticate"
#define _DEVICE_GUID "15c22adb-0230-47b1-924f-886f376cc4d0"
#define _ACCESS_KEY "IEER3SR02Q9S7S5T"

char AuthenticationPost[256] = "\0"; 
char AuthenticationData[128] = "\0";
int LoopCount = 1;

void ClearMemory(void) {
  while(Serial1.available() > 0) { Serial1.read(); }
}

void PrintResult(uint32_t timeout) {
  char a;
  unsigned long start = millis();
  while (millis() - start < timeout) {
    while(Serial1.available() > 0) {
      a = Serial1.read(); 
      Serial.print(a); 
    }
  }
}

void SendCommand(String command, uint32_t timeout = 1500) {
  ClearMemory();
  Serial1.println(command);
  PrintResult(timeout);
}

void SendPost(const uint8_t * buffer, uint32_t len) {
  ClearMemory();
  Serial1.println("AT+CIPSEND=" + String(len));
  PrintResult(5000);
  ClearMemory();
  for (uint32_t i = 0; i < len; i++) {
    Serial1.write(buffer[i]);
  }
  PrintResult(10000);
}

void CreateAuthenticationData() {
  strcat(AuthenticationData, "{deviceguid:\"");
  strcat(AuthenticationData, _DEVICE_GUID);
  strcat(AuthenticationData, "\",accesskey:\"");
  strcat(AuthenticationData, _ACCESS_KEY);  
  strcat(AuthenticationData, "\"}");  
}

void CreateAuthenticationPost() {
  CreateAuthenticationData();
  strcat(AuthenticationPost, "POST ");
  strcat(AuthenticationPost, _AUTHENTICATE_URL);
  strcat(AuthenticationPost, " HTTP/1.1\r\n");
  strcat(AuthenticationPost, "Host: ");
  strcat(AuthenticationPost, _HOST);
  strcat(AuthenticationPost, "\r\n");
  strcat(AuthenticationPost,"Accept: */*\r\n");        
  strcat(AuthenticationPost, "Content-Type: application/json\r\n");
  strcat(AuthenticationPost, "Content-Length: ");
  uint32_t len = strlen(AuthenticationData);
  strcat(AuthenticationPost, String(len).c_str());
  strcat(AuthenticationPost, "\r\n\r\n");
  strcat(AuthenticationPost, AuthenticationData);
}

void setup() {
  Serial.begin(9600);
  Serial.println("Starting");
  Serial1.begin(115200);
  SendCommand("AT+GMR");
  SendCommand("AT+CWMODE=1");
  SendCommand("AT+CWJAP=\"" + String(_SSID) + "\",\"" + String(_PASS) +"\"", 10000);
  SendCommand("AT+CIFSR");
  SendCommand("AT+CIPMUX=0");
  CreateAuthenticationPost();
}

void loop() {
  Serial.println("Post number:" + String(LoopCount++));
  SendCommand("AT+CIPSTART=\"TCP\",\"" + String(_HOST) +"\"," + String(_HTTP_PORT), 5000);
  SendPost(AuthenticationPost, strlen(AuthenticationPost));
  SendCommand("AT+CIPCLOSE");
  delay(1000);
}

The symptoms you describe are charertistic of running out of memory. I would start off by removing the use of String especially in the Loop() and anything that calls. String is Evil, as it dynamically allocates memory ad there is no memory garbage collection on the Arduino so the heap can easily become very fragmented and allocation no longer works.

Thanks countrypaul,

Actually this is an extract of the class I created, I tried to make it more simple. I have uploaded this code to the arduino and it is working better. 4 hours and only some errors some times....but it is still running.

I am going to remove all those strings and let you know the results.

Thanks again.