/*
Google Apps Scriptt:
var sheetID = '106hSdjV8a73Tknx5YO5U37T_A3YDgp-L3ga6euLedaI';
function doGet(e){
var temperature = e.parameter.temperature;
var humidity = e.parameter.humidity;
var sheet = SpreadsheetApp.openByID(sheetID).getActiveSheet();
var dateTime = new Date();
var currenDate =Utilities.formatDate(dateTime, "Europe/London", "dd/MM/yyyy");
var currenTime =Utilities.formatDate(dateTime, "Europe/London", "HH/mm/ss");
sheet.appenRow([curentDate, curenTime, temperature, humidity]);
return ContentService.createTextOutput('Data logged successfully');
}
*/
// IMPORTANT!! add "attrs": { "fastTLS": "1" } in wokwi diagram.json for esp board
#include <DHT.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define DHTPIN 4 // DHT22 sensor pin
#define DHTTYPE DHT22 // DHT11/22 sensor type
const char* ssid = "Wokwi-GUEST"; // Wifi name
const char* password = ""; // Wifi password
const char* webAppUrl = "https://script.google.com/macros/s/AKfycbynJQI3mSy3uLFcrMRAaX_s7SCRqU3WPogzGtbf7bd83Z4L9UjIKhNahXOtfv2vEC4o/exec"; // Replace with your actual web app URL
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
delay(100);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
dht.begin();
}
void loop() {
float humidity = dht.readHumidity();
float 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(" %");
// Send data to Google Apps Script
sendDataToScript(temperature, humidity);
delay(5000); // Delay for 5 seconds
}
void sendDataToScript(float temperature, float humidity) {
HTTPClient http;
// Construct the URL with query parameters
String serverPath = String(webAppUrl) + "?temperature=" + String(temperature) + "&humidity=" + String(humidity);
Serial.print("Connecting to server: ");
Serial.println(serverPath);
// Send HTTP GET request
if (http.begin(serverPath)) {
int httpCode = http.GET();
if (httpCode > 0) {
Serial.print("Server response code: ");
Serial.println(httpCode);
if (httpCode == HTTP_CODE_FOUND) { // HTTP_CODE_FOUND is 302
String newUrl = http.getLocation();
Serial.print("Redirected to: ");
Serial.println(newUrl);
http.end();
// Follow the redirect
if (http.begin(newUrl)) {
httpCode = http.GET();
if (httpCode > 0) {
Serial.print("Server response code after redirect: ");
Serial.println(httpCode);
String payload = http.getString();
Serial.println("Payload: " + payload);
} else {
Serial.print("HTTP GET request after redirect failed with error code: ");
Serial.println(httpCode);
}
http.end();
} else {
Serial.println("Unable to connect to the redirected server");
}
} else {
String payload = http.getString();
Serial.println("Payload: " + payload);
}
} else {
Serial.print("HTTP GET request failed with error code: ");
Serial.println(httpCode);
}
http.end();
} else {
Serial.println("Unable to connect to the server");
}
}