#include <FlexCAN_T4.h>
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can0;
int engineCoolantTemp = 0; // Save the data here
void setup(){
while (!Serial && millis() < 5000);
Serial.begin(115200);
Can0.begin();
Can0.setBaudRate(500000);
Can0.setMBFilter(REJECT_ALL);
Can0.setMBFilter(MB1, 0x7E8);
Can0.enableMBInterrupt(MB1);
Can0.onReceive(canSniff);
}
const long loopDelay1 = 500; // Make a request every 500ms
unsigned long timeNow1 = 0;
void loop(){
Can0.events();
if (millis() > timeNow1 + loopDelay1)
{
timeNow1 = millis();
canTx_OBD(); // Query data from CAN BUS via OBD
//canTx_UDS(); // Query data from CAN BUS via UDS
}
}
void canSniff (const CAN_message_t &msg){ // Global callback to catch any CAN frame coming in
Serial.println(" ");
Serial.print("MB: ");
Serial.print(msg.mb);
Serial.print(" ID: 0x");
Serial.print(msg.id, HEX);
Serial.print(" EXT: ");
Serial.print(msg.flags.extended);
Serial.print(" LEN: ");
Serial.print(msg.len);
Serial.print(" DATA: ");
for (uint8_t i = 0; i < 8; i++)
{
Serial.print(msg.buf[i], HEX);
Serial.print(" ");
}
if (msg.buf[1] == 0x01 && msg.buf[2] == 0x05){
engineCoolantTemp = msg.buf[3] - 40;
Serial.println(" ");
Serial.printf("OBD: Engine Coolant Temp: %d c", engineCoolantTemp);
}
if (msg.buf[1] == 0x22 && msg.buf[2] == 0xf4 && msg.buf[3] == 0x05){
engineCoolantTemp = msg.buf[4] - 40;
Serial.println(" ");
Serial.printf("UDS: Engine Coolant Temp: %d c", engineCoolantTemp);
}
}
void canTx_OBD(){ // Request function to ask for Engine Coolant Temp via OBD request
CAN_message_t msgTx, msgRx;
msgTx.buf[0] = 0x02;
msgTx.buf[1] = 0x01;
msgTx.buf[2] = 0x05;
msgTx.buf[3] = 0;
msgTx.buf[4] = 0;
msgTx.buf[5] = 0;
msgTx.buf[6] = 0;
msgTx.buf[7] = 0;
msgTx.len = 8; // number of bytes in request
msgTx.flags.extended = 0; // 11 bit header, not 29 bit
msgTx.flags.remote = 0;
msgTx.id = 0x7E0; // request header for OBD
Can0.write(msgTx);
Serial.println("canTx_OBD REQ sent");
}
void canTx_UDS(){ // Request function to ask for Engine Coolant Temp via UDS request
CAN_message_t msgTx, msgRx;
msgTx.buf[0] = 0x03;
msgTx.buf[1] = 0x22;
msgTx.buf[2] = 0xf4;
msgTx.buf[3] = 0x05;
msgTx.buf[4] = 0;
msgTx.buf[5] = 0;
msgTx.buf[6] = 0;
msgTx.buf[7] = 0;
msgTx.len = 8; // number of bytes in request
msgTx.flags.extended = 0; // 11 bit header, not 29 bit
msgTx.flags.remote = 0;
msgTx.id = 0x7E0; // request header for OBD
Can0.write(msgTx);
Serial.println("canTx_UDS REQ sent");
}