ESP crashes after http post

Hi there.

I need to send a dataset to a google sheets spreadsheet using esp32.
For that, I used the library: GitHub - electronicsguy/HTTPSRedirect: A library for seamless data logging, communication and control for Internet of Things.

The data is being sent and recorded in the spreadsheet, but as soon as esp32 performs the post, it simply hangs on the post command line: client->POST(url, host, payload, true).

What can I do to make esp32 continue to run normally after the Post command?

On the serial monitor, these are the only messages I got:

*wm:AutoConnect

*wm:Connecting to SAVED AP: VIVOFIBRA-0620

*wm:connectTimeout not set, ESP waitForConnectResult...

*wm:AutoConnect: SUCCESS

*wm:STA IP Address: 192.168.15.171

Client connected

Publishing data...

{"command": "insert_row", "sheet_name": "Registros", "values": "0,1,3,3,4,5,6"}

#include <HTTPClient.h>
#include <WiFi.h>
#include <WiFiManager.h>
#include "HTTPSRedirect.h"
#include "DebugMacros.h"
int value0 = 0;                      
int value1 = 1;                    
int value2 = 2;        
int value3 = 3;           
int value4 = 4;            
int value5 = 5;  
int value6 = 6;         

// Enter Google Script Deployment ID:
const char* GScriptId = "AKfycbw-K3_DS283dm4bHs4i2nCiPv7pkx7wz_NbhfHpv9CYvZq6hgukrbn8hwbdEIv0qll3";
// Enter command (insert_row or append_row) and your Google Sheets sheet name (default is Sheet1):
String payload_base = "{\"command\": \"insert_row\", \"sheet_name\": \"Registros\", \"values\": ";
String payload = "";
// Google Sheets setup (do not edit)
const char* host = "script.google.com";
const int httpsPort = 443;
String url = String("/macros/s/") + GScriptId + "/exec";

HTTPSRedirect* client = nullptr;

// Declare variables that will keep track of whether or not the data has been published
bool data_published = false;
int error_count = 0;

void sendData() {
  // before attempting to publish to Google Sheets, set the data_published variable to false and error_count to 0
  data_published = false;
  error_count = 0;
  static bool flag = false;
  if (!flag) {
    client = new HTTPSRedirect(httpsPort);
    client->setInsecure();
    flag = true;
    client->setPrintResponseBody(true);
    client->setContentTypeHeader("application/json");
  }
  if (client != nullptr) {
    if (!client->connected()) {
      client->connect(host, httpsPort);
      Serial.println("Client connected");
    }
  } else {
    Serial.println("Error creating client object!");
  }
  // Create json object string to send to Google Sheets
  payload = payload_base + "\"" + value0 + "," + value1 + "," + value2 + "," + value3 + "," + value4 + "," + value5 + "," + value6 + "\"}";
  // Publish data to Google Sheets
  Serial.println("Publishing data...");
  Serial.println(payload);
  if (client->POST(url, host, payload,true)) {
    // do stuff here if publish was successful
    data_published = true;
    Serial.println("Publish successful");
  } else {
    // do stuff here if publish was not successful
    Serial.println("Error while connecting");
    error_count++;
  }
  yield();
}

void setup() {
  Serial.begin(9600);
  WiFiManager wifiManager;
  wifiManager.autoConnect();
}

void loop() {
  value2 = value2+1;           
  sendData();
  delay(2000);
}

Have you tried to eliminate the WiFiManager from that sketch? Just use a fixed WiFi setup for testing. Once you got that running nicely you can try to add the WiFiManager.

I had a project some time ago (so this information might not be up-to-date) that used the WifiManager too and I never got it running smoothly. I ended up having the watchdog resetting the ESP periodically, otherwise it froze after a few minutes. Without the WiFiManager it ran without a problem so I got the impression that library has a memory leak or something similar but didn't find the time to search for that bug.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.