electronicsguy / HTTPSRedirect

A library for seamless data logging, communication and control for Internet of Things
GNU Affero General Public License v3.0
57 stars 25 forks source link

Problem with ESP32 #1

Closed PatrikPatrik2 closed 2 years ago

PatrikPatrik2 commented 2 years ago

I got your scrips to run perfectly on an ESP8266, but for the ESP32 I get it to update the sheet once, but thereafter the scripts seems to stop.

I put two println for debugging - in the serial monitor it writes "före2"; the google sheet gets updated with hello and a time, but then it never print "EFTER". I have tested several different configurrations and it never get past the first update of the sheet.

Serial.println("före2"); // fetch spreadsheet data client->GET(url, host); Serial.println("EFTER");

What can be the problem?

github-actions[bot] commented 2 years ago

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

PatrikPatrik2 commented 2 years ago

The complete code is below

`/* HTTPS on ESP8266 with follow redirects, chunked encoding support

include

include "HTTPSRedirect.h"

include "DebugMacros.h"

extern const char ssid; extern const char password; extern const char host; extern const char GScriptId;

const int httpsPort = 443;

// echo | openssl s_client -connect script.google.com:443 |& openssl x509 -fingerprint -noout const char* fingerprint = ""; //const uint8_t fingerprint[20] = {};

// Write to Google Spreadsheet String url = String("/macros/s/") + GScriptId + "/exec?value=Hello"; // Fetch Google Calendar events for 1 week ahead String url2 = String("/macros/s/") + GScriptId + "/exec?cal"; // Read from Google Spreadsheet String url3 = String("/macros/s/") + GScriptId + "/exec?read";

String payload_prefix = "{\"command\": \"appendRow\", \ \"sheet_name\": \"Sheet1\", \ \"values\": \""; String payload_suffix = "\"}"; String payload = "";

HTTPSRedirect* client = nullptr; // used to store the values of free stack and heap // before the HTTPSRedirect object is instantiated // so that they can be written to Google sheets // upon instantiation

void setup() { Serial.begin(115200); Serial.flush();

Serial.println(); Serial.print("Connecting to wifi: "); Serial.println(ssid); // flush() is needed to print the above (connecting...) message reliably, // in case the wireless connection doesn't go through Serial.flush();

WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP());

// Use HTTPSRedirect class to create a new TLS connection client = new HTTPSRedirect(httpsPort); client->setInsecure(); client->setPrintResponseBody(true); client->setContentTypeHeader("application/json");

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

// Try to connect for a maximum of 5 times bool flag = false; for (int i=0; i<5; i++){ int retval = client->connect(host, httpsPort); if (retval == 1) { flag = true; break; } else Serial.println("Connection failed. Retrying..."); }

if (!flag){ Serial.print("Could not connect to server: "); Serial.println(host); Serial.println("Exiting..."); return; }

/
if (client->setFingerprint(fingerprint)) { Serial.println("Certificate match."); } else { Serial.println("Certificate mis-match"); }
/

// Send memory data to Google Sheets // payload = payload_prefix + free_heap_before + "," + free_stack_before + payload_suffix;

//client->POST(url2, host, payload, false);

Serial.println("före2"); // fetch spreadsheet data client->GET(url, host); Serial.println("EFTER");

// delete HTTPSRedirect object delete client; client = nullptr; }

void loop() { /*static int error_count = 0; static int connect_count = 0; const unsigned int MAX_CONNECT = 20; static bool flag = false; //Serial.printf("Free heap: %u\n", ESP.getFreeHeap()); //Serial.printf("Free stack: %u\n", ESP.getFreeContStack());

if (!flag){ free_heap_before = 51; free_stack_before = 52; 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); payload = payload_prefix + free_heap_before + "," + free_stack_before + payload_suffix; client->POST(url2, host, payload, false); } } else{ DPRINTLN("Error creating client object!"); error_count = 5; }

if (connect_count > MAX_CONNECT){ //error_count = 5; connect_count = 0; flag = false; delete client; return; }

Serial.println("GET Data from cell 'A1':"); if (client->GET(url3, host)){ ++connect_count; } else{ ++error_count; DPRINT("Error-count while connecting: "); DPRINTLN(error_count); }

Serial.println("POST append memory data to spreadsheet:"); payload = payload_prefix + "61" + "," + "62" + payload_suffix; if(client->POST(url2, host, payload)){ ; } else{ ++error_count; DPRINT("Error-count while connecting: "); DPRINTLN(error_count); }

/* if (!(client.reConnectFinalEndpoint())){ ++error_count; DPRINT("Error-count while connecting: "); DPRINTLN(error_count); } else error_countunt = 0;

if (error_count > 3){ Serial.println("Halting processor..."); delete client; client = nullptr; Serial.printf("Final free heap: %u\n", "71"); Serial.printf("Final stack: %u\n", "72"); Serial.flush(); ESP.deepSleep(0); }

// In my testing on a ESP-01, a delay of less than 1500 resulted // in a crash and reboot after about 50 loop runs. */ Serial.println("qw"); delay(4000);

}`

electronicsguy commented 2 years ago

Yes, I get similar errors with ESP32. It has been reported in other places as well:

https://github.com/espressif/arduino-esp32/issues/5021

https://community.blynk.cc/t/esp32-stopped-working-with-ssl-connection/51845/4

AFAIU, ssl/tls support is not fully compatible on esp32 as yet. I haven't gotten the time to debug further. You're welcome to try.

lucasromeiro commented 1 year ago

Yes, I get similar errors with ESP32. It has been reported in other places as well:

espressif/arduino-esp32#5021

https://community.blynk.cc/t/esp32-stopped-working-with-ssl-connection/51845/4

AFAIU, ssl/tls support is not fully compatible on esp32 as yet. I haven't gotten the time to debug further. You're welcome to try.

Hello! I have the same problem! Do you find the solution?

probonopd commented 1 year ago

Please reopen as this issue seems to persist. Thanks!