|
|
|
@ -31,6 +31,7 @@ time_t now; // Current time in UTC - number of seconds
|
|
|
|
|
struct tm timeinfo; // Structure containing time elements
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
bool validTimeFlag = false; // Indicate whether we have reliable time
|
|
|
|
|
bool validRtcFlag = false; // Is RTC date and time valid?
|
|
|
|
|
time_t lastNTPFetchSuccess = 0; // Last time that a successful NTP fetch was made
|
|
|
|
|
bool isDST; // Keeps track of Daylight Savings Time vs Standard Time
|
|
|
|
|
long slewSecs = 0; // When time is set this is the number of seconds the time changes
|
|
|
|
@ -42,7 +43,61 @@ time_t lastDstCheck = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sendTimeLoRa();
|
|
|
|
|
void printTime();
|
|
|
|
|
esp_err_t sendTimeESPNow();
|
|
|
|
|
bool setTime(time_t);
|
|
|
|
|
|
|
|
|
|
#ifdef USE_RTC_DS3231
|
|
|
|
|
#include <RtcDS3231.h>
|
|
|
|
|
RtcDS3231<TwoWire> rtc(Wire);
|
|
|
|
|
#elif defined(USE_RTC_DS1307)
|
|
|
|
|
#include <RtcDS3231.h>
|
|
|
|
|
RtcDS3231<TwoWire> rtc(Wire);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(USE_RTC_DS3231) || defined(USE_RTC_DS1307)
|
|
|
|
|
void begin_rtc() {
|
|
|
|
|
DBG("Starting RTC");
|
|
|
|
|
rtc.Begin();
|
|
|
|
|
|
|
|
|
|
// Is Date and time valid?
|
|
|
|
|
if(!rtc.IsDateTimeValid()) {
|
|
|
|
|
uint8_t err = rtc.LastError();
|
|
|
|
|
if(err != 0) {
|
|
|
|
|
// 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 error: Date and Time not valid! Err: " + String(err));
|
|
|
|
|
validRtcFlag = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
validRtcFlag = true;
|
|
|
|
|
}
|
|
|
|
|
// Is RTC running?
|
|
|
|
|
if(!rtc.GetIsRunning()) {
|
|
|
|
|
uint8_t err = rtc.LastError();
|
|
|
|
|
if(err != 0) {
|
|
|
|
|
DBG("RTC was not actively running, starting now. Err: " + String(err));
|
|
|
|
|
rtc.SetIsRunning(true);
|
|
|
|
|
validRtcFlag = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(validRtcFlag) {
|
|
|
|
|
// Set date and time on the system
|
|
|
|
|
DBG("Using Date and Time from RTC.");
|
|
|
|
|
setTime(rtc.GetDateTime().Unix32Time());
|
|
|
|
|
printTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// never assume the Rtc was last configured by you, so
|
|
|
|
|
// just clear them to your needed state
|
|
|
|
|
rtc.Enable32kHzPin(false);
|
|
|
|
|
rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
|
|
|
|
|
}
|
|
|
|
|
#endif // USE_RTC_DS3231 || USE_RTC_DS1307
|
|
|
|
|
|
|
|
|
|
bool validTime() {
|
|
|
|
|
if(now < 1672000000 || (millis() - lastNTPFetchSuccess > (24*60*60*1000))) {
|
|
|
|
@ -80,7 +135,7 @@ void printTime() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void checkDST() {
|
|
|
|
|
if(validTime() && (time(NULL) - lastDstCheck > 5)) {
|
|
|
|
|
if(validTime() && ((time(NULL) - lastDstCheck > 5) || lastDstCheck == 0)) {
|
|
|
|
|
lastDstCheck = time(NULL);
|
|
|
|
|
int dstFlag = -1;
|
|
|
|
|
localtime_r(&now, &timeinfo);
|
|
|
|
@ -212,9 +267,14 @@ bool setTime(time_t currentTime) {
|
|
|
|
|
// Check for DST/STD time and adjust accordingly
|
|
|
|
|
checkDST();
|
|
|
|
|
tv.tv_sec = now;
|
|
|
|
|
#if defined(ESP32) || defined(ESP8266) // settimeofday may only work with Espressif chips
|
|
|
|
|
#if defined(ESP32) || defined(ESP8266) // settimeofday may only work with Espressif chips
|
|
|
|
|
settimeofday(&tv,NULL); // set the RTC time
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(USE_RTC_DS3231) || defined(USE_RTC_DS1307)
|
|
|
|
|
RtcDateTime rtcNow;
|
|
|
|
|
rtcNow.InitWithUnix32Time(now);
|
|
|
|
|
rtc.SetDateTime(rtcNow);
|
|
|
|
|
#endif
|
|
|
|
|
// Uncomment below to send time and slew rate to the MQTT server
|
|
|
|
|
// loadFDRS(now, TIME_T, 111);
|
|
|
|
|
// loadFDRS(slewSecs, TIME_T, 111);
|
|
|
|
|