ESP32 Google Sheets (Sending Data and Reading Data) I Google Apps Script Code
ESP32 Google Sheets (Sending Data and Reading Data) I Google Apps Script Code
________________________________________________________________________________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
///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// 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 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;
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";
}
}
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, "");
}
//________________________________________________________________________________