diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index 185383bab3b28088c80694f31e84bf332396e238..b4dde5a830ed4cdd114c108d127d17e1442ab5ea 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -162,6 +162,10 @@ uint16_t makeWord(byte h, byte l); unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); +extern "C" bool getLocalTime(struct tm * info, uint32_t ms = 5000); +extern "C" void configTime(long gmtOffset_sec, int daylightOffset_sec, + const char* server1, const char* server2 = nullptr, const char* server3 = nullptr); + // WMath prototypes long random(long); #endif /* __cplusplus */ diff --git a/cores/esp32/Print.cpp b/cores/esp32/Print.cpp index 5afa9dedd424456c3cc8be3276096164b3ba4a56..8c1d46e353296a83b5148283682e8aab7d2429e9 100644 --- a/cores/esp32/Print.cpp +++ b/cores/esp32/Print.cpp @@ -29,7 +29,7 @@ #include "Print.h" extern "C" { -//#include "esp_common.h" + #include "time.h" } // Public Methods ////////////////////////////////////////////////////////////// @@ -153,6 +153,18 @@ size_t Print::print(const Printable& x) return x.printTo(*this); } +size_t Print::print(struct tm * timeinfo, const char * format) +{ + const char * f = format; + if(!f){ + f = "%c"; + } + char buf[64]; + size_t written = strftime(buf, 64, f, timeinfo); + print(buf); + return written; +} + size_t Print::println(void) { return print("\r\n"); @@ -228,6 +240,13 @@ size_t Print::println(const Printable& x) return n; } +size_t Print::println(struct tm * timeinfo, const char * format) +{ + size_t n = print(timeinfo, format); + n += println(); + return n; +} + // Private Methods ///////////////////////////////////////////////////////////// size_t Print::printNumber(unsigned long n, uint8_t base) diff --git a/cores/esp32/Print.h b/cores/esp32/Print.h index b792799dd9c4be26739374a13f537bacc74bf9e2..d1763aa676a928609b0b42f767807100462941f5 100644 --- a/cores/esp32/Print.h +++ b/cores/esp32/Print.h @@ -83,6 +83,7 @@ public: size_t print(unsigned long, int = DEC); size_t print(double, int = 2); size_t print(const Printable&); + size_t print(struct tm * timeinfo, const char * format = NULL); //size_t println(const __FlashStringHelper *); size_t println(const String &s); @@ -95,6 +96,7 @@ public: size_t println(unsigned long, int = DEC); size_t println(double, int = 2); size_t println(const Printable&); + size_t println(struct tm * timeinfo, const char * format = NULL); size_t println(void); }; diff --git a/cores/esp32/esp32-hal-time.c b/cores/esp32/esp32-hal-time.c new file mode 100644 index 0000000000000000000000000000000000000000..ad22bc354f2d15d3e79b5390f0c3cf928177fc2e --- /dev/null +++ b/cores/esp32/esp32-hal-time.c @@ -0,0 +1,82 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "esp32-hal.h" +#include "apps/sntp/sntp.h" + +static void setTimeZone(long offset, int daylight) +{ + char cst[16] = {0}; + char cdt[16] = "CDT"; + char tz[32] = {0}; + + if(offset % 3600){ + sprintf(cst, "CST%ld:%02u:%02u", offset / 3600, abs((offset % 3600) / 60), abs(offset % 60)); + } else { + sprintf(cst, "CST%ld", offset / 3600); + } + if(daylight != 3600){ + long tz_dst = offset - daylight; + if(tz_dst % 3600){ + sprintf(cdt, "CDT%ld:%02u:%02u", tz_dst / 3600, abs((tz_dst % 3600) / 60), abs(tz_dst % 60)); + } else { + sprintf(cdt, "CDT%ld", tz_dst / 3600); + } + } + sprintf(tz, "%s%s", cst, cdt); + setenv("TZ", tz, 1); + tzset(); +} + +/* + * configTime + * Source: https://github.com/esp8266/Arduino/blob/master/cores/esp8266/time.c + * */ +void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3) +{ + + if(sntp_enabled()){ + sntp_stop(); + } + sntp_setoperatingmode(SNTP_OPMODE_POLL); + sntp_setservername(0, (char*)server1); + sntp_setservername(1, (char*)server2); + sntp_setservername(2, (char*)server3); + sntp_init(); + setTimeZone(gmtOffset_sec, daylightOffset_sec); +} + +bool getLocalTime(struct tm * info, uint32_t ms) +{ + uint32_t count = ms / 10; + time_t now; + + time(&now); + localtime_r(&now, info); + + if(info->tm_year > (2016 - 1900)){ + return true; + } + + while(count--) { + delay(10); + time(&now); + localtime_r(&now, info); + if(info->tm_year > (2016 - 1900)){ + return true; + } + } + return false; +} +