#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN,DHTTYPE);
Adafruit_SSD1306 display = Adafruit_SSD1306(128,64,&Wire);
#define LDR 2
int Relay1 = 19;
int Relay2 = 18;
int Relay3 = 23;
float ADC_value = 0.0048828125;
int Read = 0;

void setup() {
Serial.begin(115200);
dht.begin();
display.begin(SSD1306_SWITCHCAPVCC,0x3c);
display.clearDisplay();
display.setTextColor(WHITE,BLACK);
pinMode(LDR, INPUT);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
}

void loop() {
//เซ็นเซอร์จับแสง LDR
Read = analogRead(LDR);
Serial.print("Reading : "+String(Read));
delay(1000);
int lux = int((250.000000/(ADC_value*Read))-50.000000);
Serial.println("LUX : "+String(lux));
delay(1000);

//เซ็นเซอร์วัดอุณหภูมิ DHT22
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
display.clearDisplay();
display.setTextColor(WHITE,BLACK);
display.setCursor(10,20);
display.print("temperature ="+String(temperature));
display.print("C");
display.setCursor(10,35);
display.print("humidity ="+String(humidity));
display.print("%");
display.display();
delay(1000);

//การควบคุมการทำงานของRelay
if(temperature > 45 && humidity < 40){
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, LOW);
}
else if(humidity > 60){
digitalWrite(Relay1, LOW);
digitalWrite(Relay2, HIGH);
}
else{
digitalWrite(Relay1, LOW);
digitalWrite(Relay2, LOW);
}

if(lux< 20){
  digitalWrite(Relay3, HIGH);

}
else{
   digitalWrite(Relay3, LOW);
}
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module