From 2097aba754f8c369fd9650cbc8564522f5ff4263 Mon Sep 17 00:00:00 2001 From: M-Factory Date: Fri, 30 Jan 2026 11:31:49 +0900 Subject: [PATCH] Improvements to weather fetch stability across ESP8266 and ESP32. This resolves random reboots and memory exhaustion when refreshing the UI while a weather update is in progress on ESP8266. - ESP8266 now uses HTTP for OpenWeatherMap to avoid TLS-related OOM crashes - ESP32 continues to use HTTPS without regression - Verified stability during forced mid-fetch refreshes from the web UI --- ESPTimeCast_ESP32/ESPTimeCast_ESP32.ino | 31 ++++++++++++++----- ESPTimeCast_ESP8266/ESPTimeCast_ESP8266.ino | 33 ++++++++++++++++----- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/ESPTimeCast_ESP32/ESPTimeCast_ESP32.ino b/ESPTimeCast_ESP32/ESPTimeCast_ESP32.ino index 49f218a..6f1cdb9 100644 --- a/ESPTimeCast_ESP32/ESPTimeCast_ESP32.ino +++ b/ESPTimeCast_ESP32/ESPTimeCast_ESP32.ino @@ -1997,7 +1997,11 @@ bool isFiveDigitZip(const char *str) { // Weather Fetching and API settings // ----------------------------------------------------------------------------- String buildWeatherURL() { +#if defined(ESP8266) + String base = "http://api.openweathermap.org/data/2.5/weather?"; +#else String base = "https://api.openweathermap.org/data/2.5/weather?"; +#endif float lat = atof(openWeatherCity); float lon = atof(openWeatherCountry); @@ -2061,13 +2065,26 @@ void fetchWeather() { Serial.print(F("[WEATHER] URL: ")); // Use F() with Serial.print Serial.println(url); - WiFiClientSecure client; // use secure client for HTTPS - client.stop(); // ensure previous session closed - yield(); // Allow OS to process socket closure - client.setInsecure(); // no cert validation - HTTPClient http; // Create an HTTPClient object - http.begin(client, url); // Pass the WiFiClient object and the URL - http.setTimeout(10000); // Sets both connection and stream timeout to 10 seconds + HTTPClient http; // Create an HTTPClient object + +#if defined(ESP8266) + // ===== ESP8266 → HTTP ===== + WiFiClient client; + client.stop(); + yield(); + + http.begin(client, url); +#else + // ===== ESP32 → HTTPS ===== + WiFiClientSecure client; + client.stop(); + yield(); + client.setInsecure(); // no cert validation + + http.begin(client, url); +#endif + + http.setTimeout(10000); // Sets both connection and stream timeout to 10 seconds Serial.println(F("[WEATHER] Sending GET request...")); int httpCode = http.GET(); // Send the GET request diff --git a/ESPTimeCast_ESP8266/ESPTimeCast_ESP8266.ino b/ESPTimeCast_ESP8266/ESPTimeCast_ESP8266.ino index ffe42ed..5be6c55 100644 --- a/ESPTimeCast_ESP8266/ESPTimeCast_ESP8266.ino +++ b/ESPTimeCast_ESP8266/ESPTimeCast_ESP8266.ino @@ -1990,7 +1990,11 @@ bool isFiveDigitZip(const char *str) { // Weather Fetching and API settings // ----------------------------------------------------------------------------- String buildWeatherURL() { +#if defined(ESP8266) + String base = "http://api.openweathermap.org/data/2.5/weather?"; +#else String base = "https://api.openweathermap.org/data/2.5/weather?"; +#endif float lat = atof(openWeatherCity); float lon = atof(openWeatherCountry); @@ -2051,15 +2055,28 @@ void fetchWeather() { Serial.println(F("[WEATHER] Connecting to OpenWeatherMap...")); String url = buildWeatherURL(); - Serial.println(F("[WEATHER] URL: ") + url); + Serial.print(F("[WEATHER] URL: ")); // Use F() with Serial.print + Serial.println(url); + + HTTPClient http; // Create an HTTPClient object + +#if defined(ESP8266) + // ===== ESP8266 → HTTP ===== + WiFiClient client; + client.stop(); + yield(); + + http.begin(client, url); +#else + // ===== ESP32 → HTTPS ===== + WiFiClientSecure client; + client.stop(); + client.setInsecure(); // no cert validation + yield(); + + http.begin(client, url); +#endif - WiFiClientSecure client; // use secure client for HTTPS - client.stop(); // ensure previous session closed - yield(); // Allow OS to process socket closure - client.setInsecure(); // no cert validation - HTTPClient http; // Create an HTTPClient object - http.begin(client, url); // Pass the WiFiClient object and the URL - client.setBufferSizes(512, 512); http.setTimeout(10000); // Sets both connection and stream timeout to 10 seconds Serial.println(F("[WEATHER] Sending GET request..."));