#include <WiFi.h>
#include "ThingsBoard.h"
#include "DHTesp.h"

//WiFi Setup
#define WIFI_SSID           "Wokwi-GUEST"
#define WIFI_PASSWORD       ""
WiFiClient espClient;
ThingsBoard tb(espClient);
int status = WL_IDLE_STATUS;

//ThingsBoard Setup
#define CURRENT_FIRMWARE_TITLE    "TEST"
#define CURRENT_FIRMWARE_VERSION  "1.0.0"
#define TOKEN               "D5btWeesqbUhT3HRODAV" 
#define THINGSBOARD_SERVER  "thingsboard.cloud"

#define ledMerah 13
#define ledHijau 12

//#define WIFI_AP_NAME              "Wokwi-GUEST"
//#define WIFI_PASSWORD             ""
//#define THINGSBOARD_SERVER        "thingsboard.cloud"
//#define THINGSBOARD_ACCESSTOKEN   "pu0TDLdTgmiytnNUUKe4" //Ubah Token

//DHT Setup
const int DHT_PIN = 15;
DHTesp dhtSensor;

void setup() {
  Serial.begin(115200);
  Serial.println();
  InitWiFi();
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  pinMode(ledMerah, OUTPUT);
  pinMode(ledHijau, OUTPUT);
  pinMode(33, INPUT);
}

void loop() {
  delay(1000);

  if (WiFi.status() != WL_CONNECTED) {
    reconnect();
  }

  if (!tb.connected()) {
    // Connect to the ThingsBoard
    Serial.print("Connecting to: ");
    Serial.print(THINGSBOARD_SERVER);
    Serial.print(" with token ");
    Serial.println(TOKEN);
    if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
      Serial.println("Failed to connect");
      return;
    }
  }
  Serial.println("Sending data...");
  float adc=analogRead(33);
  adc = map(adc, 0, 4092, 0, 100);
  Serial.print("Suhu dalam air: ");
  Serial.println(adc);

  tb.sendTelemetryFloat("Suhu dalam air", adc);

  //Serial.println("Sending data...");
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  tb.sendTelemetryFloat("Suhu", data.temperature);
  tb.sendTelemetryFloat("Kelembaban", data.humidity);
  Serial.print("Suhu : ");
  Serial.print(data.temperature);
  Serial.print(" Kelembaban : ");
  Serial.println(data.humidity);

 if (data.temperature > 33)
  {
    Serial.println("Suhu Terlalu Panas, Pompa Air Masuk Hidup");
    digitalWrite(ledHijau, LOW);
    //digitalWrite(ledKuning, LOW);
    digitalWrite(ledMerah, HIGH);
    tb.sendTelemetryString("Pompa Air Masuk","Hidup");
    tb.sendTelemetryString("Pompa Air Keluar","Mati");
  }
  else if (data.temperature < 28)
  {
    Serial.println("Suhu Terlalu Dingin, Pompa Air Keluar Hidup");
    digitalWrite(ledHijau, HIGH);
    //digitalWrite(ledKuning, HIGH);
    digitalWrite(ledMerah, LOW);
    tb.sendTelemetryString("Pompa Air Masuk","Mati");
    tb.sendTelemetryString("Pompa Air Keluar","Hidup");
  }
  else 
  {
    Serial.println("Suhu Normal, Semua Pompa Air Mati");
    digitalWrite(ledHijau, LOW);
  //  digitalWrite(ledKuning, LOW);
    digitalWrite(ledMerah, LOW);
    tb.sendTelemetryString("Pompa Air Masuk","Mati");
    tb.sendTelemetryString("Pompa Air Keluar","Mati");
  }


  tb.loop();
}

void InitWiFi()
{
  Serial.println("Connecting to AP ...");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to AP");
}

void reconnect() {
  status = WiFi.status();
  if ( status != WL_CONNECTED) {
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("Connected to AP");
  }
}