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. 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
// Create an instance of the object
CAN myCAN = CAN();
// Setting up our devices and I/Os
void setup() {
// Using the Socket 0 onto which Can bus module is plugged in
Utils.setONSocket0();
Utils.setMUXSocket0();
// Initializes the UART
Serial.begin(115200);
delay(100);
// Let's start talking to the bus. Mention bus speed as follows:
// 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>
// Cooking API libraries
#include <arduinoUtils.h>
// Include always these libraries before using the CAN Bus functions
#include <arduinoCAN.h>
#include <SPI.h>
// Create an instance of the object
CAN myCAN = CAN();
void setup() {
// Turn on the Serial
Serial.begin(115200);
delay(100);
// Print init message
Serial.println("Initializing CANBUS...");
// Configuring the BUS at 500 Kbit/s
// Only allowed in SOCKET 0
myCAN.begin(500);
Serial.println("CANBUS initialized at 500 KBits/s");
Serial.println();
}
void loop() {
// Read the value of the Vehicle Speed
int vehicleSpeed = myCAN.getVehicleSpeed();
// Read the value of RPM of the engine
int engineRPM = myCAN.getEngineRPM();
// Get the fuel level
int fuelLevel = myCAN.getFuelLevel();
// Get the throttle position
int throttlePosition = myCAN.getThrottlePosition();
Serial.print(F("\tVehicle Speed => "));
Serial.print(vehicleSpeed);
Serial.println(" Km / h");
Serial.print(F("\tEngine RPM => "));
Serial.print(engineRPM);
Serial.println(" RPM");
Serial.print(F("\tFuel Level => "));
Serial.print(fuelLevel);
Serial.println(" %");
Serial.print(F("\tThrottle Position => "));
Serial.print(throttlePosition);
Serial.println(" % ");
Serial.println(F("<============================================>"));
Serial.println();
delay(1000);
}