Skip to content

Commit 3dec845

Browse files
authored
Merge pull request #1 from theandy94/master
NTPClient PR arduino-libraries#71: Add method to test if time was set correctly
2 parents 020aaf8 + 81ac5d7 commit 3dec845

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

NTPClient.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ bool NTPClient::update() {
101101
return true;
102102
}
103103

104+
bool NTPClient::isTimeSet() const {
105+
return (this->_lastUpdate != 0); // returns true if the time has been set, else false
106+
}
107+
104108
unsigned long NTPClient::getEpochTime() const {
105109
return this->_timeOffset + // User offset
106110
this->_currentEpoc + // Epoc returned by the NTP server

NTPClient.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ class NTPClient {
6565
*/
6666
bool forceUpdate();
6767

68+
/**
69+
* This allows to check if the NTPClient successfully received a NTP packet and set the time.
70+
*
71+
* @return true if time has been set, else false
72+
*/
73+
bool isTimeSet() const;
74+
6875
int getDay() const;
6976
int getHours() const;
7077
int getMinutes() const;

examples/IsTimeSet/IsTimeSet.ino

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
// initialized to a time offset of 10 hours
13+
NTPClient timeClient(ntpUDP,"pool.ntp.org", 36000, 60000);
14+
// HH:MM:SS
15+
// timeClient initializes to 10:00:00 if it does not receive an NTP packet
16+
// before the 100ms timeout.
17+
// without isTimeSet() the LED would be switched on, although the time
18+
// was not yet set correctly.
19+
20+
// blue LED on ESP-12F
21+
const int led = 2;
22+
const int hour = 10;
23+
const int minute = 0;
24+
25+
void setup(){
26+
Serial.begin(115200);
27+
28+
pinMode(led, OUTPUT);
29+
// led is off when pin is high
30+
digitalWrite(led, 1);
31+
32+
WiFi.begin(ssid, password);
33+
34+
while (WiFi.status() != WL_CONNECTED) {
35+
delay (500);
36+
Serial.print (".");
37+
}
38+
39+
timeClient.begin();
40+
}
41+
42+
void loop() {
43+
timeClient.update();
44+
45+
Serial.println(timeClient.getFormattedTime());
46+
if(timeClient.isTimeSet()) {
47+
if (hour == timeClient.getHours() && minute == timeClient.getMinutes()) {
48+
digitalWrite(led, 0);
49+
}
50+
}
51+
52+
delay(1000);
53+
}

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ begin KEYWORD2
1212
end KEYWORD2
1313
update KEYWORD2
1414
forceUpdate KEYWORD2
15+
isTimeSet KEYWORD2
1516
getDay KEYWORD2
1617
getHours KEYWORD2
1718
getMinutes KEYWORD2

0 commit comments

Comments
 (0)