unsigned long ntp::getInternetTime(){ unsigned long epoch; int i=0; int t=0; sendNTPpacket(); delay(500); epoch=receiveTime(); t=0; // count sent tries while ((epoch == 0xFFFFFFFF) && (t<4)){ i=0; // count receve tries while ((epoch == 0xFFFFFFFF) && (i<4)){ delay(500); epoch=receiveTime(); i++; } if (epoch == 0xFFFFFFFF) { sendNTPpacket(); t++; } } internetEpoch=epoch; arduinoShift=millis()/1000; return (epoch); }
time_t getNtpTime() { while (Udp.parsePacket() > 0) ; // discard any previously received packets //Serial.println("$CLS,Y0,X0#TX NTP RQ"); PulseLed(BLUE, 2, 50, 10); sendNTPpacket(timeServer); uint32_t beginWait = millis(); uint32_t waited = millis() - beginWait; while (waited < 3000) { int size = Udp.parsePacket(); if (size >= NTP_PACKET_SIZE) { //Serial.println("$CLS,Y0,X0#RX NTP OK"); Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer unsigned long secsSince1900; // convert four bytes starting at location 40 to a long integer secsSince1900 = (unsigned long)packetBuffer[40] << 24; secsSince1900 |= (unsigned long)packetBuffer[41] << 16; secsSince1900 |= (unsigned long)packetBuffer[42] << 8; secsSince1900 |= (unsigned long)packetBuffer[43]; analogWrite(GREEN, 100); analogWrite(RED, 0); analogWrite(BLUE, 0); return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR + (waited / 1000); } waited = millis() - beginWait; } //Serial.println("$CLS,Y0,X0#*** NTP TO ***"); PulseLed(RED, 4, 50, 10); analogWrite(GREEN, 0); analogWrite(RED, 100); analogWrite(BLUE, 0); return 0; // return 0 if unable to get the time }
time_t getNtpTime() { while (Udp.parsePacket() > 0) ; // discard any previously received packets Serial.println("Transmit NTP Request"); sendNTPpacket(ntp_server); uint32_t beginWait = millis(); while (millis() - beginWait < 2000) { int size = Udp.parsePacket(); if (size >= NTP_PACKET_SIZE) { Serial.println("Receive NTP Response"); Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer unsigned long secsSince1900; unsigned long fracSecs; // convert four bytes starting at location 40 to a long integer secsSince1900 = (unsigned long)packetBuffer[40] << 24; secsSince1900 |= (unsigned long)packetBuffer[41] << 16; secsSince1900 |= (unsigned long)packetBuffer[42] << 8; secsSince1900 |= (unsigned long)packetBuffer[43]; fracSecs = (unsigned long)packetBuffer[44] << 24; fracSecs |= (unsigned long)packetBuffer[45] << 16; fracSecs |= (unsigned long)packetBuffer[46] << 8; fracSecs |= (unsigned long)packetBuffer[47]; return ((time_t)(secsSince1900 - 2208988800UL)<<32) + fracSecs; } } Serial.println("No NTP Response :-("); return 0; // return 0 if unable to get the time }
unsigned long getNTPTimestamp() { unsigned long ulSecs2000; udp.begin(ntpPort); sendNTPpacket(timeServer); // send an NTP packet to a time server delay(1000); // wait to see if a reply is available int cb = udp.parsePacket(); if(!cb) { Serial.println("Timeserver not accessible! - No RTC support!"); ulSecs2000=0; } else { Serial.print("packet received, length="); Serial.println(cb); 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): ulSecs2000 = highWord << 16 | lowWord; ulSecs2000 -= 2208988800UL; // go from 1900 to 1970 ulSecs2000 -= 946684800UL; // go from 1970 to 2000 } return(ulSecs2000); }
time_t NTP::getNtpTime(void) { while (UDP.parsePacket() > 0) ; // discard any previously received packets //get a random server from the pool IPAddress timeServerIP; WiFi.hostByName(_serverName, timeServerIP); sendNTPpacket(timeServerIP); uint32_t beginWait = millis(); while (millis() - beginWait < 1500) { int size = UDP.parsePacket(); if (size >= NTP_PACKET_SIZE) { Serial.println("Receive NTP Response"); UDP.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer time_t secsSince1900; // convert four bytes starting at location 40 to a long integer secsSince1900 = (time_t)packetBuffer[40] << 24; secsSince1900 |= (time_t)packetBuffer[41] << 16; secsSince1900 |= (time_t)packetBuffer[42] << 8; secsSince1900 |= (time_t)packetBuffer[43]; time_t secsSince1970 = secsSince1900 - 2208988800UL; int8_t totalOffset = (int8_t)(_timeZoneOffset); return secsSince1970 + (time_t)(totalOffset * SECS_PER_HOUR) ; } yield(); } Serial.println("No NTP Response :-("); return 0; // return 0 if unable to get the time }
time_t getNtptime() { time_t epoch = 0; //get a random server from the pool WiFi.hostByName(ntpServerName, timeServerIP); sendNTPpacket(timeServerIP); // send an NTP packet to a time server // wait to see if a reply is available delay(500); int cb = udp.parsePacket(); if (!cb) { //Serial.println("no packet!?"); wsSend("NTP Error"); } 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); // now convert NTP time into everyday time: // Serial.print("Unix time = "); // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: const unsigned long seventyYears = 2208988800UL; // subtract seventy years: epoch = secsSince1900 - seventyYears; epoch = epoch - (60 * 60 * ntpOffset); // take off 4 hrs for EDT offset sprintf(str, "NTP epoch=%d", epoch); wsSend(str); } return epoch; }
unsigned long getNtpTime() { // We need to call the begin to reset the socket. // Because localClient.connect() may occupy the same socket. EthernetUDP theUDP; theUDP.begin(localPort); Serial.println("Sending time sync request to NTP server."); sendNTPpacket(timeServer); // send an NTP packet to a time server unsigned long startMillis = millis(); int tryCounter = 1; while( millis() - startMillis < 1000) // wait up to one second for the response { // wait to see if a reply is available if ( theUDP.available() ) { theUDP.readPacket(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; // now convert NTP time into Arduino Time format: // Time starts on Jan 1 1970. In seconds, that's 2208988800: const unsigned long seventyYears = 2208988800UL; // subtract seventy years: unsigned long epoch = secsSince1900 - seventyYears; Serial.println("Time sync successfully."); return epoch; } Serial.print("No date data available. Try counter: ."); Serial.println(tryCounter++); delay(100); } Serial.println("Time sync failed."); return 0; // return 0 if unable to get the time }
time_t DHwifi::GetSystemTime() { IPAddress timeServerIP; // time.nist.gov NTP server address const char* ntpServerName = "time.nist.gov"; //get a random server from the pool WiFi.hostByName(ntpServerName, timeServerIP); sendNTPpacket(timeServerIP); // send an NTP packet to a time server // wait to see if a reply is available delay(1000); int cb = udp.parsePacket(); if (!cb) { Serial.println("no packet yet"); } else { Serial.print("NTP 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; // 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; return epoch + 3600 + 3600; } // wait ten seconds before asking for the time again return 0; }
unsigned long getNtpTime() { sendNTPpacket(timeServer); // send an NTP packet to a time server // wait to see if a reply is available delay(1000); if ( Udp.parsePacket() ) { // 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; #ifdef LOGGING Serial.print("Seconds since Jan 1 1900 = " ); Serial.println(secsSince1900); #endif // 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; // print Unix time: #ifdef LOGGING Serial.print(UNIX_TIME); Serial.println(epoch); #endif return epoch; } return 0; // return 0 if unable to get the time }
time_t getNtpTime() { while (Udp.parsePacket() > 0) ; // discard any previously received packets println_dbg("Transmit NTP Request"); sendNTPpacket(timeServer); uint32_t beginWait = millis(); while (millis() - beginWait < 1500) { int size = Udp.parsePacket(); if (size >= NTP_PACKET_SIZE) { println_dbg("Receive NTP Response"); Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer unsigned long secsSince1900; // convert four bytes starting at location 40 to a long integer secsSince1900 = (unsigned long)packetBuffer[40] << 24; secsSince1900 |= (unsigned long)packetBuffer[41] << 16; secsSince1900 |= (unsigned long)packetBuffer[42] << 8; secsSince1900 |= (unsigned long)packetBuffer[43]; return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR; } } println_dbg("No NTP Response :-("); return 0; // return 0 if unable to get the time }
// http://www.arduino.cc/en/Tutorial/WiFiRTC unsigned long readLinuxEpochUsingNTP() { Udp.begin(localPort); sendNTPpacket(timeServer); // send an NTP packet to a time server // wait to see if a reply is available delay(1000); if ( Udp.parsePacket() ) { Serial.println("NTP time received"); // 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; // 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: Udp.stop(); return (secsSince1900 - seventyYears + timeZone * SECS_PER_HOUR); } else { Udp.stop(); return 0; } }
uint64_t getNTPtime() { // send an NTP packet to a time server sendNTPpacket(timeServer); // wait to see if a reply is available delay(1000); if ( Udp.available() ) { // read the packet into the buffer Udp.read(pb, NTP_PACKET_SIZE); // New from IDE 1.0, // NTP contains four timestamps with an integer part and a fraction part // we only use the integer part here unsigned long t1, t2, t3, t4; t1 = t2 = t3 = t4 = 0; for (int i=0; i< 4; i++) { t1 = t1 << 8 | pb[16+i]; t2 = t2 << 8 | pb[24+i]; t3 = t3 << 8 | pb[32+i]; t4 = t4 << 8 | pb[40+i]; } // part of the fractional part // could be 4 bytes but this is more precise than the 1307 RTC // which has a precision of ONE second // in fact one byte is sufficient for 1307 float f1,f2,f3,f4; f1 = ((long)pb[20] * 256 + pb[21]) / 65536.0; f2 = ((long)pb[28] * 256 + pb[29]) / 65536.0; f3 = ((long)pb[36] * 256 + pb[37]) / 65536.0; f4 = ((long)pb[44] * 256 + pb[45]) / 65536.0; // NOTE: // one could use the fractional part to set the RTC more precise // 1) at the right (calculated) moment to the NEXT second! // t4++; // delay(1000 - f4*1000); // RTC.adjust(DateTime(t4)); // keep in mind that the time in the packet was the time at // the NTP server at sending time so one should take into account // the network latency (try ping!) and the processing of the data // ==> delay (850 - f4*1000); // 2) simply use it to round up the second // f > 0.5 => add 1 to the second before adjusting the RTC // (or lower threshold eg 0.4 if one keeps network latency etc in mind) // 3) a SW RTC might be more precise, => ardomic clock :) // convert NTP to UNIX time, differs seventy years = 2208988800 seconds // NTP starts Jan 1, 1900 // Unix time starts on Jan 1 1970. const unsigned long seventyYears = 2208988800UL; t1 -= seventyYears; t2 -= seventyYears; t3 -= seventyYears; t4 -= seventyYears; //Serial.println("T1 .. T4 && fractional parts"); //PrintDateTime(DateTime(t1)); Serial.println(f1,4); //PrintDateTime(DateTime(t2)); Serial.println(f2,4); //PrintDateTime(DateTime(t3)); Serial.println(f3,4); //PrintDateTime(t4); char ntpTime[21]; //GetDatetimeString(t4, ntpTime); Serial.println(f4,4); Serial.println(); // Adjust timezone and DST... in my case substract 4 hours for Chile Time // or work in UTC? t4 -= (3 * 3600L); // Notice the L for long calculations!! t4 += 1; // adjust the delay(1000) at begin of loop! if (f4 > 0.4) t4++; // adjust fractional part, see above } else { Serial.println("Failed to get NTP UDP packet!"); } }
bool doNTPupdate() { bool ntpValid; //get a random server from the pool WiFi.hostByName(ntpServerName, timeServerIP); sendNTPpacket(timeServerIP); // send an NTP packet to a time server // wait to see if a reply is available delay(1000); int cb = udp.parsePacket(); if (!cb) { Serial.println("no packet yet"); ntpValid = false; } else { lastNTP.updateTimeMillis = millis(); //track time of update, so we can estimate time between updates 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); // now convert NTP time into everyday time: //Serial.print("Unix 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; // print Unix time: //Serial.println(epoch); int8_t h_temp = (uint8_t) ((epoch % 86400L) / 3600); h_temp -= 8; //PST offset if (h_temp < 0) { h_temp += 24; } lastNTP.hours = (uint8_t) h_temp; ; lastNTP.minutes = (uint8_t) ((epoch % 3600) / 60); lastNTP.seconds = (uint8_t) (epoch % 60); ntpValid = true; // print the hour, minute and second: Serial.print("hours = "); Serial.println(lastNTP.hours); // print the hour (86400 equals secs per day) Serial.print("mins = "); Serial.println(lastNTP.minutes); Serial.print("secs = "); Serial.println(lastNTP.seconds); } return ntpValid; }