// CONNECTIONS:
// DS1302 CLK/SCLK --> 5
// DS1302 DAT/IO --> 4
// DS1302 RST/CE --> 2
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND
 
#include <ThreeWire.h>  
#include <RtcDS1302.h>
 
Treswire mywire (4,5,2); // io, sclk, ce // correspondiente al número io de esp32
RtcDS1302<ThreeWire> Rtc(myWire);
 
void setup () 
{
    Serial.begin(57600);
    
    Serial.print("compiled: ");
         Serial.print (__ date__); // Imprima la fecha del sistema
         Serial.println (__ Time__); // Imprima el tiempo del sistema
 
         Rtc.begin (); // Use el tubo para declarar para inicializar RTC
 
         RtcdateTime compilado = rtcdateTime (__ date__, __time__); // deja que el tiempo del sistema declare una estructura de tiempo
         PrintDateTime (compilado); // tiempo de impresión
    Serial.println();
 
         if (! rtc.isdateTimeValid ()) // Lea la hora RTC, si no se lee, la batería está fuera de energía
    {
        // Common Causes:
        //    1) first time you ran and the device wasn't running yet
        //    2) the battery on the device is low or even missing
 
        Serial.println("RTC lost confidence in the DateTime!");
                 Rtc.setDateTime (compilado); // Establecer el tiempo del sistema es la hora actual
    }
 
         if (rtc.getisWriteProTected ()) //
    {
        Serial.println("RTC was write protected, enabling writing now");
        Rtc.SetIsWriteProtected(false);
    }
 
         if (! rtc.getisrunning ()) // verifique si es legible
    {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }
 
         RtcdateTime ahora = rtc.getDateTime (); // Obtenga el tiempo de RTC
         if (ahora <compilado) // Si el tiempo lee el tiempo del sistema, luego reinicie el tiempo RTC
    {
        Serial.println("RTC is older than compile time!  (Updating DateTime)");
        Rtc.SetDateTime(compiled);
    }
         más si (ahora> compilado) // Si el tiempo que lee es posterior al tiempo del sistema, luego restablezca el tiempo RTC
    {
        Serial.println("RTC is newer than compile time. (this is expected)");
    }
    else if (now == compiled) 
    {
        Serial.println("RTC is the same as compile time! (not expected but all is fine)");
    }
}
 
void loop () 
{
         RtcdateTime ahora = rtc.getDateTime (); // función central, lea el tiempo RTC actual
 
    printDateTime(now);
    Serial.println();
 
    if (!now.IsValid())
    {
        // Common Causes:
        //    1) the battery on the device is low or even missing and the power line was disconnected
        Serial.println("RTC lost confidence in the DateTime!");
    }
 
         retraso (10000); // diez segundos // retraso 10s 10s
}
 
#define countof(a) (sizeof(a) / sizeof(a[0]))
 
 Void printDateTime (const rtcdateTime & dt) // Una función que formatea la fecha y la hora al formatear la fecha y hora impresas en el puerto serie
{
    char datestring[20];
 
         Snprintf_p (dataTring, // función de formato de impresión de AVR
            countof(datestring),
                         PSTR ("%04U/%02U/%02U%02U:%02U:%02U"), similar
            dt.Year(),
            dt.Month(),
            dt.Day(),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
    Serial.print(datestring);
}
GND5VSDASCLSQWRTCDS1307+