#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 previousOutputText = ""; 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); Serial.write(0x0C); // Clear display Serial.print("VFD display\n"); Serial.print("Starting\n"); WiFi.setHostname(HOSTNAME); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.write(0x0C); // Clear display Serial.printf("Hostname: %s\n", HOSTNAME); Serial.printf("Connecting to: %s\n", WIFI_SSID); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print('.'); } Serial.write(0x0C); // Clear display Serial.println("Connected, IP address: "); Serial.println(WiFi.localIP()); delay(1000); waitForSync(); CopenhagenTime.setLocation("Europe/Copenhagen"); Serial.write(0x0C); // Clear display callAPI(); } void loop() { String outputText = ""; if (millis() - aPIPreviousMillis >= aPIInterval) { aPIPreviousMillis = millis(); callAPI(); } outputText = weatherText + "\n\r"; // Get Unix epoch timestamp /*char buffer [3]; sprintf(buffer,"%03d",ms()); String milliEpoch = String(now()) + buffer; String truncatedEpoch = milliEpoch.substring(0, 10); Serial.print(truncatedEpoch); Serial.print(" ");*/ // https://github.com/ropg/ezTime#getting-date-and-time // 16:36:15 Sat 16 Aug String time_formatted = CopenhagenTime.dateTime("H:i:s D d M"); outputText += time_formatted; if (outputText != previousOutputText) { Serial.println(outputText); previousOutputText = outputText; } events(); // Run NTP }