Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Tech500
Last active January 18, 2023 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tech500/27f751b9292f3097a75408b4a1cc8522 to your computer and use it in GitHub Desktop.
Save Tech500/27f751b9292f3097a75408b4a1cc8522 to your computer and use it in GitHub Desktop.
Google Sheets Script:
Based on this article:
https://iotdesignpro.com/articles/esp32-data-logging-to-google-sheets-with-google-scripts
var sheet_id = "SHEET_ID_REMOVED";
var sheet_name = "Weather1";
function doGet(e){
var ss = SpreadsheetApp.openById(sheet_id);
var sheet = ss.getSheetByName(sheet_name);
var dtstamp = String(e.parameter.dtstamp);
var temp = Number(e.parameter.temp);
var heatindex = Number(e.parameter.heatindex);
var humidity = Number(e.parameter.humidity);
var dewpoint = Number(e.parameter.dewpoint);
var pressure = Number(e.parameter.pressure);
var diff = Number(e.parameter.diff);
sheet.appendRow([dtstamp,temp,heatindex,humidity,dewpoint,pressure,diff]);
}
void googleSheet() //Updated 1/17/2023
{
char fahr[7];
dtostrf(temperature, 6, 1, fahr);
char heatindex[7];
dtostrf(heat, 6, 1, heatindex);
char humid[7];
dtostrf(hum, 6, 1, humid);
char dewpoint[7];
dtostrf(dew,6, 1, dewpoint);
char barometric[8];
dtostrf(currentPressure, 7, 3, barometric);
char diff[7];
dtostrf(difference, 6, 3, diff);
String data = "&dtstamp=" + dtStamp
+ "&temp=" + fahr
+ "&heatindex=" + heatindex
+ "&humidity=" + humid
+ "&dewpoint=" + dewpoint
+ "&pressure=" + barometric
+ "&diff=" + diff;
String urlFinal = "https://script.google.com/macros/s/"+GOOGLE_SCRIPT_ID+"/exec?"+data;
Serial.print("POST data to spreadsheet:");
Serial.println(urlFinal);
urlFinal.replace(" ", "%20"); //Remove spaces in url. Spaces not permitted in url's.
HTTPClient http;
http.begin(urlFinal.c_str());
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
int httpCode = http.GET();
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
//getting response from google sheet
String payload;
if (httpCode > 0) {
payload = http.getString();
Serial.println("Payload: "+payload);
}
http.end();
}
/*
Google Sheet scripting returns following error:
Removed GOOGLE_SCRIPT_ID for security. Verified GOOGLE_SCRIPT_ID is correct several times.
POST data to spreadsheet:https://script.google.com/macros/s/GOOGLE_SCRIPT_ID/exec?&dtstamp=Fri 01/06/2023 15:55:00&temp=68.27&heatindex=66.32&humidity=32.37&dewpoint=37.57&pressure=30.16&diff=0.00
HTTP Status Code: 400
<p><b>400.</b> <ins>That’s an error.</ins>
<p>Your client has issued a malformed or illegal request. <ins>That’s all we know.</ins>
However, was able to copy the returned URL and using browser address field; Google Sheet Row is successfully populated.
Update January 7, 2023
Issue has been resolved! Turns out the URL formatting of dtStamp (time) was incorrect. HTTP URL does not like spaces in URL;
used: dtStamp.replace(“”, -); to format dtStamp.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment