1

There are lots of examples around how to connect with an NTP server, but I do not see how to find such an IP-address. From the examples I conclude I need to find it on for example pool.ntp.org, but I do not even see how to get IP-addresses out of there myself, let alone to let my Arduino do it. Of course I looked at the How do I use pool.ntp.org page, but it doesn't seem to say anything about finding IP-addresses.

Questions

  1. Is it possible to let my Arduino find a working NTP server (using the EtherCard library), and if so, how?
  2. All the examples I saw have an NTP server IP address hard coded, isn't that a bad practice and also a bad idea for a longer running project?

The code I currently find the easiest to understand is from the Arduino forum, and has an IP address hard coded.

I have an Arduino Nano ATmega328 with a ENC28j60 ethernet shield, and therefore use the EtherCard libray. I do know of existence of RTC devices but I would prefer NTP.

asked Jul 5, 2016 at 13:48
5
  • You need to perform a DNS lookup in order to convert the host name (e.g. "pool.ntp.org") into an IP address. Doesn't the EtherCard library have a facility for that? Commented Jul 5, 2016 at 13:57
  • @EdgarBonet That sounds logical, indeed EtherCard can do that, googling it it would be something like if (!ether.dnsLookup(website)) { Serial.println("DNS failed"); } ether.printIp("Server: ", ether.hisip); but I'm new to the ethernet connections so I need to find out how to get an ip address out of that to use later in the code... Commented Jul 5, 2016 at 14:06
  • I found an ip address with the code in the form of a string, but didn't manage to convert it to an array even with stackoverflow.com/questions/35227449/… so I'm leaving that for now. This question is solved I guess, although I am not sure of course if the ip is a correct one because I can't test it. Commented Jul 5, 2016 at 14:55
  • ether.hisip is not a string, it's an array of 4 bytes. Commented Jul 5, 2016 at 15:05
  • @EdgarBonet Oh indeed you are right! That solves my whole problem, thanks a lot! I was tricked into thinking it was a string by what ether.hisip printed to, but now I see that's why ether.printIP was used. Commented Jul 5, 2016 at 15:26

1 Answer 1

2

The EtherCard library offers a method called ether.dnsLookup(host) where you ask it to look up the IP address for you.

etherCard.dnsLookup("pool.ntp.org");

...only that might fail, so it returns a false:

if (!etherCard.dnsLookup("pool.ntp.org")) {
 // Error! Do something!
 return;
} // if

But if it works...

if (!etherCard.dnsLookup("pool.ntp.org")) {
 // Error! Do something!
 return;
} // if
// At this stage, dnsLookup() worked...
// Can now connect to IP adress in etherCard.hispip

Now: please note that ntp.org would prefer you to use a more precise locale than pool.ntp.org. If you're going to distribute your application worldwide, then that's fine. But I always use au.pool.ntp.org because I'm in the Australian region. There are others that you can/should use instead.

answered Jul 5, 2016 at 14:54
1
  • Thanks for writing Edgar's suggestion into an answer, the locale is a nice addition! Commented Jul 5, 2016 at 14:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.