|
| 1 | +/** |
| 2 | + * The MIT License (MIT) |
| 3 | + * Copyright (c) 2015 by Fabrice Weinberg |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | + * SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +#include "NTPClient.h" |
| 23 | + |
| 24 | +NTPClient::NTPClient(UDP& udp) { |
| 25 | + this->_udp = &udp; |
| 26 | +} |
| 27 | + |
| 28 | +NTPClient::NTPClient(UDP& udp, int timeOffset) { |
| 29 | + this->_udp = &udp; |
| 30 | + this->_timeOffset = timeOffset; |
| 31 | +} |
| 32 | + |
| 33 | +NTPClient::NTPClient(UDP& udp, const char* poolServerName) { |
| 34 | + this->_udp = &udp; |
| 35 | + this->_poolServerName = poolServerName; |
| 36 | +} |
| 37 | + |
| 38 | +NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset) { |
| 39 | + this->_udp = &udp; |
| 40 | + this->_timeOffset = timeOffset; |
| 41 | + this->_poolServerName = poolServerName; |
| 42 | +} |
| 43 | + |
| 44 | +NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval) { |
| 45 | + this->_udp = &udp; |
| 46 | + this->_timeOffset = timeOffset; |
| 47 | + this->_poolServerName = poolServerName; |
| 48 | + this->_updateInterval = updateInterval; |
| 49 | +} |
| 50 | + |
| 51 | +void NTPClient::begin() { |
| 52 | + this->begin(NTP_DEFAULT_LOCAL_PORT); |
| 53 | +} |
| 54 | + |
| 55 | +void NTPClient::begin(int port) { |
| 56 | + this->_port = port; |
| 57 | + |
| 58 | + this->_udp->begin(this->_port); |
| 59 | + |
| 60 | + this->_udpSetup = true; |
| 61 | +} |
| 62 | + |
| 63 | +bool NTPClient::forceUpdate() { |
| 64 | + #ifdef DEBUG_NTPClient |
| 65 | + Serial.println("Update from NTP Server"); |
| 66 | + #endif |
| 67 | + |
| 68 | + this->sendNTPPacket(); |
| 69 | + |
| 70 | + // Wait till data is there or timeout... |
| 71 | + byte timeout = 0; |
| 72 | + int cb = 0; |
| 73 | + do { |
| 74 | + delay ( 10 ); |
| 75 | + cb = this->_udp->parsePacket(); |
| 76 | + if (timeout > 100) return false; // timeout after 1000 ms |
| 77 | + timeout++; |
| 78 | + } while (cb == 0); |
| 79 | + |
| 80 | + this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time |
| 81 | + |
| 82 | + this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE); |
| 83 | + |
| 84 | + unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]); |
| 85 | + unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]); |
| 86 | + // combine the four bytes (two words) into a long integer |
| 87 | + // this is NTP time (seconds since Jan 1 1900): |
| 88 | + unsigned long secsSince1900 = highWord << 16 | lowWord; |
| 89 | + |
| 90 | + this->_currentEpoc = secsSince1900 - SEVENZYYEARS; |
| 91 | + |
| 92 | + return true; |
| 93 | +} |
| 94 | + |
| 95 | +bool NTPClient::update() { |
| 96 | + if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval |
| 97 | + || this->_lastUpdate == 0) { // Update if there was no update yet. |
| 98 | + if (!this->_udpSetup) this->begin(); // setup the UDP client if needed |
| 99 | + return this->forceUpdate(); |
| 100 | + } |
| 101 | + return true; |
| 102 | +} |
| 103 | + |
| 104 | +unsigned long NTPClient::getEpochTime() { |
| 105 | + return this->_timeOffset + // User offset |
| 106 | + this->_currentEpoc + // Epoc returned by the NTP server |
| 107 | + ((millis() - this->_lastUpdate) / 1000); // Time since last update |
| 108 | +} |
| 109 | + |
| 110 | +int NTPClient::getDay() { |
| 111 | + return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday |
| 112 | +} |
| 113 | +int NTPClient::getHours() { |
| 114 | + return ((this->getEpochTime() % 86400L) / 3600); |
| 115 | +} |
| 116 | +int NTPClient::getMinutes() { |
| 117 | + return ((this->getEpochTime() % 3600) / 60); |
| 118 | +} |
| 119 | +int NTPClient::getSeconds() { |
| 120 | + return (this->getEpochTime() % 60); |
| 121 | +} |
| 122 | + |
| 123 | +String NTPClient::getFormattedTime() { |
| 124 | + unsigned long rawTime = this->getEpochTime(); |
| 125 | + unsigned long hours = (rawTime % 86400L) / 3600; |
| 126 | + String hoursStr = hours < 10 ? "0" + String(hours) : String(hours); |
| 127 | + |
| 128 | + unsigned long minutes = (rawTime % 3600) / 60; |
| 129 | + String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes); |
| 130 | + |
| 131 | + unsigned long seconds = rawTime % 60; |
| 132 | + String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds); |
| 133 | + |
| 134 | + return hoursStr + ":" + minuteStr + ":" + secondStr; |
| 135 | +} |
| 136 | + |
| 137 | +void NTPClient::end() { |
| 138 | + this->_udp->stop(); |
| 139 | + |
| 140 | + this->_udpSetup = false; |
| 141 | +} |
| 142 | + |
| 143 | +void NTPClient::setTimeOffset(int timeOffset) { |
| 144 | + this->_timeOffset = timeOffset; |
| 145 | +} |
| 146 | + |
| 147 | +void NTPClient::setUpdateInterval(int updateInterval) { |
| 148 | + this->_updateInterval = updateInterval; |
| 149 | +} |
| 150 | + |
| 151 | +void NTPClient::sendNTPPacket() { |
| 152 | + // set all bytes in the buffer to 0 |
| 153 | + memset(this->_packetBuffer, 0, NTP_PACKET_SIZE); |
| 154 | + // Initialize values needed to form NTP request |
| 155 | + // (see URL above for details on the packets) |
| 156 | + this->_packetBuffer[0] = 0b11100011; // LI, Version, Mode |
| 157 | + this->_packetBuffer[1] = 0; // Stratum, or type of clock |
| 158 | + this->_packetBuffer[2] = 6; // Polling Interval |
| 159 | + this->_packetBuffer[3] = 0xEC; // Peer Clock Precision |
| 160 | + // 8 bytes of zero for Root Delay & Root Dispersion |
| 161 | + this->_packetBuffer[12] = 49; |
| 162 | + this->_packetBuffer[13] = 0x4E; |
| 163 | + this->_packetBuffer[14] = 49; |
| 164 | + this->_packetBuffer[15] = 52; |
| 165 | + |
| 166 | + // all NTP fields have been given values, now |
| 167 | + // you can send a packet requesting a timestamp: |
| 168 | + this->_udp->beginPacket(this->_poolServerName, 123); //NTP requests are to port 123 |
| 169 | + this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE); |
| 170 | + this->_udp->endPacket(); |
| 171 | +} |
0 commit comments