#include <WiFi.h>
#include <HTTPClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "DHT.h"
// DHT22 sensor configuration
#define DHTPIN 4 // Pin connected to DHT22
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Ultrasonic sensor configuration
#define TRIG_PIN 2 // Trigger pin
#define ECHO_PIN 5 // Echo pin
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Google Apps Script Web App URL
const char* serverName = "https://script.google.com/macros/s/AKfycbzEFD1myl1Ke0p9pW_zJz44TfuzuHoiUSnK2rSYn3viwGgROjwCaQszbx400ogeZOrL/exec";
// Initialize WiFi and NTP client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 7200, 10); // UTC+2 timezone, updates every 10 seconds
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize DHT22 sensor
dht.begin();
// Initialize NTP client
timeClient.begin();
timeClient.update();
// Configure ultrasonic sensor pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
float getDistance() {
// Send a 10-microsecond pulse to the trigger pin
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the duration of the echo pulse
long duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance in centimeters (speed of sound = 34300 cm/s)
float distance = (duration * 0.034) / 2; // Divide by 2 for round-trip time
return distance;
}
String getRecommendation(float temperature, float humidity) {
String recommendation = "";
// Temperature thresholds (WHO suggests indoor temperature between 18°C and 24°C for comfort)
if (temperature < 18) {
recommendation += "Temperature too low! Consider heating the room. ";
} else if (temperature > 24) {
recommendation += "Temperature too high! Consider cooling the room. ";
}
// Humidity thresholds (Recommended range: 30% to 60%)
if (humidity < 30) {
recommendation += "Humidity too low! Consider using a humidifier. ";
} else if (humidity > 60) {
recommendation += "Humidity too high! Consider using a dehumidifier. ";
}
return recommendation;
}
void loop() {
if (WiFi.status() == WL_CONNECTED) { // Check if connected to WiFi
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/json");
// Fetch time from NTP client
timeClient.update();
String formattedTime = timeClient.getFormattedTime();
// Read data from DHT22 sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if the DHT sensor data is valid
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Get distance from ultrasonic sensor
float distance = getDistance();
// Get recommendations based on temperature and humidity
String recommendation = getRecommendation(temperature, humidity);
// Print sensor data to Serial Monitor for debugging
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.print(" %, Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (recommendation != "") {
Serial.println("Recommendation: " + recommendation);
}
// Prepare JSON payload
String jsonData = "{\"method\":\"append\", \"temperature\":" + String(temperature) +
", \"humidity\":" + String(humidity) +
", \"timestamp\":\"" + formattedTime + "\"" +
", \"distance\":" + String(distance) +
", \"recommendation\":\"" + recommendation + "\"}";
// Send HTTP POST request
int httpResponseCode = http.POST(jsonData);
// Handle HTTP response
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("HTTP Response Code: " + String(httpResponseCode));
Serial.println("Response: " + response);
} else {
Serial.println("Error sending POST: " + String(httpResponseCode));
}
http.end(); // Close HTTP connection
} else {
Serial.println("WiFi Disconnected");
}
delay(10000); // Delay 10 seconds before sending the next request
}