Skip to content

Commit 0ac84df

Browse files
committed
Update NTPClient.ino
Instead of hardwiring the IP address of one server into the program, look-up an IP address from the host name. This way you get a random server from the pool each time.
1 parent 49c25b9 commit 0ac84df

File tree

1 file changed

+8
-1
lines changed
  • hardware/esp8266com/esp8266/libraries/ESP8266WiFi/examples/NTPClient

1 file changed

+8
-1
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/examples/NTPClient/NTPClient.ino

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ char pass[] = "********"; // your network password
2727

2828
unsigned int localPort = 2390; // local port to listen for UDP packets
2929

30-
IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
30+
/* Don't hardwire the IP address or we won't get the benefits of the pool.
31+
* Lookup the IP address for the host name instead */
32+
//IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
33+
IPAddress timeServerIP; // time.nist.gov NTP server address
34+
const char* ntpServerName = "time.nist.gov";
3135

3236
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
3337

@@ -65,6 +69,9 @@ void setup()
6569

6670
void loop()
6771
{
72+
//get a random server from the pool
73+
WiFi.hostByName(ntpServerName, timeServerIP);
74+
6875
sendNTPpacket(timeServer); // send an NTP packet to a time server
6976
// wait to see if a reply is available
7077
delay(1000);

0 commit comments

Comments
 (0)