From ac8ab6285853cdf29e08b009bd829fbdda36fc96 Mon Sep 17 00:00:00 2001 From: M-Factory Date: Sun, 26 Oct 2025 15:21:00 +0900 Subject: [PATCH] Fix URL encoding for city names with spaces (e.g., New York, Belo Horizonte) --- ESPTimeCast_ESP32/ESPTimeCast_ESP32.ino | 17 +++++++++++------ ESPTimeCast_ESP8266/ESPTimeCast_ESP8266.ino | 18 +++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/ESPTimeCast_ESP32/ESPTimeCast_ESP32.ino b/ESPTimeCast_ESP32/ESPTimeCast_ESP32.ino index c9882d6..236d3d2 100644 --- a/ESPTimeCast_ESP32/ESPTimeCast_ESP32.ino +++ b/ESPTimeCast_ESP32/ESPTimeCast_ESP32.ino @@ -20,9 +20,9 @@ #define HARDWARE_TYPE MD_MAX72XX::FC16_HW #define MAX_DEVICES 4 -#define CLK_PIN 7 //D5 -#define CS_PIN 11 // D7 -#define DATA_PIN 12 //D8 +#define CLK_PIN 7 //D5 +#define CS_PIN 11 // D7 +#define DATA_PIN 12 //D8 MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES); AsyncWebServer server(80); @@ -665,7 +665,7 @@ void setupWebServer() { else if (n == "openWeatherApiKey") { if (v != "********************************") { // ignore mask only - doc[n] = v; // save new key (even if empty) + doc[n] = v; // save new key (even if empty) Serial.print(F("[SAVE] API key updated: ")); Serial.println(v.length() == 0 ? "(empty)" : v); } else { @@ -1262,19 +1262,24 @@ String buildWeatherURL() { bool latValid = isNumber(openWeatherCity) && isNumber(openWeatherCountry) && lat >= -90.0 && lat <= 90.0 && lon >= -180.0 && lon <= 180.0; + // Create encoded copies + String cityEncoded = String(openWeatherCity); + String countryEncoded = String(openWeatherCountry); + cityEncoded.replace(" ", "%20"); + countryEncoded.replace(" ", "%20"); + if (latValid) { base += "lat=" + String(lat, 8) + "&lon=" + String(lon, 8); } else if (isFiveDigitZip(openWeatherCity) && String(openWeatherCountry).equalsIgnoreCase("US")) { base += "zip=" + String(openWeatherCity) + "," + String(openWeatherCountry); } else { - base += "q=" + String(openWeatherCity) + "," + String(openWeatherCountry); + base += "q=" + cityEncoded + "," + countryEncoded; } base += "&appid=" + String(openWeatherApiKey); base += "&units=" + String(weatherUnits); String langForAPI = String(language); - if (langForAPI == "eo" || langForAPI == "ga" || langForAPI == "sw" || langForAPI == "ja") { langForAPI = "en"; } diff --git a/ESPTimeCast_ESP8266/ESPTimeCast_ESP8266.ino b/ESPTimeCast_ESP8266/ESPTimeCast_ESP8266.ino index b9be6ba..f5e0698 100644 --- a/ESPTimeCast_ESP8266/ESPTimeCast_ESP8266.ino +++ b/ESPTimeCast_ESP8266/ESPTimeCast_ESP8266.ino @@ -20,9 +20,9 @@ #define HARDWARE_TYPE MD_MAX72XX::FC16_HW #define MAX_DEVICES 4 -#define CLK_PIN 14 //D5 -#define CS_PIN 13 //D7 -#define DATA_PIN 15 //D8 +#define CLK_PIN 14 //D5 +#define CS_PIN 13 //D7 +#define DATA_PIN 15 //D8 MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES); AsyncWebServer server(80); @@ -666,7 +666,7 @@ void setupWebServer() { else if (n == "openWeatherApiKey") { if (v != "********************************") { // ignore mask only - doc[n] = v; // save new key (even if empty) + doc[n] = v; // save new key (even if empty) Serial.print(F("[SAVE] API key updated: ")); Serial.println(v.length() == 0 ? "(empty)" : v); } else { @@ -1262,19 +1262,24 @@ String buildWeatherURL() { bool latValid = isNumber(openWeatherCity) && isNumber(openWeatherCountry) && lat >= -90.0 && lat <= 90.0 && lon >= -180.0 && lon <= 180.0; + // Create encoded copies + String cityEncoded = String(openWeatherCity); + String countryEncoded = String(openWeatherCountry); + cityEncoded.replace(" ", "%20"); + countryEncoded.replace(" ", "%20"); + if (latValid) { base += "lat=" + String(lat, 8) + "&lon=" + String(lon, 8); } else if (isFiveDigitZip(openWeatherCity) && String(openWeatherCountry).equalsIgnoreCase("US")) { base += "zip=" + String(openWeatherCity) + "," + String(openWeatherCountry); } else { - base += "q=" + String(openWeatherCity) + "," + String(openWeatherCountry); + base += "q=" + cityEncoded + "," + countryEncoded; } base += "&appid=" + String(openWeatherApiKey); base += "&units=" + String(weatherUnits); String langForAPI = String(language); - if (langForAPI == "eo" || langForAPI == "ga" || langForAPI == "sw" || langForAPI == "ja") { langForAPI = "en"; } @@ -1284,7 +1289,6 @@ String buildWeatherURL() { } - void fetchWeather() { Serial.println(F("[WEATHER] Fetching weather data...")); if (WiFi.status() != WL_CONNECTED) {