#include <WiFi.h>           // WiFi control for ESP32
//#include <ThingsBoard.h>    // ThingsBoard SDK
#include "ThingsBoard.h"

#define WIFI_AP_NAME                  "Wokwi-GUEST"
#define WIFI_PASSWORD                 ""
#define THINGSBOARD_MQTT_SERVER       "thingsboard.cloud"
#define THINGSBOARD_MQTT_ACESSTOKEN   "aoo5t6ZanKoN0k3OTVao"
#define THINGSBOARD_MQTT_PORT         1883
#define SERIAL_DEBUG_BAUD    115200
#define POT_PIN 34


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

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

const float BETA = 3950; // should match the Beta Coefficient of the thermistor
#include "DHTesp.h"
const int DHT_PIN = 22;
DHTesp dhtSensor;

// Start - Modified By Revi - 31 Juli 2022
#include <ESP32Servo.h>
#define LDRPIN 33
#define servoPin 25
Servo myservo;

#define minCahaya 2045    // = 100 lux
#define maxCahaya 1291    // = 300 lux

unsigned long d1 = 100;
unsigned long lastD1 = 0;

unsigned long d2 = 400;
unsigned long lastD2 = 0;

unsigned long d3 = 200;
unsigned long lastD3 = 0;

int analogValue = 0;
int dataPot = 0;
int potValue = 0;

int h = 0;
int t = 0;
int LDRVal = 0;

const float GAMMA = 0.7;
const float RL10 = 50;

float voltage = 0;
float resistance = 0;
float lux = 0;

TempAndHumidity data;

// End

void setup() {

  Serial.begin(SERIAL_DEBUG_BAUD);
  WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
  InitWiFi();
  pinMode(POT_PIN, INPUT);

  pinMode(14,OUTPUT);
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  lcd.init();
  lcd.backlight();

  // Modified By Revi - 31 Juli 2022
	ESP32PWM::allocateTimer(0);
	ESP32PWM::allocateTimer(1);
	ESP32PWM::allocateTimer(2);
	ESP32PWM::allocateTimer(3);  
  myservo.setPeriodHertz(50);// Standard 50hz servo
  myservo.attach(servoPin, 500, 2400); 
  pinMode(LDRPIN, INPUT);
  // End
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    reconnect();
    return;
  }
	
  // ***** READ POTENTIOMETER AS SOIL SENSOR ***** //
  dataPot = analogRead(POT_PIN);
  potValue = map(dataPot, 0, 800, 0, 255);
  Serial.print("Kelembapan Tanah : ");
  Serial.println(potValue);
  for (int i = 13; i <= 20; i++) {
    lcd.setCursor(i,2);
    lcd.print(" ");
  }
		
  // ***** MENYALAKAN LED KALAU KELEMBAPAN TANAH KERING ***** //
  if(potValue <= 500) {
    digitalWrite(14, HIGH); 
  } else {
    digitalWrite(14, LOW);
  }

  // ***** READ LDR AS INTENSITAS CAHAYA MATAHARI ***** //
  LDRVal = analogRead(LDRPIN);
  voltage = LDRVal / 4095. * 5;
  resistance = 2000 * voltage / (1 - voltage / 5);
  lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));

  if (LDRVal >= maxCahaya) {
    myservo.write(0);
  } else {
    myservo.write(90);
  }
  Serial.print("Sinar Matahari : ");
  Serial.println(lux);

  // lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Suhu : ");
  lcd.print(t);
  lcd.print(" C ");
  lcd.setCursor(0,1);
  lcd.print("Humidity : ");
  lcd.print(h);
  lcd.print("%");
  lcd.setCursor(0,2);
  lcd.print("klbpn tanah : ");
  lcd.print(potValue);
  lcd.setCursor(0,3);
  lcd.print("Sinar : ");
  lcd.print(lux);
  lcd.print(" lux");

	unsigned long currentMillis = millis();

	if ((currentMillis - lastD2) > d2) {
		lastD2 = currentMillis;

    // TempAndHumidity data = dhtSensor.getTempAndHumidity();
		Serial.print("Suhu : ");
		Serial.print(data.temperature);
    t = data.temperature;
		Serial.print(" Kelembaban : ");
		Serial.println(data.humidity);
    h = data.humidity;
	}
  
	if ((currentMillis - lastD3) > d3) {
    lastD3 = currentMillis;
    sendData();
	}
}

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(WiFi.localIP());
  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(WiFi.localIP());
    Serial.println("Connected to AP");
  }
}

void sendData() {
  Serial.println("Sending data...");
  if (!tb.connected()) {
    // Connect to the ThingsBoard
    Serial.print("Connecting MQTT to: ");
    Serial.print(THINGSBOARD_MQTT_SERVER);
    Serial.print(" at port no. ");
    Serial.println(THINGSBOARD_MQTT_PORT);
    Serial.print(" with access token ");
    Serial.println(THINGSBOARD_MQTT_ACESSTOKEN);
    if (!tb.connect(THINGSBOARD_MQTT_SERVER, THINGSBOARD_MQTT_ACESSTOKEN, THINGSBOARD_MQTT_PORT)) {
      Serial.println("Failed to connect");
      return;
    }
  }
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();

  tb.sendTelemetryInt("temperature", data.temperature);
  tb.sendTelemetryFloat("humidity", data.humidity);
  tb.sendTelemetryFloat("Kelembapan Tanah", potValue);
  tb.sendTelemetryInt("kecerahan", lux);
  Serial.println("Data Sended...");
}