AceTime  0.1
Date and time classes for Arduino that supports the TZ DAtabase, and a system clock synchronized from an NTP server or an RTC chip.
NtpTimeProvider.cpp
1 #include "NtpTimeProvider.h"
2 
3 #if defined(ESP8266) || defined(ESP32)
4 
5 namespace ace_time {
6 namespace provider {
7 
8 const char NtpTimeProvider::kNtpServerName[] = "us.pool.ntp.org";
9 
10 // Moved from. h to .cpp because F() strings in inline contexts under ESP8266
11 // causes problems with other F() strings.
12 void NtpTimeProvider::setup(const char* ssid, const char* password) {
13  uint16_t startMillis = millis();
14  WiFi.begin(ssid, password);
15  while (WiFi.status() != WL_CONNECTED) {
16  uint16_t elapsedMillis = millis() - startMillis;
17  if (elapsedMillis >= kConnectTimeoutMillis) {
18  mIsSetUp = false;
19  return;
20  }
21 
22  delay(500);
23  }
24 
25  mUdp.begin(mLocalPort);
26 
27 #if ACE_TIME_NTP_TIME_PROVIDER_DEBUG == 1
28  #if defined(ESP8266)
29  Serial.print(F("Local port: "));
30  Serial.println(mUdp.localPort());
31  #endif
32 #endif
33 
34  mIsSetUp = true;
35 }
36 
37 }
38 }
39 
40 #endif