Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gps driven time synch #648

Merged
merged 3 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/gpsread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ void gps_loop(void *pvParameters) {
delay(2); // 2ms delay according L76 datasheet
}
#endif

// if time hasn't been synchronised yet, and we have a valid GPS time,
// update time from GPS.
if (timeSource == _unsynced && gpstime.isUpdated() && gpstime.isValid()) {
calibrateTime();
}

} // if

// show NMEA data in verbose mode, useful for debugging GPS, bu tvery noisy
Expand Down
5 changes: 0 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,6 @@ void setup() {
#warning you did not specify a time source, time will not be synched
#endif

// initialize gps time
#if (HAS_GPS)
get_gpstime();
#endif

#if (defined HAS_IF482 || defined HAS_DCF77)
ESP_LOGI(TAG, "Starting Clock Controller...");
clock_init();
Expand Down
13 changes: 7 additions & 6 deletions src/timekeeper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ Ticker timesyncer;
void timeSync() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); }

void calibrateTime(void) {

ESP_LOGD(TAG, "[%0.3f] calibrateTime, timeSource == %d", millis() / 1000.0,
timeSource);
time_t t = 0;
uint16_t t_msec = 0;

// kick off asychronous lora timesync if we have
#if (HAS_LORA) && (TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN)
#if (HAS_LORA) && ((TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN))
timesync_request();
#endif

Expand All @@ -43,17 +44,17 @@ void calibrateTime(void) {
// has RTC -> fallback to RTC time
#ifdef HAS_RTC
t = get_rtctime();
timeSource = _rtc;
// set time from RTC
setMyTime((uint32_t)t, t_msec, _rtc);
#endif

// no RTC -> fallback to GPS time
#if (HAS_GPS)
t = get_gpstime(&t_msec);
timeSource = _gps;
// set time from GPS - method will check if time is valid
setMyTime((uint32_t)t, t_msec, _gps);
#endif

setMyTime((uint32_t)t, t_msec, timeSource); // set time

} // fallback

else
Expand Down