From 3025d35554568c4a3813bddf34412c20d023a867 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 24 Oct 2025 14:37:15 -0700 Subject: [PATCH 1/4] must still be in ram on 8266 --- esphome/components/sntp/sntp_component.cpp | 12 +--------- esphome/components/sntp/sntp_component.h | 7 +----- esphome/components/sntp/time.py | 26 +++++----------------- 3 files changed, 8 insertions(+), 37 deletions(-) diff --git a/esphome/components/sntp/sntp_component.cpp b/esphome/components/sntp/sntp_component.cpp index 1457045d29..331a9b3509 100644 --- a/esphome/components/sntp/sntp_component.cpp +++ b/esphome/components/sntp/sntp_component.cpp @@ -42,16 +42,7 @@ void SNTPComponent::setup() { size_t i = 0; for (auto &server : this->servers_) { -#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) @@ -68,8 +59,7 @@ void SNTPComponent::dump_config() { ESP_LOGCONFIG(TAG, "SNTP Time:"); size_t i = 0; for (auto &server : this->servers_) { - // LOG_STR_ARG handles both PROGMEM (ESP8266) and regular pointers - ESP_LOGCONFIG(TAG, " Server %zu: '%s'", i++, LOG_STR_ARG(server)); + ESP_LOGCONFIG(TAG, " Server %zu: '%s'", i++, server); } } void SNTPComponent::update() { diff --git a/esphome/components/sntp/sntp_component.h b/esphome/components/sntp/sntp_component.h index d5ce25e8ee..bd6877cc5c 100644 --- a/esphome/components/sntp/sntp_component.h +++ b/esphome/components/sntp/sntp_component.h @@ -34,13 +34,8 @@ 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_; -#else - // On other platforms, store regular const char pointers + // Store const char pointers - compiler stores string literals in flash on all platforms std::array servers_; -#endif bool has_time_{false}; #if defined(USE_ESP32) diff --git a/esphome/components/sntp/time.py b/esphome/components/sntp/time.py index 78b586ea5a..afa7f6d1fd 100644 --- a/esphome/components/sntp/time.py +++ b/esphome/components/sntp/time.py @@ -12,7 +12,6 @@ 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") @@ -49,25 +48,12 @@ async def to_code(config): # Define server count at compile time cg.add_define("SNTP_SERVER_COUNT", server_count) - # Generate PROGMEM strings for ESP8266, regular strings for other platforms - if CORE.is_esp8266: - # On ESP8266, use PROGMEM to store strings in flash - 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"; - assignment = ProgmemAssignmentExpression( - "char", var_name, cg.safe_exp(server) - ) - cg.add(assignment) - 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, pass regular string literals to constructor - var = cg.new_Pvariable( - config[CONF_ID], cg.ArrayInitializer(*[cg.safe_exp(s) for s in servers]) - ) + # Pass string literals to constructor - stored in flash/rodata by compiler + # On ESP8266, LWIP doesn't support PROGMEM pointers, so strings are in rodata (RAM) + # but we still avoid the ~24 byte std::string overhead per server + 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) From ccdce3508ca330e1c2af7fe187abbc5137a72d46 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 24 Oct 2025 14:37:29 -0700 Subject: [PATCH 2/4] must still be in ram on 8266 --- esphome/components/sntp/sntp_component.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/esphome/components/sntp/sntp_component.h b/esphome/components/sntp/sntp_component.h index bd6877cc5c..d22d7c669d 100644 --- a/esphome/components/sntp/sntp_component.h +++ b/esphome/components/sntp/sntp_component.h @@ -34,7 +34,9 @@ class SNTPComponent : public time::RealTimeClock { void time_synced(); protected: - // Store const char pointers - compiler stores string literals in flash on all platforms + // Store const char pointers to string literals + // ESP8266: strings in rodata (RAM), but avoids std::string overhead (~24 bytes each) + // Other platforms: strings in flash std::array servers_; bool has_time_{false}; From 9e798ffa4f052204978690dd73adb9b6d6826c3b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 24 Oct 2025 14:37:35 -0700 Subject: [PATCH 3/4] must still be in ram on 8266 --- esphome/components/sntp/sntp_component.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/esphome/components/sntp/sntp_component.h b/esphome/components/sntp/sntp_component.h index d22d7c669d..74c342a38a 100644 --- a/esphome/components/sntp/sntp_component.h +++ b/esphome/components/sntp/sntp_component.h @@ -4,10 +4,6 @@ #include "esphome/components/time/real_time_clock.h" #include -#ifdef USE_ESP8266 -#include -#endif - namespace esphome { namespace sntp { From 01b1844e9dec96d2a5505c8610a74031d31a1ddb Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 24 Oct 2025 14:38:46 -0700 Subject: [PATCH 4/4] must still be in ram on 8266 --- esphome/components/sntp/sntp_component.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/sntp/sntp_component.h b/esphome/components/sntp/sntp_component.h index 74c342a38a..de40faa259 100644 --- a/esphome/components/sntp/sntp_component.h +++ b/esphome/components/sntp/sntp_component.h @@ -18,7 +18,7 @@ namespace sntp { /// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html class SNTPComponent : public time::RealTimeClock { public: - template SNTPComponent(Args... servers) : servers_{servers...} {} + SNTPComponent(std::array servers) : servers_(servers) {} void setup() override; void dump_config() override;