0% found this document useful (0 votes)
65 views3 pages

ESP32 Google Sheets (Sending Data and Reading Data) I Google Apps Script Code

Uploaded by

Jonas Santana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views3 pages

ESP32 Google Sheets (Sending Data and Reading Data) I Google Apps Script Code

Uploaded by

Jonas Santana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

//

________________________________________________________________________________Goo
gle Sheets and Google Apps Script Project Information.
Google Sheets Project Name : ESP32_Google_Spreadsheet
Google Sheets ID :
Sheet Name : ESP32_Google_Sheets_Sheet

DHT11 Sensor Data Logger


Date | Time | Sensor Reading Status | Temperature (°C) | Humidity (%) | Switch 1 |
Switch 2

Latest DHT11 Sensor Data


Date | Time | Sensor Reading Status | Temperature (°C) | Humidity (%) | Switch 1 |
Switch 2

Google Apps Script Project Name : ESP32_Google_Spreadsheet_Apps_Script


Web app URL :

Web app URL Test Write :


?sts=write&srs=Success&temp=32.5&humd=95&swtc1=Off&swtc2=Off

Web app URL Test Read :


?sts=read
//________________________________________________________________________________

///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// Before you use this "Google Apps Script" code, I hope you have watched the video
until the end. //
// Because there are several configurations and settings for this project and this
Google Apps Script code that is shown or can be seen in the video. //
// And also so that you know how to use this Google Apps Script code.
//
//
//
// If you see some differences between the code I shared and the code that appears
in the video, it's because after I made the video, //
// I fixed several lines of code. So follow the code that I have shared.
//
///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

//
________________________________________________________________________________Goo
gle Apps Script
function doGet(e) {
Logger.log(JSON.stringify(e));
var result = 'Ok';
if (e.parameter == 'undefined') {
result = 'No Parameters';
}
else {
var sheet_id = 'REPLACE_WITH_YOUR_SPREADSHEET_ID'; // Spreadsheet ID.
var sheet_name = "REPLACE_WITH_YOUR_SHEET_NAME_IN_GOOGLE_SHEETS"; // Sheet
Name in Google Sheets.

var sheet_open = SpreadsheetApp.openById(sheet_id);


var sheet_target = sheet_open.getSheetByName(sheet_name);
var newRow = sheet_target.getLastRow() + 1;

var rowDataLog = [];

var Data_for_I3;
var Data_for_J3;
var Data_for_K3;
var Data_for_L3;
var Data_for_M3;
var Data_for_N3;
var Data_for_O3;

var Curr_Date = Utilities.formatDate(new Date(), "Asia/Jakarta", 'dd/MM/yyyy');


rowDataLog[0] = Curr_Date; // Date will be written in column A (in the "DHT11
Sensor Data Logger" section).
Data_for_I3 = Curr_Date; // Date will be written in column I3 (in the "Latest
DHT11 Sensor Data" section).

var Curr_Time = Utilities.formatDate(new Date(), "Asia/Jakarta", 'HH:mm:ss');


rowDataLog[1] = Curr_Time; // Time will be written in column B (in the "DHT11
Sensor Data Logger" section).
Data_for_J3 = Curr_Time; // Time will be written in column J3 (in the "Latest
DHT11 Sensor Data" section).

var sts_val = '';

for (var param in e.parameter) {


Logger.log('In for loop, param=' + param);
var value = stripQuotes(e.parameter[param]);
Logger.log(param + ':' + e.parameter[param]);
switch (param) {
case 'sts':
sts_val = value;
break;

case 'srs':
rowDataLog[2] = value; // Sensor Reading Status will be written in
column C (in the "DHT11 Sensor Data Logger" section).
Data_for_K3 = value; // Sensor Reading Status will be written in column
K3 (in the "Latest DHT11 Sensor Data" section).
result += ', Sensor Reading Status Written on column C';
break;

case 'temp':
rowDataLog[3] = value; // The temperature value will be written in
column D (in the "DHT11 Sensor Data Logger" section).
Data_for_L3 = value; // The temperature value will be written in column
L3 (in the "Latest DHT11 Sensor Data" section).
result += ', Temperature Written on column D';
break;

case 'humd':
rowDataLog[4] = value; // The humidity value will be written in column E
(in the "DHT11 Sensor Data Logger" section).
Data_for_M3 = value; // The humidity value will be written in column M3
(in the "Latest DHT11 Sensor Data" section).
result += ', Humidity Written on column E';
break;
case 'swtc1':
rowDataLog[5] = value; // The state of Switch_1 will be written in
column F (in the "DHT11 Sensor Data Logger" section).
Data_for_N3 = value; // The state of Switch_1 will be written in column
N3 (in the "Latest DHT11 Sensor Data" section).
result += ', Switch_1 Written on column F';
break;

case 'swtc2':
rowDataLog[6] = value; // The state of Switch_2 will be written in
column G (in the "DHT11 Sensor Data Logger" section).
Data_for_O3 = value; // The state of Switch_2 will be written in column
O3 (in the "Latest DHT11 Sensor Data" section).
result += ', Switch_2 Written on column G';
break;

default:
result += ", unsupported parameter";
}
}

// Conditions for writing data received from ESP32 to Google Sheets.


if (sts_val == 'write') {
// Writes data to the "DHT11 Sensor Data Logger" section.
Logger.log(JSON.stringify(rowDataLog));
var newRangeDataLog = sheet_target.getRange(newRow, 1, 1, rowDataLog.length);
newRangeDataLog.setValues([rowDataLog]);

// Write the data to the "Latest DHT11 Sensor Data" section.


var RangeDataLatest = sheet_target.getRange('I3:O3');
RangeDataLatest.setValues([[Data_for_I3, Data_for_J3, Data_for_K3,
Data_for_L3, Data_for_M3, Data_for_N3, Data_for_O3]]);

return ContentService.createTextOutput(result);
}

// Conditions for sending data to ESP32 when ESP32 reads data from Google
Sheets.
if (sts_val == 'read') {
// Use the line of code below if you want ESP32 to read data from columns I3
to O3 (Date,Time,Sensor Reading Status,Temperature,Humidity,Switch 1, Switch 2).
// var all_Data = sheet_target.getRange('I3:O3').getDisplayValues();

// Use the line of code below if you want ESP32 to read data from columns K3
to O3 (Sensor Reading Status,Temperature,Humidity,Switch 1, Switch 2).
var all_Data = sheet_target.getRange('K3:O3').getValues();
return ContentService.createTextOutput(all_Data);
}
}
}
function stripQuotes( value ) {
return value.replace(/^["']|['"]$/g, "");
}
//________________________________________________________________________________

You might also like