#include <WiFi.h>
#include <ESP32Time.h>
#include "ThingsBoard.h"
#include "DHTesp.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               "xUNpRbFgxbx9daCb70XW"
#define THINGSBOARD_SERVER  "thingsboard.cloud"

ESP32Time rtc(0);
#define TRIG_PIN 33 
#define ECHO_PIN 34
#define ldr 36        // sensor cahaya
#define lampu 27       // pencahayaan ketika gelap, simulasi LED Putih
#define wifi 12       // indikasi wifi tersambung, simulasi LED Hijau
#define DHT_PIN1 14   // sensor suhu air DS18B20 di Kolam, disimulasikan dengan DHT22
#define DHT_PIN2 15   // sensor suhu air DS18B20 di Tangki Pemanas, disimulasikan dengan DHT22
#define heater 13      // Elemen pemanas air Tangki, di simulasikan LED Merah
#define katup 19      // Katup & pompa air, disimulasikan LED Ungu dan Biru
#define kincir 33      // Kincir air, disimulasikan bergantian LED Kuning dan LED Biru Tua
float t1,t2;
int jam, menit;
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 setup() {
  Serial.begin(115200);
  Serial.println("Project Monitor Kolam Udang Vaname");
  pinMode(ldr, INPUT);
  pinMode(wifi, OUTPUT);
  pinMode(heater, OUTPUT);
  pinMode(lampu, OUTPUT);
  pinMode(katup, OUTPUT);
  pinMode(kincir, OUTPUT);
  analogReadResolution(10);
  digitalWrite(heater, HIGH);
  digitalWrite(lampu, HIGH);
  InitWiFi();
  rtc.setTime(0, 0, 20, 29, 10, 2022);
  Serial.println(" ");
  Serial.println("Setting Done!");
  Serial.print("Jam Sekarang: ");
  Serial.println(rtc.getTime());
  delay(100);
}
 
void loop() {
 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); 

  //pengaturan kincir air hidup dan mati secara bergantian
   jam = rtc.getHour(true);
   menit = rtc.getMinute();
  if (jam>=6 && jam<18){digitalWrite(kincir, HIGH);}
   else {digitalWrite(kincir, LOW);}  
  
  //pembacaan suhu kolam dan tangki pemanas
    dht1();
    dht2();

  //pembacaan intensitas cahaya
    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 di tangki
    if (t2 < 40){
      digitalWrite(heater, HIGH);}
    if (t2 > 50){
      digitalWrite(heater, LOW);}

  //Otomatisasi Pemanasan Kolam Udang
    if (t1 < 26){
      digitalWrite(katup, HIGH);}

    if (t1 > 34){
      digitalWrite(katup, LOW);}
  
  //Otomatisasi lampu penerangan
    if (lux < 50){
      digitalWrite(lampu, HIGH);}
    else {
      digitalWrite(lampu, LOW);}

  //tampilkan variabel di wokwi
    Serial.println("Suhu Kolam: " + String(t1,2) + "°C");
    Serial.println("Suhu Tangki: " + String(t2,2) + "°C");
    Serial.println("Intensitas Cahaya: " + String(lux,2) + " lux");
    Serial.println("Kirim data..."); 
    
  //Pengiriman data ke Thingsboard 
    tb.sendTelemetryFloat("Suhu Kolam: ", t1);
    tb.sendTelemetryFloat("Suhu Tangki: ", t2);
    tb.sendTelemetryFloat("Intensitas Cahaya: ", lux);
    delay(500);
 }