Sparkfun can-bus shield code partly works

Hallo!

I’m trying to sniff all data out of the OBD port of a VW Polo 2012 using sparkfun can-bus shield and an arduino uno.

Liblary used: name=SparkFun CAN-Bus version=5.1.0

I found the following code that does not work:

/****************************************************************************
CAN Read Demo for the SparkFun CAN Bus Shield. 

Written by Stephen McCoy. 
Original tutorial available here: http://www.instructables.com/id/CAN-Bus-Sniffing-and-Broadcasting-with-Arduino
Used with permission 2016. License CC By SA. 

Distributed as-is; no warranty is given.
*************************************************************************/

#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

//********************************Setup Loop*********************************//

void setup() {
  Serial.begin(9600); // For debug use
  Serial.println("CAN Read - Testing receival of CAN Bus message");  
  delay(1000);
  
  if(Canbus.init(CANSPEED_500))  //Initialise MCP2515 CAN controller at the specified speed
    Serial.println("CAN Init ok");
  else
    Serial.println("Can't init CAN");
    
  delay(1000);
}

//********************************Main Loop*********************************//

void loop(){

  tCAN message;
if (mcp2515_check_message()) 
	{
    if (mcp2515_get_message(&message)) 
	{
               Serial.print("ID: ");
               Serial.print(message.id,HEX);
               Serial.print(", ");
               Serial.print("Data: ");
               Serial.print(message.header.length,DEC);
               for(int i=0;i<message.header.length;i++) 
                {	
                  Serial.print(message.data[i],HEX);
                  Serial.print(" ");
                }
               Serial.println("");
           }
     }

}

I’m getting the “CAN Init ok” message on the serial monitor and after that, nothing.

When I’m replacing the loop() with the following code (adding the buffer declaration at the begining: char buffer[456]; as well), I’m getting engine’s rpm corrently:

 Canbus.ecu_req(ENGINE_RPM, buffer);
Serial.print("Engine RPM: ");
Serial.println(buffer);

Can anyone tell what am I doing wrong?

Thanks in advance!

gp

After a bit of a research I found that the OBD-2 Port of the VW Polo 6R is connected to a CAN Gateway wich needs a request ID in order to reply with data.

I'll hook up my interface onto a place before this Gateway (I'll start by the place behind the Radio), and I will come back to that post..

gp

Brilliant thanks. I had been looking for days for the problem. Just a quick note, I didn't replace the Loop() but entered your code below it. Also I needed to use the ODBII to serial cable plugged into the CANBUS shield rather than the screw terminals. I'm only receiving RPM signals so will need to look further but at least I'm getting something.