I’m building my own car simulator using the next components:
- Arduino UNO
- mcp2515 breakout
- A obd female cable extension
I’m using the next code:
#include <SPI.h>
#include <mcp_can.h>
const int SPI_CS_PIN = 10;
INT32U canId = 0x000;
MCP_CAN CAN(SPI_CS_PIN);
unsigned char len = 0;
unsigned char buf[8];
int PID = 0;
int MODE = 0;
void setup() {
Serial.begin(9600);
if(CAN_OK == CAN.begin(CAN_500KBPS)) {
Serial.println("Init done");
} else {
Serial.println("Error on init");
}
}
void loop() {
//CAN.readMsgBuf(&len, buf);
if(CAN_MSGAVAIL == CAN.checkReceive()) {
CAN.readMsgBuf(&len, buf);
canId = CAN.getCanId();
PID = buf[2];
MODE = buf[1];
Serial.print("PID ");
Serial.print(PID, HEX);
Serial.print(" MODE ");
Serial.println(MODE);
if(MODE == 1) {
unsigned char OBDIImsg[8] = {4, 65, 0x0C, 0, 0, 0, 0, 0};
switch (PID){
case 0: // PID 00 hex
OBDIImsg[0] = 6;
OBDIImsg[3] = 0x88;
OBDIImsg[4] = 0x18;
OBDIImsg[5] = 0x80;
OBDIImsg[6] = 0x13;
CAN.sendMsgBuf(0x7E8, 0, 8, OBDIImsg); // Respuesta
break;
case 12:
OBDIImsg[3] = 100 * 4 / 256;
OBDIImsg[4] = 100 - (100 * 4 / 256);
CAN.sendMsgBuf(0x7E8, 0, 8, OBDIImsg);
break;
case 13:
OBDIImsg[3] = 75;
CAN.sendMsgBuf(0x7E8, 0, 8, OBDIImsg);
break;
case 4:
OBDIImsg[3] = 98;
CAN.sendMsgBuf(0x7E8, 0, 8, OBDIImsg);
break;
case 31: // PID 19 hex
OBDIImsg[3] = (millis()/1000)/256;
OBDIImsg[4] = (millis()/1000) - (millis()/1000)/256;
CAN.sendMsgBuf(0x7E8, 0, 8, OBDIImsg);
break;
case 47: // PID 2F hex
OBDIImsg[3] = 60;
CAN.sendMsgBuf(0x7E8, 0, 8, OBDIImsg);
break;
}
} else if (MODE == 3) {
unsigned char OBDIImsg[8] = {6, 67, 0, 0, 0, 0, 0, 0};
OBDIImsg[2] = 1; // Número de DTCs = 1
OBDIImsg[3] = 2; // P0217 Sobrecalentamiento del motor HIGH
OBDIImsg[4] = 23; // P0217 Sobrecalentamiento del motor LOW
CAN.sendMsgBuf(0x7E8, 0, 8, OBDIImsg);
}
}
}
If I test the code using the elm327 (bluetooth) and an app in my phone, the messages are sent/received as expected; When I connect the Carloop adapter and a sample code reader sketch the communication isn’t done.
Also, I’ve tested the carloop adapter and the same sketch in my car and the communication is performed well.
Is there any consideration I have to do in the arduino side to be compatible with the carloop adapter/library ?
UPDATE
I’m trying other approach; I’m using the can hitch connected as follows:
Particle Photon (ECU Simulator) ==> Carloop CAN Hitch ==> Female extension OBD cable ===> Carloop adapter ===> Particle electron
Again, this setup works with the ELM327; the photon receives all the requests sent by the phone app through the ELM327, but also again, the photon (the ECU simulator) doesn’t receives messages when switched to the electron.
My code is :
#include <carloop.h>
const auto OBD_REQUEST_ID = 0x7E0;
const auto OBD_REPLY_ID = 0x7E8;
const auto OBD_PID_SERVICE = 0x01;
SYSTEM_THREAD(ENABLED); // To not block the main program while searching for WiFi
Carloop<CarloopRevision2> carloop; // Instantiate Carloop Object
SYSTEM_MODE(SEMI_AUTOMATIC);
int mode = 0;
int pid = 0;
void processMode1(int pid);
void processMode2(int pid);
void setup() {
carloop.begin(); // Creates the appropriate CANChannel
Serial.begin(9600); // Open serial connection at 9600 baud, bits per second
Serial.print("Starting Up \n");
WiFi.connect();
}
void loop() {
CANMessage message;
while(carloop.can().receive(message)) {
mode = message.data[1];
pid = message.data[2];
Serial.print("Mode ");
Serial.print(mode);
Serial.print(" Pid ");
Serial.println(pid);
if(mode == 1) {
processMode1(pid);
}
}
}
void processMode1(int pid) {
CANMessage message;
message.id = 0x7E8;
message.len = 8;
if(pid == 0) {
message.data[0] = 6;
message.data[3] = 0x88;
message.data[4] = 0x18;
message.data[5] = 0x80;
message.data[6] = 0x13;
} else if(pid == 12) {
message.data[3] = 100 * 4 / 256;
message.data[4] = 100 - (100 * 4 / 256);
} else if(pid == 13) {
message.data[3] = 75;
} else if(pid == 4) {
message.data[3] = 98;
} else if(pid == 31) {
message.data[3] = (millis() / 1000)/256;
message.data[4] = (millis() / 1000) - (millis() / 1000) / 256;
} else if(pid == 47) {
message.data[3] = 60;
}
carloop.can().transmit(message);
}
void processMode2(int pid) {
}
The electron code as I wrote before works when I connect to my car. Any ideas ?