/*
Google Apps Script:
var sheetID = ''; // Replace with your actual spreadsheet ID
function doGet(e) {
var temperature = e.parameter.temperature;
var humidity = e.parameter.humidity;
var sheet = SpreadsheetApp.openById(sheetID).getActiveSheet();
var dateTime = new Date();
var currentDate = Utilities.formatDate(dateTime, "Europe/London", "dd/MM/yyyy"); // Replace Timezone ID and format
var currentTime = Utilities.formatDate(dateTime, "Europe/London", "HH:mm:ss"); // Replace Timezone ID and format
sheet.appendRow([currentDate, currentTime, 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
#define SWITCH_ON (18) //Pin D18 SWITCH ON
#define SWITCH_OFF (19) //Pin D19 SWITCH OFF
#define BUTTON_START digitalRead(SWITCH_ON) == 1 // Set Input Pin
#define BUTTON_STOP digitalRead(SWITCH_OFF)== 1 // Set Input Pin
const char* ssid = "Wokwi-GUEST"; // Wifi name
const char* password = ""; // Wifi password
const char* webAppUrl = "https://script.google.com/macros/s/AKfycbzlaJYBmXYcQ6h8G3P5SxUe7Hx0vMUDlXpk65wOOQCEwiuQ8RAvp0QWwBfgcruMaQk/exec"; // Replace with your actual web app URL
DHT dht(DHTPIN, DHTTYPE);
String Switch_1_State = "";
String Switch_2_State = "";
String Status_Read_Sensor = "";
/**********************************************************************************
***********************************************************************************/
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))
{
Status_Read_Sensor = "Failed";
temperature = 0.00;
humidity = 0.00;
Serial.println("Failed to read from DHT sensor");
delay(2000);
return;
}
else
{
Status_Read_Sensor = "Success";
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Send data to Google Apps Script
sendDataToScript(temperature, humidity);
Read_Switches_State();
delay(5000); // Delay for 5 seconds
}
/**********************************************************************************
***********************************************************************************/
void sendDataToScript(float temperature, float humidity)
{
HTTPClient http;
// Your server address
//String serverPath = String(webAppUrl) + "?temperature=" + String(temperature) + "&humidity=" + String(humidity);
String serverPath = String(webAppUrl)
+ "?sts=write"
+ "&srs=" + Status_Read_Sensor
+ "&temp=" + String(temperature)
+ "&humd=" + String(humidity)
+ "&swtc1=" + Switch_1_State
+ "&swtc2=" + Switch_2_State;
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);
//String payload = http.getString();
//Serial.println("Server response: " + 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");
}
}
/**********************************************************************************
***********************************************************************************/
void Read_Switches_State()
{
if (BUTTON_START == LOW) Switch_1_State = "Off";
if (BUTTON_START == HIGH) Switch_1_State = "On";
if (BUTTON_STOP == LOW) Switch_2_State = "Off";
if (BUTTON_STOP == HIGH) Switch_2_State = "On";
}
/**********************************************************************************
***********************************************************************************/