#define BLYNK_TEMPLATE_ID "TMPL6TqZslt_a"
#define BLYNK_TEMPLATE_NAME "advance cooling system"
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP32Servo.h>
#include <BlynkSimpleEsp32.h>
#define BLYNK_AUTH_TOKEN "e-U2bUQ8BK6J0L6szeMUW6UJIGKKN2qo"
#define BLYNK_PRINT Serial
// Pin definitions
#define DHTPIN 4
#define DHTTYPE DHT22
#define PIRPIN 14
#define LED1PIN 15
#define LED2PIN 16
#define RELAYPIN 17
#define SERVOSIGNALPIN 18
#define BUZZERPIN 19
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Initialize OLED display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Initialize Servo
Servo myServo;
bool systemState = false; // false: system off, true: system on
bool lastPirState = LOW; // To store the last state of the PIR sensor
char auth[] = BLYNK_AUTH_TOKEN; // Blynk authentication token
char ssid[] = "Wokwi-GUEST"; // Your WiFi SSID
char pass[] = ""; // Your WiFi Password
BLYNK_CONNECTED() {
Blynk.syncAll();
}
BLYNK_WRITE(V0) {
int relayState = param.asInt(); // Get the value from the Blynk app
if (relayState == 1) {
digitalWrite(RELAYPIN, HIGH); // Turn on the relay
systemState = true;
} else {
digitalWrite(RELAYPIN, LOW); // Turn off the relay
systemState = false;
}
}
void setup() {
Serial.begin(115200);
// Initialize DHT sensor
dht.begin();
// Initialize LEDs
pinMode(LED1PIN, OUTPUT);
pinMode(LED2PIN, OUTPUT);
pinMode(BUZZERPIN, OUTPUT);
// Initialize PIR sensor
pinMode(PIRPIN, INPUT);
// Initialize Relay
pinMode(RELAYPIN, OUTPUT);
digitalWrite(RELAYPIN, LOW); // Turn off relay initially
// Initialize Servo (signal pin only, no power)
myServo.attach(SERVOSIGNALPIN);
myServo.write(0); // Initial position
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.display();
delay(2000);
display.clearDisplay();
display.display();
// Connect to WiFi and Blynk
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run(); // Run the Blynk library
// Read PIR sensor
int pirState = digitalRead(PIRPIN);
// Read temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Automatic system control based on temperature
if (temperature >= 27) {
digitalWrite(RELAYPIN, HIGH);
} else if (temperature <= 22) {
digitalWrite(RELAYPIN, LOW);
}
// Check if motion is detected and toggle system state
if (pirState == HIGH && lastPirState == LOW) {
systemState = !systemState;
delay(500); // Debounce delay
}
lastPirState = pirState;
if (systemState) {
digitalWrite(LED2PIN, HIGH); // System ON indicator
digitalWrite(RELAYPIN, HIGH); // Turn on relay to power servo
// Wait for the relay to switch
delay(100);
// Move servo to active position
myServo.write(90);
// Check temperature and control LED1
if (temperature > 30) {
digitalWrite(LED1PIN, HIGH);
Serial.print("Temp: ");
Serial.println(temperature);
Serial.print("Motion state: ");
Serial.println(pirState);
Serial.print("Humidity: ");
Serial.println(humidity);
Serial.println("led temp: ON ");
Blynk.virtualWrite(V4, 1);
soundBuzzer(); // Turn on buzzer if temperature > 30
} else {
digitalWrite(LED1PIN, LOW);
Serial.print("Temp: ");
Serial.println(temperature);
Serial.print("Motion state: ");
Serial.println(pirState);
Serial.print("Humidity: ");
Serial.println(humidity);
Serial.println("led temp: OFF ");
Blynk.virtualWrite(V4, 0);
noTone(BUZZERPIN); // Turn off buzzer otherwise
}
// Display information on OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Temp: ");
display.print(temperature);
display.println(" C");
display.print("Humidity: ");
display.print(humidity);
display.println(" %");
display.print("System: ON");
display.setCursor(0, 30);
display.print("Motion: ");
display.println(pirState == HIGH ? "Detected" : "None");
display.display();
} else {
digitalWrite(LED2PIN, LOW); // System OFF indicator
digitalWrite(RELAYPIN, LOW); // Turn off relay to cut power to servo
// Move servo to inactive position
myServo.write(0);
digitalWrite(LED1PIN, LOW); // Turn off LED1
// Display system off message
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Temp: ");
display.print(temperature);
display.println(" C");
display.print("Humidity: ");
display.print(humidity);
display.println(" %");
display.print("System: OFF");
display.setCursor(0, 30);
display.print("Motion: ");
display.println(pirState == HIGH ? "Detected" : "None");
display.display();
}
// Control the LED for motion detection
if (pirState == HIGH) {
digitalWrite(LED2PIN, HIGH); // Turn on LED1 if motion is detected
Blynk.virtualWrite(V3, 1);
} else {
digitalWrite(LED2PIN, LOW);
Blynk.virtualWrite(V3, 0); // Turn off LED1 if no motion is detected
}
// Add a short delay to avoid bouncing
delay(500);
Blynk.virtualWrite(V1, temperature); // Send temperature to Blynk
Blynk.virtualWrite(V2, humidity); // Send humidity to Blynk
}
void soundBuzzer() {
for (int i = 0; i < 3; i++) { // Change the loop to run 3 times instead of 2
tone(BUZZERPIN, 1000); // Sound the buzzer
delay(200); // Sound the buzzer for 200 ms
noTone(BUZZERPIN); // Stop the buzzer sound // Delay for 100 ms before repeating
}
}