From 08c47f00f8076aff1a00eb1bd70f04db9a6a6551 Mon Sep 17 00:00:00 2001 From: Eduard Iten Date: Tue, 22 Jul 2025 08:55:30 +0200 Subject: [PATCH] feat(esphome): Add CAN configuration Signed-off-by: Eduard Iten --- software/esphome/can.yaml | 106 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 software/esphome/can.yaml diff --git a/software/esphome/can.yaml b/software/esphome/can.yaml new file mode 100644 index 0000000..b1469fe --- /dev/null +++ b/software/esphome/can.yaml @@ -0,0 +1,106 @@ +# =================================================================== +# ESPHome Configuration +# CAN-Bus Master für ein Bewässerungssystem auf Basis des ESP32-C6 +# +# Version 10: Finale Korrektur der Lambda-Signatur gemäß Dokumentation +# =================================================================== + +esphome: + name: can-bridge + friendly_name: Irrigation can bridge + +esp32: + board: esp32-c6-devkitm-1 + framework: + type: esp-idf # Erforderlich für den ESP32-C6 + +# --- Netzwerk & Sicherheit --- +wifi: + ssid: !secret wifi_ssid + password: !secret wifi_password + fast_connect: true + +api: + encryption: + key: !secret api_key + +ota: + platform: esphome + password: !secret ota_password + +logger: + +web_server: + +# --- Globale Variablen --- +globals: + - id: ventil_2_can_state + type: int + initial_value: '0' # Startet als "geschlossen" + +# --- CAN-Bus Konfiguration --- +canbus: + - platform: esp32_can + id: my_can_bus + tx_pin: GPIO5 + rx_pin: GPIO4 + bit_rate: 125kbps + can_id: 0x000 # Erforderlich, um Parser-Fehler zu beheben. + on_frame: + # Horcht nur auf die Statusmeldung von Knoten 2 (ID 0x422) + - can_id: 0x422 + then: + - lambda: |- + if (x.size() < 1) { + ESP_LOGW("on_can_frame", "Received empty Frame for ID 0x422"); + return; + } + int received_state = x[0]; + id(ventil_2_can_state) = received_state; + ESP_LOGD("on_can_frame", "Received state from Valve 2: %i", received_state); + - valve.template.publish: + id: ventil_2 + current_operation: !lambda |- + int state = id(ventil_2_can_state); + if (state == 2) { + return VALVE_OPERATION_OPENING; + } else if (state == 3) { + return VALVE_OPERATION_CLOSING; + } else { + return VALVE_OPERATION_IDLE; + } + +# --- Home Assistant Entitäten --- +valve: + - platform: template + name: "Ventil 2" + id: ventil_2 + + # Diese Lambda meldet nur den binären End-Zustand (offen/geschlossen) + lambda: |- + if (id(ventil_2_can_state) == 0) { + return VALVE_CLOSED; + } else if (id(ventil_2_can_state) == 1) { + return VALVE_OPEN; + } else { + return NAN; + } + + # Aktionen zum Steuern des Ventils + open_action: + - canbus.send: + canbus_id: my_can_bus + can_id: 0x210 + data: [0x02, 0x01] + + close_action: + - canbus.send: + canbus_id: my_can_bus + can_id: 0x210 + data: [0x02, 0x00] + + stop_action: + - canbus.send: + canbus_id: my_can_bus + can_id: 0x210 + data: [0x02, 0x03]