ESP32 call Google sheet script return 500

Lexfee
Posts: 1
Joined: Sun Jun 30, 2024 12:22 pm

ESP32 call Google sheet script return 500

Postby Lexfee » Sun Jun 30, 2024 12:56 pm

Why I keep get 500 error on this code? The link works fine on the browser, but I keep get error on ESP32. I can see the call on Google sheet, it just say failed but no logs at all. Anyone can quick test the code for me, would this be internet issue or something wrong with my code? I do set "HTTPC_STRICT_FOLLOW_REDIRECTS", what else I am missing?

Code: Select all

#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>

const char* rootCACertificate = "-----BEGIN CERTIFICATE-----\n" \
"MIIFVzCCAz+gAwIBAgINAgPlk28xsBNJiGuiFzANBgkqhkiG9w0BAQwFADBHMQsw\n" \
"CQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2VzIExMQzEU\n" \
"MBIGA1UEAxMLR1RTIFJvb3QgUjEwHhcNMTYwNjIyMDAwMDAwWhcNMzYwNjIyMDAw\n" \
"MDAwWjBHMQswCQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZp\n" \
"Y2VzIExMQzEUMBIGA1UEAxMLR1RTIFJvb3QgUjEwggIiMA0GCSqGSIb3DQEBAQUA\n" \
"A4ICDwAwggIKAoICAQC2EQKLHuOhd5s73L+UPreVp0A8of2C+X0yBoJx9vaMf/vo\n" \
"27xqLpeXo4xL+Sv2sfnOhB2x+cWX3u+58qPpvBKJXqeqUqv4IyfLpLGcY9vXmX7w\n" \
"Cl7raKb0xlpHDU0QM+NOsROjyBhsS+z8CZDfnWQpJSMHobTSPS5g4M/SCYe7zUjw\n" \
"TcLCeoiKu7rPWRnWr4+wB7CeMfGCwcDfLqZtbBkOtdh+JhpFAz2weaSUKK0Pfybl\n" \
"qAj+lug8aJRT7oM6iCsVlgmy4HqMLnXWnOunVmSPlk9orj2XwoSPwLxAwAtcvfaH\n" \
"szVsrBhQf4TgTM2S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eJ9r/tSQdirpLsQBqvFAnZ\n" \
"0E6yove+7u7Y/9waLd64NnHi/Hm3lCXRSHNboTXns5lndcEZOitHTtNCjv0xyBZm\n" \
"2tIMPNuzjsmhDYAPexZ3FL//2wmUspO8IFgV6dtxQ/PeEMMA3KgqlbbC1j+Qa3bb\n" \
"bP6MvPJwNQzcmRk13NfIRmPVNnGuV/u3gm3c\n" \
"-----END CERTIFICATE-----\n";

const char *tz_config = "GMT0BST:M3.5.0/1:M10.5.0";
void setClock() {
  configTzTime(tz_config, "pool.ntp.org");

  Serial.print(F("Waiting for NTP time sync: "));

  struct tm timeinfo;
  while (!getLocalTime(&timeinfo))
  {
    Serial.print(".");
    delay(1000);
  }

  Serial.println("init_setup_time: sync_time success");
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}

WiFiMulti WiFiMulti;

void setup() {

  Serial.begin(115200);
  // Serial.setDebugOutput(true);

  Serial.println();
  Serial.println();
  Serial.println();
 
  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("ssid", "password");

  // wait for WiFi connection
  Serial.print("Waiting for WiFi to connect...");
  while ((WiFiMulti.run() != WL_CONNECTED)) {
    //Serial.print(".");
  }
  Serial.println(" connected");
  setClock();  
}

void loop() {
  WiFiClientSecure *client = new WiFiClientSecure;
  
  if(client) {
    client -> setCACert(rootCACertificate);
    //client->setInsecure();

    {
      // Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is 
      HTTPClient https;
      //String url = "https://jigsaw.w3.org/HTTP/connection.html";
      String url = "https://script.google.com/macros/s/AKfycbzDZlRMwrqGb_Umb1nUHaOotppC81iwcjuoXZOgmaeX/dev?action=test";
      Serial.print("[HTTPS] begin...\n");

      if (https.begin(*client, url)) {  // HTTPS
        Serial.print("[HTTPS] GET...\n");
        https.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);

        // start connection and send HTTP header
        int httpCode = https.GET();
        Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
        // httpCode will be negative on error
        if (httpCode > 0) {
          // HTTP header has been send and Server response header has been handled
          Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
          
          String payload = https.getString();
          Serial.println(payload);
            
          // file found at server
          if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
            //String payload = https.getString();
            //Serial.println(payload);
            Serial.println("Success!!!");
          }
        } else {
          Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); 
          //code: -1, https.errorToString(httpCode).c_str() = connection refused
        }

        https.end();
      } else {
        Serial.printf("[HTTPS] Unable to connect\n");
      }

      // End extra scoping block
    }
  
    delete client;
  } else {
    Serial.println("Unable to create client");
  }

  Serial.println();
  Serial.println("Waiting 30s before the next round...");
  delay(30000);
}

aliarifat794
Posts: 200
Joined: Sun Jun 23, 2024 6:18 pm

Re: ESP32 call Google sheet script return 500

Postby aliarifat794 » Mon Jul 01, 2024 10:53 am

I guess it has more to do with the google Sheets API. You can try the troubleshooting tricks here: https://developers.google.com/sheets/ap ... api-errors

Who is online

Users browsing this forum: No registered users and 64 guests