From 9e1c9c7f599364b99aeaa5c98ad7b8a942981787 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Fri, 23 Jul 2021 19:10:41 +0200 Subject: [PATCH] improve lora time sync accuracy --- include/timesync.h | 2 +- src/timesync.cpp | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/timesync.h b/include/timesync.h index 241c5354..a6db2a3d 100644 --- a/include/timesync.h +++ b/include/timesync.h @@ -6,7 +6,7 @@ #include "timekeeper.h" #define TIME_SYNC_FRAME_LENGTH 6 // timeserver answer frame length [bytes] -#define TIME_SYNC_FIXUP 16 // compensation for processing time [milliseconds] +#define TIME_SYNC_FIXUP 25 // compensation for processing time [milliseconds] #define TIME_SYNC_MAX_SEQNO 0xfe // threshold for wrap around time_sync_seqNo #define TIME_SYNC_END_FLAG (TIME_SYNC_MAX_SEQNO + 1) // end of handshake marker #define GPS_UTC_DIFF 315964800UL // seconds diff between gps and utc epoch diff --git a/src/timesync.cpp b/src/timesync.cpp index 2a25d4d0..a09bdd04 100644 --- a/src/timesync.cpp +++ b/src/timesync.cpp @@ -130,18 +130,18 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) { // calculate average time offset over the summed up difference time_offset_ms /= TIME_SYNC_SAMPLES; + + // add milliseconds from latest gateway time, and apply a compensation + // constant for processing times on node and gateway, strip full seconds + time_offset_ms += timesync_timestamp[sample_idx - 1][gwtime_msec]; + time_offset_ms -= TIME_SYNC_FIXUP; + time_offset_ms %= 1000; // take latest timestamp received from gateway // and add time difference rounded to whole seconds time_offset_sec = timesync_timestamp[sample_idx - 1][gwtime_sec]; time_offset_sec += time_offset_ms / 1000; - // add milliseconds from latest gateway time, and apply a compensation - // constant for processing times on node and gateway, strip full seconds - time_offset_ms += timesync_timestamp[sample_idx - 1][gwtime_msec]; - time_offset_ms += TIME_SYNC_FIXUP; - time_offset_ms %= 1000; - ESP_LOGD(TAG, "LORA date/time: %s", myTZ.dateTime(time_offset_sec, "d.M Y H:i:s.v T").c_str());