forked from PaulStoffregen/Time
Use WiFi.hostByName to resolve IP
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Time_NTP.pde
|
||||
* TimeNTP_ESP8266WiFi.ino
|
||||
* Example showing time sync to NTP time source
|
||||
*
|
||||
* This sketch uses the ESP8266WiFi library
|
||||
@@ -13,10 +13,11 @@ const char ssid[] = "*************"; // your network SSID (name)
|
||||
const char pass[] = "********"; // your network password
|
||||
|
||||
// NTP Servers:
|
||||
IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
|
||||
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
|
||||
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov
|
||||
|
||||
static const char ntpServerName[] = "us.pool.ntp.org";
|
||||
//static const char ntpServerName[] = "time.nist.gov";
|
||||
//static const char ntpServerName[] = "time-a.timefreq.bldrdoc.gov";
|
||||
//static const char ntpServerName[] = "time-b.timefreq.bldrdoc.gov";
|
||||
//static const char ntpServerName[] = "time-c.timefreq.bldrdoc.gov";
|
||||
|
||||
const int timeZone = 1; // Central European Time
|
||||
//const int timeZone = -5; // Eastern Standard Time (USA)
|
||||
@@ -48,7 +49,6 @@ void setup()
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
|
||||
Serial.print("IP number assigned by DHCP is ");
|
||||
Serial.println(WiFi.localIP());
|
||||
Serial.println("Starting UDP");
|
||||
@@ -57,6 +57,7 @@ void setup()
|
||||
Serial.println(Udp.localPort());
|
||||
Serial.println("waiting for sync");
|
||||
setSyncProvider(getNtpTime);
|
||||
setSyncInterval(300);
|
||||
}
|
||||
|
||||
time_t prevDisplay = 0; // when the digital clock was displayed
|
||||
@@ -71,7 +72,8 @@ void loop()
|
||||
}
|
||||
}
|
||||
|
||||
void digitalClockDisplay(){
|
||||
void digitalClockDisplay()
|
||||
{
|
||||
// digital clock display of the time
|
||||
Serial.print(hour());
|
||||
printDigits(minute());
|
||||
@@ -85,9 +87,8 @@ void digitalClockDisplay(){
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void printDigits(int digits){
|
||||
void printDigits(int digits)
|
||||
{
|
||||
// utility for digital clock display: prints preceding colon and leading 0
|
||||
Serial.print(":");
|
||||
if (digits < 10)
|
||||
@@ -102,9 +103,16 @@ byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
|
||||
|
||||
time_t getNtpTime()
|
||||
{
|
||||
IPAddress ntpServerIP; // NTP server's ip address
|
||||
|
||||
while (Udp.parsePacket() > 0) ; // discard any previously received packets
|
||||
Serial.println("Transmit NTP Request");
|
||||
sendNTPpacket(timeServer);
|
||||
// get a random server from the pool
|
||||
WiFi.hostByName(ntpServerName, ntpServerIP);
|
||||
Serial.print(ntpServerName);
|
||||
Serial.print(": ");
|
||||
Serial.println(ntpServerIP);
|
||||
sendNTPpacket(ntpServerIP);
|
||||
uint32_t beginWait = millis();
|
||||
while (millis() - beginWait < 1500) {
|
||||
int size = Udp.parsePacket();
|
||||
|
Reference in New Issue
Block a user