Files
ClimatController/hardware/esp32_relay_exhaust/elica-relay.yaml
T
Dmitriy bf832ac86b work: функционал для работы с вятяжкой
* Добавил полный функционал управления elica через esp32 по MQTT
* Добавил в прошивку принудительную блокировку внешнего винтелятора при работающей вытяжке
* Добавил веб-панели для управления вытяжкой
2026-09-26 23:59:01 +03:00

349 lines
10 KiB
YAML

esphome:
name: elica-relay
friendly_name: Elica Relay Controller
on_boot:
# First disable 74HC595 outputs so relays cannot chatter during boot.
- priority: 900
then:
- lambda: |-
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
# Initialise shared bus, force all relays OFF, then enable outputs.
- priority: 600
then:
- delay: 500ms
- lambda: |-
pinMode(25, OUTPUT); // shared LATCH
pinMode(26, OUTPUT); // shared CLOCK
pinMode(33, OUTPUT); // data to 74HC595
pinMode(27, INPUT); // data from 74HC165
digitalWrite(25, LOW);
shiftOut(33, 26, MSBFIRST, 0xFF); // active-low relays: all OFF
digitalWrite(25, HIGH);
id(relay_state) = 0x00;
digitalWrite(13, LOW); // enable 74HC595 outputs
esp32:
board: esp32dev
framework:
type: arduino
logger:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
ap:
ssid: "Elica-Relay-Fallback"
password: !secret fallback_password
captive_portal:
# We use MQTT as the main connection to the controller.
# Home Assistant / native ESPHome API is deliberately not enabled.
mqtt:
broker: !secret mqtt_broker
username: !secret mqtt_username
password: !secret mqtt_password
discovery: false
birth_message:
topic: elica/status
payload: online
retain: true
will_message:
topic: elica/status
payload: offline
retain: true
ota:
- platform: esphome
password: !secret ota_password
# Handy while commissioning the board.
web_server:
port: 80
globals:
- id: relay_state
type: uint8_t
restore_value: no
initial_value: '0x00'
- id: input_state
type: uint8_t
restore_value: no
initial_value: '0xFF'
# Tracks the real Elica Power LED so OUT5 can be used as a hard
# interlock for the external fan. The LED line is active LOW.
- id: elica_power_initialized
type: bool
restore_value: no
initial_value: 'false'
- id: elica_power_was_on
type: bool
restore_value: no
initial_value: 'false'
script:
- id: write_relays
mode: restart
then:
- lambda: |-
// Relay drivers are active LOW.
digitalWrite(25, LOW);
shiftOut(33, 26, MSBFIRST, ~id(relay_state));
digitalWrite(25, HIGH);
# 74HC595 and 74HC165 share CLOCK/LATCH on this board.
# Read inputs, then immediately restore the relay register.
interval:
- interval: 100ms
then:
- lambda: |-
// Parallel-load current input state into 74HC165.
digitalWrite(25, LOW);
delayMicroseconds(10);
digitalWrite(25, HIGH);
uint8_t v = 0;
for (int i = 0; i < 8; i++) {
v <<= 1;
if (digitalRead(27)) v |= 1;
digitalWrite(26, HIGH);
delayMicroseconds(2);
digitalWrite(26, LOW);
delayMicroseconds(2);
}
id(input_state) = v;
// Elica Power LED is connected to IN5 (74HC165 mask 0x40).
// IMPORTANT: on this board the 74HC165 raw state is inverted
// relative to the MQTT binary_sensor state. Empirically:
// raw bit 1 -> Elica Power LED ON, raw bit 0 -> LED OFF.
const bool elica_power_on = (v & 0x40) != 0;
bool relay5_changed = false;
if (!id(elica_power_initialized)) {
id(elica_power_initialized) = true;
// If ESP32 boots while Elica is already running, enforce
// the interlock immediately. If Elica is OFF, boot defaults
// already leave Relay 5 OFF -> NC closed -> fan ON.
if (elica_power_on && !(id(relay_state) & 0x10)) {
id(relay_state) |= 0x10;
relay5_changed = true;
}
} else if (elica_power_on) {
// Hard interlock: while Elica is ON, Relay 5 must stay ON.
// Relay 5 ON opens the NC contact and stops the external fan.
if (!(id(relay_state) & 0x10)) {
id(relay_state) |= 0x10;
relay5_changed = true;
}
} else if (id(elica_power_was_on)) {
// Elica has just switched OFF: automatically restore the
// external fan. Relay 5 OFF closes NC and starts the fan.
if (id(relay_state) & 0x10) {
id(relay_state) &= ~0x10;
relay5_changed = true;
}
}
id(elica_power_was_on) = elica_power_on;
// Shared bus changed while reading 74HC165:
// restore the requested relay state in 74HC595.
digitalWrite(25, LOW);
shiftOut(33, 26, MSBFIRST, ~id(relay_state));
digitalWrite(25, HIGH);
// Keep MQTT/web-server state in sync when the interlock itself
// changes Relay 5 rather than an external command doing it.
if (relay5_changed) {
id(relay5).publish_state((id(relay_state) & 0x10) != 0);
}
switch:
- platform: template
name: "Relay 1"
id: relay1
restore_mode: ALWAYS_OFF
lambda: return (id(relay_state) & 0x01) != 0;
turn_on_action:
- lambda: id(relay_state) |= 0x01;
- script.execute: write_relays
turn_off_action:
- lambda: id(relay_state) &= ~0x01;
- script.execute: write_relays
command_topic: "elica/relay/1/set"
state_topic: "elica/relay/1/state"
- platform: template
name: "Relay 2"
id: relay2
restore_mode: ALWAYS_OFF
lambda: return (id(relay_state) & 0x02) != 0;
turn_on_action:
- lambda: id(relay_state) |= 0x02;
- script.execute: write_relays
turn_off_action:
- lambda: id(relay_state) &= ~0x02;
- script.execute: write_relays
command_topic: "elica/relay/2/set"
state_topic: "elica/relay/2/state"
- platform: template
name: "Relay 3"
id: relay3
restore_mode: ALWAYS_OFF
lambda: return (id(relay_state) & 0x04) != 0;
turn_on_action:
- lambda: id(relay_state) |= 0x04;
- script.execute: write_relays
turn_off_action:
- lambda: id(relay_state) &= ~0x04;
- script.execute: write_relays
command_topic: "elica/relay/3/set"
state_topic: "elica/relay/3/state"
- platform: template
name: "Relay 4"
id: relay4
restore_mode: ALWAYS_OFF
lambda: return (id(relay_state) & 0x08) != 0;
turn_on_action:
- lambda: id(relay_state) |= 0x08;
- script.execute: write_relays
turn_off_action:
- lambda: id(relay_state) &= ~0x08;
- script.execute: write_relays
command_topic: "elica/relay/4/set"
state_topic: "elica/relay/4/state"
# OUT5 управляет внешним вентилятором через NC-контакт.
# relay5=OFF -> NC замкнут -> внешний вентилятор ВКЛ.
# relay5=ON -> NC разомкнут -> внешний вентилятор ВЫКЛ.
#
# HARD INTERLOCK по фактическому Power LED Elica (IN5):
# Elica ON -> Relay 5 принудительно ON -> внешний вентилятор OFF.
# Elica OFF -> при переходе автоматически Relay 5 OFF -> вентилятор ON.
# Пока Elica включена, MQTT-команда Relay 5 OFF игнорируется, чтобы
# внешний вентилятор нельзя было случайно включить одновременно с Elica.
- platform: template
name: "Relay 5"
id: relay5
restore_mode: ALWAYS_OFF
lambda: return (id(relay_state) & 0x10) != 0;
turn_on_action:
- lambda: id(relay_state) |= 0x10;
- script.execute: write_relays
turn_off_action:
- lambda: |-
// Use the same raw IN5 polarity as the interlock above:
// raw bit 1 means the Elica Power LED is ON.
const bool elica_power_on = (id(input_state) & 0x40) != 0;
if (elica_power_on) {
id(relay_state) |= 0x10;
} else {
id(relay_state) &= ~0x10;
}
- script.execute: write_relays
command_topic: "elica/relay/5/set"
state_topic: "elica/relay/5/state"
- platform: template
name: "Relay 6"
id: relay6
restore_mode: ALWAYS_OFF
lambda: return (id(relay_state) & 0x20) != 0;
turn_on_action:
- lambda: id(relay_state) |= 0x20;
- script.execute: write_relays
turn_off_action:
- lambda: id(relay_state) &= ~0x20;
- script.execute: write_relays
command_topic: "elica/relay/6/set"
state_topic: "elica/relay/6/state"
- platform: template
name: "Relay 7"
id: relay7
restore_mode: ALWAYS_OFF
lambda: return (id(relay_state) & 0x40) != 0;
turn_on_action:
- lambda: id(relay_state) |= 0x40;
- script.execute: write_relays
turn_off_action:
- lambda: id(relay_state) &= ~0x40;
- script.execute: write_relays
command_topic: "elica/relay/7/set"
state_topic: "elica/relay/7/state"
- platform: template
name: "Relay 8"
id: relay8
restore_mode: ALWAYS_OFF
lambda: return (id(relay_state) & 0x80) != 0;
turn_on_action:
- lambda: id(relay_state) |= 0x80;
- script.execute: write_relays
turn_off_action:
- lambda: id(relay_state) &= ~0x80;
- script.execute: write_relays
command_topic: "elica/relay/8/set"
state_topic: "elica/relay/8/state"
binary_sensor:
- platform: template
name: "Input 1"
lambda: return !(id(input_state) & 0x10);
state_topic: "elica/input/1/state"
- platform: template
name: "Input 2"
lambda: return !(id(input_state) & 0x08);
state_topic: "elica/input/2/state"
- platform: template
name: "Input 3"
lambda: return !(id(input_state) & 0x20);
state_topic: "elica/input/3/state"
- platform: template
name: "Input 4"
lambda: return !(id(input_state) & 0x04);
state_topic: "elica/input/4/state"
- platform: template
name: "Input 5"
lambda: return !(id(input_state) & 0x40);
state_topic: "elica/input/5/state"
- platform: template
name: "Input 6"
lambda: return !(id(input_state) & 0x02);
state_topic: "elica/input/6/state"
- platform: template
name: "Input 7"
lambda: return !(id(input_state) & 0x80);
state_topic: "elica/input/7/state"
- platform: template
name: "Input 8"
lambda: return !(id(input_state) & 0x01);
state_topic: "elica/input/8/state"
- platform: status
name: "Controller Status"