'I get the wrong hour using the Arduino Ethernet shield and NTP server
I'm trying to get the time using an NTP server, and instead of the right our, i get the right hour plus two hours, for exemple, i should get 22:40 and i get 01:40, i've alerady tryed to change the server from i get the time, using one in my timezone.
this is the function that gets the time from the server:
time_t getTime()
{
while (Udp.parsePacket() > 0) ; // discard packets remaining to be parsed
Serial.println("Transmit NTP Request message");
// send packet to request time from NTP server
sendRequest(timeServer);
// wait for response
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= 48) {
Serial.println("Receiving NTP Response");
// read data and save to messageBuffer
Udp.read(messageBuffer, 48);
// NTP time received will be the seconds elapsed since 1 January 1900
unsigned long secsSince1900;
// convert to an unsigned long integer the reference timestamp found at byte 40 to 43
secsSince1900 = (unsigned long)messageBuffer[40] << 24;
secsSince1900 |= (unsigned long)messageBuffer[41] << 16;
secsSince1900 |= (unsigned long)messageBuffer[42] << 8;
secsSince1900 |= (unsigned long)messageBuffer[43];
return secsSince1900 - 2208981600UL;
}
}
// error if no response
Serial.println("Error: No Response.");
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
