#include <DHT.h>
#define DHTPIN 4 // DHT22 sensor pin
#define DHTTYPE DHT22 // DHT22 sensor type
#define RELAYPIN 16 // Relay pin
#define BUTTONPIN 5 // Pushbutton pin
#define BUZZERPIN 17 // Buzzer pin
DHT dht(DHTPIN, DHTTYPE);
void setup() {
pinMode(RELAYPIN, OUTPUT);
digitalWrite(RELAYPIN, LOW);
pinMode(BUTTONPIN, INPUT_PULLUP); // Assuming the button is connected to GND
pinMode(BUZZERPIN, OUTPUT);
digitalWrite(BUZZERPIN, LOW);
dht.begin();
Serial.begin(9600); // Initialize serial communication
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Error reading temperature or humidity!");
return;
}
bool buttonState = digitalRead(BUTTONPIN);
if (temperature >= 30.0 || humidity <= 85.0 || buttonState == LOW) { // Relay turns on if any condition is met
digitalWrite(RELAYPIN, HIGH);
digitalWrite(BUZZERPIN, HIGH); // Turn on the buzzer when the relay is on
} else {
digitalWrite(RELAYPIN, LOW);
digitalWrite(BUZZERPIN, LOW); // Turn off the buzzer when the relay is off
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.print(" %, Button State: ");
Serial.println(buttonState ? "Not Pressed" : "Pressed");
delay(1000); // Delay 1 second
}