Hello,
I'm pretty much new to this Arduino programming language. I have made earlier few Arduino projects for more simple things. Now I would like to create a Arduino LCD display, which prints engine parameters to the screen from which it reads from the CAN. I have read a lot about CAN BUS system, have checked the various libraries examples etc. I think I will use Sparkfun CAN BUS library as this code seems pretty much understandable.
So my plan would be. Send ECU request over BMW PT-CAN for various engine parameters, like turbo boost, coolant temp, rpm, Nm etc. Then get a response from the ECU with the CAN data. Then need to extract the CAN data to the understandable format which can be read easily and then print the data to the LCD.
I do not know at the moment what the ID for would be. Need to sniff the CAN. I have heard that the usual diagnosis request is with the CAN ID 0x7E0 but I'm not sure.
My project hardware is:
Arduino UNO
Elecfreaks CAN BUS shield
20x4 LCD display I2C module
At the moment I would use this sketch, which I found from the instructables homepage.
First I would like to find out the CAN ID which usually is sent over the CAN BUS with the diagnostic tester. There I could see the CAN id, witht the relevant data.
My first question at the moment would be, does this sketch written below will work as I think. That would be. Send message with ID and DATA, then get a response with the ECU with ID and Data?
As I understand this request is first sent and then received? And then looping this program over and over?
/*
This is for use with Sparkfun's CAN Bus Shield: https://www.sparkfun.com/products/10039
*/
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
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);
}
void loop(){
tCAN message;
if (mcp2515_check_message())
{
if (mcp2515_get_message(&message))
{
//if(message.id == 0x620 and message.data[2] == 0xFF) //uncomment when you want to filter
if (message.id == 0x620)
//{
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("");
//}
}}
{
tCAN message1;
message1.id = 0x631; //formatted in HEX
message1.header.rtr = 0;
message1.header.length = 8; //formatted in DEC
message1.data[0] = 0x40;
message1.data[1] = 0x05;
message1.data[2] = 0x30;
message1.data[3] = 0xFF; //formatted in HEX
message1.data[4] = 0x00;
message1.data[5] = 0x40;
message1.data[6] = 0x00;
message1.data[7] = 0x00;
mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
mcp2515_send_message(&message1);
delay(1000);
}
}
Any help is appreciated.
Best regards,
Silver