| 82 | } |
| 83 | |
| 84 | bool NTPClient::forceUpdate() { |
| 85 | #ifdef DEBUG_NTPClient |
| 86 | Serial.println("Update from NTP Server"); |
| 87 | #endif |
| 88 | |
| 89 | // flush any existing packets |
| 90 | while(this->_udp->parsePacket() != 0) |
| 91 | this->_udp->flush(); |
| 92 | |
| 93 | this->sendNTPPacket(); |
| 94 | |
| 95 | // Wait till data is there or timeout... |
| 96 | byte timeout = 0; |
| 97 | int cb = 0; |
| 98 | do { |
| 99 | delay ( 10 ); |
| 100 | cb = this->_udp->parsePacket(); |
| 101 | if (timeout > 100) return false; // timeout after 1000 ms |
| 102 | timeout++; |
| 103 | } while (cb == 0); |
| 104 | |
| 105 | this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time |
| 106 | |
| 107 | this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE); |
| 108 | |
| 109 | unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]); |
| 110 | unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]); |
| 111 | // combine the four bytes (two words) into a long integer |
| 112 | // this is NTP time (seconds since Jan 1 1900): |
| 113 | unsigned long secsSince1900 = highWord << 16 | lowWord; |
| 114 | |
| 115 | this->_currentEpoc = secsSince1900 - SEVENZYYEARS; |
| 116 | |
| 117 | return true; // return true after successful update |
| 118 | } |
| 119 | |
| 120 | bool NTPClient::update() { |
| 121 | if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval |