#include <WiFi.h>
#include "ThingsBoard.h"
#define WIFI_SSID           "Wokwi-GUEST"
#define WIFI_PASSWORD       ""
#define SERIAL_DEBUG_BAUD   115200
#define CURRENT_FIRMWARE_TITLE    "TEST"
#define CURRENT_FIRMWARE_VERSION  "1.0.0"
#define TOKEN               "dzPe1JjL41xmJZnjQGpE"
#define THINGSBOARD_SERVER  "thingsboard.cloud"
#include "DHTesp.h"
#define TRIG_PIN 33 
#define ECHO_PIN 34
#define ldr 35        // sensor cahaya, otomatisasi lampu led putih
#define lampu 5       // pencahayaan ketika gelap, input dari ldr
#define wifi 12       // indikasi wifi konek
#define DHT_PIN1 14   // sensor suhu air DS18B20, disimulasikan dengan DHT22
#define DHT_PIN2 15   // sensor suhu air DS18B20, disimulasikan dengan DHT22
#define kolam 13      // pemanas air Tangki
#define pompa 19      // relay pompa agar bekerja bergantian
#define katup 26      // led biru
float t1,t2,durasi, jarak;
int air;
DHTesp dhtSensor;
// Initialize ThingsBoard client
WiFiClient espClient;
// Initialize ThingsBoard instance
ThingsBoard tb(espClient);
// the Wifi radio's status
int status = WL_IDLE_STATUS;
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;

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");
  digitalWrite(wifi, HIGH);
}

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

void dht1(){
  dhtSensor.setup(DHT_PIN1, DHTesp::DHT22);
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  t1 = data.temperature;
  delay(1000);
}

void dht2(){
  dhtSensor.setup(DHT_PIN2, DHTesp::DHT22);
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  t2 = data.temperature;
  delay(1000);
}

void air_tangki(){
// generate 10-microsecond pulse to TRIG pin
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  // measure duration of pulse from ECHO pin
  durasi = pulseIn(ECHO_PIN, HIGH);
  // calculate the distance
  jarak = 0.017 * durasi;
  jarak = 250 - jarak; 
  }

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Project Udang Vaname");
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(ldr, INPUT);
  pinMode(wifi, OUTPUT);
  pinMode(kolam, OUTPUT);
  pinMode(lampu, OUTPUT);
  pinMode(pompa, OUTPUT);
  pinMode(katup, OUTPUT);
  analogReadResolution(10);
  digitalWrite(kolam, HIGH);
  digitalWrite(lampu, HIGH);
  InitWiFi();
  delay(100);
}

void loop() {
  // put your main code here, to run repeatedly:
  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;
    }
  }
    delay(1000);
    //baca jarak
    air_tangki();

  //atur ketinggian air tangki
   if (jarak <= 100){
     digitalWrite(katup, HIGH);
   }
   else if (jarak >= 200)
   {digitalWrite(katup, LOW);}
  //baca sensor dht
  dht1();
  dht2();
  //baca sensor cahaya ldr
  float data = analogRead(ldr);
  float voltage = data / 1024 * 5;
  float resistance = 2000 * voltage / (1 - voltage / 5);
  float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
  //Otomatisasi pemanas
  if (t1 > 34){
    digitalWrite(kolam, HIGH);
  }
  else if (t1<26){digitalWrite(kolam, LOW);}
  if (t2 > 34){
    digitalWrite(kolam, HIGH);
  }
//Otomatisasi lampu
if (lux < 1000){digitalWrite(lampu, LOW);}
else {digitalWrite(lampu, HIGH);}
  //tampilkan variabel
  Serial.println("Suhu Kolam: " + String(t1,2) + "°C");
  Serial.println("Suhu Tangki: " + String(t2,2) + "°C");
  Serial.println("Ketinggian Air Tangki: " + String(jarak,2) + " cm");
  Serial.println("Intensitas Cahaya: " + String(lux,2) + " lux");
  Serial.println("Kirim data...");
  tb.sendTelemetryFloat("Suhu Kolam: ", t1);
  tb.sendTelemetryFloat("Suhu Tangki: ", t2);
  tb.sendTelemetryFloat("Ketinggian Air Tangki: ", jarak);
  tb.sendTelemetryFloat("Intensitas Cahaya: ", lux);
  delay(500);
}