electronicsguy / ESP8266

ESP8266 Projects
242 stars 183 forks source link

Failure in client->POST on a loop leading to subsequent failures on all calls to client->connect #92

Closed specau closed 4 years ago

specau commented 4 years ago

Using library Version 2.1. If the POST method fails once, then the library stops working for all further attempts, till the ESP8266 is reset.

Here's an extract of the code being used:

// New HTTPS Redirect Method
void sendData(String probetime, float sensor1, float sensor2) {
  wifiON(); //helper method to connect to wifi

  HTTPSRedirect* client = new HTTPSRedirect(HTTPS_PORT);
  client->setInsecure();
  client->setPrintResponseBody(true);
  client->setContentTypeHeader("application/json");

  Serial.print("Connecting to ");
  Serial.println(GOOGLE_SCRIPT_HOST);

  // Try to connect for a maximum of 5 times
  bool flag = false;
  for (int i = 0; i < 5; i++) {
    int retval = client->connect(GOOGLE_SCRIPT_HOST, HTTPS_PORT);
    if (retval == 1) {
      flag = true;
      break;
    }
    else
      Serial.println("Connection failed. Retry ");
      // Wait for a while before attempting to reconnect to host
      delay(5000);
  }

  if (flag) {
    // POST Method
    String payload = "{\"ProbeTime\":\"" + probetime + "\",\"Sensor1\":\"" + sensor1 + "\",\"Sensor2\":\"" + sensor2 + "\"}";

    Serial.print("Payload=");
    Serial.println(payload);

    Serial.println("Posting data to sheets");
    boolean postsuccess;
    postsuccess = client->POST(googleScriptURL, GOOGLE_SCRIPT_HOST, payload, true);
    if(postsuccess) {
      Serial.println("Data sent");
    }
    else {
      Serial.println("Send failed");
    }

  }
  else {
    Serial.print("Could not connect to server: ");
    Serial.println(GOOGLE_SCRIPT_HOST);
    Serial.println("Exiting...");
  }

  wifiOFF();
}

In the Arduino loop, there is a call to the sensors function (not shown here) every 5 mins. After obtaining the values, the function above is called. This works fine for sometimes a few hours and sometimes it starts failing within an hour. Once it fails, it never recovers, I've tried waiting for over 2 hours to monitor it.

Details of how the failure looks like: The function works, up to a point where there is a failure in the line

postsuccess = client->POST(googleScriptURL, GOOGLE_SCRIPT_HOST, payload, true);

Logs show:

Posting data to sheets
 :abort
Connection to re-directed URL failed!
Send failed

Once this failure occurs in any loop, in the execution of future loop, the function starts failing at this line

int retval = client->connect(GOOGLE_SCRIPT_HOST, HTTPS_PORT);

Logs start showing:

Connecting to script.google.com
:abort
Connection failed. Retrying...
:abort
Connection failed. Retrying...
:abort
Connection failed. Retrying...
:abort
Connection failed. Retrying...
:abort
Connection failed. Retrying...
Could not connect to server: script.google.com
Exiting... 

it never reaches the POST call and fails in the connect itself.

Is there something in the library code which has a cache and once it fails, it's unable to recover ?

github-actions[bot] commented 4 years ago

Welcome to HTTPSRedirect! Please provide enough info to debug the issue.

specau commented 4 years ago

this issue is due to memory management. The library relies on the code cleaning up used memory every time, method termination doesn't seem to clean up.

Added this to the code, just before close of method.

  // Clean up
  delete client;
  client = nullptr;