//esp32 rtc tod,tou
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS1307 rtc;
int day;
int month;
int year;
int hour;
int minute;
int second;
int dnum; //more
int tou = 15;
int tod = 2;
char daysOfTheWeek[7][6] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};//more
void setup() {
pinMode(tou, OUTPUT);
pinMode(tod, OUTPUT);
Serial.begin(115200);
rtc.begin();
lcd.init();
lcd.backlight();
} ////////////////////end loop
void loop() {
//digitalWrite(tou, HIGH);// test relay
//delay(500);
//digitalWrite(tou, LOW);
//delay(500);
//digitalWrite(tod, HIGH);
//delay(500);
//digitalWrite(tod, LOW);
//delay(500);
Date_Time();
if(dnum >=1 && dnum <=5){ //1แทนวัน mon-fri 09.00-22.00 tod ทำงาน
if (hour>= 9 && hour <= 21){ // หล้ง 22.00 - 09.00 sat sun tou ทำงาน
digitalWrite( tod , HIGH);
digitalWrite(tou, LOW);
} else {
digitalWrite(tod , LOW);
digitalWrite(tou, HIGH);
}
}else{
digitalWrite( tod, LOW);
digitalWrite( tou, HIGH);
}
} ////////////////////end loop
void Date_Time(){ ////////fuction Date_Time
DateTime now = rtc.now(); //
day = now.day();
month = now.month();
year = now.year();
hour = now.hour();
minute = now.minute();
second = now.second();
dnum = now.dayOfTheWeek(); /////more 2 day no s (daysOfTheWeek[now.dayOfTheWeek()]);
//////////////////////////////date
lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[dnum]);//more 3
lcd.setCursor(6, 0);
if (day <= 9) {
lcd.print("0");
}
lcd.print(day);
lcd.setCursor(8, 0);
lcd.print("/");
lcd.setCursor(9, 0);
if (month <= 9) {
lcd.print("0");
}
lcd.print(month);
lcd.setCursor(11, 0);
lcd.print("/");
lcd.setCursor(12, 0);
lcd.print(year);
lcd.setCursor(0, 1);
//////////////////////////////time
lcd.print("Time");
lcd.setCursor(6, 1);
if (hour <= 9){ ////////////h
lcd.print("0");
}
lcd.print(hour);
lcd.setCursor(8, 1);
lcd.print(":");
lcd.setCursor(9, 1);
if (minute <= 9) { /////////////m
lcd.print("0");
}
lcd.print(minute);
lcd.setCursor(11, 1);
lcd.print(":");
lcd.setCursor(12, 1);
if (second <= 9) { //////////////s
lcd.print("0");
}
lcd.print(second);
}