#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif

#include "ThingsBoard.h"
#include "DHTesp.h"
#include "time.h"

#define CURRENT_FIRMWARE_TITLE    "TEST"
#define CURRENT_FIRMWARE_VERSION  "1.0.0"

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

// See https://thingsboard.io/docs/getting-started-guides/helloworld/
// to understand how to obtain an access token
#define TOKEN               "bfh7V10YEBQZMeFN8kw2" // Sendiri
// #define TOKEN               "GKDHFMmvrxhuxwBrsXHk" // Sendiri
// #define TOKEN               "0cfrPPARvvMGDvEgPC8M" // Sendiri
// #define TOKEN               "DI5u2qhuwmBycUc1yKTC" // Sendiri
#define THINGSBOARD_SERVER  "thingsboard.cloud"

// 1. ECSw9hwpmEZ4iSIMq3PZ
// 2. Pak Affan togroN6SDO5ebExopII6
// 3. Pak Rifqi ZavlqhmlIUS2fubXZT6y
// 4. Pak Eko yvWtvIClxzAoTuY18lmL


// Baud rate for debug serial
#define SERIAL_DEBUG_BAUD   115200

#define ECHO_PIN 2
#define TRIG_PIN 4
#define LED_RED 14
#define LED_YELLOW 12
#define LED_GREEN 13
#define LED_CYAN 27
#define LED_BLUE 26
const int DHT_PIN = 15;
const char* ntpServer = "pool.ntp.org";

DHTesp dhtSensor;

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

void InitWiFi()
{
  Serial.println("Connecting to AP ...");
  // attempt to connect to WiFi network

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

    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to AP");
}

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("Connected to AP");
  }
}

void setup() {
  // initialize serial for debugging
  Serial.begin(SERIAL_DEBUG_BAUD);
  Serial.println("Initializing...");
  InitWiFi();
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  configTime(0, 0, ntpServer);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
  pinMode(LED_CYAN, OUTPUT);
}

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...");

  // Uploads new telemetry to ThingsBoard using MQTT.
  // See https://thingsboard.io/docs/reference/mqtt-api/#telemetry-upload-api
  // for more details


  float distance = readDistanceCM();
  // String dateTime = getTime();
  tb.sendTelemetryFloat("distance", distance);

  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  tb.sendTelemetryInt("temperature", data.temperature);
  tb.sendTelemetryFloat("humidity", data.humidity);

  if (data.temperature < 25) {
    digitalWrite(LED_RED, LOW);
    digitalWrite(LED_YELLOW, HIGH);
    digitalWrite(LED_GREEN, LOW);
    Serial.println("RENDAH");
  } else  if (data.temperature <= 28) {
    digitalWrite(LED_RED, LOW);
    digitalWrite(LED_YELLOW, LOW);
    digitalWrite(LED_GREEN, HIGH);
    Serial.println("NYAMAN");
  }  else {
    digitalWrite(LED_RED, HIGH);
    digitalWrite(LED_YELLOW, LOW);
    digitalWrite(LED_GREEN, LOW);
    Serial.println("TINGGI");
  }
  tb.sendTelemetryInt("TINGGI", digitalRead(LED_RED));
  tb.sendTelemetryInt("RENDAH", digitalRead(LED_YELLOW));
  tb.sendTelemetryInt("NYAMAN", digitalRead(LED_GREEN));

  if (distance < 50) {
    digitalWrite(LED_BLUE, LOW);
    digitalWrite(LED_CYAN, HIGH);
    Serial.println("WASPADA");
  } else {
    digitalWrite(LED_BLUE, HIGH);
    digitalWrite(LED_CYAN, LOW);
    Serial.println("AMAN");
  }
  tb.sendTelemetryInt("WASPADA", digitalRead(LED_CYAN));
  tb.sendTelemetryInt("AMAN", digitalRead(LED_BLUE));

  Serial.print("Suhu : ");
  Serial.print(data.temperature);
  Serial.print(" Kelembaban : ");
  Serial.print(data.humidity);
  Serial.print(" Distance : ");
  Serial.println(distance);


  // int minTemp = 18;
  // int maxTemp = 36;
  // int randNumberTemp = (maxTemp - minTemp) * ( (double)rand() / (double)RAND_MAX ) + minTemp;
  // int minHum = 30;
  // int maxHum = 50;
  // int randNumberHum = (maxHum - minHum) * ( (double)rand() / (double)RAND_MAX ) + minHum;

  // tb.sendTelemetryInt("temperature", randNumberTemp);
  // tb.sendTelemetryFloat("humidity", randNumberHum);
  // Serial.print("Suhu : ");
  // Serial.print(randNumberTemp);
  // Serial.print(" Kelembaban : ");
  // Serial.println(randNumberHum);

  tb.loop();

  tb.loop();
}


float readDistanceCM() {
  digitalWrite(TRIG_PIN, LOW);
  delay(20);
  digitalWrite(TRIG_PIN, HIGH);
  delay(100);
  digitalWrite(TRIG_PIN, LOW);

  int duration = pulseIn(ECHO_PIN, HIGH);
  return duration * 0.0343 / 2;
}

String getTime() {
  // time_t now;
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    return "Failed to obtain time";
  }
  // time(&now);
  // Serial.println(&timeinfo, "%A, %d %B %Y %H:%M:%S");
  char timeStringBuff[50];
  strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %d %B %Y %H:%M:%S", &timeinfo);
  return timeStringBuff;
}