同步ntp服务器时间
time_t getNtpTime(){
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
sendNTPpacket(timeServer);
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
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 =(unsigned long)packetBuffer << 24;
secsSince1900 |= (unsigned long)packetBuffer << 16;
secsSince1900 |= (unsigned long)packetBuffer << 8;
secsSince1900 |= (unsigned long)packetBuffer;
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
这是获取时间的函数,具体是什么算法看不太明白,望多多指教!
页:
[1]