From 45c24e95508fc18ffdf046466b570abaac34167f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 24 Oct 2025 14:09:59 -0700 Subject: [PATCH 1/3] [sntp] Store server strings in flash memory --- esphome/components/sntp/sntp_component.cpp | 16 ++++++++++--- esphome/components/sntp/sntp_component.h | 22 +++++++++++++----- esphome/components/sntp/time.py | 26 +++++++++++++++++++++- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/esphome/components/sntp/sntp_component.cpp b/esphome/components/sntp/sntp_component.cpp index 1cca5e8043..1457045d29 100644 --- a/esphome/components/sntp/sntp_component.cpp +++ b/esphome/components/sntp/sntp_component.cpp @@ -27,7 +27,7 @@ void SNTPComponent::setup() { esp_sntp_setoperatingmode(ESP_SNTP_OPMODE_POLL); size_t i = 0; for (auto &server : this->servers_) { - esp_sntp_setservername(i++, server.c_str()); + esp_sntp_setservername(i++, server); } esp_sntp_set_sync_interval(this->get_update_interval()); esp_sntp_set_time_sync_notification_cb([](struct timeval *tv) { @@ -42,7 +42,16 @@ void SNTPComponent::setup() { size_t i = 0; for (auto &server : this->servers_) { - sntp_setservername(i++, server.c_str()); +#if defined(USE_ESP8266) + // On ESP8266, server is PGM_P pointing to PROGMEM + // LWIP's sntp_setservername is not PROGMEM-aware, so copy to stack buffer first + char server_buf[64]; + strncpy_P(server_buf, server, sizeof(server_buf) - 1); + server_buf[sizeof(server_buf) - 1] = '\0'; + sntp_setservername(i++, server_buf); +#else + sntp_setservername(i++, server); +#endif } #if defined(USE_ESP8266) @@ -59,7 +68,8 @@ void SNTPComponent::dump_config() { ESP_LOGCONFIG(TAG, "SNTP Time:"); size_t i = 0; for (auto &server : this->servers_) { - ESP_LOGCONFIG(TAG, " Server %zu: '%s'", i++, server.c_str()); + // LOG_STR_ARG handles both PROGMEM (ESP8266) and regular pointers + ESP_LOGCONFIG(TAG, " Server %zu: '%s'", i++, LOG_STR_ARG(server)); } } void SNTPComponent::update() { diff --git a/esphome/components/sntp/sntp_component.h b/esphome/components/sntp/sntp_component.h index dd4c71e082..a320bf474c 100644 --- a/esphome/components/sntp/sntp_component.h +++ b/esphome/components/sntp/sntp_component.h @@ -2,10 +2,18 @@ #include "esphome/core/component.h" #include "esphome/components/time/real_time_clock.h" +#include + +#ifdef USE_ESP8266 +#include +#endif namespace esphome { namespace sntp { +// Server count is calculated at compile time by Python codegen +// SNTP_SERVER_COUNT will always be defined + /// The SNTP component allows you to configure local timekeeping via Simple Network Time Protocol. /// /// \note @@ -14,10 +22,7 @@ namespace sntp { /// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html class SNTPComponent : public time::RealTimeClock { public: - SNTPComponent(const std::vector &servers) : servers_(servers) {} - - // Note: set_servers() has been removed and replaced by a constructor - calling set_servers after setup would - // have had no effect anyway, and making the strings immutable avoids the need to strdup their contents. + SNTPComponent() = default; void setup() override; void dump_config() override; @@ -28,8 +33,15 @@ class SNTPComponent : public time::RealTimeClock { void time_synced(); +#ifdef USE_ESP8266 + // On ESP8266, store pointers to PROGMEM strings to save RAM + std::array servers_{}; +#else + // On other platforms, store regular const char pointers + std::array servers_{}; +#endif + protected: - std::vector servers_; bool has_time_{false}; #if defined(USE_ESP32) diff --git a/esphome/components/sntp/time.py b/esphome/components/sntp/time.py index 1c8ee402ad..7571cf198b 100644 --- a/esphome/components/sntp/time.py +++ b/esphome/components/sntp/time.py @@ -12,6 +12,7 @@ from esphome.const import ( PLATFORM_RTL87XX, ) from esphome.core import CORE +from esphome.cpp_generator import ProgmemAssignmentExpression DEPENDENCIES = ["network"] sntp_ns = cg.esphome_ns.namespace("sntp") @@ -43,11 +44,34 @@ CONFIG_SCHEMA = cv.All( async def to_code(config): servers = config[CONF_SERVERS] - var = cg.new_Pvariable(config[CONF_ID], servers) + server_count = len(servers) + + # Define server count at compile time + cg.add_define("SNTP_SERVER_COUNT", server_count) + + var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) await time_.register_time(var, config) + # Generate PROGMEM strings for ESP8266, regular strings for other platforms + if CORE.is_esp8266: + # On ESP8266, use PROGMEM to store strings in flash + # Use ProgmemAssignmentExpression to generate: static const char name[] PROGMEM = "value"; + for i, server in enumerate(servers): + var_name = f"{config[CONF_ID].id}_server_{i}" + # Create PROGMEM string: static const char var_name[] PROGMEM = "server"; + assignment = ProgmemAssignmentExpression( + "char", var_name, cg.safe_exp(server) + ) + cg.add(assignment) + # Assign pointer to array element + cg.add(cg.RawStatement(f"{var}->servers_[{i}] = {var_name};")) + else: + # On other platforms, use regular string literals + for i, server in enumerate(servers): + cg.add(cg.RawStatement(f"{var}->servers_[{i}] = {cg.safe_exp(server)};")) + if CORE.is_esp8266 and len(servers) > 1: # We need LwIP features enabled to get 3 SNTP servers (not just one) cg.add_build_flag("-DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY") From 45770811d22f395f5779c9a4bfef2349f6819da2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 24 Oct 2025 14:13:41 -0700 Subject: [PATCH 2/3] [sntp] Store server strings in flash memory --- esphome/core/defines.h | 1 + 1 file changed, 1 insertion(+) diff --git a/esphome/core/defines.h b/esphome/core/defines.h index 39698c1004..8095ffed4a 100644 --- a/esphome/core/defines.h +++ b/esphome/core/defines.h @@ -87,6 +87,7 @@ #define USE_MDNS_STORE_SERVICES #define MDNS_SERVICE_COUNT 3 #define MDNS_DYNAMIC_TXT_COUNT 3 +#define SNTP_SERVER_COUNT 3 #define USE_MEDIA_PLAYER #define USE_NEXTION_TFT_UPLOAD #define USE_NUMBER From 54fb391f13a72a76fe5b0ddfd0d821d62fc00b0d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 24 Oct 2025 14:26:17 -0700 Subject: [PATCH 3/3] cleanup --- esphome/components/sntp/sntp_component.h | 9 ++++----- esphome/components/sntp/time.py | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/esphome/components/sntp/sntp_component.h b/esphome/components/sntp/sntp_component.h index a320bf474c..d5ce25e8ee 100644 --- a/esphome/components/sntp/sntp_component.h +++ b/esphome/components/sntp/sntp_component.h @@ -22,7 +22,7 @@ namespace sntp { /// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html class SNTPComponent : public time::RealTimeClock { public: - SNTPComponent() = default; + template SNTPComponent(Args... servers) : servers_{servers...} {} void setup() override; void dump_config() override; @@ -33,15 +33,14 @@ class SNTPComponent : public time::RealTimeClock { void time_synced(); + protected: #ifdef USE_ESP8266 // On ESP8266, store pointers to PROGMEM strings to save RAM - std::array servers_{}; + std::array servers_; #else // On other platforms, store regular const char pointers - std::array servers_{}; + std::array servers_; #endif - - protected: bool has_time_{false}; #if defined(USE_ESP32) diff --git a/esphome/components/sntp/time.py b/esphome/components/sntp/time.py index 7571cf198b..78b586ea5a 100644 --- a/esphome/components/sntp/time.py +++ b/esphome/components/sntp/time.py @@ -49,15 +49,10 @@ async def to_code(config): # Define server count at compile time cg.add_define("SNTP_SERVER_COUNT", server_count) - var = cg.new_Pvariable(config[CONF_ID]) - - await cg.register_component(var, config) - await time_.register_time(var, config) - # Generate PROGMEM strings for ESP8266, regular strings for other platforms if CORE.is_esp8266: # On ESP8266, use PROGMEM to store strings in flash - # Use ProgmemAssignmentExpression to generate: static const char name[] PROGMEM = "value"; + server_vars = [] for i, server in enumerate(servers): var_name = f"{config[CONF_ID].id}_server_{i}" # Create PROGMEM string: static const char var_name[] PROGMEM = "server"; @@ -65,12 +60,17 @@ async def to_code(config): "char", var_name, cg.safe_exp(server) ) cg.add(assignment) - # Assign pointer to array element - cg.add(cg.RawStatement(f"{var}->servers_[{i}] = {var_name};")) + server_vars.append(cg.RawExpression(var_name)) + # Pass PROGMEM string pointers to constructor using ArrayInitializer + var = cg.new_Pvariable(config[CONF_ID], cg.ArrayInitializer(*server_vars)) else: - # On other platforms, use regular string literals - for i, server in enumerate(servers): - cg.add(cg.RawStatement(f"{var}->servers_[{i}] = {cg.safe_exp(server)};")) + # On other platforms, pass regular string literals to constructor + var = cg.new_Pvariable( + config[CONF_ID], cg.ArrayInitializer(*[cg.safe_exp(s) for s in servers]) + ) + + await cg.register_component(var, config) + await time_.register_time(var, config) if CORE.is_esp8266 and len(servers) > 1: # We need LwIP features enabled to get 3 SNTP servers (not just one)