/*
Google Apps Script:
function doGet(e) {
Logger.log( JSON.stringify(e) ); // view parameters
var result = 'Ok'; // assume success
if (e.parameter == 'undefined') {
result = 'No Parameters';
}
else {
var sheet_id = '1TW5akRoMF5s0a5bUQGn0bu9Z_5_d5yWmIFemgBx8_TQ'; // Spreadsheet ID
var sheet = SpreadsheetApp.openById(sheet_id).getActiveSheet(); // get Active sheet
var newRow = sheet.getLastRow() + 1;
var rowData = [];
d=new Date();
rowData[0] = d; // Timestamp in column A
rowData[1] = d.toLocaleTimeString(); // Timestamp in column A
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 'value1': //Parameter 1, It has to be updated in Column in Sheets in the code, orderwise
rowData[2] = value; //Value in column A
result = 'Written on column A';
break;
case 'value2': //Parameter 2, It has to be updated in Column in Sheets in the code, orderwise
rowData[3] = value; //Value in column B
result += ' Written on column B';
break;
case 'value3': //Parameter 3, It has to be updated in Column in Sheets in the code, orderwise
rowData[4] = value; //Value in column C
result += ' Written on column C';
break;
case 'value4': //Parameter 4, It has to be updated in Column in Sheets in the code, orderwise
rowData[5] = value; //Value in column C
result += ' Written on column C';
break;
default:
result = "unsupported parameter";
}
}
Logger.log(JSON.stringify(rowData));
// Write new row below
var newRange = sheet.getRange(newRow, 1, 1, rowData.length);
newRange.setValues([rowData]);
}
// Return result of operation
return ContentService.createTextOutput(result);
}
function stripQuotes( value ) {
return value.replace(/^["']|['"]$/g, "");
}
*/
//Include required libraries
#include "WiFi.h"
#include <HTTPClient.h>
#include "time.h"
#include <DHT.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define DHTPIN 4 // DHT22 sensor pin
#define DHTTYPE DHT22 // DHT11/22 sensor type
DHT dht(DHTPIN, DHTTYPE);
float humidity, temperature;
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 28800;
// WiFi credentials
const char* ssid = "Wokwi-GUEST"; // Wifi name
const char* password = ""; // Wifi password
// Google script ID and required credentials
String GOOGLE_SCRIPT_ID = "AKfycbwoH5n8Get37F1IW-Plwt_bpuPZN8IwQnwAuCwB4WEy_5GDtpUyU4gdRCx4UCHXEjlc"; // change Gscript ID
//String GOOGLE_SCRIPT_ID = "AKfycbzNQi7AhpZHs0UTiWjQoufi8RWcQ48BvdoOXRf0HznRcgRdrON0NHvdxvJrxT0NvAr2";
int count = 0,count1=0,count2=0,count3=0;
void setup() {
delay(1000);
Serial.begin(115200);
delay(1000);
// connect to WiFi
Serial.println();
Serial.print("Connecting to wifi: ");
Serial.println(ssid);
Serial.flush();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
dht.begin();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
static bool flag = false;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
//return;
}
char timeStringBuff[50]; //50 chars should be enough
strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo);
String asString(timeStringBuff);
asString.replace(" ", "-");
Serial.print("Time:");
Serial.println(asString);
humidity = dht.readHumidity();
temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor");
delay(2000);
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
//String urlFinal = "https://script.google.com/macros/s/"+GOOGLE_SCRIPT_ID+"/exec?"+"value1=" + asString + "&value2=" + String(count)"&value3=" + String(count1);
String urlFinal = "https://script.google.com/macros/s/"+GOOGLE_SCRIPT_ID+"/exec?"+"value1=" + String(humidity) + "&value2=" + String(temperature) + "&value3=" + String(count2) + "&value4=" + String(count3);
//String urlFinal = "https://script.google.com/macros/s/"+GOOGLE_SCRIPT_ID+"/exec?"+"value1=" + String(humidity) + "&value2=" + String(temperature) + "&value3=" + String(count2) + "&value4=" + String(count3);
Serial.print("POST data to spreadsheet:");
Serial.println(urlFinal);
HTTPClient http;
http.begin(urlFinal.c_str());
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();
}
count++;
delay(1000);
count1=count;
count2=count1+count;
count3=count2+count1;
}