diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp index 59d64f4..ab46b85 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaCommon.hpp @@ -1,3 +1,3 @@ #pragma once -#define SDK_VESRION "2.8.0" \ No newline at end of file +#define SDK_VESRION "2.9.0" \ No newline at end of file diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaProOS.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaProOS.cpp index 5db70e5..d1a9c41 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaProOS.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaProOS.cpp @@ -1,4 +1,5 @@ #include +#include "esp_sntp.h" // Reserve FRAM address 0 - 1000 for ESPMegaPRO Internal Use // (34 Bytes) Address 0-33 for Built-in Digital Output Card @@ -11,6 +12,7 @@ */ ESPMegaPRO::ESPMegaPRO() { + } /** @@ -102,6 +104,13 @@ void ESPMegaPRO::loop() if (iotEnabled) { iot->loop(); + static int64_t lastNTPUpdate = (esp_timer_get_time() / 1000) - NTP_UPDATE_INTERVAL_MS + NTP_INITIAL_SYNC_DELAY_MS; + if ((esp_timer_get_time() / 1000) - lastNTPUpdate > NTP_UPDATE_INTERVAL_MS) + { + ESP_LOGV("ESPMegaPRO", "Updating time from NTP"); + lastNTPUpdate = esp_timer_get_time() / 1000; + this->updateTimeFromNTP(); + } } if (internalDisplayEnabled) { @@ -153,19 +162,35 @@ bool ESPMegaPRO::installCard(uint8_t slot, ExpansionCard *card) bool ESPMegaPRO::updateTimeFromNTP() { struct tm timeinfo; - if (getLocalTime(&timeinfo)) + uint32_t start = esp_timer_get_time() / 1000; + time_t now; + time(&now); + localtime_r(&now, &timeinfo); + if (!(timeinfo.tm_year > (2016 - 1900))) { - rtctime_t rtctime = this->getTime(); - if (rtctime.hours != timeinfo.tm_hour || rtctime.minutes != timeinfo.tm_min || - rtctime.seconds != timeinfo.tm_sec || rtctime.day != timeinfo.tm_mday || - rtctime.month != timeinfo.tm_mon + 1 || rtctime.year != timeinfo.tm_year + 1900) - { - this->setTime(timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, - timeinfo.tm_mday, timeinfo.tm_mon + 1, timeinfo.tm_year + 1900); - } - return true; + ESP_LOGI("ESPMegaPRO", "NTP is not ready yet!"); + return false; } - return false; + rtctime_t rtctime = this->getTime(); + if (rtctime.hours != timeinfo.tm_hour || rtctime.minutes != timeinfo.tm_min || + rtctime.seconds != timeinfo.tm_sec || rtctime.day != timeinfo.tm_mday || + rtctime.month != timeinfo.tm_mon + 1 || rtctime.year != timeinfo.tm_year + 1900) + { + this->setTime(timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, + timeinfo.tm_mday, timeinfo.tm_mon + 1, timeinfo.tm_year + 1900); + } + ESP_LOGV("ESPMegaPRO", "Time updated from NTP: %s", asctime(&timeinfo)); + return true; +} + +#include + +void ESPMegaPRO::setUTCOffset(int offset) +{ + std::ostringstream oss; + oss << "UTC" << (offset >= 0 ? "+" : "") << offset; + setenv("TZ", oss.str().c_str(), 1); + tzset(); } /** @@ -222,6 +247,10 @@ void ESPMegaPRO::enableIotModule() this->iot->bindFRAM(&fram); this->iot->intr_begin(cards); iotEnabled = true; + sntp_setoperatingmode(SNTP_OPMODE_POLL); + sntp_setservername(0, this->iot->getMqttConfig()->mqtt_server); + sntp_setservername(1, "pool.ntp.org"); + sntp_init(); } /** diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaProOS.hpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaProOS.hpp index 4b6bd14..2721a58 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaProOS.hpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaProOS.hpp @@ -24,6 +24,11 @@ #define PWM_BANK_ADDRESS 0x5F #define RTC_ADDRESS 0x68 +// Constants +#define NTP_TIMEOUT_MS 5000 +#define NTP_UPDATE_INTERVAL_MS 60000 +#define NTP_INITIAL_SYNC_DELAY_MS 15000 + /** * @brief The ESPMegaPRO class is the main class for the ESPMegaPRO library. * @@ -47,6 +52,7 @@ class ESPMegaPRO { void enableIotModule(); void enableInternalDisplay(HardwareSerial *serial); void enableWebServer(uint16_t port); + void setUTCOffset(int offset); rtctime_t getTime(); void dumpFRAMtoSerial(uint16_t start, uint16_t end); void dumpFRAMtoSerialASCII(uint16_t start, uint16_t end); diff --git a/ESPMegaPRO-OS-SDK/src/main.cpp b/ESPMegaPRO-OS-SDK/src/main.cpp index aa0bedf..03572b6 100644 --- a/ESPMegaPRO-OS-SDK/src/main.cpp +++ b/ESPMegaPRO-OS-SDK/src/main.cpp @@ -14,15 +14,16 @@ // #define FRAM_DEBUG // #define MQTT_DEBUG +// #define RTC_DEBUG #define WRITE_DEFAULT_NETCONF #define CLIMATE_CARD_ENABLE #define MQTT_CARD_REGISTER -#define DISPLAY_ENABLE +// #define DISPLAY_ENABLE #define WEB_SERVER_ENABLE #define LCD_OTA_ENABLE // #define REMOTE_VARIABLE_ENABLE // #define CT_ENABLE -#define SMART_VARIABLE_ENABLE +// #define SMART_VARIABLE_ENABLE // Demo PLC firmware using the ESPMegaPRO OOP library @@ -162,6 +163,7 @@ void setup() { ESP_LOGI("Initializer", "Starting ESPMegaPRO OOP demo"); espmega.begin(); + espmega.setUTCOffset(7); ESP_LOGI("Initializer", "Enabling IOT module"); espmega.enableIotModule(); ESP_LOGI("Initializer", "Enabling Ethernet"); @@ -326,4 +328,13 @@ void loop() smartVar.setValue(last_smartvar_state ? "true" : "false"); } #endif +#ifdef RTC_DEBUG + static uint32_t last_time_print = 0; + if (millis() - last_time_print >= 1000) + { + last_time_print = millis(); + rtctime_t time = espmega.getTime(); + Serial.printf("Time: %02d:%02d:%02d %02d/%02d/%04d\n", time.hours, time.minutes, time.seconds, time.day, time.month, time.year); + } +#endif } \ No newline at end of file