|
| 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(int timeOffset) { |
| 25 | + this->_timeOffset = timeOffset; |
| 26 | +} |
| 27 | + |
| 28 | +NTPClient::NTPClient(const char* poolServerName) { |
| 29 | + this->_poolServerName = poolServerName; |
| 30 | +} |
| 31 | + |
| 32 | +NTPClient::NTPClient(const char* poolServerName, int timeOffset) { |
| 33 | + this->_timeOffset = timeOffset; |
| 34 | + this->_poolServerName = poolServerName; |
| 35 | +} |
| 36 | + |
| 37 | +NTPClient::NTPClient(const char* poolServerName, int timeOffset, int updateInterval) { |
| 38 | + this->_timeOffset = timeOffset; |
| 39 | + this->_poolServerName = poolServerName; |
| 40 | + this->_updateInterval = updateInterval; |
| 41 | +} |
| 42 | + |
| 43 | +void NTPClient::begin() { |
| 44 | + #ifdef DEBUG_NTPClient |
| 45 | + Serial.println("Begin NTPClient"); |
| 46 | + Serial.print("Start udp connection on port: "); |
| 47 | + Serial.println(this->_port); |
| 48 | + #endif |
| 49 | + this->_udp.begin(this->_port); |
| 50 | + this->forceUpdate(); |
| 51 | +} |
| 52 | + |
| 53 | +void NTPClient::forceUpdate() { |
| 54 | + #ifdef DEBUG_NTPClient |
| 55 | + Serial.println("Update from NTP Server"); |
| 56 | + #endif |
| 57 | + |
| 58 | + IPAddress address; |
| 59 | + WiFi.hostByName(this->_poolServerName, address); |
| 60 | + |
| 61 | + this->sendNTPPacket(address); |
| 62 | + |
| 63 | + // Wait till data is there or timeout... |
| 64 | + byte timeout = 0; |
| 65 | + int cb = 0; |
| 66 | + do { |
| 67 | + delay ( 10 ); |
| 68 | + cb = this->_udp.parsePacket(); |
| 69 | + if (timeout > 100) return; // timeout after 1000 ms |
| 70 | + timeout++; |
| 71 | + } while (cb == 0); |
| 72 | + |
| 73 | + this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time |
| 74 | + |
| 75 | + this->_udp.read(this->_packetBuffer, NTP_PACKET_SIZE); |
| 76 | + |
| 77 | + unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]); |
| 78 | + unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]); |
| 79 | + // combine the four bytes (two words) into a long integer |
| 80 | + // this is NTP time (seconds since Jan 1 1900): |
| 81 | + unsigned long secsSince1900 = highWord << 16 | lowWord; |
| 82 | + |
| 83 | + this->_currentEpoc = secsSince1900 - SEVENZYYEARS; |
| 84 | +} |
| 85 | + |
| 86 | +void NTPClient::update() { |
| 87 | + unsigned long runtime = millis(); |
| 88 | + if (runtime - this->_lastUpdate >= this->_updateInterval && this->_updateInterval != 0) { |
| 89 | + this->forceUpdate(); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +unsigned long NTPClient::getRawTime() { |
| 94 | + return this->_timeOffset + // User offset |
| 95 | + this->_currentEpoc + // Epoc returned by the NTP server |
| 96 | + ((millis() - this->_lastUpdate) / 1000); // Time since last update |
| 97 | +} |
| 98 | + |
| 99 | +String NTPClient::getHours() { |
| 100 | + return String((this->getRawTime() % 86400L) / 3600); |
| 101 | +} |
| 102 | +String NTPClient::getMinutes() { |
| 103 | + return String((this->getRawTime() % 3600) / 60); |
| 104 | +} |
| 105 | + |
| 106 | +String NTPClient::getSeconds() { |
| 107 | + return String(this->getRawTime() % 60); |
| 108 | +} |
| 109 | + |
| 110 | +String NTPClient::getFormattedTime() { |
| 111 | + unsigned long rawTime = this->getRawTime(); |
| 112 | + unsigned long hours = (rawTime % 86400L) / 3600; |
| 113 | + String hoursStr = hours < 10 ? "0" + String(hours) : String(hours); |
| 114 | + |
| 115 | + unsigned long minutes = (rawTime % 3600) / 60; |
| 116 | + String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes); |
| 117 | + |
| 118 | + unsigned long seconds = rawTime % 60; |
| 119 | + String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds); |
| 120 | + |
| 121 | + return hoursStr + ":" + minuteStr + ":" + secondStr; |
| 122 | +} |
| 123 | + |
| 124 | +void NTPClient::sendNTPPacket(IPAddress ip) { |
| 125 | + // set all bytes in the buffer to 0 |
| 126 | + memset(this->_packetBuffer, 0, NTP_PACKET_SIZE); |
| 127 | + // Initialize values needed to form NTP request |
| 128 | + // (see URL above for details on the packets) |
| 129 | + this->_packetBuffer[0] = 0b11100011; // LI, Version, Mode |
| 130 | + this->_packetBuffer[1] = 0; // Stratum, or type of clock |
| 131 | + this->_packetBuffer[2] = 6; // Polling Interval |
| 132 | + this->_packetBuffer[3] = 0xEC; // Peer Clock Precision |
| 133 | + // 8 bytes of zero for Root Delay & Root Dispersion |
| 134 | + this->_packetBuffer[12] = 49; |
| 135 | + this->_packetBuffer[13] = 0x4E; |
| 136 | + this->_packetBuffer[14] = 49; |
| 137 | + this->_packetBuffer[15] = 52; |
| 138 | + |
| 139 | + // all NTP fields have been given values, now |
| 140 | + // you can send a packet requesting a timestamp: |
| 141 | + this->_udp.beginPacket(ip, 123); //NTP requests are to port 123 |
| 142 | + this->_udp.write(this->_packetBuffer, NTP_PACKET_SIZE); |
| 143 | + this->_udp.endPacket(); |
| 144 | +} |
| 145 | + |
0 commit comments