Redesigned weather features for radio @MAJSTER XXL on open-meteo.com. This is for Ruda Śląska. Easy to change. Latitude and longitude swapped into their own.
// Function to retrieve API data from the Open-Meteo weather server
void getWeatherData() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("[WEATHER] No WiFi");
weatherServerConnection = false;
return;
}
HTTPClient http;
String url = "https://api.open-meteo.com/v1/forecast"
"?latitude=50.25&longitude=18.85" // ← Rudy Śląska, change if you want another city
"¤t=temperature_2m,relative_humidity_2m,apparent_temperature,"
"surface_pressure,wind_speed_10m,wind_gusts_10m,weather_code"
"&timezone=Europe%2FWarsaw"
"&forecast_days=1";
Serial.println("[WEATHER] Sending request: " + url);
http.begin(url);
http.setTimeout(8000); // 8 second timeout - safer
int httpCode = http.GET();
Serial.print("[WEATHER] HTTP Code: ");
Serial.println(httpCode);
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
//Serial.println("[WEATHER] Answer (first 300 characters): " + payload.substring(0,300));
DynamicJsonDocument doc(1536); // I increased to 1.5 KB - just in case
DeserializationError error = deserializeJson(doc, payload);
if (!error) {
JsonObject current = doc["current"];
if (!current.isNull()) {
float t = current["temperature_2m"];
float tf = current["apparent_temperature"];
int h = current["relative_humidity_2m"];
float p = current["surface_pressure"];
float w = current["wind_speed_10m"];
float wg = current["wind_gusts_10m"];
// Writing to the variables displayed on the TFT
tempStr = "Temperature: " + String(t, 1) + " °C";
feels_likeStr = "Feeling: " + String(tf, 1) + " °C";
humidityStr = "Humidity: " + String(h) + " %";
humidityStr = NormalizePolish(humidityStr);
pressureStr = "Pressure: " + String(p, 0) + " hPa";
pressureStr = normalisePolish( pressureStr);
windStr = "Wind: " + String(w, 1) + " km/h";
windGustStr = "Gust: " + String(wg, 1) + " km/h";
weatherServerConnection = true;
// The most important debug - shows what really came in
Serial.println("[WEATHER] SUCCESS!");
Serial.printf(" Temp: %.1f °C Read: %.1f °C", t, tf);
Serial.printf(" Humidity: %d %% Pressure: %.0f hPan", h, p);
Serial.printf(" Wind: %.1f km/h Gust: %.1f km/h", w, wg);
} else {
Serial.println("[WEATHER] No 'current' key in JSON");
}
} else {
Serial.print("[WEATHER] JSON parsing error: ");
Serial.println(error.c_str());
}
} else {
Serial.print("[WEATHER] HTTP error: ");
Serial.print(httpCode);
Serial.print(" → ");
Serial.println(http.errorToString(httpCode));
weatherServerConnection = false;
}
http.end();
}
// Function to update weather data
void updateWeather() {
JsonObject root = doc.as<JsonObject>(); // Converts a JSON document to an object of type JsonObject
JsonObject main = root["main"]; // Retrieves a "main" object containing master data such as temperature, humidity, pressure
JsonObject weather = root["weather_code"][0]; // Retrieves first an element from the 'weather' array that contains weather data
JsonObject wind = root["wind_speed_10m"]; // Retrieves a "wind" object containing wind data
unsigned long timestamp = root["dt"]; // Retrieves timestamp (time in seconds) from JSON
String formattedDate = convertTimestampToDate(timestamp); // Converts timestamp to formatted date and time
float temp = main["temperature_2m"].as<float>() - 273.15; // Takes the temperature in Kelvins and converts it to °C
float feels_like = main["apparent_temperature"].as<float>() - 273.15; // Takes the felt temperature and converts it to °C
int humidity = main["relative_humidity_2m"]; // Retrieves the humidity of the air
String weatherDescription = weather["description"].as<String>(); // Retrieves a description of the weather (e.g. "light rain")
String icon = weather["icon"].as<String>(); // Retrieves the weather icon code (e.g. "10d" for rain)
float windSpeed = wind["wind_speed"]; // Retrieves the wind speed in m/s
float windGust = wind["wind_gusts_10m"]; // Retrieves wind gust speed in m/s
float pressure = main["surface_pressure"].as<float>(); // Retrieves air pressure in hPa
Serial.println("Data from JSON:");
Serial.print("Data ");
Serial.println(formattedDate);
Serial.print("Temperature ");
Serial.print(temp, 2);
Serial.println(" °C");
tempStr = "Temperature " + String(temp, 2) + " C."
Serial.print("Feels like temperature ");
Serial.print(feels_like, 2);
Serial.println(" °C");
feels_likeStr = "Feels " + String(feels_like, 2) + " C";
Serial.print("humidity ");
Serial.print(humidity);
Serial.println(" %");
humidityStr = "Humidity " + String(humidity) + " %";
humidityStr = NormalizePolish(humidityStr);
Serial.print("Pressure ");
Serial.print(pressure);
Serial.println(" hPa");
pressureStr = "Pressure " + String(pressure, 2) + " hPa";
pressureStr = NormalizePolish(pressureStr);
Serial.print("Description of the weather ");
Serial.println(weatherDescription);
Serial.print("Icon ");
Serial.println(icon);
Serial.print("Wind speed ");
Serial.print(windSpeed, 2);
Serial.println(" m/s");
windStr = "Wind " + String(windSpeed) + " m/s";
Serial.print("Wind gusts ");
Serial.print(windGust, 2);
Serial.println(" m/s");
windGustStr = "In gusts " + String(windGust) + " m/s";
//******
}