diff --git a/src/gpsread.cpp b/src/gpsread.cpp index 45f1b65d..650c5866 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -21,6 +21,9 @@ static uint16_t nmea_txDelay_ms = static uint16_t nmea_txDelay_ms = 0; #endif +// did the last packet contain a valid time? +bool hasValidTime = false; + // initialize and configure GPS int gps_init(void) { @@ -159,6 +162,18 @@ void gps_loop(void *pvParameters) { delay(2); // 2ms delay according L76 datasheet } #endif + +#if !TIME_SYNC_LORASERVER && !TIME_SYNC_LORAWAN + if (!hasValidTime) { + if (gpstime.isUpdated() && gpstime.isValid()) { + hasValidTime = true; + if (timeSource == _unsynced) { + calibrateTime(); + } + } + } +#endif + } // if // show NMEA data in verbose mode, useful for debugging GPS, bu tvery noisy diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index bc9e52b5..01777be7 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -26,7 +26,8 @@ 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; @@ -49,7 +50,9 @@ void calibrateTime(void) { // no RTC -> fallback to GPS time #if (HAS_GPS) t = get_gpstime(&t_msec); - timeSource = _gps; + if (t) { + timeSource = _gps; + } #endif setMyTime((uint32_t)t, t_msec, timeSource); // set time