From 93914417e61a886debd2923e025af2d9536bab26 Mon Sep 17 00:00:00 2001 From: "user@node5.net" Date: Sun, 15 Feb 2026 00:44:46 +0100 Subject: API call - Weather -2C Cloudy 5km/h 00:34:46 Sun 15 Feb --- src/main.cpp | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 166 insertions(+), 4 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 021d0e7..4809d0f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,160 @@ #include #include +#include +#include #include +#include Timezone CopenhagenTime; +WiFiClient client; // or WiFiClientSecure for HTTPS +HTTPClient http; + +unsigned long aPIPreviousMillis = 0; +const long aPIInterval = 600000; // call API every 10 minutes +String weatherText = ""; + +String weatherDescription(int code, bool isDay) { + switch(code) { + case 0: + if (isDay){ + return "Sunny"; + } else { + return "Clear"; + } + break; + case 1: + if (isDay){ + return "Mainly Sunny"; + } else { + return "Mainly Clear"; + } + break; + case 2: + return "Partly Cloudy"; + break; + case 3: + return "Cloudy"; + break; + case 45: + return "Foggy"; + break; + case 48: + return "Rime Fog"; + break; + case 51: + return "Light Drizzle"; + break; + case 53: + return "Drizzle"; + break; + case 55: + return "Heavy Drizzle"; + break; + case 56: + return "Light Freezing Drizzle"; + break; + case 57: + return "Freezing Drizzle"; + break; + case 61: + return "Light Rain"; + break; + case 63: + return "Rain"; + break; + case 65: + return "Heavy Rain"; + break; + case 66: + return "Light Freezing Rain"; + break; + case 67: + return "Freezing Rain"; + break; + case 71: + return "Light Snow"; + break; + case 73: + return "Snow"; + break; + case 75: + return "Heavy Snow"; + break; + case 77: + return "Snow Grains"; + break; + case 80: + return "Light Showers"; + break; + case 81: + return "Showers"; + break; + case 82: + return "Heavy Showers"; + break; + case 85: + return "Light Snow Showers"; + break; + case 86: + return "Snow Showers"; + break; + case 95: + return "Thunderstorm"; + break; + case 96: + return "Light Thunderstorms With Hail"; + break; + case 99: + return "Thunderstorm With Hail"; + break; + default: + return "Unkown"; + break; + } +} + +void callAPI(){ + // Send request + http.useHTTP10(true); + // https://open-meteo.com/en/docs?hourly=&timezone=Europe%2FBerlin&latitude=55.6759&longitude=12.5655¤t=is_day,temperature_2m,wind_speed_10m&forecast_days=1&daily=weather_code + http.begin(client, "http://api.open-meteo.com/v1/forecast?latitude=55.6759&longitude=12.5655&daily=weather_code¤t=is_day,temperature_2m,wind_speed_10m&timezone=Europe%2FBerlin&forecast_days=1"); + http.GET(); + + // Parse response + DynamicJsonDocument weather(2048); + DeserializationError error = deserializeJson(weather, http.getStream()); + if (error) { + Serial.print(F("deserializeJson() failed: ")); + Serial.println(error.f_str()); + } + + // Temperature + String temperature = weather["current"]["temperature_2m"].as(); + int tempDotIndex = temperature.indexOf('.'); + if (tempDotIndex > -1) { + temperature = temperature.substring(0, tempDotIndex); // Take the part before the decimal point + } + weatherText = temperature; + weatherText += "C "; + + // Weather description + int weather_code = weather["daily"]["weather_code"][0]; + bool weather_is_day = weather["current"]["is_day"].as(); + weatherText += weatherDescription(weather_code, weather_is_day) + " "; + + // Wind speed + String windSpeed = weather["current"]["wind_speed_10m"].as(); + int windDotIndex = windSpeed.indexOf('.'); + if (windDotIndex > -1) { + windSpeed = windSpeed.substring(0, windDotIndex); // Take the part before the decimal point + } + weatherText += windSpeed; + weatherText += "km/h"; + + // Disconnect + http.end(); +} void setup() { Serial.begin(9600); @@ -33,20 +184,31 @@ void setup() { CopenhagenTime.setLocation("Europe/Copenhagen"); Serial.write(0x0C); // Clear display + callAPI(); } void loop() { + if (millis() - aPIPreviousMillis >= aPIInterval) { + aPIPreviousMillis = millis(); + callAPI(); + } + // Get Unix epoch timestamp - char buffer [3]; + /*char buffer [3]; sprintf(buffer,"%03d",ms()); String milliEpoch = String(now()) + buffer; - Serial.println(milliEpoch); + String truncatedEpoch = milliEpoch.substring(0, 10); + Serial.print(truncatedEpoch); + Serial.print(" ");*/ + + Serial.print(weatherText); + Serial.println(); delay(50); // https://github.com/ropg/ezTime#getting-date-and-time - // 16;36;15 Sat 16 Aug 33 + // 16:36:15 Sat 16 Aug String time_formatted = CopenhagenTime.dateTime("H:i:s D d M"); Serial.println(time_formatted); - events(); // Needed to update time continuously + events(); // Run NTP } -- cgit v1.2.3