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