Skip to content

Commit f73103b

Browse files
aentingerchrisy
authored andcommitted
Merge pull request arduino-libraries#71 from theandy94/master
added method to test if time was set correctly
1 parent 89a26c6 commit f73103b

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

NTPClient.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ bool NTPClient::update() {
153153
return false; // return false if update does not occur
154154
}
155155

156+
bool NTPClient::isTimeSet() const {
157+
return (this->_lastUpdate != 0); // returns true if the time has been set, else false
158+
}
159+
156160
unsigned long NTPClient::getEpochTime() const {
157161
return this->_timeOffset + // User offset
158162
this->_currentEpoc + // Epoc returned by the NTP server

NTPClient.h

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ class NTPClient {
7878
*/
7979
bool forceUpdate();
8080

81+
/**
82+
* This allows to check if the NTPClient successfully received a NTP packet and set the time.
83+
*
84+
* @return true if time has been set, else false
85+
*/
86+
bool isTimeSet() const;
87+
8188
int getDay() const;
8289
int getHours() const;
8390
int getMinutes() const;

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

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)