#include <DHTesp.h>         // DHT for ESP32 library
#include <WiFi.h>           // WiFi control for ESP32
#include "ThingsBoard.h"

#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))

#define WIFI_AP_NAME              "Wokwi-GUEST"
#define WIFI_PASSWORD             ""
#define THINGSBOARD_SERVER        "103.189.235.22"
#define THINGSBOARD_ACCESSTOKEN   "oI1jmp564fzAnihhilEs"

WiFiClient espClient;
ThingsBoard tb(espClient);
int status = WL_IDLE_STATUS;

// deklarasi array untuk pin pendingin yang dikontrol melalui thingsboard
uint8_t aircond_control[] = { 13, 12 };

// deklarasi array untuk pin led yang dikontrol melalui thingsboard
uint8_t leds_control[] = { 2, 4 };

DHTesp dht;
#define DHT_PIN 15
int i;
// Main application loop delay
int quant = 20;
float control_tem = 0;
int control_lamp1 = 0;
int control_lamp2 = 0;
int pendingin = 0;
int pemanas = 0;
// Period of sending a temperature/humidity data.
int send_delay = 2000;

// Time passed after temperature/humidity data was sent, milliseconds.
int send_passed = 0;

// Set to true if application is subscribed for the RPC messages.
bool subscribed = false;

float temperature = 0;
float humidity = 0;

RPC_Response processTempChange(const RPC_Data &data)
{
  Serial.println("Received the set delay RPC method");

  // Process data

  control_tem = data;

  Serial.print("Atur suhu ke: ");
  Serial.println(control_tem);

  return String(control_tem);
}

RPC_Response processGetTemp(const RPC_Data &data)
{
  Serial.println("Received the get value method");
  return String(control_tem);
}

RPC_Response processSwitch1Change(const RPC_Data &data)
{
  Serial.println("Received the set delay RPC method");

  // Process data

  control_lamp1 = data;

  Serial.println(control_lamp1);

  return String(control_lamp1);
}

RPC_Response processGetSwitch1(const RPC_Data &data)
{
  Serial.println("Received the get value method");
  return String(control_lamp1);
}

RPC_Response processSwitch2Change(const RPC_Data &data)
{
  Serial.println("Received the set delay RPC method");

  // Process data

  control_lamp2 = data;

  Serial.println(control_lamp2);

  return String(control_lamp2);
}

RPC_Response processGetSwitch2(const RPC_Data &data)
{
  Serial.println("Received the get value method");
  return String(control_lamp2);
}

/*RPC_Response processCheckPemanas(const RPC_Data &data)
{
  return String(pemanas);
}

RPC_Response processCheckPendingin(const RPC_Data &data)
{
  return String(pendingin);
}*/

// RPC handlers
RPC_Callback callbacks[] = {
  { "setValue",         processTempChange },
  { "getValue",         processGetTemp },
  { "setSwitch1",       processSwitch1Change },
  { "getSwitch1",       processGetSwitch1 },  
  { "setSwitch2",       processSwitch2Change },
  { "getSwitch2",       processGetSwitch2 }, 
  //{ "checkPemanas",     processCheckPemanas }, 
  //{ "checkPendingin",   processCheckPendingin }, 
};

void setup() {
  Serial.begin(115200);
  connect();

  // Inisialiasi pinmode
  for (size_t i = 0; i < COUNT_OF(aircond_control); ++i) {
    pinMode(aircond_control[i], OUTPUT);
  }
  for (size_t i = 0; i < COUNT_OF(leds_control); ++i) {
    pinMode(leds_control[i], OUTPUT);
  }

  // Initialize temperature sensor
  dht.setup(DHT_PIN, DHTesp::DHT22);
}

void loop() 
{
  delay(quant);

  send_passed += quant;

  // Reconnect to WiFi, if needed
  if (WiFi.status() != WL_CONNECTED) {
    connect();
    return;
  }

  // Reconnect to ThingsBoard, if needed
  if (!tb.connected()) {
    subscribed = false;
    // Connect to the ThingsBoard
    Serial.print("Connecting to: ");
    Serial.print(THINGSBOARD_SERVER);
    Serial.print(" with token ");
    Serial.println(THINGSBOARD_ACCESSTOKEN);
    if (!tb.connect(THINGSBOARD_SERVER, THINGSBOARD_ACCESSTOKEN)) {
      Serial.println("Failed to connect");
      return;
    }
  }

  // Subscribe for RPC, if needed
  if (!subscribed) {
    Serial.println("Subscribing for RPC... ");
    // Perform a subscription. All consequent data processing will happen in
    // callbacks as denoted by callbacks[] array.
    if (!tb.RPC_Subscribe(callbacks, COUNT_OF(callbacks))) {
      Serial.println("Failed to subscribe for RPC");
      return;
    }
    Serial.println("Subscribe done");
    subscribed = true;
  } 

  digitalWrite(leds_control[0], control_lamp1);
  digitalWrite(leds_control[1], control_lamp2);

  // Check if it is a time to send DHT22 temperature and humidity
  if (send_passed > send_delay) {
    Serial.println();
    Serial.print("Sending data... ");
    TempAndHumidity lastValues = dht.getTempAndHumidity();    

    if (isnan(lastValues.humidity) || isnan(lastValues.temperature)) {
      Serial.println("Failed to read from DHT sensor!");
    } else {
      temperature = lastValues.temperature;
      Serial.print("Suhu: ");
      Serial.print(temperature);
      humidity = lastValues.humidity;
      Serial.print(" Kelembaban: ");
      Serial.println(humidity);
      tb.sendTelemetryFloat("suhu", temperature);
      if (temperature < control_tem) {
        // Nyalakan pemanas
        pendingin = 0;
        pemanas = 1;
       }
      else if(temperature > control_tem){
        // Nyalakan pendingin
        pendingin = 1;
        pemanas = 0;
      }
      else{
        pendingin = 0;
        pemanas = 0;
      }
      digitalWrite(aircond_control[0], pemanas);
      digitalWrite(aircond_control[1], pendingin);
      //tb.sendTelemetryInt("pemanas", pemanas);
      //tb.sendTelemetryInt("pendingin", pendingin);
    }
    send_passed = 0;
  }

  tb.loop();
}

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

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