The function 'doPost' processes incoming JSON data and logs it into a Google Sheet. It ensures a header row exists and formats the ESP32 timestamp before appending sensor data such as CO levels, vibration intensity, humidity, and roll angle. Finally, it confirms successful data receipt with a text output message.
The function 'doPost' processes incoming JSON data and logs it into a Google Sheet. It ensures a header row exists and formats the ESP32 timestamp before appending sensor data such as CO levels, vibration intensity, humidity, and roll angle. Finally, it confirms successful data receipt with a text output message.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = JSON.parse(e.postData.contents); // Parse incoming JSON
// Ensure the sheet has a header row if it doesn't exist
if (sheet.getLastRow() === 0) { sheet.appendRow([ "Sheet Time", // Current time of data logging "Timestamp (MM:SS)", // ESP32 timestamp in minutes and seconds "CO (ppm)", // CO level from MQ sensor "Vibration Intensity", // Vibration intensity "Humidity (%)", // Humidity from DHT sensor "X Angle (°)", // Roll angle ]); }
// Extract ESP32 timestamp and format it to MM:SS
var esp32Time = new Date(data.timestamp || new Date()); // Fallback tto current time var minutes = esp32Time.getMinutes(); var seconds = esp32Time.getSeconds(); var formattedTimestamp = `${minutes}:${seconds}`;
// Append new sensor data to the Google Sheet
sheet.appendRow([ new Date(), // Current time in sheet formattedTimestamp, // ESP32 timestamp in MM:SS data.co || "N/A", // CO level data["vibration Intensity"] || "N/A", // Vibration intensity data.Humidity || "N/A", // Humidity data.Roll || "N/A", // X angle (roll) ]);
// Confirm data receipt
return ContentService.createTextOutput("Data received successfully"); }