moved custom lib to separate repo
This commit is contained in:
parent
0896430c2f
commit
586da5e2ce
@ -1,19 +0,0 @@
|
||||
NTPClient 3.2.0 - 2020.02.15
|
||||
* Integrated functions to process year month and date
|
||||
* Removed all functions relying on String class
|
||||
|
||||
NTPClient 3.1.0 - 2016.05.31
|
||||
|
||||
* Added functions for changing the timeOffset and updateInterval later. Thanks @SirUli
|
||||
|
||||
NTPClient 3.0.0 - 2016.04.19
|
||||
|
||||
* Constructors now require UDP instance argument, to add support for non-ESP8266 boards
|
||||
* Added optional begin API to override default local port
|
||||
* Added end API to close UDP socket
|
||||
* Changed return type of update and forceUpdate APIs to bool, and return success or failure
|
||||
* Change return type of getDay, getHours, getMinutes, and getSeconds to int
|
||||
|
||||
Older
|
||||
|
||||
* Changes not recorded
|
@ -1,245 +0,0 @@
|
||||
/**
|
||||
* The MIT License (MIT)
|
||||
* Copyright (c) 2015 by Fabrice Weinberg
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "NTPClient.h"
|
||||
|
||||
NTPClient::NTPClient(UDP& udp) {
|
||||
this->_udp = &udp;
|
||||
}
|
||||
|
||||
NTPClient::NTPClient(UDP& udp, int timeOffset) {
|
||||
this->_udp = &udp;
|
||||
this->_timeOffset = timeOffset;
|
||||
}
|
||||
|
||||
NTPClient::NTPClient(UDP& udp, const char* poolServerName) {
|
||||
this->_udp = &udp;
|
||||
this->_poolServerName = poolServerName;
|
||||
}
|
||||
|
||||
NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset) {
|
||||
this->_udp = &udp;
|
||||
this->_timeOffset = timeOffset;
|
||||
this->_poolServerName = poolServerName;
|
||||
}
|
||||
|
||||
NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval) {
|
||||
this->_udp = &udp;
|
||||
this->_timeOffset = timeOffset;
|
||||
this->_poolServerName = poolServerName;
|
||||
this->_updateInterval = updateInterval;
|
||||
}
|
||||
|
||||
void NTPClient::begin() {
|
||||
this->begin(NTP_DEFAULT_LOCAL_PORT);
|
||||
}
|
||||
|
||||
void NTPClient::begin(int port) {
|
||||
this->_port = port;
|
||||
|
||||
this->_udp->begin(this->_port);
|
||||
|
||||
this->_udpSetup = true;
|
||||
}
|
||||
|
||||
bool NTPClient::forceUpdate() {
|
||||
#ifdef DEBUG_NTPClient
|
||||
Serial.println("Update from NTP Server");
|
||||
#endif
|
||||
|
||||
this->sendNTPPacket();
|
||||
|
||||
// Wait till data is there or timeout...
|
||||
byte timeout = 0;
|
||||
int cb = 0;
|
||||
do {
|
||||
delay ( 10 );
|
||||
cb = this->_udp->parsePacket();
|
||||
if (timeout > 100) return false; // timeout after 1000 ms
|
||||
timeout++;
|
||||
} while (cb == 0);
|
||||
|
||||
this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time
|
||||
|
||||
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
|
||||
|
||||
unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
|
||||
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
|
||||
// combine the four bytes (two words) into a long integer
|
||||
// this is NTP time (seconds since Jan 1 1900):
|
||||
unsigned long secsSince1900 = highWord << 16 | lowWord;
|
||||
|
||||
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NTPClient::update() {
|
||||
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
|
||||
|| this->_lastUpdate == 0) { // Update if there was no update yet.
|
||||
if (!this->_udpSetup) this->begin(); // setup the UDP client if needed
|
||||
return this->forceUpdate();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned long NTPClient::getEpochTime() {
|
||||
return this->_timeOffset + // User offset
|
||||
this->_currentEpoc + // Epoc returned by the NTP server
|
||||
((millis() - this->_lastUpdate) / 1000); // Time since last update
|
||||
}
|
||||
|
||||
int NTPClient::getDay() {
|
||||
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday
|
||||
}
|
||||
int NTPClient::getHours() {
|
||||
return ((this->getEpochTime() % 86400L) / 3600);
|
||||
}
|
||||
int NTPClient::getMinutes() {
|
||||
return ((this->getEpochTime() % 3600) / 60);
|
||||
}
|
||||
int NTPClient::getSeconds() {
|
||||
return (this->getEpochTime() % 60);
|
||||
}
|
||||
|
||||
int NTPClient::getYear() {
|
||||
time_t rawtime = this->getEpochTime();
|
||||
struct tm * ti;
|
||||
ti = localtime (&rawtime);
|
||||
int year = ti->tm_year + 1900;
|
||||
|
||||
return year;
|
||||
}
|
||||
|
||||
int NTPClient::getMonth() {
|
||||
time_t rawtime = this->getEpochTime();
|
||||
struct tm * ti;
|
||||
ti = localtime (&rawtime);
|
||||
int month = (ti->tm_mon + 1) < 10 ? 0 + (ti->tm_mon + 1) : (ti->tm_mon + 1);
|
||||
|
||||
return month;
|
||||
}
|
||||
|
||||
int NTPClient::getDate() {
|
||||
time_t rawtime = this->getEpochTime();
|
||||
struct tm * ti;
|
||||
ti = localtime (&rawtime);
|
||||
int day = (ti->tm_mday) < 10 ? 0 + (ti->tm_mday) : (ti->tm_mday);
|
||||
|
||||
return day;
|
||||
}
|
||||
|
||||
/*
|
||||
String NTPClient::getFormattedTime() {
|
||||
unsigned long rawTime = this->getEpochTime();
|
||||
unsigned long hours = (rawTime % 86400L) / 3600;
|
||||
String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
|
||||
|
||||
unsigned long minutes = (rawTime % 3600) / 60;
|
||||
String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes);
|
||||
|
||||
unsigned long seconds = rawTime % 60;
|
||||
String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);
|
||||
|
||||
return hoursStr + ":" + minuteStr + ":" + secondStr;
|
||||
}
|
||||
|
||||
String NTPClient::getFormattedDate() {
|
||||
time_t rawtime = this->getEpochTime();
|
||||
struct tm * ti;
|
||||
ti = localtime (&rawtime);
|
||||
|
||||
uint16_t year = ti->tm_year + 1900;
|
||||
String yearStr = String(year);
|
||||
|
||||
uint8_t month = ti->tm_mon + 1;
|
||||
String monthStr = month < 10 ? "0" + String(month) : String(month);
|
||||
|
||||
uint8_t day = ti->tm_mday;
|
||||
String dayStr = day < 10 ? "0" + String(day) : String(day);
|
||||
|
||||
|
||||
return yearStr + "-" + monthStr + "-" + dayStr;
|
||||
}
|
||||
|
||||
String NTPClient::getFullFormattedTime() {
|
||||
time_t rawtime = this->getEpochTime();
|
||||
struct tm * ti;
|
||||
ti = localtime (&rawtime);
|
||||
|
||||
uint16_t year = ti->tm_year + 1900;
|
||||
String yearStr = String(year);
|
||||
|
||||
uint8_t month = ti->tm_mon + 1;
|
||||
String monthStr = month < 10 ? "0" + String(month) : String(month);
|
||||
|
||||
uint8_t day = ti->tm_mday;
|
||||
String dayStr = day < 10 ? "0" + String(day) : String(day);
|
||||
|
||||
uint8_t hours = ti->tm_hour;
|
||||
String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
|
||||
|
||||
uint8_t minutes = ti->tm_min;
|
||||
String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes);
|
||||
|
||||
uint8_t seconds = ti->tm_sec;
|
||||
String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);
|
||||
|
||||
return yearStr + "-" + monthStr + "-" + dayStr + " " +
|
||||
hoursStr + ":" + minuteStr + ":" + secondStr;
|
||||
}
|
||||
*/
|
||||
|
||||
void NTPClient::end() {
|
||||
this->_udp->stop();
|
||||
|
||||
this->_udpSetup = false;
|
||||
}
|
||||
|
||||
void NTPClient::setTimeOffset(int timeOffset) {
|
||||
this->_timeOffset = timeOffset;
|
||||
}
|
||||
|
||||
void NTPClient::setUpdateInterval(int updateInterval) {
|
||||
this->_updateInterval = updateInterval;
|
||||
}
|
||||
|
||||
void NTPClient::sendNTPPacket() {
|
||||
// set all bytes in the buffer to 0
|
||||
memset(this->_packetBuffer, 0, NTP_PACKET_SIZE);
|
||||
// Initialize values needed to form NTP request
|
||||
// (see URL above for details on the packets)
|
||||
this->_packetBuffer[0] = 0b11100011; // LI, Version, Mode
|
||||
this->_packetBuffer[1] = 0; // Stratum, or type of clock
|
||||
this->_packetBuffer[2] = 6; // Polling Interval
|
||||
this->_packetBuffer[3] = 0xEC; // Peer Clock Precision
|
||||
// 8 bytes of zero for Root Delay & Root Dispersion
|
||||
this->_packetBuffer[12] = 49;
|
||||
this->_packetBuffer[13] = 0x4E;
|
||||
this->_packetBuffer[14] = 49;
|
||||
this->_packetBuffer[15] = 52;
|
||||
|
||||
// all NTP fields have been given values, now
|
||||
// you can send a packet requesting a timestamp:
|
||||
this->_udp->beginPacket(this->_poolServerName, 123); //NTP requests are to port 123
|
||||
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
|
||||
this->_udp->endPacket();
|
||||
}
|
@ -1,107 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Arduino.h"
|
||||
#include <time.h>
|
||||
|
||||
#include <Udp.h>
|
||||
|
||||
#define SEVENZYYEARS 2208988800UL
|
||||
#define NTP_PACKET_SIZE 48
|
||||
#define NTP_DEFAULT_LOCAL_PORT 1337
|
||||
|
||||
class NTPClient {
|
||||
private:
|
||||
UDP* _udp;
|
||||
bool _udpSetup = false;
|
||||
|
||||
const char* _poolServerName = "time.nist.gov"; // Default time server
|
||||
int _port = NTP_DEFAULT_LOCAL_PORT;
|
||||
int _timeOffset = 0;
|
||||
|
||||
unsigned int _updateInterval = 60000; // In ms
|
||||
|
||||
unsigned long _currentEpoc = 0; // In s
|
||||
unsigned long _lastUpdate = 0; // In ms
|
||||
|
||||
byte _packetBuffer[NTP_PACKET_SIZE];
|
||||
|
||||
void sendNTPPacket();
|
||||
|
||||
public:
|
||||
NTPClient(UDP& udp);
|
||||
NTPClient(UDP& udp, int timeOffset);
|
||||
NTPClient(UDP& udp, const char* poolServerName);
|
||||
NTPClient(UDP& udp, const char* poolServerName, int timeOffset);
|
||||
NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval);
|
||||
|
||||
/**
|
||||
* Starts the underlying UDP client with the default local port
|
||||
*/
|
||||
void begin();
|
||||
|
||||
/**
|
||||
* Starts the underlying UDP client with the specified local port
|
||||
*/
|
||||
void begin(int port);
|
||||
|
||||
/**
|
||||
* This should be called in the main loop of your application. By default an update from the NTP Server is only
|
||||
* made every 60 seconds. This can be configured in the NTPClient constructor.
|
||||
*
|
||||
* @return true on success, false on failure
|
||||
*/
|
||||
bool update();
|
||||
|
||||
/**
|
||||
* This will force the update from the NTP Server.
|
||||
*
|
||||
* @return true on success, false on failure
|
||||
*/
|
||||
bool forceUpdate();
|
||||
|
||||
int getDay();
|
||||
int getHours();
|
||||
int getMinutes();
|
||||
int getSeconds();
|
||||
|
||||
/**
|
||||
* Changes the time offset. Useful for changing timezones dynamically
|
||||
*/
|
||||
void setTimeOffset(int timeOffset);
|
||||
|
||||
/**
|
||||
* Set the update interval to another frequency. E.g. useful when the
|
||||
* timeOffset should not be set in the constructor
|
||||
*/
|
||||
void setUpdateInterval(int updateInterval);
|
||||
|
||||
/**
|
||||
* @return time formatted like `hh:mm:ss`
|
||||
*/
|
||||
// String getFormattedTime();
|
||||
|
||||
/**
|
||||
* @return time formatted like `DD:MM:YYYY`
|
||||
*/
|
||||
// String getFormattedDate();
|
||||
|
||||
/**
|
||||
* @return time formatted like `DD:MM:YYYY hh:mm:ss`
|
||||
*/
|
||||
// String getFullFormattedTime();
|
||||
|
||||
|
||||
int getYear();
|
||||
int getMonth();
|
||||
int getDate();
|
||||
|
||||
/**
|
||||
* @return time in seconds since Jan. 1, 1970
|
||||
*/
|
||||
unsigned long getEpochTime();
|
||||
|
||||
/**
|
||||
* Stops the underlying UDP client
|
||||
*/
|
||||
void end();
|
||||
};
|
@ -1,23 +0,0 @@
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
NTPClient KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
begin KEYWORD2
|
||||
end KEYWORD2
|
||||
update KEYWORD2
|
||||
forceUpdate KEYWORD2
|
||||
getDay KEYWORD2
|
||||
getHours KEYWORD2
|
||||
getMinutes KEYWORD2
|
||||
getSeconds KEYWORD2
|
||||
getFormattedTime KEYWORD2
|
||||
getEpochTime KEYWORD2
|
||||
setTimeOffset KEYWORD2
|
||||
setUpdateInterval KEYWORD2
|
||||
setPoolServerName KEYWORD2
|
@ -1,20 +0,0 @@
|
||||
{
|
||||
"name": "NTPClient",
|
||||
"keywords": "ntp, client, time",
|
||||
"description": "A NTPClient to connect to a time server",
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "Fabrice Weinberg",
|
||||
"email": "fabrice@weinberg.me"
|
||||
}
|
||||
],
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/arduino-libraries/NTPClient.git"
|
||||
},
|
||||
"version": "3.2.0",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "espressif"
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
name=NTPClient
|
||||
version=3.2.0
|
||||
author=Fabrice Weinberg
|
||||
maintainer=Fabrice Weinberg <fabrice@weinberg.me>
|
||||
sentence=An NTPClient to connect to a time server
|
||||
paragraph=Get time from a NTP server and keep it in sync.
|
||||
category=Timing
|
||||
url=https://github.com/arduino-libraries/NTPClient
|
||||
architectures=*
|
Loading…
Reference in New Issue
Block a user