#include <Wire.h>
#include <DHT.h>
#include <ESP32Servo.h>
DHT dht(15, DHT22);
Servo Servo1;
Servo Servo2;
const int btn3 = 5;
const int btn2 = 4;
const int btn1 = 16;
const int fan = 23;
int threshold = 30;
int state = 0;
int analogTemp = 0 ;
int analogHumi = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
dht.begin();
Servo2.attach(2);
Servo1.attach(17);
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(btn3, INPUT);
pinMode(fan, OUTPUT);
delay(2000);
}
void loop() {
// Gets the Data from DHT22
float temp = dht.readTemperature();
float humi = dht.readHumidity();
// See if Manual or auto by looking if number = odd/even
int press3 = digitalRead(btn3);
if (press3 == HIGH){
state = state + 1;
}
delay(50);
// Checks if Manual or Auto
//Auto
if (state % 2 == 0) {
// Getting Analog Temperature
analogTemp = 180 - (((float)temp/50) * 180);
Servo1.write(analogTemp);
// Getting Analog of humidity
analogHumi = 180 - (((float) humi / 100) * 180);
Servo2.write(analogHumi);
// Reduces Temperature Threshold
int press1 = digitalRead(btn1);
if (press1 == HIGH){
threshold = threshold - 1;
}
delay(30);
// Increases Temperature Threshold
int press2 = digitalRead(btn2);
if (press2 == HIGH){
threshold = threshold + 1;
}
delay(30);
// Process the data and check if Cold or Hot Weather
if (temp <= threshold){
digitalWrite(fan, HIGH);
} else if (temp >= threshold){
digitalWrite(fan, LOW);
}
Serial.print("\t");
Serial.println("Auto ");
} else {
// Close Fan
int press1 = digitalRead(btn1);
if (press1 == HIGH){
digitalWrite(fan, LOW);
}
// Open Fan
int press2 = digitalRead(btn2);
if (press2 == HIGH){
digitalWrite(fan, HIGH);
}
Serial.print("\t");
Serial.println("Manual ");
}
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("\t");
Serial.print("Humidity: ");
Serial.print(humi);
Serial.print("\t");
Serial.print("Threshold: ");
Serial.println(threshold);
Serial.print("Temp Log: ");
Serial.print(analogTemp);
Serial.print("\t");
Serial.print("Humi Log: ");
Serial.println(analogHumi);
}