Skip to content

Commit 367254d

Browse files
authored
Merge pull request #71 from theandy94/master
added method to test if time was set correctly
2 parents 8bcfd2e + 81ac5d7 commit 367254d

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

Diff for: NTPClient.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ bool NTPClient::update() {
126126
return false; // return false if update does not occur
127127
}
128128

129+
bool NTPClient::isTimeSet() const {
130+
return (this->_lastUpdate != 0); // returns true if the time has been set, else false
131+
}
132+
129133
unsigned long NTPClient::getEpochTime() const {
130134
return this->_timeOffset + // User offset
131135
this->_currentEpoc + // Epoch returned by the NTP server

Diff for: NTPClient.h

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ class NTPClient {
7474
*/
7575
bool forceUpdate();
7676

77+
/**
78+
* This allows to check if the NTPClient successfully received a NTP packet and set the time.
79+
*
80+
* @return true if time has been set, else false
81+
*/
82+
bool isTimeSet() const;
83+
7784
int getDay() const;
7885
int getHours() const;
7986
int getMinutes() const;

Diff for: examples/IsTimeSet/IsTimeSet.ino

+53
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+
}

Diff for: keywords.txt

+1
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)