Chat freely about anything...

User avatar
By batstru
#71452 Hi all,

So far all the NTP libraries I've seen do not allow to have a precision below the seconds.
The most promising alternative I found is this nice article (https://www.geekstips.com/arduino-time- ... p8266-udp/), which describes how to get the time from reading the UDP packets.

Unfortunately it's a little unclear to me how I should change the code to get the milliseconds.

Can anyone help me?

This is the core of the code:

Code: Select all  int cb = udp.parsePacket();
  if (!cb) {
    Serial.println("no packet yet");
  }
  else {
    Serial.print("packet received, length=");
    Serial.println(cb);
    // We've received a packet, read the data from it
    udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:
    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;
    Serial.print("Seconds since Jan 1 1900 = " );
    Serial.println(secsSince1900);


Thanks!