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
This commit is contained in:
M-Factory
2026-01-30 11:31:49 +09:00
parent f3a53ee836
commit 2097aba754
2 changed files with 49 additions and 15 deletions

View File

@@ -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

View File

@@ -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..."));