ESP32 not connecting to Google Sheets

Hi Community
Currently I am working on ESP32 and trying to build a simple project to connect to google sheets via script editor and post the ESP32 device ID and I am trying from last 5 hours but again and again getting failed after multiple attempts for searching and hit and try I am finally here to seek help

Here is my code

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "****";
const char* password = "***";
String GOOGLE_SCRIPT_ID = "***";

const int sendInterval = 5000;

void setup() {
  Serial.begin(115200);
  delay(10);
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("OK");
}

void loop() {
  // Store the device ID in a variable
  String deviceID = "ESP32 ID: " + String((uint32_t)(ESP.getEfuseMac() >> 32), HEX) + String((uint32_t)ESP.getEfuseMac(), HEX);

  // Print the device ID variable
  Serial.println(deviceID);
  
  String param = "deviceID=" + deviceID;  // Removed unnecessary function call

  Serial.println(param);
  write_to_google_sheet(param);
  
  delay(sendInterval);
}

void write_to_google_sheet(String params) {
  HTTPClient http;
  String url = "https://script.google.com/macros/s/" + GOOGLE_SCRIPT_ID + "/exec?" + params;
  
  Serial.println("Posting GPS data to Google Sheet");
  http.begin(url.c_str());
  http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
  int httpCode = http.GET();
  Serial.print("HTTP Status Code: ");
  Serial.println(httpCode);
  
  String payload;
  if (httpCode > 0) {
    payload = http.getString();
    Serial.println("Payload: " + payload);
  }
  
  http.end();
}

And here is my google script code

var ss = SpreadsheetApp.openById(***');
var sheet = ss.getSheetByName('Sheet1');

function doGet(e) {
  if (e.parameter.deviceID === undefined) {
    return ContentService.createTextOutput("Received data is undefined");
  }
  
  var deviceID = e.parameter.deviceID;
  var dateTime = new Date();
  var nextRow = sheet.getLastRow() + 1;
  
  sheet.getRange("A" + nextRow).setValue(deviceID);
  
  return ContentService.createTextOutput("Status Updated in Google Sheet");
}

function doPost(e) {
  var val = e.parameter.value;
  
  if (val !== undefined){
    var range = sheet.getRange('A2');
    range.setValue(val);
  }
}

I have hidden some personal information with ****

Any help will be great Appreciable. Please guide me how to do the same and if possible please provide the code

Thanks & Regards
Krishna :}

Can anyone Please help with the same I am eagerly waiting

You do have debugging Serial.print to serial monitor. What do they show?

Hi,
Can I suggest you edit your subject to.

ESP32 not connecting to Google Sheets.

That way you may get some Google Sheet experts to read this thread.

Tom... :smiley: :+1: :coffee: :australia:

Yes I do and it is showing "Status Updated in Google Sheet" but as soon as I open the sheet nothing is being posted

Thanks for the suggestion

Okey. The ESP sent dat and makes rhe debug serial.print. Is there any acknowledge that the data was received properly?

Yes in the code I have written to push data and print "Status Updated in Google Sheet" and I am receiving the same but nothing is being displayed I have already done posting ldr sensor data to google sheets and its easily worked out but when I change the code for getting the device id nothing is being posted also without pushing the data I directly tried to fetch device ID in serial monitor that too work can anyone please help me in the code I am really stuck :sneezing_face:

Regarding the technical details I've got no knowledge. Good that You managed to push ldr data to G S. Getting device ID needs more studying.
I'm sure a knowing helper will pop up.

Thanks for the appreciation I will surely wait for any help and I am dam sure in this big community anyone will surely help me out of this

What do You think about reply #4?

Hi there actually on the same day I have updated the title but haven't checked that and today I have seen that it is not been updated :slightly_frowning_face:

Hit it again! Strange things sometimes happen.
Make sure You don't create a duplicate question.

For Sure I will never do that to waste anyone presious time

Can you please confirm is it updated or not?

It's changed.

Thanks for the information hope someone will help me out in this

Can anyone please response to it

Hi Members

Any Information regarding the same?

Trying this Tutorial, works fine here.

1 Like

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