#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define DHTPIN 15  // Pin tempat Anda menghubungkan output sensor DHT22
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal_I2C lcd(0x27, 16, 2);  // Alamat I2C LCD dan jumlah kolom dan baris

#define RELAY_PIN_1 5  // Pin untuk Relay 1
#define RELAY_PIN_2 25 // Pin untuk Relay 2

void setup() {
  Serial.begin(9600);
  dht.begin();
  
  lcd.init();  // Inisialisasi LCD
  lcd.backlight();

  displayWelcomeScreen();
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(RELAY_PIN_2, OUTPUT);
}

void loop() {
  
  delay(3000);  // Delay sebelum membaca sensor
  float temperature = dht.readTemperature();  // Baca suhu dari sensor DHT22

  if (!isnan(temperature)) {
    displayTemperature(temperature);
    controlRelays(temperature);
  }
 delay(3000);   // Delay sebelum membaca sensor kembali
}

void displayWelcomeScreen() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("SIMULASI ESP32");
  lcd.setCursor(0, 1);
  lcd.print("***************");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("SENSOR SUHU");
  lcd.setCursor(0, 1);
  lcd.print("***************");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("LCD I2C dan RELAY");
  lcd.setCursor(0, 1);
  lcd.print("***************");
}

void displayTemperature(float temperature) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Suhu: ");
  lcd.print(temperature);
  lcd.print(" C");
  delay(2000);
}

void controlRelays(float temperature) {
  if (temperature < 32.0) {
      lcd.clear();
    digitalWrite(RELAY_PIN_1, HIGH);  // Menyalakan Relay 1 (lampu pemanas) 
    lcd.setCursor(0, 1);
    lcd.print("PEMANAS MENYALA");
    
  } else {
    digitalWrite(RELAY_PIN_1, LOW);  // Mematikan Relay 1 (lampu pemanas)
    delay(3000);
  }

  if (temperature > 35.0) {
     lcd.clear(); 
    digitalWrite(RELAY_PIN_2, HIGH);  // Menyalakan Relay 2 (kipas pendingin)
    lcd.setCursor(0, 1);
    lcd.print("KIPAS MENYALA");
     
  } else {
    digitalWrite(RELAY_PIN_2, LOW);  // Mematikan Relay 2 (kipas pendingin)
     delay(3000);
      
  }
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module