#include <DHT.h>

#define DHTPIN 4     // Pin DHT22
#define DHTTYPE DHT22

#define SOIL_SENSOR A1
#define LDR_SENSOR A0
#define PUMP_PIN 5
#define LIGHT_PIN 6
#define FAN_PIN 7

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  dht.begin();

  pinMode(PUMP_PIN, OUTPUT);
  pinMode(LIGHT_PIN, OUTPUT);
  pinMode(FAN_PIN, OUTPUT);

  digitalWrite(PUMP_PIN, LOW);
  digitalWrite(LIGHT_PIN, LOW);
  digitalWrite(FAN_PIN, LOW);
}

void loop() {
  // Baca data sensor
  float temp = dht.readTemperature();
  float humidity = dht.readHumidity();
  int soilMoisture = analogRead(SOIL_SENSOR);
  int lightLevel = analogRead(LDR_SENSOR);

  Serial.print("Temperature: "); Serial.println(temp);
  Serial.print("Humidity: "); Serial.println(humidity);
  Serial.print("Soil Moisture: "); Serial.println(soilMoisture);
  Serial.print("Light Level: "); Serial.println(lightLevel);

  // Kontrol Aktuator
  if (soilMoisture < 500) {
    digitalWrite(PUMP_PIN, HIGH);  // Hidupkan pompa
  } else {
    digitalWrite(PUMP_PIN, LOW);   // Matikan pompa
  }

  if (lightLevel < 200) {
    digitalWrite(LIGHT_PIN, HIGH); // Hidupkan grow light
  } else {
    digitalWrite(LIGHT_PIN, LOW);  // Matikan grow light
  }

  if (temp > 30) {
    digitalWrite(FAN_PIN, HIGH);   // Hidupkan kipas
  } else {
    digitalWrite(FAN_PIN, LOW);    // Matikan kipas
  }

  delay(2000); // Tunggu 2 detik sebelum membaca ulang
}