Skip to content

Instantly share code, notes, and snippets.

@JavanXD
Last active April 22, 2025 10:47
Show Gist options
  • Save JavanXD/696d026ef202a7d6455ed4745df63e39 to your computer and use it in GitHub Desktop.
Save JavanXD/696d026ef202a7d6455ed4745df63e39 to your computer and use it in GitHub Desktop.
ESPHome/Homeassistant - Sniff CAN-Bus (MCP2515) from LEDA LUC2
substitutions:
name: esphome-ledaluc2
friendly_name: ESPHome LEDA LUC2
esphome:
name: ${name}
friendly_name: ${friendly_name}
min_version: 2023.6.0 # Use a stable ESPHome version for compatibility
name_add_mac_suffix: false # Prevent adding MAC suffix to the device name
project:
name: esphome.web
version: dev # Version of the project
esp32:
board: esp32dev # Specify the ESP32 development board
framework:
type: arduino # Use the Arduino framework
# Enable logging for debugging purposes
logger:
# Enable Home Assistant API with encryption key from secrets.yaml
api:
encryption:
key: !secret api_key
# Enable over-the-air updates for firmware
ota:
platform: esphome
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Fallback hotspot in case Wi-Fi connection fails
ap:
ssid: "ESPHome-LEDALUC2"
password: !secret ap_password
# Allow Wi-Fi provisioning via serial connection
improv_serial:
# Enable captive portal for Wi-Fi provisioning via the fallback hotspot
captive_portal:
# Import specific components from an example configuration without overwriting local settings
dashboard_import:
package_import_url: github://esphome/example-configs/esphome-web/esp32.yaml@main
import_full_config: false
# Host a simple web server (e.g., for Improv Wi-Fi)
web_server:
# Configure the SPI interface for the MCP2515 CAN bus module
spi:
clk_pin: GPIO22 # Clock pin
miso_pin: GPIO17 # Master In Slave Out pin
mosi_pin: GPIO21 # Master Out Slave In pin
# Configure the CAN bus using the MCP2515 module
canbus:
- platform: mcp2515
cs_pin: GPIO16
can_id: 0x28A
bit_rate: 125KBPS
on_frame:
- can_id: 0x28A
then:
- lambda: |-
// Log all received CAN frames for debugging
if (x.size() > 0 && x.size() < 8) {
// Log the received frame data safely
std::string frame_data;
for (size_t i = 0; i < x.size(); i++) {
char byte_str[5];
snprintf(byte_str, sizeof(byte_str), "0x%02X ", x[i]);
frame_data += byte_str;
}
// Log the frame data in a single line
ESP_LOGD("CAN", "Received CAN Frame (Size: %d bytes): %s", x.size(), frame_data.c_str());
// Check for specific sizes and log states
if (x.size() == 2) {
ESP_LOGI("CAN", "Interpreted State: Ventilation turned OFF via Display");
} else if (x.size() == 1) {
ESP_LOGI("CAN", "Interpreted State: Ventilation turned ON via Display");
} else {
ESP_LOGW("CAN", "Interpreted State: Unknown or Additional Data");
}
}
else if (x.size() == 8) {
uint8_t frame_type = x[0]; // First byte determines the frame type
// Frame type 0x00: Pressure difference and exhaust temperature
if (frame_type == 0x00) {
// Extract pressure difference from Byte 2
float pressure_difference = x[1] * 0.1f; // Convert to Pascals
// Check for adjustment flag in Byte 3
if (x[2] == 0x81) {
const float PRESSURE_ADJUSTMENT_VALUE = 25.5f;
pressure_difference += PRESSURE_ADJUSTMENT_VALUE;
ESP_LOGI("CAN", "Adjustment Applied: +%.1f Pa", PRESSURE_ADJUSTMENT_VALUE);
}
// Extract exhaust temperature from Byte 4
float exhaust_temperature = static_cast<float>(x[3]); // °C
// Log decoded values
ESP_LOGI("CAN", "Pressure: %.1f Pa, Temperature: %.1f °C", pressure_difference, exhaust_temperature);
// Publish to sensors
id(pressure_difference_sensor).publish_state(pressure_difference);
id(exhaust_temperature_sensor).publish_state(exhaust_temperature);
}
// Frame type 0x01: Ventilation status
else if (frame_type == 0x01) {
// Extract ventilation status from Byte 6
bool ventilation_active = (x[5] == 0x01);
// Log ventilation status
ESP_LOGI("CAN", "Ventilation Active: %s", ventilation_active ? "Yes" : "No");
ESP_LOGD("CAN", "Ventilation Bytes: Data=%02X %02X %02X %02X %02X %02X %02X %02X",
x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]);
// Publish to binary sensor
id(ventilation_status_sensor).publish_state(ventilation_active);
}
// Frame type 0x09: Heartbeat signal
else if (frame_type == 0x09) {
// Log loop and counter for reference
ESP_LOGD("CAN", "Heartbeat (0x09): Loop=%02X, Counter=%02X", x[2], x[1]);
}
// Frame type 0x55: Heartbeat signal
else if (frame_type == 0x55) {
// Log loop and counter for reference
ESP_LOGD("CAN", "Heartbeat (0x55): Loop=%02X, Counter=%02X", x[2], x[1]);
}
else if (frame_type == 0x80) {
ESP_LOGW("CAN", "Error Frame (0x80): Data=%02X %02X %02X %02X %02X %02X %02X %02X",
x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]);
}
else if (frame_type == 0x81) {
ESP_LOGW("CAN", "Error Frame (0x81): Data=%02X %02X %02X %02X %02X %02X %02X %02X",
x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]);
}
// Handle unknown frame types
else {
ESP_LOGW("CAN", "Unknown Frame Type. Data=%02X %02X %02X %02X %02X %02X %02X %02X",
x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]);
}
} else {
ESP_LOGW("CAN", "Unexpected frame size: %d bytes", x.size());
}
# Define template sensors to hold the values published from the CAN bus data
sensor:
- platform: template
name: "Pressure Difference"
id: pressure_difference_sensor
unit_of_measurement: "Pa"
accuracy_decimals: 1
device_class: pressure # Device class for pressure sensors
icon: "mdi:air-filter" # Optional custom icon for visual clarity
filters:
- throttle_average: 3s
- platform: template
name: "Exhaust Temperature"
id: exhaust_temperature_sensor
unit_of_measurement: "°C"
accuracy_decimals: 1
device_class: temperature # Device class for temperature sensors
icon: "mdi:thermometer"
filters:
- throttle_average: 3s
binary_sensor:
- platform: template
name: "Ventilation Status"
id: ventilation_status_sensor
device_class: running # Device class for indicating system activity
icon: "mdi:fan" # Optional custom icon for ventilation
@Hooorny
Copy link

Hooorny commented Jan 18, 2025

Hi, ich bin nach der Anleitung vorgegangen, bekomme aber keine Werte angezeigt. Anscheinend werden keine CAN-Frames empfangen, weil auch nichts geloggt wird.
Plus- und Minuspol waren bei mir genau andersrum, als auf dem Bild. Evtl sind die CAN Pins dann auch andere? Evtl kann mir ja jemand weiterhelfen ...
Vielen Dank im Voraus!

@JavanXD
Copy link
Author

JavanXD commented Feb 4, 2025

Hi, ich bin nach der Anleitung vorgegangen, bekomme aber keine Werte angezeigt. Anscheinend werden keine CAN-Frames empfangen, weil auch nichts geloggt wird. Plus- und Minuspol waren bei mir genau andersrum, als auf dem Bild. Evtl sind die CAN Pins dann auch andere? Evtl kann mir ja jemand weiterhelfen ... Vielen Dank im Voraus!

Hi @Hooorny - du hast Recht, im Bild waren die Kabel vertauscht. Die musst du korrekt anstecken. So sieht die Korrekte Verkabelung aus:
image

@Maigus2510
Copy link

Hey, irgendwie bin ich zu doof das Zeug zusammen zu löten. Könnte mir jemand, natürlich gegen Aufwandsentschädigung, so ein Ding zusammen bauen?

@FredAnn
Copy link

FredAnn commented Apr 22, 2025

Hallo, ich habe eine 30-Pin-ESP32 hier. Kann mir bitte jemand die PIN-Belegung dafür zum MCP2525 nennen? Muss ich im Code dann Änderungen vornehmen?
Danke!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment