mirror of https://github.com/sqfmi/Watchy.git
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
parent
8c9d100b5b
commit
600e4be7d8
|
@ -10,7 +10,7 @@ RTC_DATA_ATTR bool WIFI_CONFIGURED;
|
||||||
RTC_DATA_ATTR bool BLE_CONFIGURED;
|
RTC_DATA_ATTR bool BLE_CONFIGURED;
|
||||||
RTC_DATA_ATTR weatherData currentWeather;
|
RTC_DATA_ATTR weatherData currentWeather;
|
||||||
RTC_DATA_ATTR int weatherIntervalCounter = WEATHER_UPDATE_INTERVAL;
|
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)
|
String getValue(String data, char separator, int index)
|
||||||
{
|
{
|
||||||
|
@ -75,47 +75,41 @@ void Watchy::init(String datetime){
|
||||||
showWatchFace(false); //full update on reset
|
showWatchFace(false); //full update on reset
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
syncNtpTime();
|
|
||||||
deepSleep();
|
deepSleep();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Watchy::syncNtpTime(){
|
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
|
// 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){
|
if(ntpSyncTimeCounter >= NTP_TIME_SYNC_INTERVAL && NTP_TIME_SYNC_INTERVAL != 0){
|
||||||
//only run this at midnight,8am and 4pm
|
//only run this at midnight,8am and 4pm
|
||||||
// three times a day to correct time drift.
|
// three times a day to correct time drift.
|
||||||
|
|
||||||
//make sure WiFi is connected
|
struct tm timeinfo;
|
||||||
if(connectWiFi()){
|
|
||||||
// wifi connected so proceed to get NTP time
|
|
||||||
|
|
||||||
//get NTP Time
|
//get NTP Time
|
||||||
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
|
configTime(GMT_OFFSET_SEC, DAYLIGHT_OFFSET_SEC, NTP_SERVER);
|
||||||
getLocalTime(&timeinfo);
|
getLocalTime(&timeinfo);
|
||||||
|
|
||||||
// convert NTP time into proper format
|
// convert NTP time into proper format
|
||||||
tmElements_t tm;
|
tmElements_t tm;
|
||||||
tm.Month = timeinfo.tm_mon + 1;// 0-11 based month so we have to add 1
|
tm.Month = timeinfo.tm_mon + 1;// 0-11 based month so we have to add 1
|
||||||
tm.Day = timeinfo.tm_mday;
|
tm.Day = timeinfo.tm_mday;
|
||||||
tm.Year = timeinfo.tm_year + 1900 - YEAR_OFFSET;//offset from 1970, since year is stored in uint8_t
|
tm.Year = timeinfo.tm_year + 1900 - YEAR_OFFSET;//offset from 1970, since year is stored in uint8_t
|
||||||
tm.Hour = timeinfo.tm_hour;
|
tm.Hour = timeinfo.tm_hour;
|
||||||
tm.Minute = timeinfo.tm_min;
|
tm.Minute = timeinfo.tm_min;
|
||||||
tm.Second = timeinfo.tm_sec;
|
tm.Second = timeinfo.tm_sec;
|
||||||
time_t t = makeTime(tm);
|
time_t t = makeTime(tm);
|
||||||
|
|
||||||
//set the RTC time to the NTP time
|
ntpSyncTimeCounter = 0;
|
||||||
RTC.set(t);
|
|
||||||
|
|
||||||
// shut down the radio to save power
|
//set the RTC time to the NTP time
|
||||||
WiFi.mode(WIFI_OFF);
|
RTC.set(t);
|
||||||
btStop();
|
|
||||||
}
|
// 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 error
|
||||||
}
|
}
|
||||||
http.end();
|
http.end();
|
||||||
|
|
||||||
|
// syncing time when checking for weatcher and connected to wifi
|
||||||
|
syncNtpTime();
|
||||||
|
|
||||||
//turn off radios
|
//turn off radios
|
||||||
WiFi.mode(WIFI_OFF);
|
WiFi.mode(WIFI_OFF);
|
||||||
btStop();
|
btStop();
|
||||||
|
|
|
@ -48,7 +48,11 @@
|
||||||
#define SET_MONTH 3
|
#define SET_MONTH 3
|
||||||
#define SET_DAY 4
|
#define SET_DAY 4
|
||||||
#define YEAR_OFFSET 1970
|
#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
|
//BLE OTA
|
||||||
#define BLE_DEVICE_NAME "Watchy BLE OTA"
|
#define BLE_DEVICE_NAME "Watchy BLE OTA"
|
||||||
#define WATCHFACE_NAME "Watchy 7 Segment"
|
#define WATCHFACE_NAME "Watchy 7 Segment"
|
||||||
|
|
Loading…
Reference in New Issue