Created
September 19, 2023 15:17
-
-
Save mukowman/2305b092b1c0290c859627f19e64738c to your computer and use it in GitHub Desktop.
WiCAN Esphome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
substitutions: | |
device_name: wican | |
esp32: | |
variant: ESP32C3 | |
board: esp32-c3-devkitm-1 | |
esphome: | |
name: ${device_name} | |
logger: | |
level: INFO #NONE# ERROR #INFO #DEBUG #VERBOSE | |
#baud_rate: 0 #to disable logging via UART | |
logs: | |
text_sensor : ERROR | |
homeassistant.sensor: ERROR | |
canbus: INFO | |
#Disabled DEBUG as crashing ESP | |
api: | |
encryption: | |
key: "7SkGsleOStJiBft1zRunhr3Sg0b46t3lLsfZtDlyvDE=" | |
reboot_timeout: 0s | |
time: | |
platform: homeassistant | |
web_server: | |
version: 2 | |
ota: false | |
include_internal: true | |
wifi: | |
ssid: !secret wifi_ssid | |
password: !secret wifi_password | |
ap: | |
ssid: "Wican Fallback Hotspot" | |
password: "" | |
power_save_mode: none | |
captive_portal: | |
ota: | |
password: "" | |
# Sleep duration | |
number: | |
id: sleep_min | |
platform: template | |
name: '${device_name} sleep duration' | |
max_value: 300 | |
min_value: 1 | |
step: 1 | |
initial_value: 5 | |
restore_value: True | |
optimistic: True | |
icon: "mdi:volume-high" | |
output: | |
- id: blue_led_output | |
platform: gpio | |
pin: 7 | |
- id: green_led_output | |
platform: gpio | |
pin: {number: 8, inverted: true} | |
- id: yellow_led_output | |
platform: gpio | |
pin: {number: 9, inverted: true} | |
light: | |
- platform: binary | |
id: blue_led | |
output: blue_led_output | |
internal: True | |
effects: | |
- strobe: | |
name: "sleeping" | |
colors: | |
- state: True | |
duration: 500ms | |
- state: False | |
duration: 500ms | |
- strobe: | |
name: "sleep" | |
colors: | |
- state: True | |
duration: 2000ms | |
- state: False | |
duration: 2000ms | |
restore_mode: ALWAYS_ON | |
- platform: binary | |
id: green_led | |
output: green_led_output | |
- platform: binary | |
id: yellow_led | |
output: yellow_led_output | |
button: | |
- platform: restart | |
name: '${device_name} Restart' | |
canbus: | |
- platform: esp32_can | |
tx_pin: 0 | |
rx_pin: 3 | |
bit_rate: 500kbps | |
can_id: 0 # mandatory but we do not use it | |
on_frame: | |
# dump all frames to log | |
- can_id: 0x00000025 | |
#can_id_mask: 0 | |
then: | |
- lambda: |- | |
auto data_pretty = remote_transmission_request ? "n/a" : format_hex_pretty(x).c_str(); | |
ESP_LOGI("eup_dump", "can_id: 0x%08x, rtr: %d, length: %d, content: %s", can_id, remote_transmission_request, x.size(), data_pretty); | |
ESP_LOGI("eup_dump", "angle: %f", (float)((int16_t)(x[1]<< 8) + x[0])*1.5); | |
- can_id: 0x00000025 | |
then: | |
- lambda: |- | |
if (x.size() >= 5) | |
id(steer_angle).publish_state((float)((int16_t)(x[1]<< 8) + x[0])*1.5); | |
#return (float)((int16_t)(x[2]<< 8) + x[1])*1.5; | |
switch: | |
- platform: gpio | |
id: can_enabled | |
name: '${device_name} Enable CAN' | |
pin: | |
number: 6 | |
inverted: true | |
restore_mode: ALWAYS_ON | |
# TBD: disabling CAN seems to disturb the driver badly and it will not work again after enabling again but only after reboot | |
# on_turn_on: | |
# - lambda: twai_start(); | |
# on_turn_off: | |
# - lambda: twai_stop(); # keep driver error counter low | |
- platform: template | |
id: sleep_mode | |
name: '${device_name} Sleep Mode' | |
optimistic: true | |
restore_mode: RESTORE_DEFAULT_ON | |
on_turn_on: | |
- logger.log: "Sleep mode: ON" | |
- script.stop: sleep_script | |
- script.execute: sleep_script | |
on_turn_off: | |
- logger.log: "Sleep mode: OFF" | |
- script.stop: sleep_script | |
- script.execute: sleep_script | |
script: | |
- id: sleep_script | |
mode: restart # important, otherwise delays will not cancel each other | |
then: | |
- if: | |
condition: | |
and: | |
- binary_sensor.is_off: car_is_on | |
- switch.is_on: sleep_mode | |
then: | |
- logger.log: | |
format: "Going to sleep in 30 seconds" | |
level: INFO | |
- light.turn_on: | |
id: blue_led | |
effect: sleeping | |
- logger.log: | |
format: "Sleeping for %.1f minutes" | |
args: [ 'id(sleep_min).state'] | |
level: INFO | |
- delay: 30sec # allow to send the latest state to HA before disconnecting wifi | |
# - switch.turn_off: can_enabled | |
- wifi.disable: | |
- light.turn_on: | |
id: blue_led | |
effect: sleep | |
- delay: !lambda "return id(sleep_min).state * 1000 * 60;" | |
- wifi.enable: | |
- delay: 5sec | |
- script.execute: sleep_script | |
else: | |
- light.turn_on: | |
id: blue_led | |
effect: none | |
- interval: 1s | |
then: | |
if: | |
condition: | |
wifi.connected: | |
then: | |
- light.turn_on: green_led | |
else: | |
- light.turn_off: green_led | |
sensor: | |
- platform: template | |
id: steer_angle | |
name: '${device_name} Steering Angle' | |
unit_of_measurement: '°' | |
accuracy_decimals: 0 | |
- platform: adc | |
id: starter_battery_voltage | |
name: '${device_name} Battery Voltage' | |
pin: 4 | |
attenuation: 11db # https://github.com/meatpiHQ/wican-fw/blob/bf212132f8e506f2c520e917daf86e53a1070302/main/sleep_mode.c#L234 | |
filters: | |
- lambda: return x * 116 / 16; # https://github.com/meatpiHQ/wican-fw/blob/bf212132f8e506f2c520e917daf86e53a1070302/main/sleep_mode.c#L397 | |
update_interval: 5s | |
unit_of_measurement: V | |
accuracy_decimals: 1 | |
device_class: voltage | |
state_class: measurement | |
binary_sensor: | |
- platform: template | |
id: car_is_on | |
name: '${device_name} Car Running' | |
lambda: return id(starter_battery_voltage).state >= 12.9; | |
# did not find any other way to realize different timeouts for on and off :-( | |
# neither delay_on and delay_off nor on_state with delays based on state did work reliably | |
on_state: | |
- script.execute: sleep_script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment