#include <Wire.h>
#include <RTClib.h>
#define RTC_DATA_PIN 21 // Pin SDA
#define RTC_CLK_PIN 22 // Pin SCL
#define RELAY_PIN 5
String stringOne = "13:52"; // Start timer
String stringTwo = "13:53"; // Stop timer
int8_t startHour, stopHour, startMinute, stopMinute;
boolean state=true;
RTC_DS1307 rtc;
void setup() {
Serial.begin(9600);
Serial.println("Hello, ESP32!");
Wire.begin();
rtc.begin();
pinMode(5, OUTPUT);
// rtc.adjust(DateTime(2023, 8, 8, 14, 40, 0));
startHour=stringOne.substring(0, 2).toInt();
startMinute=stringOne.substring(3, 5).toInt();
stopHour=stringTwo.substring(0, 2).toInt();
stopMinute=stringTwo.substring(3, 5).toInt();
Serial.print(startHour);
Serial.print(":");
Serial.println(startMinute);
Serial.print(stopHour);
Serial.print(":");
Serial.println(stopMinute);
Serial.println("---------------------------");
}
void loop() {
DateTime now = rtc.now();
Serial.print(now.hour(), DEC); //Imprimimos la hora configurada en la placa RTC DS1302
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.println(now.second(), DEC);
delay(1000);
// put your main code here, to run repeatedly:
if (state) {
if (now.hour() == startHour && now.minute() == startMinute){
digitalWrite(5, HIGH);
Serial.println("Relay ON");
} else {
if (now.hour() == stopHour && now.minute() == stopMinute){
digitalWrite(5, LOW);
Serial.println("Relay OFF");
}
}
}
}
/*
if (now.hour() == startHour && now.minute() == startMinute){
if (now.hour() == stopHour && now.minute() == stopMinute){
if (now.minute() == startHour && now.second() == startMinute){
if (now.minute() == stopHour && now.second() == stopMinute){
*/