Getting detailed NTP Diagnostic data

D.h.zh
Posts: 2
Joined: Mon Jun 10, 2024 9:40 am

Getting detailed NTP Diagnostic data

Postby D.h.zh » Tue Jun 11, 2024 4:01 am

I have recently been looking into syncing time on an esp32, and managed to follow some guides to get the esp32 to sync with NTP using configTime().
However, as I am looking to get milisecond accuracy timestamps, I was wondering if there was any way to get the detailed diagnostic data, e.g. Jitter, offset and delay. After searching for a while, I have tried looking into esp_netif and Iwif sntp, but I could not find anything regarding NTP diagnostic, so I am not sure if I am looking in the right place.
Any advice would be greatly appreciated!

lbernstone
Posts: 791
Joined: Mon Jul 22, 2019 3:20 pm

Re: Getting detailed NTP Diagnostic data

Postby lbernstone » Tue Jun 11, 2024 6:42 am

Client side or server? I have an ntpd (although it isn't very good) in my repos as well.

Code: Select all

#include <WiFiUdp.h>

timeval sntpTime() {
  WiFiUDP udp;
  uint64_t sec = 0;
  uint64_t fractional = 0;
  uint8_t sendPacket[48] = {0};
  sendPacket[0] = 0x1B;

  udp.begin(123);
  udp.beginPacket(NTP_SRV, 123);
  udp.write(sendPacket, 48);
  udp.endPacket();

  for (int z=0; z<60; z++) {
    delay(500);
    if (udp.parsePacket()) { // we've got a packet
        byte recvPacket[48];
        if (udp.read(recvPacket, 48) == 48) {
          udp.stop();
          for (int x=0; x<4; x++) sec += (uint64_t)recvPacket[40+x] << ((3-x)*8);
          sec -= 2208988800L;
          // We only need micros, so ignore the last packet of fractional
          for (int x=0; x<3; x++) fractional += (uint64_t)recvPacket[44+x] << ((2-x)*8);
          fractional = fractional * 1000000 / (1ULL<<24);
          return (timeval){(long int)sec, (long int)fractional};
        }
    }
  }
  return (timeval){0, 0};
}

Who is online

Users browsing this forum: No registered users and 91 guests