diff --git a/src/gpsread.cpp b/src/gpsread.cpp index 8b7f0d0a..2036b611 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -81,21 +81,16 @@ time_t get_gpstime(void) { time_t t = 0; - for (uint8_t i = 0; i <= 9; i++) { // trying to get a recent time.age + if ((gps.time.age() < gpsDelay_ms) && (gps.time.isValid()) && + (gps.date.isValid())) { - if ((gps.time.age() < gpsDelay_ms) && (gps.time.isValid()) && - (gps.date.isValid())) { + ESP_LOGD(TAG, "GPS time age: %dms, is valid: %s, second: %d", + gps.time.age(), + (gps.time.isValid() && gps.date.isValid()) ? "yes" : "no", + gps.time.second()); - ESP_LOGD(TAG, "GPS time age: %dms, is valid: %s, second: %d, trials: %d", - gps.time.age(), - (gps.time.isValid() && gps.date.isValid()) ? "yes" : "no", - gps.time.second(), i); - - t = tmConvert(gps.date.year(), gps.date.month(), gps.date.day(), - gps.time.hour(), gps.time.minute(), gps.time.second()); - - break; // exit for - } + t = tmConvert(gps.date.year(), gps.date.month(), gps.date.day(), + gps.time.hour(), gps.time.minute(), gps.time.second()); } return timeIsValid(t); } // get_gpstime() diff --git a/src/lorawan.cpp b/src/lorawan.cpp index e9efe4ca..2c0fec3b 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -465,9 +465,9 @@ void user_request_network_time_callback(void *pVoidUserUTCTime, // Update system time with time read from the network if (timeIsValid(*pUserUTCTime)) { - xSemaphoreTake(TimePulse, pdMS_TO_TICKS(1000)); // wait for pps - setTime(*pUserUTCTime + 1); + setTime(*pUserUTCTime); timeSource = _lora; + timesyncer.attach(TIME_SYNC_INTERVAL * 60, timeSync); // regular repeat ESP_LOGI(TAG, "Received recent time from LoRa"); } else ESP_LOGI(TAG, "Invalid time received from LoRa"); diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 83a3bd8c..3575df28 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -21,6 +21,7 @@ time_t timeProvider(void) { set_rtctime(t); // calibrate RTC #endif timeSource = _gps; + timesyncer.attach(TIME_SYNC_INTERVAL * 60, timeSync); // regular repeat return t; } #endif @@ -30,6 +31,7 @@ time_t timeProvider(void) { t = get_rtctime(); if (t) { timeSource = _rtc; + timesyncer.attach(60, timeSync); // short retry } #endif @@ -38,8 +40,10 @@ time_t timeProvider(void) { LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime); #endif - if (!t) + if (!t) { timeSource = _unsynced; + timesyncer.attach(60, timeSync); // short retry + } return t; @@ -78,7 +82,7 @@ uint8_t timepulse_init() { #else // use ESP32 hardware timer as time base with adjustable frequency - ppsIRQ = timerBegin(1, 8000, true); // set 80 MHz prescaler to 1/10000 sec + ppsIRQ = timerBegin(1, 8000, true); // set 80 MHz prescaler to 1/10000 sec timerAlarmWrite(ppsIRQ, 10000, true); // 1000ms ESP_LOGI(TAG, "Timepulse: internal (ESP32 hardware timer)"); return 1; // success