changes for ntp time syncing

moved all related constants to config.h

syncing time only when weather is checked and the time sync interval matches.

also to note: NTP_TIME_SYNC_INTERVAL is now in minutes.
pull/70/head
etwasmitbaum 2021-06-25 22:35:22 +02:00
parent 8c9d100b5b
commit 600e4be7d8
2 changed files with 35 additions and 33 deletions

View File

@ -10,7 +10,7 @@ RTC_DATA_ATTR bool WIFI_CONFIGURED;
RTC_DATA_ATTR bool BLE_CONFIGURED;
RTC_DATA_ATTR weatherData currentWeather;
RTC_DATA_ATTR int weatherIntervalCounter = WEATHER_UPDATE_INTERVAL;
RTC_DATA_ATTR int ntpTimeSyncInterval = NTP_TIME_SYNC_INTERVAL;
RTC_DARA_ATTR int ntpSyncTimeCounter = NTP_TIME_SYNC_INTERVAL;
String getValue(String data, char separator, int index)
{
@ -75,47 +75,41 @@ void Watchy::init(String datetime){
showWatchFace(false); //full update on reset
break;
}
syncNtpTime();
deepSleep();
}
void Watchy::syncNtpTime(){
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = -21600; // set time zone to central standard time
// ie UTC -6 * 60 * 60 = -21600
const int daylightOffset_sec = 3600;// if observing Daylight saving time 3600 otherwise 0
struct tm timeinfo;
// Watchy updates every minute but we really only need to sync a few times a day
if(currentTime.Hour % ntpTimeSyncInterval == 0 && currentTime.Minute == 0 && ntpTimeSyncInterval != 0){
//only run this at midnight,8am and 4pm
// three times a day to correct time drift.
if(ntpSyncTimeCounter >= NTP_TIME_SYNC_INTERVAL && NTP_TIME_SYNC_INTERVAL != 0){
//only run this at midnight,8am and 4pm
// three times a day to correct time drift.
//make sure WiFi is connected
if(connectWiFi()){
// wifi connected so proceed to get NTP time
struct tm timeinfo;
//get NTP Time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
getLocalTime(&timeinfo);
//get NTP Time
configTime(GMT_OFFSET_SEC, DAYLIGHT_OFFSET_SEC, NTP_SERVER);
getLocalTime(&timeinfo);
// convert NTP time into proper format
tmElements_t tm;
tm.Month = timeinfo.tm_mon + 1;// 0-11 based month so we have to add 1
tm.Day = timeinfo.tm_mday;
tm.Year = timeinfo.tm_year + 1900 - YEAR_OFFSET;//offset from 1970, since year is stored in uint8_t
tm.Hour = timeinfo.tm_hour;
tm.Minute = timeinfo.tm_min;
tm.Second = timeinfo.tm_sec;
time_t t = makeTime(tm);
// convert NTP time into proper format
tmElements_t tm;
tm.Month = timeinfo.tm_mon + 1;// 0-11 based month so we have to add 1
tm.Day = timeinfo.tm_mday;
tm.Year = timeinfo.tm_year + 1900 - YEAR_OFFSET;//offset from 1970, since year is stored in uint8_t
tm.Hour = timeinfo.tm_hour;
tm.Minute = timeinfo.tm_min;
tm.Second = timeinfo.tm_sec;
time_t t = makeTime(tm);
//set the RTC time to the NTP time
RTC.set(t);
ntpSyncTimeCounter = 0;
// shut down the radio to save power
WiFi.mode(WIFI_OFF);
btStop();
}
//set the RTC time to the NTP time
RTC.set(t);
// shut down the radio to save power
WiFi.mode(WIFI_OFF);
btStop();
} else {
ntpSyncTimeCounter = ntpSyncTimeCounter + WEATHER_UPDATE_INTERVAL;
}
}
@ -672,6 +666,10 @@ weatherData Watchy::getWeatherData(){
//http error
}
http.end();
// syncing time when checking for weatcher and connected to wifi
syncNtpTime();
//turn off radios
WiFi.mode(WIFI_OFF);
btStop();

View File

@ -48,7 +48,11 @@
#define SET_MONTH 3
#define SET_DAY 4
#define YEAR_OFFSET 1970
#define NTP_TIME_SYNC_INTERVAL 8 //hours 0 = update never
#define NTP_TIME_SYNC_INTERVAL 480 // minutes, will have no effect if it's lower than WEATHER_UPDATE_INTERVAL 0 = update never
#define GMT_OFFSET_SEC -21600 // set time zone to central standard time
// ie UTC -6 * 60 * 60 = -21600
#define DAYLIGHT_OFFSET_SEC 3600 // if observing Daylight saving time 3600 otherwise 0
#define NTP_SERVER "pool.ntp.org"
//BLE OTA
#define BLE_DEVICE_NAME "Watchy BLE OTA"
#define WATCHFACE_NAME "Watchy 7 Segment"