- Neue Kategorie Extras: Lampe in wählbarer Farbe, Laufschrift mit eigener 5-LED-Schrift (A-Z, ÄÖÜß, Ziffern, Satzzeichen, °, ♥), Uhr per NTP, Wetter von Open-Meteo (eigener Task auf Kern 0, alle 15 min) - Favoriten: bis zu 12, aus den Favoriten gestartet wechselt der Würfel nur durch sie (playFavorites) - Effekt, Autowechsel, Helligkeit, Favoriten, Lampenfarbe, Text und Ort werden 2 s nach einer Änderung im NVS gespeichert und beim Start geladen - API: neue Felder in /api/state, GET /api/weather - App: Favoriten-Zeile, langes Drücken = Favorit, Einstellungen für Lampe, Laufschrift und Wetter-Ort (Suche über Open-Meteo) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
97 lines
3.2 KiB
C++
97 lines
3.2 KiB
C++
// Wetter: ein eigener Task auf dem anderen Prozessorkern holt alle 15 Minuten das
|
|
// aktuelle Wetter bei Open-Meteo (kostenlos, ohne Konto), damit der Abruf die
|
|
// Animation nicht anhält. Gesendet werden nur die auf 0,01° (~1 km) gerundeten
|
|
// Koordinaten des Orts, den die App eingestellt hat.
|
|
|
|
#include "cube.h"
|
|
#include <WiFi.h>
|
|
#include <HTTPClient.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
static portMUX_TYPE lock = portMUX_INITIALIZER_UNLOCKED; // Ort und Wetter teilen sich beide Kerne
|
|
static char place[48] = "";
|
|
static float latitude = 0, longitude = 0;
|
|
static uint32_t locationVersion = 0; // zählt bei jedem neuen Ort hoch
|
|
static WeatherInfo weather = {};
|
|
|
|
void setLocation(const char *name, float lat, float lon) {
|
|
portENTER_CRITICAL(&lock);
|
|
strlcpy(place, name, sizeof(place));
|
|
latitude = lat;
|
|
longitude = lon;
|
|
locationVersion++;
|
|
weather.valid = false; // altes Wetter gehört zum alten Ort
|
|
portEXIT_CRITICAL(&lock);
|
|
}
|
|
|
|
bool getLocation(char *name, size_t size, float &lat, float &lon) {
|
|
portENTER_CRITICAL(&lock);
|
|
strlcpy(name, place, size);
|
|
lat = latitude;
|
|
lon = longitude;
|
|
portEXIT_CRITICAL(&lock);
|
|
return name[0];
|
|
}
|
|
|
|
WeatherInfo weatherNow() {
|
|
portENTER_CRITICAL(&lock);
|
|
WeatherInfo w = weather;
|
|
portEXIT_CRITICAL(&lock);
|
|
return w;
|
|
}
|
|
|
|
static bool fetchWeather(float lat, float lon, uint32_t version) {
|
|
char url[160];
|
|
snprintf(url, sizeof(url),
|
|
"http://api.open-meteo.com/v1/forecast?latitude=%.2f&longitude=%.2f¤t=temperature_2m,weather_code,is_day",
|
|
lat, lon);
|
|
HTTPClient http;
|
|
http.setTimeout(8000);
|
|
http.begin(url);
|
|
int status = http.GET();
|
|
bool ok = false;
|
|
if (status == 200) {
|
|
JsonDocument doc;
|
|
if (!deserializeJson(doc, http.getString())) {
|
|
JsonObject current = doc["current"];
|
|
if (!current["temperature_2m"].isNull() && !current["weather_code"].isNull()) {
|
|
WeatherInfo w = {true, current["temperature_2m"].as<float>(), uint8_t(current["weather_code"].as<int>()),
|
|
current["is_day"].as<int>() == 1, millis()};
|
|
portENTER_CRITICAL(&lock);
|
|
if (version == locationVersion) weather = w; // Ort inzwischen geändert: verwerfen
|
|
portEXIT_CRITICAL(&lock);
|
|
ok = true;
|
|
Serial.printf("Wetter: %.1f °C, Code %u\n", w.temperature, w.code);
|
|
}
|
|
}
|
|
}
|
|
if (!ok) Serial.printf("Wetter: Abruf fehlgeschlagen (HTTP %d)\n", status);
|
|
http.end();
|
|
return ok;
|
|
}
|
|
|
|
static void weatherTask(void *) {
|
|
uint32_t fetchedVersion = UINT32_MAX, lastTry = 0;
|
|
bool ok = false;
|
|
for (;;) {
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
char name[48];
|
|
float lat, lon;
|
|
uint32_t version;
|
|
portENTER_CRITICAL(&lock);
|
|
version = locationVersion;
|
|
portEXIT_CRITICAL(&lock);
|
|
if (!getLocation(name, sizeof(name), lat, lon) || WiFi.status() != WL_CONNECTED) continue;
|
|
// neuer Ort sofort, sonst alle 15 min; nach einem Fehler nach 1 min erneut
|
|
uint32_t now = millis();
|
|
if (version == fetchedVersion && now - lastTry < (ok ? 15 * 60000UL : 60000UL)) continue;
|
|
fetchedVersion = version;
|
|
lastTry = now;
|
|
ok = fetchWeather(lat, lon, version);
|
|
}
|
|
}
|
|
|
|
void weatherBegin() {
|
|
xTaskCreatePinnedToCore(weatherTask, "wetter", 8192, nullptr, 1, nullptr, 0);
|
|
}
|