Compare commits
3
Commits
ecab22386e
...
3907b07171
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3907b07171 | ||
|
|
5053c4978f | ||
|
|
8bbd9a0dfe |
No files matched your search
@@ -3,7 +3,11 @@
|
||||
Touch-Dashboard für das ESP32-2432S028R ("Cheap Yellow Display"), das Heizkörper und Lampen über Home Assistant steuert.
|
||||
|
||||
- **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. `switch.*`-Entitäten funktionieren auch.
|
||||
- **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
|
||||
|
||||
@@ -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)
|
||||
|
||||
+6
-2
@@ -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 <stdlib.h>
|
||||
#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"
|
||||
|
||||
+325
-19
@@ -47,15 +47,36 @@ 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
|
||||
};
|
||||
struct LightUi {
|
||||
lv_obj_t* sw = nullptr;
|
||||
lv_obj_t* slider = nullptr;
|
||||
lv_obj_t* palette = nullptr;
|
||||
lv_obj_t* card = nullptr;
|
||||
uint32_t ignoreUntil = 0; // ignore poll results right after a command
|
||||
};
|
||||
@@ -101,8 +122,13 @@ static void touchRead(lv_indev_drv_t*, lv_indev_data_t* data) {
|
||||
data->state = LV_INDEV_STATE_REL;
|
||||
return;
|
||||
}
|
||||
TS_Point p = ts.getPoint();
|
||||
int rx = p.x, ry = p.y;
|
||||
// Average a few samples: raw XPT2046 readings jitter, which makes taps look like drags.
|
||||
int sx = 0, sy = 0, n = 0;
|
||||
for (int k = 0; k < 4; k++) {
|
||||
TS_Point q = ts.getPoint();
|
||||
sx += q.x; sy += q.y; n++;
|
||||
}
|
||||
int rx = sx / n, ry = sy / n;
|
||||
if (TOUCH_SWAP_XY) std::swap(rx, ry);
|
||||
int x = map(rx, TOUCH_X_MIN, TOUCH_X_MAX, 0, SCREEN_W - 1);
|
||||
int y = map(ry, TOUCH_Y_MIN, TOUCH_Y_MAX, 0, SCREEN_H - 1);
|
||||
@@ -200,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];
|
||||
@@ -225,18 +252,32 @@ static void applyLight(size_t i, JsonDocument& doc) {
|
||||
else lv_obj_clear_state(u.sw, LV_STATE_DISABLED);
|
||||
|
||||
if (!u.slider) return;
|
||||
// Only show the slider if the lamp is dimmable.
|
||||
// Only show the sliders if the lamp supports them.
|
||||
JsonArray modes = doc["attributes"]["supported_color_modes"];
|
||||
bool dimmable = !modes.isNull() && modes.size() > 0 && strcmp(modes[0] | "onoff", "onoff") != 0;
|
||||
bool dimmable = false, colorable = false;
|
||||
for (JsonVariant m : modes) {
|
||||
const char* ms = m | "onoff";
|
||||
if (strcmp(ms, "onoff")) dimmable = true;
|
||||
if (!strcmp(ms, "hs") || !strcmp(ms, "rgb") || !strcmp(ms, "rgbw") || !strcmp(ms, "rgbww") || !strcmp(ms, "xy"))
|
||||
colorable = true;
|
||||
}
|
||||
int h = 52;
|
||||
if (dimmable) {
|
||||
lv_obj_clear_flag(u.slider, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_set_height(u.card, 84);
|
||||
h += 32;
|
||||
int b = doc["attributes"]["brightness"] | 0;
|
||||
if (!lv_slider_is_dragged(u.slider)) lv_slider_set_value(u.slider, on ? max(1, b * 100 / 255) : 0, LV_ANIM_OFF);
|
||||
} else {
|
||||
lv_obj_add_flag(u.slider, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_set_height(u.card, 52);
|
||||
}
|
||||
if (colorable) {
|
||||
lv_obj_clear_flag(u.palette, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_align(u.palette, LV_ALIGN_TOP_LEFT, 0, h - 8);
|
||||
h += 34;
|
||||
} else {
|
||||
lv_obj_add_flag(u.palette, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
lv_obj_set_height(u.card, h);
|
||||
}
|
||||
|
||||
static bool pollEntity(size_t idx) {
|
||||
@@ -292,6 +333,35 @@ static void onLightSlider(lv_event_t* e) {
|
||||
}
|
||||
}
|
||||
|
||||
// Colour palette: index 0 = warm white, others = hue in degrees (full saturation).
|
||||
static const int PALETTE_HUES[] = {-1, 0, 30, 55, 120, 180, 240, 280, 320};
|
||||
static constexpr size_t N_PALETTE = sizeof(PALETTE_HUES) / sizeof(PALETTE_HUES[0]);
|
||||
|
||||
static void onPalette(lv_event_t* e) {
|
||||
intptr_t v = (intptr_t)lv_event_get_user_data(e);
|
||||
size_t i = v / N_PALETTE, k = v % N_PALETTE;
|
||||
JsonDocument doc;
|
||||
doc["entity_id"] = LIGHTS[i].entity;
|
||||
if (PALETTE_HUES[k] < 0) {
|
||||
doc["color_temp_kelvin"] = 3000;
|
||||
} else {
|
||||
JsonArray hs = doc["hs_color"].to<JsonArray>();
|
||||
hs.add(PALETTE_HUES[k]);
|
||||
hs.add(100);
|
||||
}
|
||||
lightUi[i].ignoreUntil = millis() + 3000;
|
||||
lv_obj_add_state(lightUi[i].sw, LV_STATE_CHECKED);
|
||||
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);
|
||||
@@ -336,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);
|
||||
@@ -350,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));
|
||||
@@ -365,24 +441,35 @@ 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);
|
||||
lv_obj_set_style_text_font(name, &lv_font_montserrat_20, 0);
|
||||
lv_label_set_long_mode(name, LV_LABEL_LONG_DOT);
|
||||
lv_obj_set_width(name, 125);
|
||||
lv_obj_set_size(name, 125, 26);
|
||||
lv_label_set_text(name, CLIMATES[i].name.c_str());
|
||||
lv_obj_align(name, LV_ALIGN_TOP_LEFT, 0, -2);
|
||||
|
||||
@@ -409,7 +496,7 @@ static void populateUi() {
|
||||
lv_obj_t* name = lv_label_create(c);
|
||||
lv_obj_set_style_text_font(name, &lv_font_montserrat_20, 0);
|
||||
lv_label_set_long_mode(name, LV_LABEL_LONG_DOT);
|
||||
lv_obj_set_width(name, 200);
|
||||
lv_obj_set_size(name, 200, 26);
|
||||
lv_label_set_text(name, LIGHTS[i].name.c_str());
|
||||
lv_obj_align(name, LV_ALIGN_TOP_LEFT, 0, 4);
|
||||
|
||||
@@ -422,12 +509,75 @@ static void populateUi() {
|
||||
lightUi[i].slider = lv_slider_create(c);
|
||||
lv_slider_set_range(lightUi[i].slider, 0, 100);
|
||||
lv_obj_set_width(lightUi[i].slider, lv_pct(92));
|
||||
lv_obj_align(lightUi[i].slider, LV_ALIGN_BOTTOM_MID, 0, -6);
|
||||
lv_obj_align(lightUi[i].slider, LV_ALIGN_TOP_MID, 0, 42);
|
||||
lv_obj_add_flag(lightUi[i].slider, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_event_cb(lightUi[i].slider, onLightSlider, LV_EVENT_RELEASED, (void*)(intptr_t)i);
|
||||
|
||||
// Colour palette (shown only for colour-capable lamps)
|
||||
lv_obj_t* pal = lv_obj_create(c);
|
||||
lightUi[i].palette = pal;
|
||||
lv_obj_set_size(pal, lv_pct(100), 30);
|
||||
lv_obj_set_style_bg_opa(pal, LV_OPA_TRANSP, 0);
|
||||
lv_obj_set_style_border_width(pal, 0, 0);
|
||||
lv_obj_set_style_pad_all(pal, 0, 0);
|
||||
lv_obj_set_style_pad_column(pal, 3, 0);
|
||||
lv_obj_set_flex_flow(pal, LV_FLEX_FLOW_ROW);
|
||||
lv_obj_clear_flag(pal, LV_OBJ_FLAG_SCROLLABLE);
|
||||
lv_obj_add_flag(pal, LV_OBJ_FLAG_HIDDEN);
|
||||
for (size_t k = 0; k < N_PALETTE; k++) {
|
||||
lv_obj_t* b = lv_btn_create(pal);
|
||||
lv_obj_set_size(b, 27, 27);
|
||||
lv_obj_set_style_radius(b, 6, 0);
|
||||
lv_obj_set_style_border_width(b, 1, 0);
|
||||
lv_obj_set_style_border_color(b, lv_color_hex(0x606a74), 0);
|
||||
lv_obj_set_style_bg_color(b, PALETTE_HUES[k] < 0 ? lv_color_hex(0xffd9a0)
|
||||
: lv_color_hsv_to_rgb(PALETTE_HUES[k], 100, 100), 0);
|
||||
lv_obj_add_event_cb(b, onPalette, LV_EVENT_CLICKED, (void*)(intptr_t)(i * N_PALETTE + k));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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"); }
|
||||
}
|
||||
@@ -457,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;
|
||||
@@ -482,6 +640,8 @@ static bool discoverDevices() {
|
||||
String id = ids[i] | "";
|
||||
String nm = asciiName(String(names[i] | ""));
|
||||
dst[n].entity = id;
|
||||
if (nm.startsWith("Heizung ") && nm.length() > 8) nm = nm.substring(8);
|
||||
else if (nm.startsWith("Licht ") && nm.length() > 6) nm = nm.substring(6);
|
||||
dst[n].name = nm.length() ? nm : id;
|
||||
n++;
|
||||
}
|
||||
@@ -489,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<JsonArray>()) {
|
||||
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);
|
||||
@@ -522,6 +813,8 @@ void setup() {
|
||||
lv_indev_drv_init(&indevDrv);
|
||||
indevDrv.type = LV_INDEV_TYPE_POINTER;
|
||||
indevDrv.read_cb = touchRead;
|
||||
indevDrv.scroll_limit = 18; // pixels of movement before a touch counts as scrolling (default 10)
|
||||
indevDrv.scroll_throw = 8; // less inertia
|
||||
lv_indev_drv_register(&indevDrv);
|
||||
|
||||
buildUi();
|
||||
@@ -602,12 +895,25 @@ void loop() {
|
||||
// Round-robin polling (paused while the screen is off; starts with a full pass)
|
||||
static uint32_t lastPoll = 0;
|
||||
static size_t pollIdx = 0;
|
||||
if (!screenOff && (N_CLIMATE + N_LIGHT) > 0 && now - lastPoll > POLL_STEP_MS) {
|
||||
if (!screenOff && (N_CLIMATE + N_LIGHT) > 0 && now - lastPoll > POLL_STEP_MS &&
|
||||
lv_disp_get_inactive_time(nullptr) > 700) { // a blocking HTTP call would make scrolling stutter
|
||||
lastPoll = now;
|
||||
bool ok = pollEntity(pollIdx);
|
||||
if (ok) setStatus(LV_SYMBOL_WIFI, lv_palette_main(LV_PALETTE_GREEN));
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user