#include <ESP32Servo.h>
#include "RTClib.h"
#include <Wire.h>
#include "SPI.h"
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
const int I2C_ADDRESS = 0x68;
LiquidCrystal_I2C lcd (0x27, 16,2);

const int DHT_PIN = 15;
DHTesp dhtSensor;


const float GAMMA = 0.7;
const float RL10 = 50;
const int relayPinUV = 34;
RTC_DS1307 rtc;
int pos;
#define Trig 27
#define Echo 14
#define push 13
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Servo myservo1;  
Servo myservo2;  


volatile byte pulseCount;
float debit;
unsigned int flowmlt;
unsigned long totalmlt;
unsigned long oldTime;



void setup() {

  Serial.begin(115200);  
  // print the Message on the LCD. 

    lcd. init ();
  
  // Turn on the backlight on LCD. 
  lcd. backlight ();   

 
pulseCount = 0;
debit = 0.0;
flowmlt = 0;
totalmlt = 0;
oldTime = 0;

    myservo1.attach(19);
    myservo2.attach(18);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
pinMode(relayPinUV, OUTPUT);
pinMode(push, INPUT);


// put your setup code here, to run once:
  Wire.begin(21,22);

    if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }

dhtSensor.setup(DHT_PIN, DHTesp::DHT22);  
         // Initialize the LCD connected 
 
 
}



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

void pulseCounter()
{
// Increment the pulse counter
pulseCount++;
}

void loop() {
  TempAndHumidity  data = dhtSensor.getTempAndHumidity();
  Serial.println("Temp: " + String(data.temperature, 2) + "°C");
  Serial.println("Humidity: " + String(data.humidity, 1) + "%");
  Serial.println("---");
  delay(1000);

  // put your main code here, to run repeatedly:
   int analogValue = analogRead(12);
  float voltage = analogValue / 1024. * 5;
  float resistance = 2000 * voltage / (1 - voltage / 5);
  float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
  float distance = readDistanceCM();

    if(lux >3800 & distance >=200){
       myservo1.write(90);
       myservo2.write(90); 
       digitalWrite( relayPinUV, LOW); 
      
              
    }

else if (lux <3800 & lux >2800 & distance >=200) {
myservo1.write(0);
myservo2.write(90);

}


else if (lux <2800 & lux >0 & distance <=200) {
myservo1.write(90);
myservo2.write(0);

  
delay( 600);

}

else if (lux <3600 ) {

digitalWrite( relayPinUV, HIGH);
  


}

else if (lux <3600 ) {

digitalWrite( relayPinUV, LOW);
  
}


else {
myservo1.write(0);  
myservo2.write(90);
 
}

  DateTime now = rtc.now();

  Serial.print("Current time: ");
   Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
 
 
if((millis() - oldTime) > 1000) 
{ 

debit = (pulseCount * distance *100 )/((millis() - oldTime))  ;
oldTime = millis();
flowmlt = (debit / 60) *1000;
totalmlt += flowmlt;

Serial.print("Debit Hujan: ");
Serial.print(int(debit));
Serial.print("mm/min");

}

 
if (digitalRead (push) == HIGH)
myservo1.write(30);


    //Here cursor is placed on first position (col: 0) of the second line (row: 1) 
  lcd. setCursor (0, 1);
   // We write the number of seconds elapsed 

  lcd.print("Temp: " + String(data.temperature, 2) + "°C");
  delay (100);


}
GND5VSDASCLSQWRTCDS1307+