Subsecond accuracy NTP

This commit is contained in:
cschwinne 2021-05-24 19:05:34 +02:00
parent b455f432d5
commit 852f758be3
4 changed files with 17 additions and 10 deletions

View File

@ -481,6 +481,7 @@ build_flags = ${common.build_flags_esp8266}
board = esp32dev board = esp32dev
platform = espressif32@3.2 platform = espressif32@3.2
upload_speed = 921600 upload_speed = 921600
upload_port = COM8
lib_deps = ${env.lib_deps} lib_deps = ${env.lib_deps}
TFT_eSPI TFT_eSPI
build_flags = ${common.build_flags_esp32} -D WLED_DISABLE_BROWNOUT_DET -D WLED_DISABLE_INFRARED build_flags = ${common.build_flags_esp32} -D WLED_DISABLE_BROWNOUT_DET -D WLED_DISABLE_INFRARED

View File

@ -40,9 +40,10 @@ class ElekstubeIPSUsermod : public Usermod {
void setup() { void setup() {
tfts.begin(); tfts.begin();
tfts.fillScreen(TFT_BLACK); tfts.fillScreen(TFT_BLACK);
tfts.setTextColor(TFT_WHITE, TFT_BLACK);
tfts.setCursor(0, 100, 2); for (int8_t i = 5; i >= 0; i--) {
tfts.println("<STARTUP>"); tfts.setDigit(i, 255, TFTs::force); //turn all off
}
} }
void loop() { void loop() {

View File

@ -192,16 +192,21 @@ bool checkNTPResponse()
Toki::Time arrived = toki.fromNTP(pbuf + 32); Toki::Time arrived = toki.fromNTP(pbuf + 32);
Toki::Time departed = toki.fromNTP(pbuf + 40); Toki::Time departed = toki.fromNTP(pbuf + 40);
//basic half roundtrip estimation //basic half roundtrip estimation
uint32_t offset = (ntpPacketReceivedTime - ntpPacketSentTime - toki.msDifference(arrived, departed)) >> 1; uint32_t serverDelay = toki.msDifference(arrived, departed);
offset += millis() - ntpPacketReceivedTime +1; uint32_t offset = (ntpPacketReceivedTime - ntpPacketSentTime - serverDelay) >> 1;
toki.printTime(departed);
toki.adjust(departed, offset); toki.adjust(departed, offset);
toki.setTime(departed); toki.setTime(departed);
Serial.print("Arrived: ");
toki.printTime(arrived);
Serial.print("Time: ");
toki.printTime(departed);
Serial.print("Roundtrip: "); Serial.print("Roundtrip: ");
Serial.println(ntpPacketReceivedTime - ntpPacketSentTime); Serial.println(ntpPacketReceivedTime - ntpPacketSentTime);
Serial.print("Offset: "); Serial.print("Offset: ");
Serial.println(offset); Serial.println(offset);
Serial.print("Time: "); Serial.print("Serverdelay: ");
toki.printTime(toki.getTime()); Serial.println(serverDelay);
DEBUG_PRINT(F("Unix time = ")); DEBUG_PRINT(F("Unix time = "));
uint32_t epoch = toki.second(); uint32_t epoch = toki.second();

View File

@ -62,7 +62,7 @@ class Toki {
if (!unix) return {0,0}; if (!unix) return {0,0};
unix -= YEARS_70; //NTP begins 1900, Unix 1970 unix -= YEARS_70; //NTP begins 1900, Unix 1970
unsigned long frac = word(timestamp[5], timestamp[6]); //65536ths of a second unsigned long frac = word(timestamp[4], timestamp[5]); //65536ths of a second
frac = (frac*1000) >> 16; //convert to ms frac = (frac*1000) >> 16; //convert to ms
return {unix, (uint16_t)frac}; return {unix, (uint16_t)frac};
} }
@ -96,11 +96,11 @@ class Toki {
void adjust(Time&t, int32_t offset) { void adjust(Time&t, int32_t offset) {
int32_t secs = offset /1000; int32_t secs = offset /1000;
int32_t ms = offset - secs*1000; int32_t ms = offset - secs*1000;
t.sec += offset /1000; t.sec += secs;
int32_t nms = t.ms + ms; int32_t nms = t.ms + ms;
if (nms > 1000) {nms -= 1000; t.sec++;} if (nms > 1000) {nms -= 1000; t.sec++;}
if (nms < 0) {nms += 1000; t.sec--;} if (nms < 0) {nms += 1000; t.sec--;}
t.ms += nms; t.ms = nms;
} }
Time getTime() { Time getTime() {