Compare commits

...

2 Commits

Author SHA1 Message Date
Siwat Sirichai b62ef700ed working timezone config 2024-05-22 00:06:12 +07:00
Siwat Sirichai b30cfde0b0 working ntp without offset 2024-05-21 23:50:21 +07:00
4 changed files with 60 additions and 14 deletions

View File

@ -1,3 +1,3 @@
#pragma once #pragma once
#define SDK_VESRION "2.8.0" #define SDK_VESRION "2.9.0"

View File

@ -1,4 +1,5 @@
#include <ESPMegaProOS.hpp> #include <ESPMegaProOS.hpp>
#include "esp_sntp.h"
// Reserve FRAM address 0 - 1000 for ESPMegaPRO Internal Use // Reserve FRAM address 0 - 1000 for ESPMegaPRO Internal Use
// (34 Bytes) Address 0-33 for Built-in Digital Output Card // (34 Bytes) Address 0-33 for Built-in Digital Output Card
@ -11,6 +12,7 @@
*/ */
ESPMegaPRO::ESPMegaPRO() ESPMegaPRO::ESPMegaPRO()
{ {
} }
/** /**
@ -102,6 +104,13 @@ void ESPMegaPRO::loop()
if (iotEnabled) if (iotEnabled)
{ {
iot->loop(); 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) if (internalDisplayEnabled)
{ {
@ -153,19 +162,35 @@ bool ESPMegaPRO::installCard(uint8_t slot, ExpansionCard *card)
bool ESPMegaPRO::updateTimeFromNTP() bool ESPMegaPRO::updateTimeFromNTP()
{ {
struct tm timeinfo; 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(); ESP_LOGI("ESPMegaPRO", "NTP is not ready yet!");
if (rtctime.hours != timeinfo.tm_hour || rtctime.minutes != timeinfo.tm_min || return false;
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;
} }
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;
}
/**
* @brief Sets the timezone for the internal RTC.
*
* @note This function takes POSIX timezone strings (e.g. "EST5EDT,M3.2.0,M11.1.0").
*/
void ESPMegaPRO::setTimezone(const char* offset)
{
setenv("TZ", offset, 1);
} }
/** /**
@ -222,6 +247,10 @@ void ESPMegaPRO::enableIotModule()
this->iot->bindFRAM(&fram); this->iot->bindFRAM(&fram);
this->iot->intr_begin(cards); this->iot->intr_begin(cards);
iotEnabled = true; iotEnabled = true;
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, this->iot->getMqttConfig()->mqtt_server);
sntp_setservername(1, "pool.ntp.org");
sntp_init();
} }
/** /**

View File

@ -24,6 +24,11 @@
#define PWM_BANK_ADDRESS 0x5F #define PWM_BANK_ADDRESS 0x5F
#define RTC_ADDRESS 0x68 #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. * @brief The ESPMegaPRO class is the main class for the ESPMegaPRO library.
* *
@ -47,6 +52,7 @@ class ESPMegaPRO {
void enableIotModule(); void enableIotModule();
void enableInternalDisplay(HardwareSerial *serial); void enableInternalDisplay(HardwareSerial *serial);
void enableWebServer(uint16_t port); void enableWebServer(uint16_t port);
void setTimezone(const char* offset);
rtctime_t getTime(); rtctime_t getTime();
void dumpFRAMtoSerial(uint16_t start, uint16_t end); void dumpFRAMtoSerial(uint16_t start, uint16_t end);
void dumpFRAMtoSerialASCII(uint16_t start, uint16_t end); void dumpFRAMtoSerialASCII(uint16_t start, uint16_t end);

View File

@ -14,15 +14,16 @@
// #define FRAM_DEBUG // #define FRAM_DEBUG
// #define MQTT_DEBUG // #define MQTT_DEBUG
// #define RTC_DEBUG
#define WRITE_DEFAULT_NETCONF #define WRITE_DEFAULT_NETCONF
#define CLIMATE_CARD_ENABLE #define CLIMATE_CARD_ENABLE
#define MQTT_CARD_REGISTER #define MQTT_CARD_REGISTER
#define DISPLAY_ENABLE // #define DISPLAY_ENABLE
#define WEB_SERVER_ENABLE #define WEB_SERVER_ENABLE
#define LCD_OTA_ENABLE #define LCD_OTA_ENABLE
// #define REMOTE_VARIABLE_ENABLE // #define REMOTE_VARIABLE_ENABLE
// #define CT_ENABLE // #define CT_ENABLE
#define SMART_VARIABLE_ENABLE // #define SMART_VARIABLE_ENABLE
// Demo PLC firmware using the ESPMegaPRO OOP library // Demo PLC firmware using the ESPMegaPRO OOP library
@ -162,6 +163,7 @@ void setup()
{ {
ESP_LOGI("Initializer", "Starting ESPMegaPRO OOP demo"); ESP_LOGI("Initializer", "Starting ESPMegaPRO OOP demo");
espmega.begin(); espmega.begin();
espmega.setTimezone("UTC-7");
ESP_LOGI("Initializer", "Enabling IOT module"); ESP_LOGI("Initializer", "Enabling IOT module");
espmega.enableIotModule(); espmega.enableIotModule();
ESP_LOGI("Initializer", "Enabling Ethernet"); ESP_LOGI("Initializer", "Enabling Ethernet");
@ -326,4 +328,13 @@ void loop()
smartVar.setValue(last_smartvar_state ? "true" : "false"); smartVar.setValue(last_smartvar_state ? "true" : "false");
} }
#endif #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
} }