logo elektroda
logo elektroda
X
logo elektroda
REKLAMA
REKLAMA
Adblock/uBlockOrigin/AdGuard mogą powodować znikanie niektórych postów z powodu nowej reguły.

VW MQB - Gadanie z OBD2 przez arduino albo cokolwiek

ddokupil 24 Maj 2023 23:51 345 0
REKLAMA
  • #1 20593514
    ddokupil
    Poziom 15  
    Rozpoznaję temat w sumie dopiero, może ktoś przeczyta, kto ma więcej doświadczenia. Potrzebuję odczytać z samochodu na żywo aktualną prędkość i stan ACC (tempomat) -czy jest włączony, standby, czy wyłączony. Mogę to zrobić za pomocą OBD11 lub vcds (teoretycznie - bo tego nie posiadam) ale potrzebuje czegoś, co:
    1. Da się dalej oprogramować żeby wykorzystać odczytane wartości do czegoś - może być arduino, rpi, esp32, cokolwiek
    2. Najlepiej żeby nie blokowało złącza OBD i żeby to pozostało funkcjonalne, że w każdej chwili mogę podłączyć interfejs i będzie działał równolegle z moim hackiem.

    Szukam projektów, schematów, bibliotek, czegokolwiek co by było pomocne. Z góry dziękuję.

    Teoretycznie poniższy kod na arduino + mcp2515 umożliwi mi odczyt prędkości ale tempomatu to chyba nie ma wśród standardowych PID.

    
    #include <SPI.h>
    #include <mcp2515.h>
    
    // MCP2515 CAN bus module pins
    const int CAN_CS_PIN = 10;   // Chip Select pin
    const int CAN_INT_PIN = 2;   // Interrupt pin
    
    MCP2515 mcp2515(CAN_CS_PIN);
    
    // OBD2 PID for vehicle speed
    const int PID_VEHICLE_SPEED = 0x0D;
    
    void setup() {
      Serial.begin(9600);
      
      // Initialize the MCP2515 module
      if (!mcp2515.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ)) {
        Serial.println("Error initializing MCP2515!");
        while (1);
      }
      
      // Set the CAN bus speed to 500 kbps
      mcp2515.setBitrate(CAN_500KBPS, MCP_16MHZ);
      
      // Set the receive mask and filter to accept all messages
      mcp2515.setNormalMode();
      
      // Attach the interrupt handler for receiving CAN messages
      attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), handleCanInterrupt, FALLING);
    }
    
    void loop() {
      // Request vehicle speed from the OBD2 interface
      requestOBD2Value(PID_VEHICLE_SPEED);
      
      // Wait for 1 second to allow the response to be received
      delay(1000);
    }
    
    void handleCanInterrupt() {
      // Check if a CAN message has been received
      if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
        // Check if the received message is a response to our request
        if (canMsg.data[2] == PID_VEHICLE_SPEED) {
          // Calculate the vehicle speed from the received data
          int vehicleSpeed = canMsg.data[3];
          
          Serial.print("Vehicle Speed: ");
          Serial.print(vehicleSpeed);
          Serial.println(" km/h");
        }
      }
    }
    
    void requestOBD2Value(int pid) {
      // Construct the OBD2 request message
      // Use mode 0x01 (show current data) and PID (Parameter ID)
      CANMessage obd2Request;
      obd2Request.id = 0x7DF;  // OBD2 request ID
      obd2Request.len = 8;     // Data length
      obd2Request.data[0] = 0x02;  // Mode
      obd2Request.data[1] = 0x01;  // PID count
      obd2Request.data[2] = pid;   // PID
      
      // Send the OBD2 request message
      mcp2515.sendMessage(&obd2Request);
    }
    
  • REKLAMA
REKLAMA