From 3907b07171757c97959ed5ab19ab822004504a80 Mon Sep 17 00:00:00 2001 From: Louis-Alexander Kerst Date: Wed, 4 Nov 2026 21:33:09 +0100 Subject: [PATCH] Szenen-Tab, Temperaturverlauf, Wetter und Hinweise LVGL-Speicher aus dem Heap, da der statische Pool nicht mehr ins DRAM passte. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 4 + include/config.example.h | 2 + include/lv_conf.h | 8 +- src/main.cpp | 256 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 259 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b038479..afd28c4 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ Touch-Dashboard für das ESP32-2432S028R ("Cheap Yellow Display"), das Heizkörp - **Heizung-Tab:** Ist- und Solltemperatur, Modus, Heizsymbol bei aktivem Heizen. Mit +/- stellst du die Solltemperatur ein; sie wird 1,2 s nach dem letzten Tippen gesendet und ist bis dahin gelb. - **Licht-Tab:** Ein/Aus-Schalter. Dimmbare Lampen bekommen automatisch einen Helligkeitsregler, Farblampen zusätzlich eine kleine Farbpalette (erstes Feld = Warmweiß). `switch.*`-Entitäten funktionieren auch. +- **Szenen-Tab:** Alle `scene.*`-Entitäten als Knöpfe, ein Tipp aktiviert die Szene (mit `INCLUDE_SCRIPTS 1` auch `script.*`). +- **Verlauf-Tab:** Temperaturkurve aller Heizungen der letzten 24 h (alle 10 min ein Punkt). Die Daten sammelt das Board selbst und sie gehen beim Neustart verloren. +- **Wetter:** Außentemperatur und Zustand der ersten `weather.*`-Entität oben links, alle 10 min aktualisiert. +- **Hinweise (Glocken-Tab):** Offene Fenster/Türen (mit Warnung, wenn dabei geheizt wird), offline Heizungen/Lampen, Batterien unter 20 % und HA-Benachrichtigungen. Bei Hinweisen erscheint eine rote Glocke mit Anzahl in der Kopfzeile, und ein neuer Hinweis weckt das Display. - Uhrzeit über NTP, WLAN-Signalstärke (Balken + dBm), WLAN- und HA-Status, Display geht nach 60 s ohne Bedienung aus (Tippen weckt es wieder). ## Einrichtung diff --git a/include/config.example.h b/include/config.example.h index 49dce1a..f77c284 100644 --- a/include/config.example.h +++ b/include/config.example.h @@ -16,6 +16,8 @@ // Heizungen (climate.*) und Lichter (light.*) werden automatisch aus Home Assistant geladen. // 1 = zusaetzlich alle switch.*-Entitaeten im Licht-Tab anzeigen (z.B. Steckdosen) #define INCLUDE_SWITCHES 0 +// 1 = im Szenen-Tab zusaetzlich script.*-Entitaeten anzeigen +#define INCLUDE_SCRIPTS 0 // ---- Behaviour ---- #define POLL_STEP_MS 500 // poll one entity every X ms (round robin) diff --git a/include/lv_conf.h b/include/lv_conf.h index a4615a0..f9f3f8c 100644 --- a/include/lv_conf.h +++ b/include/lv_conf.h @@ -6,8 +6,12 @@ #define LV_COLOR_DEPTH 16 #define LV_COLOR_16_SWAP 0 // swapping happens in TFT_eSPI::pushColors -#define LV_MEM_CUSTOM 0 -#define LV_MEM_SIZE (48U * 1024U) +// LVGL memory comes from the normal heap: a static pool would not fit into the ESP32 DRAM segment. +#define LV_MEM_CUSTOM 1 +#define LV_MEM_CUSTOM_INCLUDE +#define LV_MEM_CUSTOM_ALLOC malloc +#define LV_MEM_CUSTOM_FREE free +#define LV_MEM_CUSTOM_REALLOC realloc #define LV_TICK_CUSTOM 1 #define LV_TICK_CUSTOM_INCLUDE "Arduino.h" diff --git a/src/main.cpp b/src/main.cpp index 79a67a9..91d7601 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,9 +47,29 @@ static lv_obj_t* tabLight; static lv_obj_t* hintHeat; static lv_obj_t* hintLight; +static constexpr size_t MAX_SCENE = 12; +static constexpr size_t MAX_ALERT = 10; +static constexpr int HIST_POINTS = 144; // 24 h at one sample per 10 min +static constexpr uint32_t HIST_STEP_MS = 600000; +static Device SCENES[MAX_SCENE]; +static size_t N_SCENE = 0; +static String weatherEntity; +static uint32_t discoveredAt = 0; +static lv_obj_t *tabScene, *tabHist, *tabAlert; +static lv_obj_t *hintScene, *hintHist; +static lv_obj_t* titleLabel; +static lv_obj_t* alertLabel; +static lv_obj_t* chart; +static lv_chart_series_t* chartSer[8]; +static lv_obj_t* histLegend[8]; +static lv_coord_t histY[8][HIST_POINTS]; +static String alerts[MAX_ALERT]; +static size_t nAlerts = 0; + struct ClimateUi { lv_obj_t* target = nullptr; lv_obj_t* info = nullptr; + float cur = NAN; float targetVal = NAN, minT = 5, maxT = 30, step = 0.5; uint32_t pendingSince = 0; // != 0: local change not sent yet }; @@ -206,6 +226,7 @@ static void applyClimate(size_t i, JsonDocument& doc) { u.maxT = a["max_temp"] | 30.0f; u.step = a["target_temp_step"] | 0.5f; float cur = a["current_temperature"] | NAN; + u.cur = cur; const char* action = a["hvac_action"] | ""; char buf[48]; @@ -333,6 +354,14 @@ static void onPalette(lv_event_t* e) { callService("light", "turn_on", doc); } +static void onScene(lv_event_t* e) { + size_t i = (size_t)(intptr_t)lv_event_get_user_data(e); + JsonDocument doc; + doc["entity_id"] = SCENES[i].entity; + if (callService(domainOf(SCENES[i].entity).c_str(), "turn_on", doc)) + setStatus(LV_SYMBOL_OK, lv_palette_main(LV_PALETTE_GREEN)); +} + // ---------------------------------------------------------------- UI construction static lv_obj_t* makeCard(lv_obj_t* parent, int h) { lv_obj_t* c = lv_obj_create(parent); @@ -377,9 +406,11 @@ static void buildUi() { lv_obj_set_style_bg_color(hdr, lv_color_hex(0x1c232b), 0); lv_obj_clear_flag(hdr, LV_OBJ_FLAG_SCROLLABLE); - lv_obj_t* title = lv_label_create(hdr); - lv_label_set_text(title, LV_SYMBOL_HOME " HomeBoard"); - lv_obj_align(title, LV_ALIGN_LEFT_MID, 0, 0); + titleLabel = lv_label_create(hdr); + lv_label_set_long_mode(titleLabel, LV_LABEL_LONG_CLIP); + lv_obj_set_width(titleLabel, 125); + lv_label_set_text(titleLabel, LV_SYMBOL_HOME " HomeBoard"); + lv_obj_align(titleLabel, LV_ALIGN_LEFT_MID, 0, 0); clockLabel = lv_label_create(hdr); lv_label_set_text(clockLabel, "--:--"); lv_obj_align(clockLabel, LV_ALIGN_CENTER, 0, 0); @@ -391,13 +422,17 @@ static void buildUi() { lv_obj_set_style_border_width(rssiBars[i], 0, 0); lv_obj_set_style_pad_all(rssiBars[i], 0, 0); lv_obj_clear_flag(rssiBars[i], LV_OBJ_FLAG_SCROLLABLE); - lv_obj_align(rssiBars[i], LV_ALIGN_CENTER, 52 + i * 5, 6 - (5 + i * 3) / 2 + 0); + lv_obj_align(rssiBars[i], LV_ALIGN_CENTER, 30 + i * 5, 6 - (5 + i * 3) / 2 + 0); } rssiLabel = lv_label_create(hdr); lv_obj_set_style_text_color(rssiLabel, lv_palette_lighten(LV_PALETTE_GREY, 1), 0); lv_label_set_text(rssiLabel, ""); - lv_obj_align(rssiLabel, LV_ALIGN_CENTER, 98, 0); + lv_obj_align(rssiLabel, LV_ALIGN_CENTER, 60, 0); updateRssi(); + alertLabel = lv_label_create(hdr); + lv_obj_set_style_text_color(alertLabel, lv_palette_main(LV_PALETTE_RED), 0); + lv_label_set_text(alertLabel, ""); + lv_obj_align(alertLabel, LV_ALIGN_RIGHT_MID, -50, 0); statusLabel = lv_label_create(hdr); lv_obj_align(statusLabel, LV_ALIGN_RIGHT_MID, 0, 0); setStatus(LV_SYMBOL_WIFI " ...", lv_palette_main(LV_PALETTE_GREY)); @@ -406,18 +441,29 @@ static void buildUi() { lv_obj_t* tv = lv_tabview_create(scr, LV_DIR_BOTTOM, 34); lv_obj_set_size(tv, SCREEN_W, SCREEN_H - 26); lv_obj_set_pos(tv, 0, 26); - tabHeat = makeTab(tv, LV_SYMBOL_CHARGE " Heizung"); - tabLight = makeTab(tv, LV_SYMBOL_EYE_OPEN " Licht"); + tabHeat = makeTab(tv, "Heizung"); + tabLight = makeTab(tv, "Licht"); + tabScene = makeTab(tv, "Szenen"); + tabHist = makeTab(tv, "Verlauf"); + tabAlert = makeTab(tv, LV_SYMBOL_BELL); hintHeat = lv_label_create(tabHeat); lv_label_set_text(hintHeat, "Lade Geraete aus HA ..."); hintLight = lv_label_create(tabLight); lv_label_set_text(hintLight, "Lade Geraete aus HA ..."); + hintScene = lv_label_create(tabScene); + lv_label_set_text(hintScene, "Lade Geraete aus HA ..."); + hintHist = lv_label_create(tabHist); + lv_label_set_text(hintHist, "Lade Geraete aus HA ..."); + lv_obj_t* h = lv_label_create(tabAlert); + lv_label_set_text(h, "Pruefe Hinweise ..."); } static void populateUi() { if (hintHeat) { lv_obj_del(hintHeat); hintHeat = nullptr; } if (hintLight) { lv_obj_del(hintLight); hintLight = nullptr; } + if (hintScene) { lv_obj_del(hintScene); hintScene = nullptr; } + if (hintHist) { lv_obj_del(hintHist); hintHist = nullptr; } for (size_t i = 0; i < N_CLIMATE; i++) { lv_obj_t* c = makeCard(tabHeat, 64); lv_obj_t* name = lv_label_create(c); @@ -491,6 +537,47 @@ static void populateUi() { } } + // Scenes / scripts: one big button each + for (size_t i = 0; i < N_SCENE; i++) { + lv_obj_t* b = lv_btn_create(tabScene); + lv_obj_set_size(b, lv_pct(100), 44); + lv_obj_t* l = lv_label_create(b); + lv_obj_set_style_text_font(l, &lv_font_montserrat_20, 0); + lv_label_set_long_mode(l, LV_LABEL_LONG_DOT); + lv_obj_set_size(l, 270, 26); + lv_label_set_text_fmt(l, LV_SYMBOL_PLAY " %s", SCENES[i].name.c_str()); + lv_obj_align(l, LV_ALIGN_LEFT_MID, 4, 0); + lv_obj_add_event_cb(b, onScene, LV_EVENT_CLICKED, (void*)(intptr_t)i); + } + if (!N_SCENE) { hintScene = lv_label_create(tabScene); lv_label_set_text(hintScene, "Keine Szenen in HA gefunden"); } + + // Temperature history chart (collected on the device, see sampleHistory) + if (N_CLIMATE) { + lv_obj_t* note = lv_label_create(tabHist); + lv_obj_set_style_text_color(note, lv_palette_lighten(LV_PALETTE_GREY, 1), 0); + lv_label_set_text(note, "Temperatur, letzte 24 h (seit Start)"); + chart = lv_chart_create(tabHist); + lv_obj_set_size(chart, lv_pct(100), 100); + lv_chart_set_type(chart, LV_CHART_TYPE_LINE); + lv_chart_set_point_count(chart, HIST_POINTS); + lv_chart_set_div_line_count(chart, 3, 0); + lv_obj_set_style_size(chart, 0, LV_PART_INDICATOR); + lv_obj_set_style_line_width(chart, 2, LV_PART_ITEMS); + static const lv_palette_t cols[] = {LV_PALETTE_RED, LV_PALETTE_LIGHT_BLUE, LV_PALETTE_GREEN, LV_PALETTE_ORANGE, + LV_PALETTE_PURPLE, LV_PALETTE_CYAN, LV_PALETTE_YELLOW, LV_PALETTE_PINK}; + for (size_t i = 0; i < N_CLIMATE; i++) { + for (int k = 0; k < HIST_POINTS; k++) histY[i][k] = LV_CHART_POINT_NONE; + chartSer[i] = lv_chart_add_series(chart, lv_palette_main(cols[i % 8]), LV_CHART_AXIS_PRIMARY_Y); + lv_chart_set_ext_y_array(chart, chartSer[i], histY[i]); + histLegend[i] = lv_label_create(tabHist); + lv_obj_set_style_text_color(histLegend[i], lv_palette_main(cols[i % 8]), 0); + lv_label_set_text(histLegend[i], CLIMATES[i].name.c_str()); + } + } else { + hintHist = lv_label_create(tabHist); + lv_label_set_text(hintHist, "Keine Heizungen in HA gefunden"); + } + if (!N_CLIMATE) { hintHeat = lv_label_create(tabHeat); lv_label_set_text(hintHeat, "Keine Heizungen in HA gefunden"); } if (!N_LIGHT) { hintLight = lv_label_create(tabLight); lv_label_set_text(hintLight, "Keine Lichter in HA gefunden"); } } @@ -520,12 +607,20 @@ static bool discoverDevices() { const char* lightSrc = "(states.light | list + states.switch | list)"; #else const char* lightSrc = "(states.light | list)"; +#endif +#if INCLUDE_SCRIPTS + const char* sceneSrc = "(states.scene | list + states.script | list)"; +#else + const char* sceneSrc = "(states.scene | list)"; #endif String tpl = String("{{ {" "'c': states.climate | sort(attribute='name') | map(attribute='entity_id') | list," "'cn': states.climate | sort(attribute='name') | map(attribute='name') | list," "'l': ") + lightSrc + " | sort(attribute='name') | map(attribute='entity_id') | list," - "'ln': " + lightSrc + " | sort(attribute='name') | map(attribute='name') | list" + "'ln': " + lightSrc + " | sort(attribute='name') | map(attribute='name') | list," + "'sc': " + sceneSrc + " | sort(attribute='name') | map(attribute='entity_id') | list," + "'scn': " + sceneSrc + " | sort(attribute='name') | map(attribute='name') | list," + "'w': states.weather | map(attribute='entity_id') | list | first | default('')" "} | tojson }}"; JsonDocument req; req["template"] = tpl; @@ -554,11 +649,142 @@ static bool discoverDevices() { }; N_CLIMATE = fill(doc["c"], doc["cn"], CLIMATES, MAX_CLIMATE); N_LIGHT = fill(doc["l"], doc["ln"], LIGHTS, MAX_LIGHT); - Serial.printf("HA: %u Heizungen, %u Lichter gefunden\n", (unsigned)N_CLIMATE, (unsigned)N_LIGHT); + N_SCENE = fill(doc["sc"], doc["scn"], SCENES, MAX_SCENE); + weatherEntity = String(doc["w"] | ""); + Serial.printf("HA: %u Heizungen, %u Lichter, %u Szenen, Wetter: %s\n", (unsigned)N_CLIMATE, (unsigned)N_LIGHT, + (unsigned)N_SCENE, weatherEntity.c_str()); populateUi(); + discoveredAt = millis(); return true; } +// ---------------------------------------------------------------- Weather +static const char* weatherText(const char* c) { + if (!strcmp(c, "sunny")) return "Sonnig"; + if (!strcmp(c, "clear-night")) return "Klar"; + if (!strcmp(c, "partlycloudy")) return "Wolkig"; + if (!strcmp(c, "cloudy")) return "Bedeckt"; + if (!strcmp(c, "rainy")) return "Regen"; + if (!strcmp(c, "pouring")) return "Starkregen"; + if (!strcmp(c, "snowy")) return "Schnee"; + if (!strcmp(c, "snowy-rainy")) return "Schneeregen"; + if (!strcmp(c, "fog")) return "Nebel"; + if (!strcmp(c, "hail")) return "Hagel"; + if (!strcmp(c, "lightning") || !strcmp(c, "lightning-rainy")) return "Gewitter"; + if (!strcmp(c, "windy") || !strcmp(c, "windy-variant")) return "Windig"; + return c; +} + +static void pollWeather() { + if (!weatherEntity.length()) return; + String resp; + if (haRequest(false, "/api/states/" + weatherEntity, "", &resp) != 200) return; + JsonDocument doc; + if (deserializeJson(doc, resp)) return; + float t = doc["attributes"]["temperature"] | NAN; + const char* cond = weatherText(doc["state"] | ""); + char buf[40]; + if (isnan(t)) snprintf(buf, sizeof buf, LV_SYMBOL_HOME " %s", cond); + else snprintf(buf, sizeof buf, LV_SYMBOL_HOME " %.0f° %s", t, cond); + lv_label_set_text(titleLabel, buf); +} + +// ---------------------------------------------------------------- Temperature history +static void sampleHistory() { + if (!chart) return; + int lo = 10000, hi = -10000; + for (size_t i = 0; i < N_CLIMATE; i++) { + memmove(histY[i], histY[i] + 1, (HIST_POINTS - 1) * sizeof(lv_coord_t)); + float cur = climUi[i].cur; + histY[i][HIST_POINTS - 1] = isnan(cur) ? LV_CHART_POINT_NONE : (lv_coord_t)lroundf(cur * 10); + int mn = 10000, mx = -10000; + for (int k = 0; k < HIST_POINTS; k++) { + if (histY[i][k] == LV_CHART_POINT_NONE) continue; + mn = min(mn, (int)histY[i][k]); + mx = max(mx, (int)histY[i][k]); + } + if (mx >= mn) { + lo = min(lo, mn); hi = max(hi, mx); + lv_label_set_text_fmt(histLegend[i], "%s %.1f° (%.1f - %.1f)", CLIMATES[i].name.c_str(), + isnan(cur) ? 0.0f : cur, mn / 10.0f, mx / 10.0f); + } + } + if (hi >= lo) { + int bottom = (lo / 10) * 10 - 10, top = (hi / 10) * 10 + 20; + if (top - bottom < 40) top = bottom + 40; + lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, bottom, top); + } + lv_chart_refresh(chart); +} + +// ---------------------------------------------------------------- Alerts +static void showAlerts() { + lv_obj_clean(tabAlert); + if (!nAlerts) { + lv_obj_t* l = lv_label_create(tabAlert); + lv_obj_set_style_text_color(l, lv_palette_main(LV_PALETTE_GREEN), 0); + lv_label_set_text(l, LV_SYMBOL_OK " Alles in Ordnung"); + lv_label_set_text(alertLabel, ""); + return; + } + for (size_t i = 0; i < nAlerts; i++) { + lv_obj_t* c = lv_obj_create(tabAlert); + lv_obj_set_size(c, lv_pct(100), LV_SIZE_CONTENT); + lv_obj_set_style_pad_all(c, 6, 0); + lv_obj_set_style_radius(c, 8, 0); + lv_obj_clear_flag(c, LV_OBJ_FLAG_SCROLLABLE); + lv_obj_t* l = lv_label_create(c); + lv_obj_set_width(l, lv_pct(100)); + lv_label_set_long_mode(l, LV_LABEL_LONG_WRAP); + lv_obj_set_style_text_color(l, lv_palette_main(i == 0 ? LV_PALETTE_RED : LV_PALETTE_AMBER), 0); + lv_label_set_text_fmt(l, LV_SYMBOL_WARNING " %s", alerts[i].c_str()); + } + lv_label_set_text_fmt(alertLabel, LV_SYMBOL_BELL " %u", (unsigned)nAlerts); +} + +// One small template call: open windows/doors, offline devices, low batteries, HA notifications. +static void pollAlerts() { + static const char* TPL = R"J({% set ns = namespace(a=[], open=0) %} +{% for s in states.binary_sensor if s.state == 'on' and s.attributes.device_class in ['window','door','opening','garage_door'] %}{% set ns.open = ns.open + 1 %}{% set ns.a = ns.a + ['Offen: ' ~ s.name] %}{% endfor %} +{% if ns.open > 0 and (states.climate | selectattr('attributes.hvac_action','eq','heating') | list | count) > 0 %}{% set ns.a = ['Fenster offen waehrend geheizt wird!'] + ns.a %}{% endif %} +{% for s in states.climate if s.state == 'unavailable' %}{% set ns.a = ns.a + ['Offline: ' ~ s.name] %}{% endfor %} +{% for s in states.light if s.state == 'unavailable' %}{% set ns.a = ns.a + ['Offline: ' ~ s.name] %}{% endfor %} +{% for s in states.sensor if s.attributes.device_class == 'battery' and (s.state | float(100)) < 20 %}{% set ns.a = ns.a + ['Batterie ' ~ (s.state | int(0)) ~ '%: ' ~ s.name] %}{% endfor %} +{% for s in states.persistent_notification %}{% set ns.a = ns.a + ['HA: ' ~ (s.attributes.title or s.attributes.message or '')] %}{% endfor %} +{{ ns.a[:10] | tojson }})J"; + JsonDocument req; + req["template"] = TPL; + String body, resp; + serializeJson(req, body); + if (haRequest(true, "/api/template", body, &resp) != 200) return; + JsonDocument doc; + if (deserializeJson(doc, resp)) return; + + String fresh[MAX_ALERT]; + size_t n = 0; + for (JsonVariant v : doc.as()) { + if (n >= MAX_ALERT) break; + String t = asciiName(String(v | "")); + if (t.length() > 80) t = t.substring(0, 80); + if (t.length()) fresh[n++] = t; + } + bool changed = n != nAlerts, isNew = false; + for (size_t i = 0; i < n; i++) { + bool known = false; + for (size_t j = 0; j < nAlerts; j++) if (alerts[j] == fresh[i]) known = true; + if (!known) { isNew = true; changed = true; } + } + for (size_t i = 0; i < n; i++) alerts[i] = fresh[i]; + nAlerts = n; + static bool first = true; + if (changed || first) showAlerts(); + first = false; + if (isNew && screenOff) { // wake the display for a new alert + setBacklight(true); + lv_disp_trig_activity(nullptr); + } +} + // ---------------------------------------------------------------- Setup / loop void setup() { Serial.begin(115200); @@ -677,5 +903,17 @@ void loop() { else setStatus(LV_SYMBOL_WARNING " HA", lv_palette_main(LV_PALETTE_RED)); pollIdx = (pollIdx + 1) % (N_CLIMATE + N_LIGHT); } + + // Background jobs; the blocking HTTP calls only run while the user is not touching the screen. + if (screenOff || lv_disp_get_inactive_time(nullptr) > 700) { + static uint32_t lastAlert = 0, lastWeather = 0; + if (!lastAlert || now - lastAlert > 30000) { lastAlert = now ? now : 1; pollAlerts(); } + else if (!lastWeather || now - lastWeather > 600000) { lastWeather = now ? now : 1; pollWeather(); } + } + static uint32_t lastHist = 0; + if (lastHist ? now - lastHist > HIST_STEP_MS : now - discoveredAt > 15000) { + lastHist = now ? now : 1; + sampleHistory(); + } delay(5); }