#include  "WiFi.h" 

WiFiClient client;
//Class Object

#include "DHTesp.h"

const int DHT_PIN = 15;

DHTesp dhtSensor;;


String thingSpeakAddress = "api.thingspeak.com";
String writeAPIKey;
String tsfield1Name;
String request_string;

float lampu;
float val;

float readDistanceCM(){
  digitalWrite(12, LOW);
  delayMicroseconds(2);
  digitalWrite(12, HIGH);
  delayMicroseconds(10);
  digitalWrite(12, LOW);
  int duration = pulseIn(2, HIGH);
  return duration * 0.034/2;
}



void setup()
{
  pinMode(14, OUTPUT);
  pinMode(12, INPUT);
  pinMode(12, OUTPUT);
  pinMode(2, INPUT);
  Serial.begin(9600);
  WiFi.disconnect();
  WiFi.begin("Wokwi-GUEST", "");
  while ((!(WiFi.status() == WL_CONNECTED))) {
    delay(300);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}


void loop()
{


  
  float val = analogRead(13);
  val = map(val, 0, 1023, 0, 180);
  float angka = val;

  float jarak = readDistanceCM();
  Serial.println(angka);
  led();
  delay(2000);
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();

  float t = data.temperature;
  float h = data.humidity;
  float j = jarak;
  float l = lampu;

  kirim_thingspeak(t, h, j, l);

  if (isnan(t) || isnan(h) || isnan(j) || isnan(l)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
}

void kirim_thingspeak(float suhu, float hum, float jarak, float lampu) {
  if (client.connect("api.thingspeak.com", 80)) {
    request_string = "/update?";
    request_string += "key=";
    request_string += "LAOZLO50Y862XCHC";
    request_string += "&";
    request_string += "field1";
    request_string += "=";
    request_string += suhu;
    request_string += "&";
    request_string += "field2";
    request_string += "=";
    request_string += hum;
    request_string += "&";
    request_string += "field3";
    request_string += "=";
    request_string += jarak;
    request_string += "&";
    request_string += "field4";
    request_string += "=";
    request_string += lampu;
  

    Serial.println(String("GET ") + request_string + " HTTP/1.1\r\n" +
                 "Host: " + thingSpeakAddress + "\r\n" +
                 "Connection: close\r\n\r\n");
                 
    client.print(String("GET ") + request_string + " HTTP/1.1\r\n" +
                 "Host: " + thingSpeakAddress + "\r\n" +
                 "Connection: close\r\n\r\n");
    unsigned long timeout = millis();
    while (client.available() == 0) {
      if (millis() - timeout > 5000) {
        Serial.println(">>> Client Timeout !");
        client.stop();
        return;
      }
    }

    while (client.available()) {
      String line = client.readStringUntil('\r');
      Serial.print(line);
    }

    Serial.println();
    Serial.println("closing connection");

  }
}

void led(){
  float jarak = readDistanceCM();
  if(jarak > 0 && jarak < 200 ){
    digitalWrite(14, HIGH);
    lampu=1;
  }else{
    digitalWrite(14, LOW);
    lampu=0;
  }
}