279 lines
7.2 KiB
YAML
279 lines
7.2 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'
|
|
|
|
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;
|
|
|
|
// 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);
|
|
|
|
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"
|
|
|
|
- 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: 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"
|