Getting UTC-Time from NTP-Server

Working code (previous was not woking for me):

UDP UDP;

char string[ 17 ] = { "" };

int hour, minute, second;

unsigned int localPort = 123;
unsigned int timeZone = 4;

byte timeServer[] = { 192, 43, 244, 18}; // time.nist.gov NTP server

const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message

byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

void setup()
{
  UDP.begin(localPort);
  Serial.begin(9600);
  delay(1000);
  Serial.println("NTP-clock");
}

void loop()
{
    sendNTPpacket(timeServer); // send an NTP packet to a time server

    // wait to see if a reply is available
    delay(1000); 
    Serial.println("------");
    if ( UDP.parsePacket() ) {
        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 = (packetBuffer[40] << 8) + packetBuffer[41];
    unsigned long lowWord = (packetBuffer[42] << 8) + 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;
    secsSince1900 += timeZone*60*60;

    // now convert NTP time into everyday time:
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800UL;    
    // subtract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears; 

    hour = (epoch % 86400L) / 3600;         
    minute = (epoch % 3600) / 60;
    second = (epoch % 60);

    Serial.print (hour);
    Serial.print (":");
    Serial.print (minute);
    Serial.print (":");
    Serial.println (second);

  }
    while ( UDP.parsePacket() ) { // clean-up buffer
        UDP.read(packetBuffer, NTP_PACKET_SIZE);  // read the packet into the buffer
    }

  // wait ten seconds before asking for the time again
  delay(10000);
}

// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(byte *address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;

  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:
  UDP.beginPacket(address, 123);
  UDP.write(packetBuffer, NTP_PACKET_SIZE); //NTP requests are to port 123
  UDP.endPacket();

}
1 Like