#define BLYNK_TEMPLATE_ID "TMPL5qV9ugZYS"
#define BLYNK_TEMPLATE_NAME "Smart Home"
#define LED_PIN 22 // Define the pin for the single LED
#define DHT_PIN 4 // Define the pin where the DHT22 is connected
#define DHTTYPE DHT22 // Define the type of DHT sensor
#define BUZZER_PIN 15 // Define the pin for the buzzer
#include <DHT.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// Replace with your network credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Replace with your Blynk Auth Token
char auth[] = "HBMwrfscUyqmL1cucBsgPqR6eXxxgq_1";
DHT dht(DHT_PIN, DHTTYPE);
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
dht.begin();
Blynk.begin(auth, ssid, pass);
}
void loop() {
// Read temperature and humidity
float h = dht.readHumidity();
float t = dht.readTemperature(); // Read temperature as Celsius (the default)
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
noTone(BUZZER_PIN);
// Check if temperature is higher than 40°C and activate buzzer
if (t > 40) {
tone(BUZZER_PIN, 3000); // Turn on the buzzer
} else {
noTone(BUZZER_PIN);; // Turn off the buzzer
}
// Send temperature and humidity to Blynk
Blynk.virtualWrite(V1, t); // Assuming you have a gauge widget on V1 for temperature
Blynk.virtualWrite(V2, h); // Assuming you have a gauge widget on V3 for humidity
Blynk.run();
delay(1000); // Wait for 1 second before next reading
}
// Function to control the LED based on Blynk virtual pin V0
BLYNK_WRITE(V0) {
int pinValue = param.asInt(); // Get the value sent from Blynk app
analogWrite(LED_PIN, pinValue);
}