Geräte automatisch aus Home Assistant laden, WLAN-Signalstärke anzeigen

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Sonnet 5.5 committed 2026-11-04 21:18:51 +01:00
1 parent aec613de9f
commit ecab22386e
3 files changed
+155 -30

No files matched your search

+2 -2
View File
@@ -4,12 +4,12 @@ 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. `switch.*`-Entitäten funktionieren auch.
- Uhrzeit über NTP, WLAN- und HA-Status, Display geht nach 60 s ohne Bedienung aus (Tippen weckt es wieder).
- 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
1. In Home Assistant: Profil → Sicherheit → **Langlebiges Zugangs-Token** erstellen.
2. `include/config.h` anpassen (sie ist schon als Kopie von `config.example.h` angelegt und liegt in `.gitignore`): WLAN, `HA_URL`, `HA_TOKEN` und deine Entity-IDs (zu finden in HA unter Einstellungen → Geräte → Entitäten).
2. `include/config.h` anpassen (sie ist schon als Kopie von `config.example.h` angelegt und liegt in `.gitignore`): WLAN, `HA_URL`, `HA_TOKEN` und optional `INCLUDE_SWITCHES`. Heizungen (`climate.*`) und Lichter (`light.*`) werden automatisch aus HA geladen.
3. In VSCode mit der PlatformIO-Extension: **Upload** (oder `pio run -t upload`), danach den **Monitor** für Logs öffnen.
Hinweis: Die eingebaute Schrift hat keine Umlaute. Schreib die Namen also als "Kueche" statt "Küche".
+3 -13
View File
@@ -13,19 +13,9 @@
#define HA_TOKEN "eyJ0eXAiOiJKV1Qi..."
// ---- Devices ----
// Use names without umlauts (ae/oe/ue); the built-in font has no umlauts.
// Each list needs at least one entry.
static const ClimateCfg CLIMATES[] = {
{"climate.wohnzimmer", "Wohnzimmer"},
{"climate.schlafzimmer", "Schlafzimmer"},
{"climate.bad", "Bad"},
};
static const LightCfg LIGHTS[] = {
{"light.wohnzimmer_decke", "Decke"},
{"light.stehlampe", "Stehlampe"},
{"switch.lichterkette", "Lichterkette"}, // switch.* works too (on/off only)
};
// 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
// ---- Behaviour ----
#define POLL_STEP_MS 500 // poll one entity every X ms (round robin)
+150 -15
View File
@@ -10,8 +10,7 @@
#include <lvgl.h>
#include <time.h>
struct ClimateCfg { const char* entity; const char* name; };
struct LightCfg { const char* entity; const char* name; };
struct Device { String entity; String name; };
#if __has_include("config.h")
#include "config.h"
@@ -36,8 +35,17 @@ static lv_disp_draw_buf_t drawBuf;
static lv_color_t buf1[SCREEN_W * 24];
// ---------------------------------------------------------------- State
static constexpr size_t N_CLIMATE = sizeof(CLIMATES) / sizeof(CLIMATES[0]);
static constexpr size_t N_LIGHT = sizeof(LIGHTS) / sizeof(LIGHTS[0]);
static constexpr size_t MAX_CLIMATE = 8;
static constexpr size_t MAX_LIGHT = 12;
static Device CLIMATES[MAX_CLIMATE];
static Device LIGHTS[MAX_LIGHT];
static size_t N_CLIMATE = 0;
static size_t N_LIGHT = 0;
static bool discovered = false;
static lv_obj_t* tabHeat;
static lv_obj_t* tabLight;
static lv_obj_t* hintHeat;
static lv_obj_t* hintLight;
struct ClimateUi {
lv_obj_t* target = nullptr;
@@ -52,10 +60,12 @@ struct LightUi {
uint32_t ignoreUntil = 0; // ignore poll results right after a command
};
static ClimateUi climUi[N_CLIMATE];
static LightUi lightUi[N_LIGHT];
static ClimateUi climUi[MAX_CLIMATE];
static LightUi lightUi[MAX_LIGHT];
static lv_obj_t* clockLabel;
static lv_obj_t* statusLabel;
static lv_obj_t* rssiBars[4];
static lv_obj_t* rssiLabel;
static bool screenOff = false;
static bool swallowTouch = false;
@@ -144,12 +154,25 @@ static bool callService(const char* domain, const char* service, JsonDocument& d
return true;
}
static String domainOf(const char* entity) {
String e(entity);
static String domainOf(const String& e) {
return e.substring(0, e.indexOf('.'));
}
// ---------------------------------------------------------------- UI updates
static void updateRssi() {
bool conn = WiFi.status() == WL_CONNECTED;
int rssi = conn ? WiFi.RSSI() : -100;
int level = !conn ? 0 : rssi > -55 ? 4 : rssi > -65 ? 3 : rssi > -75 ? 2 : rssi > -85 ? 1 : 0;
lv_color_t col = level >= 3 ? lv_palette_main(LV_PALETTE_GREEN)
: level == 2 ? lv_palette_main(LV_PALETTE_AMBER)
: lv_palette_main(LV_PALETTE_RED);
for (int i = 0; i < 4; i++)
lv_obj_set_style_bg_color(rssiBars[i], i < level ? col : lv_color_hex(0x3a444e), 0);
char buf[12];
if (conn) snprintf(buf, sizeof buf, "%d", rssi); else buf[0] = 0;
lv_label_set_text(rssiLabel, buf);
}
static void showTarget(size_t i) {
char buf[16];
if (isnan(climUi[i].targetVal)) snprintf(buf, sizeof buf, "--");
@@ -218,11 +241,11 @@ static void applyLight(size_t i, JsonDocument& doc) {
static bool pollEntity(size_t idx) {
bool isClimate = idx < N_CLIMATE;
const char* entity = isClimate ? CLIMATES[idx].entity : LIGHTS[idx - N_CLIMATE].entity;
const String& entity = isClimate ? CLIMATES[idx].entity : LIGHTS[idx - N_CLIMATE].entity;
String resp;
int code = haRequest(false, String("/api/states/") + entity, "", &resp);
if (code != 200) {
Serial.printf("GET %s -> %d\n", entity, code);
Serial.printf("GET %s -> %d\n", entity.c_str(), code);
return false;
}
JsonDocument doc;
@@ -319,6 +342,21 @@ static void buildUi() {
clockLabel = lv_label_create(hdr);
lv_label_set_text(clockLabel, "--:--");
lv_obj_align(clockLabel, LV_ALIGN_CENTER, 0, 0);
// WiFi signal strength: 4 bars + dBm value
for (int i = 0; i < 4; i++) {
rssiBars[i] = lv_obj_create(hdr);
lv_obj_set_size(rssiBars[i], 3, 5 + i * 3);
lv_obj_set_style_radius(rssiBars[i], 0, 0);
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);
}
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);
updateRssi();
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));
@@ -327,16 +365,25 @@ 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);
lv_obj_t* tabHeat = makeTab(tv, LV_SYMBOL_CHARGE " Heizung");
lv_obj_t* tabLight = makeTab(tv, LV_SYMBOL_EYE_OPEN " Licht");
tabHeat = makeTab(tv, LV_SYMBOL_CHARGE " Heizung");
tabLight = makeTab(tv, LV_SYMBOL_EYE_OPEN " Licht");
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 ...");
}
static void populateUi() {
if (hintHeat) { lv_obj_del(hintHeat); hintHeat = nullptr; }
if (hintLight) { lv_obj_del(hintLight); hintLight = 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_label_set_text(name, CLIMATES[i].name);
lv_label_set_text(name, CLIMATES[i].name.c_str());
lv_obj_align(name, LV_ALIGN_TOP_LEFT, 0, -2);
climUi[i].info = lv_label_create(c);
@@ -363,7 +410,7 @@ static void buildUi() {
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_label_set_text(name, LIGHTS[i].name);
lv_label_set_text(name, LIGHTS[i].name.c_str());
lv_obj_align(name, LV_ALIGN_TOP_LEFT, 0, 4);
lightUi[i].sw = lv_switch_create(c);
@@ -380,6 +427,71 @@ static void buildUi() {
lv_obj_add_event_cb(lightUi[i].slider, onLightSlider, LV_EVENT_RELEASED, (void*)(intptr_t)i);
}
}
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"); }
}
// Converts UTF-8 umlauts to ASCII (the built-in font has none) and drops other non-ASCII bytes.
static String asciiName(const String& in) {
String out;
for (size_t i = 0; i < in.length(); i++) {
uint8_t c = in[i];
if (c < 0x80) { out += (char)c; continue; }
if (c == 0xC3 && i + 1 < in.length()) {
uint8_t d = in[++i];
switch (d) {
case 0xA4: out += "ae"; break; case 0xB6: out += "oe"; break; case 0xBC: out += "ue"; break;
case 0x84: out += "Ae"; break; case 0x96: out += "Oe"; break; case 0x9C: out += "Ue"; break;
case 0x9F: out += "ss"; break;
}
}
}
out.trim();
return out;
}
// Asks HA (template API) for all climate/light entities and builds the UI from them.
static bool discoverDevices() {
#if INCLUDE_SWITCHES
const char* lightSrc = "(states.light | list + states.switch | list)";
#else
const char* lightSrc = "(states.light | 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"
"} | tojson }}";
JsonDocument req;
req["template"] = tpl;
String body, resp;
serializeJson(req, body);
int code = haRequest(true, "/api/template", body, &resp);
if (code != 200) {
Serial.printf("Geraete-Abfrage fehlgeschlagen: %d\n", code);
return false;
}
JsonDocument doc;
if (deserializeJson(doc, resp)) { Serial.println("Geraete-Antwort ist kein JSON"); return false; }
auto fill = [&](JsonArray ids, JsonArray names, Device* dst, size_t max) {
size_t n = 0;
for (size_t i = 0; i < ids.size() && n < max; i++) {
String id = ids[i] | "";
String nm = asciiName(String(names[i] | ""));
dst[n].entity = id;
dst[n].name = nm.length() ? nm : id;
n++;
}
return n;
};
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);
populateUi();
return true;
}
// ---------------------------------------------------------------- Setup / loop
@@ -437,6 +549,13 @@ void loop() {
}
}
// WiFi signal strength
static uint32_t lastRssi = 0;
if (now - lastRssi > 2000) {
lastRssi = now;
updateRssi();
}
// Backlight timeout
if (SCREEN_TIMEOUT_S > 0 && !screenOff && lv_disp_get_inactive_time(nullptr) > SCREEN_TIMEOUT_S * 1000UL)
setBacklight(false);
@@ -451,6 +570,22 @@ void loop() {
}
if (!connected) { delay(5); return; }
// Discover devices from HA (retry every 10 s until it works)
static uint32_t lastDiscover = 0;
if (!discovered) {
if (lastDiscover == 0 || now - lastDiscover > 10000) {
lastDiscover = now ? now : 1;
if (discoverDevices()) {
discovered = true;
setStatus(LV_SYMBOL_WIFI, lv_palette_main(LV_PALETTE_GREEN));
} else {
setStatus(LV_SYMBOL_WARNING " HA", lv_palette_main(LV_PALETTE_RED));
}
}
delay(5);
return;
}
// Send pending target temperatures
for (size_t i = 0; i < N_CLIMATE; i++) {
ClimateUi& u = climUi[i];
@@ -467,7 +602,7 @@ 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 && now - lastPoll > POLL_STEP_MS) {
if (!screenOff && (N_CLIMATE + N_LIGHT) > 0 && now - lastPoll > POLL_STEP_MS) {
lastPoll = now;
bool ok = pollEntity(pollIdx);
if (ok) setStatus(LV_SYMBOL_WIFI, lv_palette_main(LV_PALETTE_GREEN));