ESP32 crashes after http post

ctnalves
Posts: 1
Joined: Sun Jun 25, 2023 12:26 pm

ESP32 crashes after http post

Postby ctnalves » Sun Jun 25, 2023 12:35 pm

Hi there.

I need to send a dataset to a google sheets spreadsheet using esp32.
For that, I used the library: https://github.com/electronicsguy/HTTPSRedirect

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"}

Code: Select all

#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);
}

Who is online

Users browsing this forum: No registered users and 35 guests