/* ============================================================================ * Banc de diagnostic Bosch eBike Gen 2 (Classic Line / eBike System 2) * Sniffer CAN en ECOUTE SEULE (listen-only) pour ESP32 * ---------------------------------------------------------------------------- * But : observer le bus CAN du systeme Bosch (moteur / batterie / afficheur) * pour diagnostiquer une panne, SANS rien emettre sur le bus. * * Securite : le mode TWAI_MODE_LISTEN_ONLY ne genere aucun bit sur le bus * (meme pas l'acquittement ACK). On regarde, on ne touche pas. * Impossible de modifier ou "briquer" quoi que ce soit avec ca. * * Materiel : ESP32 (controleur CAN "TWAI" integre) + transceiver CAN 3.3 V * type SN65HVD230. Bus Bosch = 125 kbit/s. * * Cablage (voir README.md pour le detail) : * ESP32 GPIO5 (TX) -> transceiver broche D / TXD / CTX * ESP32 GPIO4 (RX) <- transceiver broche R / RXD / CRX * ESP32 3V3 -> transceiver VCC * ESP32 GND -> transceiver GND + GND du bus Bosch * Transceiver CANH -> CAN-H du bus Bosch * Transceiver CANL -> CAN-L du bus Bosch * NE PAS ajouter de resistance de terminaison (le bus est deja termine). * * Auteur : netVD, pour Rafael. Projet droit a la reparation, machine a soi. * ========================================================================== */ #include "driver/twai.h" // ---- Broches CAN (modifiables selon ton cablage) -------------------------- #define CAN_TX_PIN GPIO_NUM_5 #define CAN_RX_PIN GPIO_NUM_4 // ---- Table des identifiants CAN connus (wiki apollo.open-resource.org) ----- // source = qui emet la trame : DU = Drive Unit (moteur), BAT = batterie, // HMI = afficheur Intuvia. Les trames DU sont les plus importantes pour une // Error 500 : si elles sont ABSENTES, le controleur moteur ne parle plus. struct CanIdInfo { uint32_t id; const char* source; // "DU", "BAT", "HMI" const char* label; // description lisible bool moteur; // true = emise par le Drive Unit (presence = moteur vivant) }; const CanIdInfo KNOWN_IDS[] = { { 0x03b, "HMI", "Niveau d'assistance (0=off,1=eco,2=tour,3=sport,4=turbo)", false }, { 0x048, "DU", "Couple pedalier (~1/10 Nm)", true }, { 0x0d1, "DU", "Vitesse (~1/100 km/h)", true }, { 0x0d2, "DU", "Cadence de pedalage", true }, { 0x0d3, "DU", "Couple moteur reel/nominal + tr/min", true }, { 0x0d4, "DU", "Puissance moteur (~1/10 W)", true }, { 0x101, "BAT", "Statut batterie (0=run,0xffff=charge), courant, U", false }, { 0x111, "BAT", "Limite decharge, derniere charge pleine (Ah), SoC %", false }, { 0x131, "HMI", "Evenements boutons", false }, { 0x170, "BAT", "Temperature moteur", false }, { 0x202, "BAT", "Distance totale / autonomie", false }, { 0x203, "BAT", "Temps de fonctionnement", false }, }; const size_t KNOWN_COUNT = sizeof(KNOWN_IDS) / sizeof(KNOWN_IDS[0]); const CanIdInfo* lookupId(uint32_t id) { for (size_t i = 0; i < KNOWN_COUNT; i++) if (KNOWN_IDS[i].id == id) return &KNOWN_IDS[i]; return nullptr; } // ---- Statistiques de presence par identifiant ----------------------------- // On compte les trames vues pour chaque ID connu, pour repondre d'un coup // d'oeil : "le drive unit est-il present sur le bus ?" struct Seen { uint32_t id; uint32_t count; bool known; bool moteur; }; #define MAX_SEEN 64 Seen seenTable[MAX_SEEN]; size_t seenCount = 0; uint32_t lastStats = 0; void noteSeen(uint32_t id) { for (size_t i = 0; i < seenCount; i++) if (seenTable[i].id == id) { seenTable[i].count++; return; } if (seenCount < MAX_SEEN) { const CanIdInfo* info = lookupId(id); seenTable[seenCount++] = { id, 1, info != nullptr, info ? info->moteur : false }; } } void printStats() { Serial.println(); Serial.println(F("========== BILAN DU BUS (presence par identifiant) ==========")); bool duVu = false; uint32_t totalDu = 0; for (size_t i = 0; i < seenCount; i++) { const CanIdInfo* info = lookupId(seenTable[i].id); char line[120]; snprintf(line, sizeof(line), " 0x%03X x%-6lu %-4s %s", (unsigned)seenTable[i].id, (unsigned long)seenTable[i].count, info ? info->source : "?", info ? info->label : "(inconnu - a identifier)"); Serial.println(line); if (seenTable[i].moteur && seenTable[i].count > 0) { duVu = true; totalDu += seenTable[i].count; } } Serial.println(F("-------------------------------------------------------------")); if (duVu) { char v[96]; snprintf(v, sizeof(v), " >> DRIVE UNIT (moteur) PRESENT sur le bus : %lu trames.", (unsigned long)totalDu); Serial.println(v); Serial.println(F(" Le controleur moteur communique. La 500 vient probablement")); Serial.println(F(" d'un sous-systeme/capteur, pas d'un controleur totalement mort.")); } else { Serial.println(F(" >> AUCUNE trame du DRIVE UNIT (0x048/0x0d1/0x0d3/0x0d4...) vue.")); Serial.println(F(" Si la batterie, elle, emet (0x101) : le moteur est MUET sur le")); Serial.println(F(" bus -> controleur moteur HS/ne demarre pas, coherent avec 500.")); Serial.println(F(" Si RIEN n'apparait : verifier cablage CAN-H/CAN-L, 5V, GND.")); } Serial.println(F("=============================================================\n")); } // ---- Affichage d'une trame ------------------------------------------------- void printFrame(const twai_message_t& msg) { char buf[180]; int n = snprintf(buf, sizeof(buf), "[%8lu ms] 0x%03X [%d] ", (unsigned long)millis(), (unsigned)msg.identifier, msg.data_length_code); for (int i = 0; i < msg.data_length_code && n < (int)sizeof(buf) - 4; i++) n += snprintf(buf + n, sizeof(buf) - n, "%02X ", msg.data[i]); Serial.print(buf); const CanIdInfo* info = lookupId(msg.identifier); if (info) { Serial.print(" | "); Serial.print(info->source); Serial.print(" "); Serial.print(info->label); } else { Serial.print(" | (ID inconnu - a identifier par observation)"); } // Decodages d'essai (HYPOTHESE little-endian, A CONFIRMER par observation). // On les affiche entre <> pour bien marquer que ce ne sont pas des valeurs sures. if (msg.identifier == 0x0d1 && msg.data_length_code >= 2) { uint16_t raw = msg.data[0] | (msg.data[1] << 8); Serial.print(" "); } Serial.println(); } // ---- Initialisation -------------------------------------------------------- void setup() { Serial.begin(115200); delay(400); Serial.println(); Serial.println(F("=== Banc diagnostic Bosch eBike Gen 2 - sniffer CAN (ECOUTE SEULE) ===")); Serial.println(F("Bus attendu : CAN 125 kbit/s. Mode passif : aucune emission sur le bus.")); twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(CAN_TX_PIN, CAN_RX_PIN, TWAI_MODE_LISTEN_ONLY); twai_timing_config_t t_config = TWAI_TIMING_CONFIG_125KBITS(); twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); if (twai_driver_install(&g_config, &t_config, &f_config) != ESP_OK) { Serial.println(F("ERREUR : installation du pilote TWAI impossible.")); while (true) delay(1000); } if (twai_start() != ESP_OK) { Serial.println(F("ERREUR : demarrage TWAI impossible.")); while (true) delay(1000); } Serial.println(F("Pilote CAN demarre. En ecoute... (bilan du bus toutes les 3 s)\n")); lastStats = millis(); } // ---- Boucle ---------------------------------------------------------------- void loop() { twai_message_t msg; // Attente d'une trame (timeout court pour pouvoir afficher le bilan). if (twai_receive(&msg, pdMS_TO_TICKS(200)) == ESP_OK) { if (!(msg.rtr)) { // on ignore les trames de requete distante noteSeen(msg.identifier); printFrame(msg); } } if (millis() - lastStats > 3000) { printStats(); lastStats = millis(); } }