From b9fa22617b56e4e6a9cdf619ca39f1fcd1f0a2e4 Mon Sep 17 00:00:00 2001 From: theandy94 Date: Sat, 18 May 2019 10:57:04 +0200 Subject: [PATCH 1/8] added method to test if time was set correctly --- NTPClient.cpp | 4 +++ NTPClient.h | 7 +++++ examples/IsTimeSet/IsTimeSet.ino | 53 ++++++++++++++++++++++++++++++++ keywords.txt | 1 + 4 files changed, 65 insertions(+) create mode 100644 examples/IsTimeSet/IsTimeSet.ino diff --git a/NTPClient.cpp b/NTPClient.cpp index 87b7a53..6dcb148 100644 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -101,6 +101,10 @@ bool NTPClient::update() { return true; } +bool NTPClient::isTimeSet() const { + return (this->_lastUpdate != 0); // returns true if the time has been set, else false +} + unsigned long NTPClient::getEpochTime() const { return this->_timeOffset + // User offset this->_currentEpoc + // Epoc returned by the NTP server diff --git a/NTPClient.h b/NTPClient.h index 02d8752..d5363b1 100644 --- a/NTPClient.h +++ b/NTPClient.h @@ -65,6 +65,13 @@ class NTPClient { */ bool forceUpdate(); + /** + * This allows to check if the NTPClient successfully received a NTP packet and set the time. + * + * @return true if time has been set, else false + */ + bool isTimeSet() const; + int getDay() const; int getHours() const; int getMinutes() const; diff --git a/examples/IsTimeSet/IsTimeSet.ino b/examples/IsTimeSet/IsTimeSet.ino new file mode 100644 index 0000000..765372b --- /dev/null +++ b/examples/IsTimeSet/IsTimeSet.ino @@ -0,0 +1,53 @@ +#include +// change next line to use with another board/shield +#include +//#include // for WiFi shield +//#include // for WiFi 101 shield or MKR1000 +#include + +const char *ssid = ""; +const char *password = ""; + +WiFiUDP ntpUDP; +// initialized to a time offset of 10 hours +NTPClient timeClient(ntpUDP,"pool.ntp.org", 36000,); +// HH:MM:SS +// timeClient initializes to 10:00:00 if it does not receive an NTP packet +// before the 100ms timeout. +// Without isTimeSet() the LED would be switched on, although the time +// was not yet set correctly. + +// blue led on ESP12F +const int led = 2; +const int hour = 10; +const int minute = 0; + +void setup(){ + Serial.begin(115200); + + pinMode(led, OUTPUT); + // led is off when pin is high + digitalWrite(led, 1); + + WiFi.begin(ssid, password); + + while ( WiFi.status() != WL_CONNECTED ) { + delay ( 500 ); + Serial.print ( "." ); + } + + timeClient.begin(); +} + +void loop() { + timeClient.update(); + + Serial.println(timeClient.getFormattedTime()); + if(timeClient.isTimeSet()) { + if (hour == timeClient.getHours() && minute == timeClient.getMinutes()) { + digitalWrite(led, 0); + } + } + + delay(1000); +} diff --git a/keywords.txt b/keywords.txt index 6f7bc6f..b6876c1 100644 --- a/keywords.txt +++ b/keywords.txt @@ -12,6 +12,7 @@ begin KEYWORD2 end KEYWORD2 update KEYWORD2 forceUpdate KEYWORD2 +isTimeSet KEYWORD2 getDay KEYWORD2 getHours KEYWORD2 getMinutes KEYWORD2 From 69ce756af8ce2aeb8c6d0ea566ebe1b2f923143c Mon Sep 17 00:00:00 2001 From: theandy94 Date: Sat, 18 May 2019 11:19:10 +0200 Subject: [PATCH 2/8] added method to test if time was set correctly --- examples/IsTimeSet/IsTimeSet.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/IsTimeSet/IsTimeSet.ino b/examples/IsTimeSet/IsTimeSet.ino index 765372b..97e2c60 100644 --- a/examples/IsTimeSet/IsTimeSet.ino +++ b/examples/IsTimeSet/IsTimeSet.ino @@ -10,7 +10,7 @@ const char *password = ""; WiFiUDP ntpUDP; // initialized to a time offset of 10 hours -NTPClient timeClient(ntpUDP,"pool.ntp.org", 36000,); +NTPClient timeClient(ntpUDP,"pool.ntp.org", 36000, 60000); // HH:MM:SS // timeClient initializes to 10:00:00 if it does not receive an NTP packet // before the 100ms timeout. From b0328556f5aedeeda02bd34951a37b57fce18373 Mon Sep 17 00:00:00 2001 From: theandy94 Date: Thu, 23 May 2019 15:42:09 +0200 Subject: [PATCH 3/8] Update keywords.txt Co-Authored-By: per1234 --- keywords.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keywords.txt b/keywords.txt index b6876c1..edce989 100644 --- a/keywords.txt +++ b/keywords.txt @@ -12,7 +12,7 @@ begin KEYWORD2 end KEYWORD2 update KEYWORD2 forceUpdate KEYWORD2 -isTimeSet KEYWORD2 +isTimeSet KEYWORD2 getDay KEYWORD2 getHours KEYWORD2 getMinutes KEYWORD2 From eea4863c9ad35f1516cdfb37416f74b25a280ad8 Mon Sep 17 00:00:00 2001 From: theandy94 Date: Thu, 23 May 2019 15:42:17 +0200 Subject: [PATCH 4/8] Update examples/IsTimeSet/IsTimeSet.ino Co-Authored-By: per1234 --- examples/IsTimeSet/IsTimeSet.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/IsTimeSet/IsTimeSet.ino b/examples/IsTimeSet/IsTimeSet.ino index 97e2c60..d4c263e 100644 --- a/examples/IsTimeSet/IsTimeSet.ino +++ b/examples/IsTimeSet/IsTimeSet.ino @@ -32,7 +32,7 @@ void setup(){ WiFi.begin(ssid, password); while ( WiFi.status() != WL_CONNECTED ) { - delay ( 500 ); + delay (500); Serial.print ( "." ); } From 471edf092cef5bdb43f7d436b12be8cfc9ed810c Mon Sep 17 00:00:00 2001 From: theandy94 Date: Thu, 23 May 2019 15:42:23 +0200 Subject: [PATCH 5/8] Update examples/IsTimeSet/IsTimeSet.ino Co-Authored-By: per1234 --- examples/IsTimeSet/IsTimeSet.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/IsTimeSet/IsTimeSet.ino b/examples/IsTimeSet/IsTimeSet.ino index d4c263e..108ef84 100644 --- a/examples/IsTimeSet/IsTimeSet.ino +++ b/examples/IsTimeSet/IsTimeSet.ino @@ -33,7 +33,7 @@ void setup(){ while ( WiFi.status() != WL_CONNECTED ) { delay (500); - Serial.print ( "." ); + Serial.print ("."); } timeClient.begin(); From 0bb26f7cccbd9aa44493636d868dc4841e18cbe2 Mon Sep 17 00:00:00 2001 From: theandy94 Date: Thu, 23 May 2019 15:42:29 +0200 Subject: [PATCH 6/8] Update examples/IsTimeSet/IsTimeSet.ino Co-Authored-By: per1234 --- examples/IsTimeSet/IsTimeSet.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/IsTimeSet/IsTimeSet.ino b/examples/IsTimeSet/IsTimeSet.ino index 108ef84..175fa1c 100644 --- a/examples/IsTimeSet/IsTimeSet.ino +++ b/examples/IsTimeSet/IsTimeSet.ino @@ -14,7 +14,7 @@ NTPClient timeClient(ntpUDP,"pool.ntp.org", 36000, 60000); // HH:MM:SS // timeClient initializes to 10:00:00 if it does not receive an NTP packet // before the 100ms timeout. -// Without isTimeSet() the LED would be switched on, although the time +// without isTimeSet() the LED would be switched on, although the time // was not yet set correctly. // blue led on ESP12F From 1ebdff655d639d7095fb04dbe2ddc9d9f911fc26 Mon Sep 17 00:00:00 2001 From: theandy94 Date: Thu, 23 May 2019 15:42:37 +0200 Subject: [PATCH 7/8] Update examples/IsTimeSet/IsTimeSet.ino Co-Authored-By: per1234 --- examples/IsTimeSet/IsTimeSet.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/IsTimeSet/IsTimeSet.ino b/examples/IsTimeSet/IsTimeSet.ino index 175fa1c..24af787 100644 --- a/examples/IsTimeSet/IsTimeSet.ino +++ b/examples/IsTimeSet/IsTimeSet.ino @@ -17,7 +17,7 @@ NTPClient timeClient(ntpUDP,"pool.ntp.org", 36000, 60000); // without isTimeSet() the LED would be switched on, although the time // was not yet set correctly. -// blue led on ESP12F +// blue LED on ESP-12F const int led = 2; const int hour = 10; const int minute = 0; From 81ac5d71a1a60dfde1759d876904d06458eda2db Mon Sep 17 00:00:00 2001 From: theandy94 Date: Thu, 23 May 2019 15:42:45 +0200 Subject: [PATCH 8/8] Update examples/IsTimeSet/IsTimeSet.ino Co-Authored-By: per1234 --- examples/IsTimeSet/IsTimeSet.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/IsTimeSet/IsTimeSet.ino b/examples/IsTimeSet/IsTimeSet.ino index 24af787..619bfde 100644 --- a/examples/IsTimeSet/IsTimeSet.ino +++ b/examples/IsTimeSet/IsTimeSet.ino @@ -31,7 +31,7 @@ void setup(){ WiFi.begin(ssid, password); - while ( WiFi.status() != WL_CONNECTED ) { + while (WiFi.status() != WL_CONNECTED) { delay (500); Serial.print ("."); }