#include "dht.h"
#define MAX_TEMP 45 // Define the maximum temperature threshold
#define MAX_HUMIDITY 70 // Define the maximum humidity threshold
#define dht_apin A1 // Analog Pin sensor is connected to
float temp, humidity;
dht DHT;
#define fan 9
#define alarmPin 10 // Define the pin for the alarm sensor
void setup() {
Serial.begin(9600); //this is for the serial monitor
delay(500); //Delay to let system boot
Serial.println("DHT22 Humidity & temperature Sensor \n\n");
pinMode(fan, OUTPUT); // Giving Fan Current.Making Fan Pin 9 For Output
pinMode(alarmPin, INPUT_PULLUP); // Set the alarm pin as input with internal pull-up resistor
}
void loop() {
DHT.read22(dht_apin);
Serial.print("Current humidity - ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C");
temp = DHT.temperature;
humidity = DHT.humidity;
// Calculate fan speed
float fan_speed = 0.00593975 * temp + 1.25319841 * humidity + 29.137272695176353;
fan_speed = (fan_speed / 100) * 255;
int speed = (int)fan_speed;
float per = (speed * 1.0 / 255) * 100;
Serial.print("fan speed ");
Serial.print(per);
// Serial.println("%");
// Control fan speed
analogWrite(fan, speed);
// Check if conditions for sounding the alarm are met
if (temp > MAX_TEMP || humidity > MAX_HUMIDITY || digitalRead(alarmPin) == LOW) {
// Activate alarm
Serial.println("ALARM! Temperature/humidity out of range or alarm sensor triggered!");
}
delay(5000); //wait for 5 seconds before accessing sensor again
}