I am using an Arduino UNO along with a Cooking Hacks CAN Bus Module and Multiprotocol Radio Shield. The CAN module is connected to the radio shield which is plugged into the Arduino. I got this radio shield keeping future applications in mind where I can combine multiple protocols, for eg. CAN and Bluetooth. I connected them through an OBD2-DB9 cable to the car's OBD port.
I managed to get the CAN messages on the serial monitor and started playing around with the headlamp switch which I know is connected to the CAN BUS. However, upon analysing the received CAN messages, I noticed that the headlamp messages weren't received by the Arduino. In fact, I didn't see any relevant CAN message on the serial monitor. I know the CAN ID for the headlamp messages having used a Kvaser CAN Sniffer earlier.
Also, in order to test the setup, I uploaded a sample sketch with standard OBD functions like Vehicle Speed, Throttle Position, Fuel level etc and the values received were highly incorrect. With a stationary vehicle with the ignition ON but the engine OFF, I received values of 256 Km/hr for vehicle speed, 3178 rpm for engine rpm , 257% for Fuel Level etc. I don't know why this is happening. Can anybody please help me?
CAN Receiver Code:
#include <arduinoClasses.h>
#include <arduinoMultiprotocol.h>
#include <arduinoUART.h>
#include <arduinoUtils.h>
#include <arduinoCAN.h>
#include <Wire.h>
#include <SPI.h>
// ID numbers
#define IDWAITED 200
#define OWNID 100
CAN myCAN = CAN();
void setup() {
Serial.begin(115200);
delay(100);
// CAN BUS speed:
// 1: 1Mbps
// 500: 500Kbps
// 250: 250Kbp
// 125: 125Kbps
myCAN.begin(500);
}
void loop() {
//Receive data
if (myCAN.messageAvailable()==1) {
// Read the last message received.
myCAN.getMessage(&myCAN.messageRx);
// Print in the serial monitor the received message
myCAN.printMessage(&myCAN.messageRx);
}
}
OBD CODE:
#include <Wire.h>
#include <arduinoUtils.h>
#include <arduinoCAN.h>
#include <SPI.h>
CAN myCAN = CAN();
void setup() {
Serial.begin(115200);
delay(100);
// Configuring the BUS at 500 Kbit/s
myCAN.begin(500);
Serial.println("CANBUS initialized at 500 KBits/s");
Serial.println();
}
void loop() {
int vehicleSpeed = myCAN.getVehicleSpeed();
int engineRPM = myCAN.getEngineRPM();
int fuelLevel = myCAN.getFuelLevel();
int throttlePosition = myCAN.getThrottlePosition();
Serial.print(F("\tVehicle Speed (km/hr) => "));
Serial.print(vehicleSpeed);
Serial.print(F("\tEngine RPM => "));
Serial.print(engineRPM);
Serial.print(F("\tFuel Level (%) => "));
Serial.print(fuelLevel);
Serial.print(F("\tThrottle Position (%) => "));
Serial.print(throttlePosition);
delay(1000);
}