Longan-Labs/Arduino_CAN_BUS_MCP2515

Identifying Car Data MCP2515

Davin-TJ opened this issue · 0 comments

Hello there

I am working on this project to read car data and sending it to cloud ,and found this library to read car data via OBD. I was wondering about the car data that I recieved my car is a Proton Exora 2014 and the data that I recieved is weird so to say here is the data I recieved https://github.com/coryjfowler/MCP_CAN_lib. I'm using MCP2515 and ESP32 TTGO CALL SIM800 to read and send the data.

And for starters I just wanted to detect Car RPM and I assume that RXid is 0x310 (knowing this is the only value that changes the moment the engine started) which is different from the normal 7DF / 7E8 request

I'm following the example to read RPM from @coryjfowler here is the code

/* OBD2 TACHO
   By moty22.co.uk
   Lib written By: Cory J. Fowler  December 20th, 2016
   https://github.com/coryjfowler/MCP_CAN_lib
*/

#include <mcp_can.h>
#include <SPI.h>
#define standard 1  //standard is 11 bits
#define txID 0x310  //0x7E9

unsigned long rxId;
byte len;
byte rxBuf[8];

unsigned char txData[8] = {0x02, 0x01, 0x0C, 0x55, 0x55, 0x55, 0x55, 0x55}; //PID to request 010C

MCP_CAN CAN0(13);                                // Set CAN0 CS to pin 10

void setup()
{
  Serial.begin(115200);
  while (!Serial);

  // Initialize MCP2515 running at baudrate of 500kb/s . If your can board has 16MHz crystal change the setting.
  if (CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
    Serial.println("Initialized Successfully!");
  else
    Serial.println("**Error Initializing**");

  CAN0.setMode(MCP_NORMAL);                          // Set operation mode to normal so the MCP2515 sends acks to received data.

}

void loop()
{
  byte back = 0;
  unsigned int rpm = 100;

  delay(500);
  CAN0.sendMsgBuf(txID, 8, txData); //request data from ECU

  for (byte i = 0; i < 20; ++i)
  { //get 20 times unless reply arrived from the ECU
    CAN0.readMsgBuf(&rxId, &len, rxBuf);     // Get CAN data - 03 41 0C 3D 0F
    if (rxId == 0x310) {
      Serial.print(rxId, HEX);  //0x7E8
      Serial.print("\t");
      for (byte i = 0; i < len; i++) // print the data
      {
        Serial.print(rxBuf[i], HEX);
        Serial.print("\t");
      }
      Serial.println();
      back = 1;

    }
    if (back) {
      rpm = (rxBuf[3] * 256 + rxBuf[4]) / 4; //calculate the rpm
      Serial.println(rpm);
      break;
    }
  }
}

what I've tried :

  1. using the standard recieve and transfer ID but it is not detecting anything,
  2. changing txid to 0x310 or 0x00 (I use 0x00 from the data that is detected from Can_recieve) and keeping rxid 0x310, I can recieve some data from this but the value is all over the place between 100 to 7000
  3. using txid to 0x310 and rxid 0x310 but changing the txdata from the default {0x02, 0x01, 0x0C, 0x55, 0x55, 0x55, 0x55, 0x55} to {0x02, 0x01, 0x0C, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA} which has the same result

I'm not sure if I define it correctly (which i don't think so) and would like to have a ittle bit of insight on this

this is the Can_recieve code that I use

// CAN Receive Example
//

#include <mcp_can.h>
#include <SPI.h>

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128];                        // Array to store serial string

#define CAN0_INT 18                              // Set INT to pin 2
MCP_CAN CAN0(13);                               // Set CS to pin 10


void setup()
{
  Serial.begin(115200);
  
  // Initialize MCP2515 running at 8MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
    Serial.println("MCP2515 Initialized Successfully!");
  else
    Serial.println("Error Initializing MCP2515...");
  
  CAN0.setMode(MCP_NORMAL);                     // Set operation mode to normal so the MCP2515 sends acks to received data.

  pinMode(CAN0_INT, INPUT);                            // Configuring pin for /INT input
  
  Serial.println("MCP2515 Recieve");
}

void loop()
{
  if(!digitalRead(CAN0_INT))                         // If CAN0_INT pin is low, read receive buffer
  {
    CAN0.readMsgBuf(&rxId, &len, rxBuf);      // Read data: len = data length, buf = data byte(s)
    
    if((rxId & 0x80000000) == 0x80000000)     // Determine if ID is standard (11 bits) or extended (29 bits)
      sprintf(msgString, "Extended ID: 0x%.8lX  DLC: %1d  Data:", (rxId & 0x1FFFFFFF), len);
    else
      sprintf(msgString, "Standard ID: 0x%.3lX       DLC: %1d  Data:", rxId, len);
  
    Serial.print(msgString);
  
    if((rxId & 0x40000000) == 0x40000000){    // Determine if message is a remote request frame.
      sprintf(msgString, " REMOTE REQUEST FRAME");
      Serial.print(msgString);
    } else {
      for(byte i = 0; i<len; i++){
        sprintf(msgString, " 0x%.2X", rxBuf[i]);
        Serial.print(msgString);
      }
    }
        
    Serial.println();
  }
}

/*********************************************************************************************************
  END FILE
*********************************************************************************************************/