#include <DHT.h>
// Define pins
#define DHTPIN 27
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define RED_PIN 26
#define GREEN_PIN 13
#define BLUE_PIN 14
#define BUZZER_PIN 12
#define SWITCH_PIN_LEFT 19
#define SWITCH_PIN_RIGHT 18
volatile int leftSwitched = 1;
volatile unsigned long lastRightSwitchTime = millis();
volatile unsigned long currentRightTime;
volatile unsigned long lastLeftSwitchTime = millis();
volatile unsigned long currentLeftTime;
volatile int switched = 0;
float humidity = 0.0;
float temperature = 0.0;
void IRAM_ATTR rightSwitchInterrupt() {
switched = 1;
}
void IRAM_ATTR leftSwitchInterrupt() {
switched = 2;
}
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(SWITCH_PIN_LEFT, INPUT);
pinMode(SWITCH_PIN_RIGHT, INPUT);
attachInterrupt(digitalPinToInterrupt(SWITCH_PIN_LEFT), leftSwitchInterrupt, FALLING);
attachInterrupt(digitalPinToInterrupt(SWITCH_PIN_RIGHT), rightSwitchInterrupt, FALLING);
Serial.println("Hello");
}
void loop() {
if (switched == 2) {
Serial.println("left switched");
switched = 0;
setRGBColour(255,0,0);
delay(2000);
}
else if (switched==1) {
Serial.println("right switched");
switched = 0;
setRGBColour(0,255,255);
delay(2000);
}
else if (switched == 0){
humidity = dht.readHumidity();
temperature = dht.readTemperature();
if (isnan(temperature) && isnan(humidity)) {
Serial.print("Failed to read from dht sensor.");
return;
}
Serial.print("Temperature: "); Serial.print(temperature); Serial.print(" °C, ");
Serial.print("Humidity: "); Serial.print(humidity); Serial.println(" %");
updateRGBColour(humidity);
checkBuzzerAlert(humidity);
delay(2000);
}
}
// Function to set RGB LED color based on humidity
void updateRGBColour(float humidity) {
if (humidity >= 30 && humidity < 40) {
setRGBColour(255, 255, 255); // White
} else if (humidity >= 40 && humidity < 50) {
setRGBColour(0, 0, 255); // Blue
} else if (humidity >= 50 && humidity < 60) {
setRGBColour(0, 255, 0); // Green
} else if (humidity >= 60 && humidity < 70) {
setRGBColour(255, 255, 0); // Yellow
} else if (humidity >= 70 && humidity < 80) {
setRGBColour(255, 165, 0); // Orange
} else if (humidity >= 80 && humidity < 90) {
setRGBColour(255, 0, 0); // Red
} else if (humidity >= 90) {
setRGBColour(128, 0, 128); // Purple
}
}
void checkBuzzerAlert(float humidity) {
if (humidity > 80) {
tone(BUZZER_PIN, 500,500);
delay(500);
}
}
void setRGBColour(int red, int green, int blue) {
analogWrite(RED_PIN, 255 - red);
analogWrite(GREEN_PIN, 255 - green);
analogWrite(BLUE_PIN, 255 - blue);
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
rgb1:R
rgb1:COM
rgb1:G
rgb1:B
sw1:1
sw1:2
sw1:3
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
bz1:1
bz1:2
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2
r4:1
r4:2
r5:1
r5:2