Extras (Lampe, Laufschrift, Uhr, Wetter), Favoriten, Einstellungen im Flash
- 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>
This commit is contained in:
1 parent
8f6bf2d841
commit
cb0cccb99c
13 files changed
+1103
-90
No files matched your search
@@ -2,10 +2,12 @@
|
||||
// Suchen und Verbinden laufen ohne Blockieren, die Animation läuft weiter.
|
||||
//
|
||||
// GET /api/state aktueller Zustand
|
||||
// PUT/PATCH /api/state ändern, alle Felder optional:
|
||||
// PUT/PATCH /api/state ändern, alle Felder optional, z. B.
|
||||
// {"effect": "feuer", "auto": false, "brightness": 64}
|
||||
// (alle Felder in docs/api.md); wird im Flash gespeichert
|
||||
// GET /api/categories alle Kategorien: [{"id": "flash", "name": "Flash"}, ...]
|
||||
// GET /api/effects alle Effekte: [{"id": "gewitter", "name": "Gewitter", "category": "flash"}, ...]
|
||||
// GET /api/effects alle Effekte: [{"id": "gewitter", "name": "Gewitter", "category": "natur"}, ...]
|
||||
// GET /api/weather zuletzt abgerufenes Wetter am eingestellten Ort
|
||||
// POST /api/specials/{id} Spezialeffekt auslösen: blitz, explosion, regenbogen
|
||||
// POST /api/lightning Blitz auslösen (wie /api/specials/blitz)
|
||||
//
|
||||
@@ -43,10 +45,51 @@ void sendState() {
|
||||
doc["effect"] = effectId(currentEffect());
|
||||
doc["auto"] = autoAdvance;
|
||||
doc["allCategories"] = autoAll;
|
||||
doc["playFavorites"] = playFavorites;
|
||||
doc["brightness"] = brightness;
|
||||
JsonArray favs = doc["favorites"].to<JsonArray>();
|
||||
for (uint8_t k = 0; k < numFavorites(); k++) favs.add(effectId(favorite(k)));
|
||||
char hex[8];
|
||||
snprintf(hex, sizeof(hex), "#%02x%02x%02x", lampColor.r, lampColor.g, lampColor.b);
|
||||
doc["lampColor"] = hex;
|
||||
doc["text"] = tickerText;
|
||||
char place[48];
|
||||
float lat, lon;
|
||||
if (getLocation(place, sizeof(place), lat, lon)) {
|
||||
doc["place"] = place;
|
||||
doc["latitude"] = lat;
|
||||
doc["longitude"] = lon;
|
||||
} else {
|
||||
doc["place"] = "";
|
||||
}
|
||||
sendJson(200, doc);
|
||||
}
|
||||
|
||||
void sendWeather() {
|
||||
JsonDocument doc;
|
||||
char place[48];
|
||||
float lat, lon;
|
||||
doc["place"] = getLocation(place, sizeof(place), lat, lon) ? place : "";
|
||||
WeatherInfo w = weatherNow();
|
||||
if (w.valid) {
|
||||
doc["temperature"] = w.temperature;
|
||||
doc["code"] = w.code;
|
||||
doc["isDay"] = w.isDay;
|
||||
doc["age"] = (millis() - w.updated) / 1000; // Sekunden seit dem Abruf
|
||||
}
|
||||
sendJson(200, doc);
|
||||
}
|
||||
|
||||
// "#rrggbb" -> Farbe
|
||||
bool parseColor(const char *s, CRGB &color) {
|
||||
if (strlen(s) != 7 || s[0] != '#') return false;
|
||||
for (uint8_t i = 1; i < 7; i++)
|
||||
if (!isxdigit(s[i])) return false;
|
||||
uint32_t v = strtoul(s + 1, nullptr, 16);
|
||||
color = CRGB(v);
|
||||
return true;
|
||||
}
|
||||
|
||||
void sendEffects() {
|
||||
JsonDocument doc;
|
||||
for (uint8_t i = 0; i < numEffects(); i++) {
|
||||
@@ -68,12 +111,6 @@ void sendCategories() {
|
||||
sendJson(200, doc);
|
||||
}
|
||||
|
||||
int findEffect(const char *id) {
|
||||
for (uint8_t i = 0; i < numEffects(); i++)
|
||||
if (strcmp(id, effectId(i)) == 0) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Erst alles prüfen, dann übernehmen: eine fehlerhafte Anfrage ändert nichts.
|
||||
void updateState() {
|
||||
// Ohne "Content-Type: application/json" zerlegt der Webserver den Body als Formular
|
||||
@@ -83,6 +120,8 @@ void updateState() {
|
||||
return sendError(400, "JSON-Objekt erwartet");
|
||||
|
||||
JsonVariant effect = body["effect"], autoOn = body["auto"], all = body["allCategories"], level = body["brightness"];
|
||||
JsonVariant playFav = body["playFavorites"], favs = body["favorites"], lamp = body["lampColor"], text = body["text"];
|
||||
JsonVariant place = body["place"], lat = body["latitude"], lon = body["longitude"];
|
||||
int index = -1;
|
||||
if (!effect.isNull()) {
|
||||
if (!effect.is<const char *>()) return sendError(400, "effect muss eine Effekt-id sein, siehe /api/effects");
|
||||
@@ -93,11 +132,44 @@ void updateState() {
|
||||
if (!all.isNull() && !all.is<bool>()) return sendError(400, "allCategories muss true oder false sein");
|
||||
if (!level.isNull() && !(level.is<int>() && level.as<int>() >= 0 && level.as<int>() <= 255))
|
||||
return sendError(400, "brightness muss eine ganze Zahl von 0 bis 255 sein");
|
||||
if (!playFav.isNull() && !playFav.is<bool>()) return sendError(400, "playFavorites muss true oder false sein");
|
||||
uint8_t favList[MAX_FAVORITES], favCount = 0;
|
||||
if (!favs.isNull()) {
|
||||
if (!favs.is<JsonArray>()) return sendError(400, "favorites muss eine Liste von Effekt-ids sein");
|
||||
if (favs.size() > MAX_FAVORITES) return sendError(400, "höchstens 12 Favoriten");
|
||||
for (JsonVariant f : favs.as<JsonArray>()) {
|
||||
int i = f.is<const char *>() ? findEffect(f.as<const char *>()) : -1;
|
||||
if (i < 0) return sendError(400, "favorites enthält eine unbekannte Effekt-id, siehe /api/effects");
|
||||
for (uint8_t k = 0; k < favCount; k++)
|
||||
if (favList[k] == i) return sendError(400, "favorites enthält einen Effekt doppelt");
|
||||
favList[favCount++] = i;
|
||||
}
|
||||
}
|
||||
CRGB lampNew;
|
||||
if (!lamp.isNull() && !(lamp.is<const char *>() && parseColor(lamp.as<const char *>(), lampNew)))
|
||||
return sendError(400, "lampColor muss eine Farbe wie \"#ffa03c\" sein");
|
||||
if (!text.isNull() && !(text.is<const char *>() && strlen(text.as<const char *>()) <= 100))
|
||||
return sendError(400, "text muss ein String mit höchstens 100 Byte sein (Umlaute zählen doppelt)");
|
||||
if (!place.isNull()) {
|
||||
if (!place.is<const char *>() || strlen(place.as<const char *>()) > 47)
|
||||
return sendError(400, "place muss ein String mit höchstens 47 Byte sein");
|
||||
if (place.as<const char *>()[0] &&
|
||||
!(lat.is<float>() && lon.is<float>() && fabsf(lat.as<float>()) <= 90 && fabsf(lon.as<float>()) <= 180))
|
||||
return sendError(400, "zu place gehören latitude (-90 bis 90) und longitude (-180 bis 180)");
|
||||
} else if (!lat.isNull() || !lon.isNull()) {
|
||||
return sendError(400, "latitude und longitude nur zusammen mit place");
|
||||
}
|
||||
|
||||
if (index >= 0) selectEffect(index);
|
||||
if (!autoOn.isNull()) autoAdvance = autoOn.as<bool>();
|
||||
if (!all.isNull()) autoAll = all.as<bool>();
|
||||
if (!level.isNull()) setBaseBrightness(level.as<int>());
|
||||
if (!playFav.isNull()) playFavorites = playFav.as<bool>();
|
||||
if (!favs.isNull()) setFavorites(favList, favCount);
|
||||
if (!lamp.isNull()) lampColor = lampNew;
|
||||
if (!text.isNull()) tickerText = text.as<const char *>();
|
||||
if (!place.isNull()) setLocation(place.as<const char *>(), lat.as<float>(), lon.as<float>());
|
||||
settingsChanged();
|
||||
sendState();
|
||||
}
|
||||
|
||||
@@ -113,6 +185,7 @@ void webBegin() {
|
||||
server.on("/api/state", HTTP_PATCH, updateState);
|
||||
server.on("/api/effects", HTTP_GET, sendEffects);
|
||||
server.on("/api/categories", HTTP_GET, sendCategories);
|
||||
server.on("/api/weather", HTTP_GET, sendWeather);
|
||||
server.on("/api/lightning", HTTP_POST, [] {
|
||||
triggerLightning(millis());
|
||||
server.send(204);
|
||||
@@ -127,6 +200,7 @@ void webBegin() {
|
||||
});
|
||||
server.onNotFound([] { sendError(404, "unbekannter Pfad oder Methode"); });
|
||||
server.begin();
|
||||
configTzTime(TIMEZONE, "pool.ntp.org"); // Uhrzeit für die Uhr, sobald das WLAN steht
|
||||
WiFi.onEvent([](WiFiEvent_t e, WiFiEventInfo_t info) {
|
||||
if (e == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) Serial.printf("WLAN: getrennt (Grund %u)\n", info.wifi_sta_disconnected.reason);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user