Skip to content

Commit 92f6b4a

Browse files
committed
add timeLib
1 parent cf52cbb commit 92f6b4a

File tree

10 files changed

+457
-0
lines changed

10 files changed

+457
-0
lines changed

NTPClient-master/.travis.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: c
2+
sudo: false
3+
before_install:
4+
- source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh)
5+
script:
6+
- build_platform esp8266
7+
notifications:
8+
email:
9+
on_success: change
10+
on_failure: change

NTPClient-master/CHANGELOG

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
NTPClient 3.1.0 - 2016.05.31
2+
3+
* Added functions for changing the timeOffset and updateInterval later. Thanks @SirUli
4+
5+
NTPClient 3.0.0 - 2016.04.19
6+
7+
* Constructors now require UDP instance argument, to add support for non-ESP8266 boards
8+
* Added optional begin API to override default local port
9+
* Added end API to close UDP socket
10+
* Changed return type of update and forceUpdate APIs to bool, and return success or failure
11+
* Change return type of getDay, getHours, getMinutes, and getSeconds to int
12+
13+
Older
14+
15+
* Changes not recorded

NTPClient-master/NTPClient.cpp

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
}

NTPClient-master/NTPClient.h

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#pragma once
2+
3+
#include "Arduino.h"
4+
5+
#include <Udp.h>
6+
7+
#define SEVENZYYEARS 2208988800UL
8+
#define NTP_PACKET_SIZE 48
9+
#define NTP_DEFAULT_LOCAL_PORT 1337
10+
11+
class NTPClient {
12+
private:
13+
UDP* _udp;
14+
bool _udpSetup = false;
15+
16+
const char* _poolServerName = "time.nist.gov"; // Default time server
17+
int _port = NTP_DEFAULT_LOCAL_PORT;
18+
int _timeOffset = 0;
19+
20+
unsigned int _updateInterval = 60000; // In ms
21+
22+
unsigned long _currentEpoc = 0; // In s
23+
unsigned long _lastUpdate = 0; // In ms
24+
25+
byte _packetBuffer[NTP_PACKET_SIZE];
26+
27+
void sendNTPPacket();
28+
29+
public:
30+
NTPClient(UDP& udp);
31+
NTPClient(UDP& udp, int timeOffset);
32+
NTPClient(UDP& udp, const char* poolServerName);
33+
NTPClient(UDP& udp, const char* poolServerName, int timeOffset);
34+
NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval);
35+
36+
/**
37+
* Starts the underlying UDP client with the default local port
38+
*/
39+
void begin();
40+
41+
/**
42+
* Starts the underlying UDP client with the specified local port
43+
*/
44+
void begin(int port);
45+
46+
/**
47+
* This should be called in the main loop of your application. By default an update from the NTP Server is only
48+
* made every 60 seconds. This can be configured in the NTPClient constructor.
49+
*
50+
* @return true on success, false on failure
51+
*/
52+
bool update();
53+
54+
/**
55+
* This will force the update from the NTP Server.
56+
*
57+
* @return true on success, false on failure
58+
*/
59+
bool forceUpdate();
60+
61+
int getDay();
62+
int getHours();
63+
int getMinutes();
64+
int getSeconds();
65+
66+
/**
67+
* Changes the time offset. Useful for changing timezones dynamically
68+
*/
69+
void setTimeOffset(int timeOffset);
70+
71+
/**
72+
* Set the update interval to another frequency. E.g. useful when the
73+
* timeOffset should not be set in the constructor
74+
*/
75+
void setUpdateInterval(int updateInterval);
76+
77+
/**
78+
* @return time formatted like `hh:mm:ss`
79+
*/
80+
String getFormattedTime();
81+
82+
/**
83+
* @return time in seconds since Jan. 1, 1970
84+
*/
85+
unsigned long getEpochTime();
86+
87+
/**
88+
* Stops the underlying UDP client
89+
*/
90+
void end();
91+
};

NTPClient-master/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# NTPClient
2+
3+
[![Build Status](https://travis-ci.org/arduino-libraries/NTPClient.svg?branch=master)](https://travis-ci.org/arduino-libraries/NTPClient)
4+
5+
Connect to a NTP server, here is how:
6+
7+
```cpp
8+
#include <NTPClient.h>
9+
// change next line to use with another board/shield
10+
#include <ESP8266WiFi.h>
11+
//#include <WiFi.h> // for WiFi shield
12+
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
13+
#include <WiFiUdp.h>
14+
15+
const char *ssid = "<SSID>";
16+
const char *password = "<PASSWORD>";
17+
18+
WiFiUDP ntpUDP;
19+
20+
// By default 'time.nist.gov' is used with 60 seconds update interval and
21+
// no offset
22+
NTPClient timeClient(ntpUDP);
23+
24+
// You can specify the time server pool and the offset, (in seconds)
25+
// additionaly you can specify the update interval (in milliseconds).
26+
// NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);
27+
28+
void setup(){
29+
Serial.begin(115200);
30+
WiFi.begin(ssid, password);
31+
32+
while ( WiFi.status() != WL_CONNECTED ) {
33+
delay ( 500 );
34+
Serial.print ( "." );
35+
}
36+
37+
timeClient.begin();
38+
}
39+
40+
void loop() {
41+
timeClient.update();
42+
43+
Serial.println(timeClient.getFormattedTime());
44+
45+
delay(1000);
46+
}
47+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <NTPClient.h>
2+
// change next line to use with another board/shield
3+
#include <ESP8266WiFi.h>
4+
//#include <WiFi.h> // for WiFi shield
5+
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
6+
#include <WiFiUdp.h>
7+
8+
const char *ssid = "<SSID>";
9+
const char *password = "<PASSWORD>";
10+
11+
WiFiUDP ntpUDP;
12+
13+
// You can specify the time server pool and the offset (in seconds, can be
14+
// changed later with setTimeOffset() ). Additionaly you can specify the
15+
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
16+
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);
17+
18+
void setup(){
19+
Serial.begin(115200);
20+
21+
WiFi.begin(ssid, password);
22+
23+
while ( WiFi.status() != WL_CONNECTED ) {
24+
delay ( 500 );
25+
Serial.print ( "." );
26+
}
27+
28+
timeClient.begin();
29+
}
30+
31+
void loop() {
32+
timeClient.update();
33+
34+
Serial.println(timeClient.getFormattedTime());
35+
36+
delay(1000);
37+
}

0 commit comments

Comments
 (0)