Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using an IP address for NTP connections #31

Closed
sheffieldnikki opened this issue Apr 24, 2017 · 3 comments · Fixed by #77
Closed

Using an IP address for NTP connections #31

sheffieldnikki opened this issue Apr 24, 2017 · 3 comments · Fixed by #77

Comments

@sheffieldnikki
Copy link
Contributor

I've modified NTPClient() to accept an IP address as an alternative to the usual server name. This often speeds up an NTP fetch by avoiding the DNS lookup step.

NTPClient.h:

const char*   _poolServerName = "time.nist.gov"; // Default time server
IPAddress     _poolServerIP;

NTPClient(UDP& udp, int timeOffset);
NTPClient(UDP& udp, IPAddress poolServerIP);
NTPClient(UDP& udp, const char* poolServerName);

NTPClient.cpp:

NTPClient::NTPClient(UDP& udp, IPAddress poolServerIP) {
  this->_udp            = &udp;
  this->_poolServerIP = poolServerIP;
  this->_poolServerName = NULL;
}


  // you can send a packet requesting a timestamp:
  if (this->_poolServerName) this->_udp->beginPacket(this->_poolServerName, 123); // NTP requests are to port 123
    else this->_udp->beginPacket(this->_poolServerIP, 123);
  this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
@blackketter
Copy link

I think that if the server name is an IP address in the form of a string, it should just work, no?

@sheffieldnikki
Copy link
Contributor Author

sheffieldnikki commented Apr 25, 2017

Yes, I expect it would still work, but all the other Arduino libraries seem to use the binary IPAddress datatype. Presumably because it takes up slightly less memory, and the connection code can avoid the need to recognise a numeric string, and convert it to binary?

(I've just checked the source, and when UDP beginPacket() is called with a string IP address, it passes it to DNS getHostByName() which then runs inet_aton() to see if it is numeric, and converts it to binary.)

@sandeepmistry
Copy link
Contributor

Hi @sheffieldnick,

That sounds like a good addition, could you please prepare a pull request for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants