#include <stdio.h>
#include <DHT.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define DHTPIN 4
#define DHTTYPE DHT22
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* webAppUrl =
"https://script.google.com/macros/s/AKfycby-xdm1o_aljdZ74jBKiV32a-ThaCOworuD49bNhxpEJaNE6jAgUVruy5BzAphl5W56/exec";
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
WiFi.begin(ssid, password);
while (WiFi.status() !=WL_CONNECTED) {
delay(500);
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 fro DHT sensor");
delay(1000);
return;
}
Serial.print("Temperature : ");
Serial.print(temperature);
Serial.print(" °C, Humidity : ");
Serial.print(humidity);
Serial.print(" %");
sendDataToScript(temperature, humidity);
delay(6000);
}
void sendDataToScript(float temperature, float humidity) {
HTTPClient http;
String severPath = String(webAppUrl) + "?temperature=" +
String(temperature) + "&humidity=" + String(humidity);
Serial.print("Connecting to server: ");
Serial.println(severPath);
if (http.begin(severPath)){
int httpCode = http.GET();
if (httpCode > 0){
Serial.print("Server response code: ");
Serial.println(httpCode);
} else{
Serial.print("HTTP GET request failed with error code: ");
Serial.println(httpCode);
}
http.end();
} else{
Serial.println("Unable to connect to the server");
}
}