177 lines
5.0 KiB
C++
177 lines
5.0 KiB
C++
|
|
// CONNECTIONS:
|
|
// DS1307 SDA --> SDA
|
|
// DS1307 SCL --> SCL
|
|
// DS1307 VCC --> 5v
|
|
// DS1307 GND --> GND
|
|
|
|
/* for software wire use below
|
|
#include <SoftwareWire.h> // must be included here so that Arduino library object file references work
|
|
#include <RtcDS1307.h>
|
|
|
|
SoftwareWire myWire(SDA, SCL);
|
|
RtcDS1307<SoftwareWire> Rtc(myWire);
|
|
for software wire use above */
|
|
|
|
/* for normal hardware wire use below */
|
|
#include <Wire.h> // must be included here so that Arduino library object file references work
|
|
#include <RtcDS1307.h>
|
|
RtcDS1307<TwoWire> Rtc(Wire);
|
|
/* for normal hardware wire use above */
|
|
|
|
// handy routine to return true if there was an error
|
|
// but it will also print out an error message with the given topic
|
|
bool wasError(const char* errorTopic = "")
|
|
{
|
|
uint8_t error = Rtc.LastError();
|
|
if (error != 0)
|
|
{
|
|
// we have a communications error
|
|
// see https://www.arduino.cc/reference/en/language/functions/communication/wire/endtransmission/
|
|
// for what the number means
|
|
Serial.print("[");
|
|
Serial.print(errorTopic);
|
|
Serial.print("] WIRE communications error (");
|
|
Serial.print(error);
|
|
Serial.print(") : ");
|
|
|
|
switch (error)
|
|
{
|
|
case Rtc_Wire_Error_None:
|
|
Serial.println("(none?!)");
|
|
break;
|
|
case Rtc_Wire_Error_TxBufferOverflow:
|
|
Serial.println("transmit buffer overflow");
|
|
break;
|
|
case Rtc_Wire_Error_NoAddressableDevice:
|
|
Serial.println("no device responded");
|
|
break;
|
|
case Rtc_Wire_Error_UnsupportedRequest:
|
|
Serial.println("device doesn't support request");
|
|
break;
|
|
case Rtc_Wire_Error_Unspecific:
|
|
Serial.println("unspecified error");
|
|
break;
|
|
case Rtc_Wire_Error_CommunicationTimeout:
|
|
Serial.println("communications timed out");
|
|
break;
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
void setup ()
|
|
{
|
|
Serial.begin(115200);
|
|
|
|
Serial.print("compiled: ");
|
|
Serial.print(__DATE__);
|
|
Serial.println(__TIME__);
|
|
|
|
//--------RTC SETUP ------------
|
|
// if you are using ESP-01 then uncomment the line below to reset the pins to
|
|
// the available pins for SDA, SCL
|
|
// Rtc.Begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
|
|
|
|
Rtc.Begin();
|
|
#if defined(WIRE_HAS_TIMEOUT)
|
|
Wire.setWireTimeout(3000 /* us */, true /* reset_on_timeout */);
|
|
#endif
|
|
|
|
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
|
|
printDateTime(compiled);
|
|
Serial.println();
|
|
|
|
if (!Rtc.IsDateTimeValid())
|
|
{
|
|
if (!wasError("setup IsDateTimeValid"))
|
|
{
|
|
// 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!");
|
|
// following line sets the RTC to the date & time this sketch was compiled
|
|
// it will also reset the valid flag internally unless the Rtc device is
|
|
// having an issue
|
|
|
|
Rtc.SetDateTime(compiled);
|
|
}
|
|
}
|
|
|
|
if (!Rtc.GetIsRunning())
|
|
{
|
|
if (!wasError("setup GetIsRunning"))
|
|
{
|
|
Serial.println("RTC was not actively running, starting now");
|
|
Rtc.SetIsRunning(true);
|
|
}
|
|
}
|
|
|
|
RtcDateTime now = Rtc.GetDateTime();
|
|
if (!wasError("setup GetDateTime"))
|
|
{
|
|
if (now < compiled)
|
|
{
|
|
Serial.println("RTC is older than compile time, updating DateTime");
|
|
Rtc.SetDateTime(compiled);
|
|
}
|
|
else if (now > compiled)
|
|
{
|
|
Serial.println("RTC is newer than compile time, this is expected");
|
|
}
|
|
else if (now == compiled)
|
|
{
|
|
Serial.println("RTC is the same as compile time, while not expected all is still fine");
|
|
}
|
|
}
|
|
|
|
// never assume the Rtc was last configured by you, so
|
|
// just clear them to your needed state
|
|
Rtc.SetSquareWavePin(DS1307SquareWaveOut_Low);
|
|
wasError("setup SetSquareWavePin");
|
|
}
|
|
|
|
void loop ()
|
|
{
|
|
if (!Rtc.IsDateTimeValid())
|
|
{
|
|
if (!wasError("loop IsDateTimeValid"))
|
|
{
|
|
// 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!");
|
|
}
|
|
}
|
|
|
|
RtcDateTime now = Rtc.GetDateTime();
|
|
if (!wasError("loop GetDateTime"))
|
|
{
|
|
printDateTime(now);
|
|
Serial.println();
|
|
}
|
|
|
|
delay(10000); // ten seconds
|
|
}
|
|
|
|
#define countof(a) (sizeof(a) / sizeof(a[0]))
|
|
|
|
void printDateTime(const RtcDateTime& dt)
|
|
{
|
|
char datestring[26];
|
|
|
|
snprintf_P(datestring,
|
|
countof(datestring),
|
|
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
|
|
dt.Month(),
|
|
dt.Day(),
|
|
dt.Year(),
|
|
dt.Hour(),
|
|
dt.Minute(),
|
|
dt.Second() );
|
|
Serial.print(datestring);
|
|
}
|
|
|