- This topic has 1 reply, 2 voices, and was last updated 4 years, 3 months ago by
Renzo Mischianti.
-
AuthorPosts
-
-
3 February 2021 at 00:20 #9669
Hello!
I have the example working from the webpage working just fine. However, I am struggling to figure out how to apply the newly correct time to the system clock…
I am trying to get this newly formated time into my sketch, but no luck.
… Assume same code up to void loop…
I just don’t think I understand how to apply the tz.toLocal method on a time object. (See time_t current_time() )
My serial monitor output:
02/02/2021 15:19:15
MyCrummyCode:23:19:15 2.2.2021 TuesdayÂvoid loop() { // I print the time from local clock but first I check DST // to add hours if needed Serial.println(getEpochStringByParams(usPT.toLocal(now()))); Serial.print("MyCrummyCode:"); digitalClockDisplay(); delay(1000); } tmElements_t tm; time_t current_time() { return make_unixtime(usPT.toLocal(second()), usPT.toLocal(minute()), usPT.toLocal(hour()), usPT.toLocal(day()), usPT.toLocal(month()), usPT.toLocal(year())); } time_t make_unixtime(uint8_t sec, uint8_t mn, uint8_t hr, uint8_t dy, uint8_t mon, uint16_t yr) { tm.Second = sec; tm.Hour = hr; tm.Minute = mn; tm.Day = dy; tm.Month = mon; tm.Year = yr - 1970; return makeTime(tm); } void digitalClockDisplay() { // digital clock display of the time if (DEBUG) { yield(); Serial.print(hour()); yield(); printDigits(minute()); yield(); printDigits(second()); yield(); Serial.print(" "); yield(); Serial.print(day()); yield(); Serial.print("."); yield(); Serial.print(month()); yield(); Serial.print("."); yield(); Serial.print(year()); yield(); Serial.print(" "); yield(); Serial.println(dayStr(weekday())); } } void printDigits(int digits) { // utility for digital clock display: prints preceding colon and leading 0 yield(); Serial.print(":"); if (digits & lt; 10) Serial.print('0'); yield(); Serial.print(digits); }
-
3 February 2021 at 08:33 #9673
Hi Cameron,
I understand what you want to do, but It’s discouraged because you lost some information (GTM for example), but here the code modified.
I advise you to use strftime that offer a lot of features here a list.I update local time directly when the value is returned because if you call the functione more than one time you apply N time the DST and you lost control of the time.
unsigned long epoch = tz.toLocal(timeClient.getEpochTime()); setTime(epoch);
and use a strftime like so
Serial.println(getEpochStringByParams(now(), "%H:%M:%S %d.%m.%Y It's a beautiful %A of %B"));
and the complete sketch
pay attention I add my timezone:/* * Simple NTP client update system time with DST applyed (discouraged) * http://mischianti.org/ * * The MIT License (MIT) * written by Renzo Mischianti */ #define DEBUG const char *ssid = ""; const char *password = ""; #include // change next line to use with another board/shield #include //#include // for WiFi shield //#include // for WiFi 101 shield or MKR1000 #include #include #include #include // https://github.com/JChristensen/Timezone /** * Input time in epoch format and return tm time format * by Renzo Mischianti */ static tm getDateTimeByParams(long time) { struct tm *newtime; const time_t tim = time; newtime = localtime(&tim); return *newtime; } /** * Input tm time format and return String with format pattern * by Renzo Mischianti */ static String getDateTimeStringByParams(tm *newtime, char *pattern = (char*) "%d/%m/%Y %H:%M:%S") { char buffer[90]; strftime(buffer, 90, pattern, newtime); return buffer; } /** * Input time in epoch format format and return String with format pattern * by Renzo Mischianti */ static String getEpochStringByParams(long time, char *pattern = (char*) "%d/%m/%Y %H:%M:%S") { // struct tm *newtime; tm newtime; newtime = getDateTimeByParams(time); return getDateTimeStringByParams(&newtime, pattern); } WiFiUDP ntpUDP; // By default 'pool.ntp.org' is used with 60 seconds update interval and // no offset // NTPClient timeClient(ntpUDP); // You can specify the time server pool and the offset, (in seconds) // additionaly you can specify the update interval (in milliseconds). int GTMOffset = 0; // SET TO UTC TIME NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", GTMOffset * 60 * 60, 60 * 60 * 1000); // Central European Time (Frankfurt, Paris) TimeChangeRule CEST = { "CEST", Last, Sun, Mar, 2, 120 }; // Central European Summer Time TimeChangeRule CET = { "CET ", Last, Sun, Oct, 3, 60 }; // Central European Standard Time Timezone tz(CEST, CET); //// US Pacific Time Zone (Las Vegas, Los Angeles) //TimeChangeRule usPDT = {"PDT", Second, Sun, Mar, 2, -420}; //TimeChangeRule usPST = {"PST", First, Sun, Nov, 2, -480}; //Timezone tz(usPDT, usPST); void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } timeClient.begin(); delay(1000); if (timeClient.update()) { Serial.print("Adjust local clock"); // unsigned long epoch = timeClient.getEpochTime(); unsigned long epoch = tz.toLocal(timeClient.getEpochTime()); setTime(epoch); } else { Serial.print("NTP Update not WORK!!"); } } void loop() { // I print the time from local clock but first I check DST // to add hours if needed // Serial.println(getEpochStringByParams(tz.toLocal(now()))); Serial.println(getEpochStringByParams(now(), "%H:%M:%S %d.%m.%Y It's a beautiful %A of %B")); Serial.print("MyCrummyCode:"); digitalClockDisplay(); delay(1000); } tmElements_t tm; time_t current_time() { return make_unixtime(tz.toLocal(second()), tz.toLocal(minute()), tz.toLocal(hour()), tz.toLocal(day()), tz.toLocal(month()), tz.toLocal(year())); } time_t make_unixtime(uint8_t sec, uint8_t mn, uint8_t hr, uint8_t dy, uint8_t mon, uint16_t yr) { tm.Second = sec; tm.Hour = hr; tm.Minute = mn; tm.Day = dy; tm.Month = mon; tm.Year = yr - 1970; return makeTime(tm); } void digitalClockDisplay() { // digital clock display of the time #ifdef DEBUG yield(); Serial.print(hour()); yield(); printDigits(minute()); yield(); printDigits(second()); yield(); Serial.print(" "); yield(); Serial.print(day()); yield(); Serial.print("."); yield(); Serial.print(month()); yield(); Serial.print("."); yield(); Serial.print(year()); yield(); Serial.print(" "); yield(); Serial.println(dayStr(weekday())); #endif } void printDigits(int digits) { // utility for digital clock display: prints preceding colon and leading 0 yield(); Serial.print(":"); if (digits < 10) Serial.print('0'); yield(); Serial.print(digits); }
and here the result
MyCrummyCode:7:26:58 3.2.2021 Wednesday 07:26:59 03.02.2021 It's a beautiful Wednesday of February MyCrummyCode:7:26:59 3.2.2021 Wednesday 07:27:00 03.02.2021 It's a beautiful Wednesday of February MyCrummyCode:7:27:00 3.2.2021 Wednesday 07:27:01 03.02.2021 It's a beautiful Wednesday of February MyCrummyCode:7:27:01 3.2.2021 Wednesday 07:27:02 03.02.2021 It's a beautiful Wednesday of February MyCrummyCode:7:27:02 3.2.2021 Wednesday 07:27:03 03.02.2021 It's a beautiful Wednesday of February MyCrummyCode:7:27:03 3.2.2021 Wednesday
Bye Renzo
Attachments:
You must be logged in to view attached files.
-
-
AuthorPosts
- You must be logged in to reply to this topic.