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

#define WIFI_SSID           "Wokwi-GUEST"
#define WIFI_PASSWORD       ""

#define TOKEN               "ZRzqBszUAOAX2H8vf06e"
#define THINGSBOARD_SERVER  "thingsboard.cloud"

// Baud rate for debug serial
#define SERIAL_DEBUG_BAUD   115200

#define lmerah 13
#define lkuning 12
#define lhijau 14
#define buzz 27
#define pindht 15
#define servopin 5
#define relay 25

DHTesp sensorsuhu;
Servo keran;

// Initialize ThingsBoard client
WiFiClient espClient;
// Initialize ThingsBoard instance
ThingsBoard tb(espClient);
// the Wifi radio's status
int status = WL_IDLE_STATUS;

void HubungkanWiFi()
{
  Serial.println("Menghubungkan ke internet ---");
  // attempt to connect to WiFi network

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {

    delay(500);
    Serial.print("-");
  }
  Serial.println("Berhasil Terhubung");
}

void reconnect() {
  // Loop until we're reconnected
  status = WiFi.status();
  if ( status != WL_CONNECTED) {
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print("-");
    }
    Serial.println("Berhasil Terhubung");
  }
}


void setup() {
  // put your setup code here, to run once:
  Serial.begin(SERIAL_DEBUG_BAUD);
  Serial.println();
  HubungkanWiFi();
  pinMode(lmerah, OUTPUT);
  pinMode(lkuning, OUTPUT);
  pinMode(lhijau, OUTPUT);
  pinMode(buzz, OUTPUT);
  pinMode(relay, OUTPUT);
  pinMode(servopin, OUTPUT);
  keran.attach(servopin);
  sensorsuhu.setup(pindht, DHTesp::DHT22);
  keran.write(0);
}

void suhutinggi()
{
  keran.write(0);  
  digitalWrite(lmerah, HIGH);
  delay(2000);
  tone(buzz, 500);
  digitalWrite(relay, HIGH);  
  tb.sendTelemetryData("Keran", "Tertutup");
  tb.sendTelemetryData("Pompa", "Hidup");
  delay(1000); 
}
void suhurendah()
{
  keran.write(180);  
  digitalWrite(lkuning, HIGH);
  delay(2000);
  tone(buzz, 50);
  digitalWrite(relay, LOW);  
  tb.sendTelemetryData("Keran", "Terbuka");
  tb.sendTelemetryData("Pompa", "Mati");
  delay(1000); 
}

void suhunormal()
{
  keran.write(0);  
  digitalWrite(lhijau, HIGH);
  delay(2000);
  noTone(buzz);
  digitalWrite(relay, LOW);  
  tb.sendTelemetryData("Keran", "Tertutup");
  tb.sendTelemetryData("Pompa", "Mati");
  delay(1000); 
}

void loop() {
  delay(1000);

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

  if (!tb.connected()) {
    // Connect to the ThingsBoard
    Serial.print("Menghubungkan ke: ");
    Serial.print(THINGSBOARD_SERVER);
    Serial.print(" dengan token ");
    Serial.println(TOKEN);
    if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
      Serial.println("Koneksi ke thingsobard gagal");
      return;
    }
  }

  digitalWrite(lmerah, LOW);
  digitalWrite(lkuning, LOW);
  digitalWrite(lhijau, LOW);

  TempAndHumidity datadht = sensorsuhu.getTempAndHumidity();
  float suhu = datadht.temperature;
  float lembab = datadht.humidity;
  tb.sendTelemetryInt("Suhu", suhu);
  tb.sendTelemetryInt("Lembab", lembab);
  if(isnan(suhu) || isnan(lembab))
  {
    Serial.println("Gagal Membaca Sensor");
    delay(1000);
    return;
  }

  if  (suhu > 34)
  {
    suhutinggi();
  }
  else if (suhu < 26)
  {
    suhurendah();
  }
  else 
  {
    suhunormal();
  }
  
  delay(1000);

}