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