Skip to content

Instantly share code, notes, and snippets.

@WarriorRocker
Created December 23, 2024 19:03
Show Gist options
  • Select an option

  • Save WarriorRocker/078e2985f393745f49556142d069c22c to your computer and use it in GitHub Desktop.

Select an option

Save WarriorRocker/078e2985f393745f49556142d069c22c to your computer and use it in GitHub Desktop.
esphome:
name: lego-box
esp8266:
board: esp01_1m
# Enable logging
logger:
baud_rate: 0
# Enable Home Assistant API
api:
encryption:
key: !secret lego-box_enc_key
services:
- service: dfplayer_play
variables:
file: int
then:
- dfplayer.play: !lambda 'return file;'
- service: dfplayer_play_loop
variables:
file: int
loop_: bool
then:
- dfplayer.play:
file: !lambda 'return file;'
loop: !lambda 'return loop_;'
- service: dfplayer_set_volume
variables:
volume: int
then:
- dfplayer.set_volume: !lambda 'return volume;'
- service: dfplayer_reset
then:
- dfplayer.reset
- service: dfplayer_stop
then:
- dfplayer.stop
ota:
password: !secret ota_password
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Lego-Box Fallback Hotspot"
password: !secret cap_password
# Enable Captive Portal for fallback hotspot
captive_portal:
# Enable web server interface
web_server:
substitutions:
devicename: "Lego-Box"
# Set up mp3 module pins
uart:
tx_pin: GPIO5
rx_pin: GPIO3
baud_rate: 9600
# Enable DFPlayer Mini
dfplayer:
on_finished_playback:
then:
- logger.log: "Playback finished"
binary_sensor:
- platform: status
id: esp_status
name: "${devicename} Status"
sensor:
- platform: uptime
name: "${devicename} Uptime Sec"
id: uptime_sec
internal: true
- platform: wifi_signal
id: esp_signal
name: "${devicename} WiFi Signal"
update_interval: 5s
internal: true
accuracy_decimals: 0
- platform: template
id: "signal_level"
name: "${devicename} WiFi Signal Level"
internal: true
update_interval: 5s
unit_of_measurement: "%"
accuracy_decimals: 0
lambda: return min(max(2 * ((int)id(esp_signal).state + 100), 0), 100);
# Set up motor driver pins
output:
- platform: esp8266_pwm
id: port_1_pin_1
pin: GPIO14
frequency: 20000 Hz
zero_means_zero: true
- platform: esp8266_pwm
id: port_1_pin_2
pin: GPIO12
frequency: 20000 Hz
zero_means_zero: true
- platform: esp8266_pwm
id: port_2_pin_1
pin: GPIO13
frequency: 20000 Hz
zero_means_zero: true
- platform: esp8266_pwm
id: port_2_pin_2
pin: GPIO4
frequency: 20000 Hz
zero_means_zero: true
# Set up switch inputs
switch:
- platform: template
id: port_1
name: "Port 1"
icon: "mdi:power"
optimistic: true
on_turn_on:
then:
- script.execute: port_1_update
on_turn_off:
then:
- script.execute: port_1_update
- platform: template
id: port_1_reverse
name: "Port 1 Reverse"
icon: "mdi:sync"
optimistic: true
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on:
then:
- script.execute: port_1_update
on_turn_off:
then:
- script.execute: port_1_update
- platform: template
id: port_2
name: "Port 2"
icon: "mdi:power"
optimistic: true
on_turn_on:
then:
- script.execute: port_2_update
on_turn_off:
then:
- script.execute: port_2_update
- platform: template
id: port_2_reverse
name: "Port 2 Reverse"
icon: "mdi:sync"
optimistic: true
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on:
then:
- script.execute: port_2_update
on_turn_off:
then:
- script.execute: port_2_update
# Set up number inputs
number:
- platform: template
id: port_1_speed
name: "Port 1 Speed"
icon: "mdi:speedometer"
step: 1
min_value: 1
max_value: 100
initial_value: 100
optimistic: true
restore_value: true
mode: slider
on_value:
then:
- script.execute: port_1_update
- platform: template
id: port_2_speed
icon: "mdi:speedometer"
name: "Port 2 Speed"
step: 1
min_value: 1
max_value: 100
initial_value: 100
optimistic: true
restore_value: true
mode: slider
on_value:
then:
- script.execute: port_2_update
# Set up motor controller scripts
script:
- id: port_1_update
then:
- if:
condition:
- switch.is_on: port_1
then:
- if:
condition:
- switch.is_on: port_1_reverse
then:
- output.turn_off: port_1_pin_1
- output.turn_on: port_1_pin_2
- lambda: id(port_1_pin_2).set_level(id(port_1_speed).state / 100.0);
else:
- output.turn_on: port_1_pin_1
- output.turn_off: port_1_pin_2
- lambda: id(port_1_pin_1).set_level(id(port_1_speed).state / 100.0);
else:
- output.turn_off: port_1_pin_1
- output.turn_off: port_1_pin_2
- id: port_2_update
then:
- if:
condition:
- switch.is_on: port_2
then:
- if:
condition:
- switch.is_on: port_2_reverse
then:
- output.turn_off: port_2_pin_1
- output.turn_on: port_2_pin_2
- lambda: id(port_2_pin_2).set_level(id(port_2_speed).state / 100.0);
else:
- output.turn_on: port_2_pin_1
- output.turn_off: port_2_pin_2
- lambda: id(port_2_pin_1).set_level(id(port_2_speed).state / 100.0);
else:
- output.turn_off: port_2_pin_1
- output.turn_off: port_2_pin_2
# Set up motor controller using Fan component
# fan:
# - platform: hbridge
# id: port_1
# name: "Port 1"
# pin_a: port_1_pin_1
# pin_b: port_1_pin_2
# decay_mode: slow
# speed_count: 100
# - platform: hbridge
# id: port_2
# name: "Port 2"
# pin_a: port_2_pin_1
# pin_b: port_2_pin_2
# decay_mode: slow
# speed_count: 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment