diff --git a/include/NTPClient.h b/include/NTPClient.h index 02d8752..9d2be54 100644 --- a/include/NTPClient.h +++ b/include/NTPClient.h @@ -26,6 +26,8 @@ class NTPClient { void sendNTPPacket(); + boolean _timeValid = false; + public: NTPClient(UDP& udp); NTPClient(UDP& udp, long timeOffset); @@ -69,6 +71,7 @@ class NTPClient { int getHours() const; int getMinutes() const; int getSeconds() const; + boolean getTimeValid() const; /** * Changes the time offset. Useful for changing timezones dynamically diff --git a/src/NTPClient.cpp b/src/NTPClient.cpp index 6de5f85..a7d14fa 100644 --- a/src/NTPClient.cpp +++ b/src/NTPClient.cpp @@ -66,6 +66,9 @@ bool NTPClient::forceUpdate() { Serial.println("Update from NTP Server"); #endif + while(this->_udp->parsePacket() != 0) + this->_udp->flush(); + this->sendNTPPacket(); // Wait till data is there or timeout... @@ -74,10 +77,13 @@ bool NTPClient::forceUpdate() { do { delay ( 10 ); cb = this->_udp->parsePacket(); - if (timeout > 100) return false; // timeout after 1000 ms + if (timeout > 100) { + this->_timeValid = false; + return false; // timeout after 1000 ms + } timeout++; } while (cb == 0); - + this->_timeValid = true; this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE); diff --git a/src/effect_clock.cpp b/src/effect_clock.cpp index 3baba3a..84ab5a1 100644 --- a/src/effect_clock.cpp +++ b/src/effect_clock.cpp @@ -24,6 +24,10 @@ void ClockEffect::loop(boolean invert, CRGB fg_color, CRGB bg_color) { } fg_color = bg_color; } + if (ntpClient.getTimeValid()==false && ntpClient.getSeconds() & 1==0) { + clear(window, bg_color); + return; + } int h = ntpClient.getHours(); drawDigit(window, numbers3x5, 3, 5, 0, 1, h / 10, fg_color, invert); drawDigit(window, numbers3x5, 3, 5, 4, 1, h % 10, fg_color, invert);