#include <WiFi.h>
#include "DHTesp.h"
#include <ESP32Servo.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

Servo myservo;

#define WIFI_SSID           "Wokwi-GUEST"
#define WIFI_PASSWORD       ""
const int DHT_PIN = 15;

char ssid[] = WIFI_SSID;
char pass[] = WIFI_PASSWORD;

const char *THINGSBOARD_SERVER = "thingsboard.cloud";
const char *ACCESS_TOKEN = "tes-ruangan1";
const char *DEVICE_ID = "19e6ce10-d786-11ee-b58c-478d284d7818";

WiFiClient espClient;
PubSubClient client(espClient);

LiquidCrystal_I2C lcd(0x27, 16, 2); // Address LCD 16x2 (I2C)

bool isWatering = false; 

unsigned long previousMillis = 0; 
const long interval = 3000; 

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to WiFi");

  client.setServer(THINGSBOARD_SERVER, 1883);
  client.setCallback(callback);

  pinMode(2, OUTPUT);
  myservo.attach(4);
  myservo.write(0);

  // Inisialisasi LCD
  lcd.init();
  lcd.backlight();

  connectToThingsBoard();
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }

  client.loop();

  delay(1000);

  // DHT
  DHTesp dhtSensor;
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  TempAndHumidity data = dhtSensor.getTempAndHumidity();

  // Tampilkan data di LCD
  displayOnLCD(data.temperature, data.humidity);

  // Send data to ThingsBoard
  publishToThingsBoard(data.temperature, data.humidity);

  // Tampilkan status di terminal
  Serial.print("Suhu : ");
  Serial.print(data.temperature);
  Serial.print(" Kelembaban : ");
  Serial.println(data.humidity);

  if (data.temperature >= 24 && data.temperature <= 29 && !isWatering) {
    myservo.write(180); //kondisi menyiram
    isWatering = true;
    previousMillis = millis(); // Simpan waktu terakhir menyiram
  }

  // Cek apakah sudah waktunya untuk membersihkan layar LCD
  if (isWatering && millis() - previousMillis >= interval) {
    lcd.clear(); // Membersihkan layar LCD
    isWatering = false; // Setel kembali status menyiram menjadi false
  }
}

void connectToThingsBoard() {
  while (!client.connected()) {
    Serial.print("Connecting to ThingsBoard...");
    if (client.connect(DEVICE_ID, ACCESS_TOKEN, nullptr)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" retrying in 5 seconds");
      delay(5000);
    }
  }
}

void reconnect() {
  Serial.println("Attempting MQTT reconnection...");
  if (client.connect(DEVICE_ID, ACCESS_TOKEN, nullptr)) {
    Serial.println("Connected to ThingsBoard");
  } else {
    Serial.print("failed, rc=");
    Serial.print(client.state());
    Serial.println(" retrying in 5 seconds");
    delay(5000);
  }
}

void callback(char *topic, byte *payload, unsigned int length) {
  // handle incoming messages
}

void publishToThingsBoard(float temperature, float humidity) {
  String dataPayload = "{ \"temperature\":" + String(temperature) + ", \"humidity\":" + String(humidity) + " }";

  client.publish("v1/devices/me/telemetry", dataPayload.c_str());
}

void displayOnLCD(float temperature, float humidity) {
  lcd.clear();

  // Tampilkan pesan "Sedang menyiram" jika suhu di atas 24 derajat dan kelembapan tanah di bawah 60%
  if (temperature > 24 && humidity < 60) {
    lcd.setCursor(0, 0);
    lcd.print("Sedang menyiram");
  }
}