#include "DHTesp.h"
int red = 2;
int green = 4;
int blue = 5;
float temp;
float hum;
int relay = 12;
const int DHT_PIN = 15;
DHTesp dhtSensor;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(relay, OUTPUT);
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
temp = data.temperature;
hum= data.humidity;
Serial.print("อุณหภูมิ : ");
Serial.print(temp);
Serial.println(" องศา");
Serial.print("ความชื้น : ");
Serial.print(hum);
Serial.println(" %");
if(hum<50){
digitalWrite(relay, HIGH);
}
else{
digitalWrite(relay, LOW);
}
if(temp<20){
digitalWrite(blue, HIGH);
digitalWrite(green, LOW);
digitalWrite(red, LOW);
}
else if(temp>=20&& temp<=40){
digitalWrite(blue, LOW);
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
}
else if(temp>40){
digitalWrite(blue, LOW);
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
}
else{
Serial.println("Error");
}
delay(1000);
/*
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
*/
}