//  DS1307ESP Edited by 3tawi
//  DS1307ESP Library: https://github.com/3tawi/DS1307ESP
//  Find All "Great Projects" Videos : https://www.youtube.com/c/GreatProjects
#include "DS1307ESP.h"
////////////////////////////////////////////////////
#include "SolarCalculator.h"
 // Date
  //int year = 2023;
  //int month = 9;
  //int day = 3;
  // Location
  double latitude = 40.7609599; 
  double longitude = 29.9323999;
  // eproma şehirlerin isimleri ve enlem/boylam bilgileri kaydedilecek 
  // web arayüzünden şehir seçildiği zaman ilgili şehrin enlem boylamı
  // bu değişkene atanacak ve ve bu değişken eproma yazılacak 
  // elektrik kesintisinde hangi şehirde olduğunu unutmayacak ;))
  int utc_offset = 3; // sabit kalacak
  double transit, sunrise, sunset;
////////////////////////////////////////////
DS1307ESP rtc;
uint32_t lastTime; 
// uint8_t sdapin = 12, sclpin = 14; 
void setup() {
  Serial.begin(9600);
// The SDA and SCL lines of the I2C bus come from GPIO21 and GPIO22 of the ESP32 board (respectively)
// The SDA and SCL lines of the I2C bus come from GPIO4 (D2) and GPIO5 (D1) of the NodeMCU ESP8266 board (respectively)
   rtc.begin(); 
// rtc.begin(sdapin, sclpin);                     
   rtc.DSadjust(23, 59, 50, 2023, 9, 4); // 00:19:21 16 Mar 2022
// zaman ayarı web arayüzünden yapılacak 
// işlemci elektrik kesintisi veya yeniden başladığı zaman güncel zaman bilgisini DS 1307 den okuyacak
// DS 1307 de dahili pil mevcut 
}
void loop() {

   if(millis() - lastTime >= 1000) {
    updateTime();
    lastTime = millis();
    }
}
void updateTime() {
     rtc.DSread(); 
////////////////////////////////////////////////////////////////////////////////////////////////////
// Calculate the times of sunrise, transit, and sunset, in hours (UTC)
  //calcSunriseSunset(year, month, day, latitude, longitude, transit, sunrise, sunset);
  calcSunriseSunset((rtc.year + 2000), (rtc.month), (rtc.dayOfMonth), latitude, longitude, transit, sunrise, sunset);
  char str[6];
  Serial.print("Gündoğumu: ");
  Serial.println(hoursToString(sunrise + utc_offset, str));
  Serial.print("Günbatımı: ");
  Serial.println(hoursToString(sunset + utc_offset, str));
////////////////////////////////////////////////////////////////////////////////////////////////////////
     // Serial.println(rtc.getMahmin(false));      //  (String) true = 00:00  false = 00 00  (24h)
     // Serial.println(rtc.getMihmin(false));      //  (String) true = 12:00  false = 12 00  (12h)
      Serial.print("Saat:  ");
      Serial.println(rtc.getTime(true));          //  (String) 00:00:03  (24h)
     // Serial.println(rtc.getTime(true));      //  (String) 12:00:03  (12h)
     // Serial.println(rtc.getDate());          //  (String) Mon, 14 Mar 2022
     // Serial.println(rtc.getDate(true));      //  (String) Monday, 14-March-2022
     // Serial.println(rtc.getDateTime());      //  (String) Mon, 14 Mar 2022 00:09:21
     // Serial.println(rtc.getDateTime(true));  //  (String) Monday, 14-March-2022 00:09:21
     // Serial.println(rtc.getTimeDate());      //  (String) 12:07:18 Mon, 14 Mar 2022
     // Serial.println(rtc.getTimeDate(true));  //  (String) 00:00:03 Monday, 14-March-2022
     // Serial.println(rtc.getDayDate());       //  (String) 2022-03-14
      Serial.print("Tarih: ");
      Serial.println(rtc.getDayDate() );       //  (String) 2022-03-14
      Serial.println("-----------------"); 
      //Serial.println(rtc.getSecond());        //  (String)     30    (00-59)
      //Serial.println(rtc.getMinute());        //  (String)     58    (00-59)
      //Serial.println(rtc.getHour());          //  (String)     12    (01-12)
      //Serial.println(rtc.getHour(true));      //  (String)     00    (00-23)
      //Serial.println(rtc.getDay());           //  (String)     14    (1-31)
      //Serial.println(rtc.getMonth());         //  (String)     Mar
      //Serial.println(rtc.getMonth(true));     //  (String)     March
      //Serial.println(rtc.getYear());          //  (String)     2022
      //Serial.println(rtc.getDayofWeek());     //  (String)     Mon
      //Serial.println(rtc.getDayofWeek(true)); //  (String)     Monday
      //Serial.println(rtc.getAmPm());          //  (String)  pm
      //Serial.println(rtc.getAmPm(true));      //  (String)  PM
      //Serial.println(""); 
      //Serial.println(rtc.getEpoch());         //  (long)    1647216003
      //Serial.println(rtc.second);             //  (int)     30    (0-59)
      //Serial.println(rtc.minute);             //  (int)     58    (0-59)
      //Serial.println(rtc.hour);               //  (int)     0     (0-23)
      //Serial.println(rtc.dayOfMonth);         //  (int)     14    (1-31)
      //Serial.println(rtc.month);              //  (int)     3     (1-12)
      //Serial.println(rtc.year + 2000);        //  (int)     2022
      //Serial.println(rtc.dayOfWeek);          //  (int)     1     (0-6)
      //Serial.println(rtc.DayofYear);          //  (int)     73    (0-365)
      //Serial.println(""); 
} 
////////////////////////////////////////////////////////
// Rounded HH:mm format
char * hoursToString(double h, char *str)
{
  int m = int(round(h * 60));
  int hr = (m / 60) % 24;
  int mn = m % 60;

  str[0] = (hr / 10) % 10 + '0';
  str[1] = (hr % 10) + '0';
  str[2] = ':';
  str[3] = (mn / 10) % 10 + '0';
  str[4] = (mn % 10) + '0';
  str[5] = '\0';
  return str;
}
/////////////////////////////////////////////////////
GND5VSDASCLSQWRTCDS1307+