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

#define WIFI_AP_NAME              "Wokwi-GUEST"
#define WIFI_PASSWORD             ""
#define THINGSBOARD_SERVER        "thingsboard.cloud" //demo.thingsboard.io
#define THINGSBOARD_ACCESSTOKEN   "muhfajarherman"
#define SERIAL_DEBUG_BAUD    115200
#define led1 13
#define led2 12
#define led3 14
#define led4 27
#define relay_lampu 5


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

DHTesp dht;
#define DHT_PIN 15

// Main application loop delay
int quant = 20;
// Initial period of LED cycling.
int led_delay = 1000;
// Period of sending a temperature/humidity data.
int send_delay = 2000;

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

// LED number that is currenlty ON.
int current_led = 0;

float temperature = 0;
float humidity = 0;

LiquidCrystal_I2C lcd(0x27, 16, 2);
byte SimbolDerajat[] = {
  B00110,
  B01001,
  B01001,
  B00110,
  B00000,
  B00000,
  B00000,
  B00000,
};



void setup() {
  lcd.init();
      lcd.createChar(0, SimbolDerajat);
      lcd.backlight();
      lcd.setCursor(0, 0);
      lcd.print("Sensor Suhu &");
      lcd.setCursor(0, 1);
      lcd.print("Kelembaban");
      delay(4000);
     lcd.clear();

  Serial.begin(SERIAL_DEBUG_BAUD);
  WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
  InitWiFi();
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(relay_lampu, OUTPUT);
  


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

}

void loop() 
{
  delay(1000);

  led_passed += quant;
  send_passed += 1000;

  // Reconnect to WiFi, if needed
  if (WiFi.status() != WL_CONNECTED) {
    reconnect();
    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;
    }
  }

  // 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("temperature: ");
      Serial.print(temperature);
      humidity = lastValues.humidity;
      Serial.print(" humidity: ");
      Serial.print(humidity);
      tb.sendTelemetryInt("temperature", temperature);
      tb.sendTelemetryFloat("humidity", humidity);
    }
    send_passed = 0;
  }
  tb.loop();

lcd.setCursor(0, 0);
  lcd.print("Suhu :" + String(temperature));
  lcd.write(0);
  lcd.print("C");
    tb.loop();  

  if (digitalRead(led1) == HIGH) {
    if (temperature >= 36) {
    digitalWrite(relay_lampu, LOW); 
    lcd.setCursor(0, 1);
    lcd.print("Lampu Off");
    lcd.print("         ");
  }
    if (temperature < 35) {
    digitalWrite(relay_lampu, HIGH);
    lcd.setCursor(0, 1);
    lcd.print("Lampu On");
    lcd.print("         ");
  }
    else{digitalWrite(relay_lampu, LOW);}
    }

  if (digitalRead(led2) == HIGH) {
    if (temperature >= 33) {
    digitalWrite(relay_lampu, LOW); 
    lcd.setCursor(0, 1);
    lcd.print("Lampu Off");
    lcd.print("         ");
  }
    if (temperature < 32) {
    digitalWrite(relay_lampu, HIGH);
    lcd.setCursor(0, 1);
    lcd.print("Lampu On");
    lcd.print("         ");
  }
    else{digitalWrite(relay_lampu, LOW);}
    }
  
  if (digitalRead(led3) == HIGH) {
    if (temperature >= 30) {
    digitalWrite(relay_lampu, LOW); 
    lcd.setCursor(0, 1);
    lcd.print("Lampu Off");
    lcd.print("         ");
  }
    if (temperature < 29) {
    digitalWrite(relay_lampu, HIGH);
    lcd.setCursor(0, 1);
    lcd.print("Lampu On");
    lcd.print("         ");
  }
    else{digitalWrite(relay_lampu, LOW);}
    }
  
  if (digitalRead(led4) == HIGH) {
    if (temperature >= 27) {
    digitalWrite(relay_lampu, LOW); 
    lcd.setCursor(0, 1);
    lcd.print("Lampu Off");
    lcd.print("         ");
  }
    if (temperature < 26) {
    digitalWrite(relay_lampu, HIGH);
    lcd.setCursor(0, 1);
    lcd.print("Lampu On");
    lcd.print("         ");
  }
    else{digitalWrite(relay_lampu, LOW);}
    }
}

void InitWiFi()
{
  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");
}

void reconnect() {
  // Loop until we're reconnected
  status = WiFi.status();
  if ( status != WL_CONNECTED) {
    WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("Connected to AP");
  }
}