#include <Arduino.h>
#include <DHT.h>
#include "esp_sleep.h"
// Pin Configuration
#define DHTPIN 14 // DHT22 sensor data pin
#define DHTTYPE DHT22 // DHT type
#define BUZZER_PIN 16 // Buzzer pin
#define SLIDE_RIGHT_SWITCH 4 // Right switch for waking up
#define SLIDE_LEFT_SWITCH 2 // Left switch for sleep mode
// RGB Pins (Pins with PWM support for analogWrite)
#define PIN_RED 17
#define PIN_GREEN 5
#define PIN_BLUE 18
// Debouncing Variables
volatile unsigned long lastRightSwitchTime = millis(); // Last right switch toggle timestamp
volatile unsigned long currentRightTime;
volatile unsigned long lastLeftSwitchTime = millis(); // Last left switch toggle timestamp
volatile unsigned long currentLeftTime;
// DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// System state counters
volatile int Offcount = 0; // Count for the OFF state
volatile int Oncount = 0; // Count for the ON state
// Function to set RGB LED color using analogWrite (PWM)
void setColor(int R, int G, int B) {
analogWrite(PIN_RED, R); // Red (0 to 255 brightness)
analogWrite(PIN_GREEN, G); // Green (0 to 255 brightness)
analogWrite(PIN_BLUE, B); // Blue (0 to 255 brightness)
}
// Interrupt Service Routine for the right switch (Wake-up)
void IRAM_ATTR switchTurnedRight() {
currentRightTime = millis();
if (currentRightTime - lastRightSwitchTime > 200) { // Debounce delay
Oncount++; // Increment On count when the system is turned on
Offcount = 0; // Reset Off count
setColor(255, 255, 255); // Turn the RGB ON (white, can be customized)
lastRightSwitchTime = currentRightTime;
}
}
// Interrupt Service Routine for the left switch (Sleep)
void IRAM_ATTR switchTurnedLeft() {
currentLeftTime = millis();
if (currentLeftTime - lastLeftSwitchTime > 200) { // Debounce delay
Offcount++; // Increment Off count when the system is turned off
Oncount = 0; // Reset On count
setColor(0, 0, 0); // Turn the RGB OFF (black)
lastLeftSwitchTime = currentLeftTime;
enterDeepSleep(); // Put the system to deep sleep
}
}
// Function to put ESP32 into deep sleep
void enterDeepSleep() {
setColor(0, 0, 0); // Ensure RGB is off before sleep
delay(100); // Small delay before sleep
// Configure right switch as wake-up source
esp_sleep_enable_ext0_wakeup((gpio_num_t)SLIDE_RIGHT_SWITCH, 1); // Wake up when right switch is pressed
// Enter deep sleep
esp_deep_sleep_start();
}
void setup() {
Serial.begin(115200);
dht.begin();
// Pin setup
pinMode(PIN_RED, OUTPUT);
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_BLUE, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(SLIDE_RIGHT_SWITCH, INPUT); // Internal pull-up for right switch
pinMode(SLIDE_LEFT_SWITCH, INPUT); // Internal pull-up for left switch
// Attach interrupts for slide switches
attachInterrupt(digitalPinToInterrupt(SLIDE_RIGHT_SWITCH), switchTurnedRight, FALLING); // Right slide switch
attachInterrupt(digitalPinToInterrupt(SLIDE_LEFT_SWITCH), switchTurnedLeft, FALLING); // Left slide switch
// If waking up from deep sleep, the LED should remain off until right switch is toggled
if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT0) {
Serial.println("Woke up from deep sleep.");
Oncount++; // The system is now active
setColor(255, 255, 255); // Turn RGB ON upon wakeup
} else {
Serial.println("System started in sleep mode.");
Offcount = 1; // System starts in sleep mode
setColor(0, 0, 0); // Ensure RGB is OFF initially
}
// Initially, the system is in sleep mode, so the RGB should be off
if (Offcount > Oncount) {
setColor(0, 0, 0); // Turn RGB OFF
}
}
void loop() {
// If the system is not active (in sleep mode), do nothing
if (Offcount > Oncount) {
return; // The system is sleeping, nothing happens here
}
// Measure humidity and temperature if the system is active
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Print temperature and humidity to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" C, ");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// If the sensor fails to read values, skip the loop
if (isnan(temperature) || isnan(humidity)) {
return;
}
// RGB LED color logic based on humidity
if (humidity < 30) {
setColor(0, 0, 255); // Blue: Low humidity
} else if (humidity >= 30 && humidity <= 50) {
setColor(0, 255, 0); // Green: Normal humidity
} else {
setColor(255, 0, 0); // Red: High humidity
tone(BUZZER_PIN, 1000); // 1kHz beep
}
// If humidity goes back down, turn off the buzzer
if (humidity <= 50) {
noTone(BUZZER_PIN); // Turn off buzzer if humidity is back to normal
}
delay(2000); // Sensor delay
}