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

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSansBold9pt7b.h>
#include <Fonts/Picopixel.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);

#include "ThingsBoard.h"
#include "DHTesp.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               "hktaFPtspCs5GFSPHufS"
#define THINGSBOARD_SERVER  "thingsboard.cloud"

#define pinRelay 12
int pinpot = A0;
int val;
// Baud rate for debug serial
#define SERIAL_DEBUG_BAUD   115200

const int DHT_PIN = 15;

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 updateDisplay() {
  val = analogRead(pinpot);
  val = map(val,0,4095,0,100);
  display.clearDisplay();
  display.setFont();
  display.setTextColor(1);
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  display.setCursor(12, 2);
  display.print("Temp Udara: ");
  display.print(data.temperature);
  display.print("'C");
  display.setCursor(12, 12);
  display.print("Hum  Udara: ");
  display.print(data.humidity);
  display.print("%");
  display.setCursor(12, 22);
  display.print("Hum  Tanah: ");
  display.print(val);
  display.print("%");
  display.setCursor(12, 32);
  display.print("MDPL : 850");

  display.setCursor(12, 42);
  display.print("Wisata Petik Apel");
  display.setCursor(32, 52);
  display.print("Agro rakyat");
  display.display();
}


void setup() {
  // initialize serial for debugging
  pinMode(pinRelay, OUTPUT);
  Serial.begin(SERIAL_DEBUG_BAUD);
  Serial.println();
  InitWiFi();
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  
}

void loop() {
  delay(1000);

  updateDisplay();

  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

  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  tb.sendTelemetryFloat("temperature", data.temperature);
  tb.sendTelemetryInt("humidity", data.humidity);
  tb.sendTelemetryInt("hum_tanah", val);
  Serial.print("Suhu Udara : ");
  Serial.print(data.temperature);
  Serial.print(" Kelembaban Udara : ");
  Serial.print(data.humidity);
  val = analogRead(pinpot);
  val = map(val,0,4095,0,100);
  Serial.print(" Kelembaban Tanah : ");
  Serial.println(val);

  tb.sendTelemetryFloat("lat", -7.8779302);
  tb.sendTelemetryFloat("long", 112.5332595);

if(val < 75){
digitalWrite(pinRelay, HIGH);
tb.sendTelemetryInt("Pompa_Air", 1);
Serial.println("Pompa Nyala");

}
  else{
    digitalWrite(pinRelay, LOW);
    tb.sendTelemetryInt("Pompa_Air", 0);
    Serial.println("Normal");
  
  }
  tb.loop();

  tb.loop();
}