#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL7PYN5csv"
#define BLYNK_DEVICE_NAME "Final Project"
#define BLYNK_AUTH_TOKEN "nm2bR0z7W9lLrXZrfcnyu7KJPVypqG0h"

#include <ESP32Servo.h>
#include <Wire.h>
#include "RTClib.h"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

RTC_DS1307 rtc;

const int ldrPin = 32;
const float GAMMA = 0.7;
const float RL10 = 50;

const int ledPin = 18;
const int ledPin2 = 19;
const int ledManual = 13;
const int ledSiram = 12;

float ldrOut;
bool manual = false;

int servoPin = 26; // select the input pin for LDR

int currentPos;

int lastSecond, lastMinute, lastHour;

char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "Wokwi-GUEST";
char pass[] = "";

WiFiClient client;
Servo servoMotor;

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt();
  Serial.println(pinValue);
  if (pinValue == 1)openCurtain();
  if (pinValue == 0)closeCurtain();
}

BLYNK_WRITE(V2)
{
  int pinValue = param.asInt();
  if (pinValue == 0) {
    manual = false;
    digitalWrite(ledManual, LOW);
  }
  if (pinValue == 1) {
    manual = true;
    digitalWrite(ledManual, HIGH);
  }
}

void setup() {

  WiFi.disconnect();
  Serial.begin(115200);

  Blynk.begin(auth, "Wokwi-GUEST", "");
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ldrPin, INPUT);
  pinMode(ledManual, OUTPUT);
  pinMode(ledSiram, OUTPUT);

  rtc.begin();
  servoMotor.attach(servoPin);
  servoMotor.write(0);
  delay(1000);
}

void loop() {
  Blynk.run();
  DateTime now = rtc.now();

  if (now.second() != lastSecond) {

    ldrOut = bacaLDR();

    Serial.print("TIME: ");
    Serial.print(now.hour());
    Serial.print(":");
    Serial.print(now.minute());
    Serial.print(":");
    Serial.print(now.second());
    Serial.print("    Intensitas = ");
    Serial.print(ldrOut);
    Serial.println("LUX");

    if (now.second() % 10 == 0) {
      Blynk.virtualWrite(V0, ldrOut);
      Serial.println("Kirim data ke Blynk");
    }

    //manual true => kontrol dari blynk
    if (manual == false) {
      if (ldrOut >= 250) {
        digitalWrite(ledPin, HIGH);
        digitalWrite(ledPin2, LOW);
        if (currentPos == 0)openCurtain();
      }
      else {
        digitalWrite(ledPin, LOW);
        digitalWrite(ledPin2, HIGH);
        if (currentPos == 180)closeCurtain();
      }
    }

    /*********************************************
        menyiram tanaman paja jam tertentu
        simulasi pada detik tertentu
    **********************************************/
    //if ((now.hour() != lastHour) && (nowHour() == 7)) {
    //  siramTanah();
    //}

    if ((now.second() > 10) && (now.second() < 40)) {
      digitalWrite(ledSiram, HIGH);
    }
    else {
      digitalWrite(ledSiram, LOW);
    }

    lastSecond = now.second();
    lastMinute = now.minute();
    lastHour = now.hour();
  }
}

float bacaLDR() {
  int analogValue = analogRead(ldrPin);
  float voltage = analogValue / 4095. * 5;
  float resistance = 2000 * voltage / (1 - voltage / 5);
  float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
  return lux;
}

void openCurtain() {
  Serial.println("Opening the curtain");
  int pos = 180;
  servoMotor.write(pos);
  currentPos = pos;
  Blynk.virtualWrite(V1, true);
}

void closeCurtain() {
  Serial.println("Closing the curtain");
  int pos = 0;
  servoMotor.write(pos);
  currentPos = pos;
  Blynk.virtualWrite(V1, false);
}
GND5VSDASCLSQWRTCDS1307+