From 8ff75c35eada34bd6db64f637253f73513a500fd Mon Sep 17 00:00:00 2001 From: loovee Date: Tue, 27 Jun 2017 17:16:25 +0800 Subject: [PATCH] add OBDII PIDs example --- examples/OBDII_PIDs/OBDII_PIDs.ino | 140 ++++++++++++++++++ examples/receive_Blink/receive_Blink.ino | 21 ++- .../{debug/debug.ino => recv_sd/recv_sd.ino} | 58 +++++--- examples/send/send.ino | 7 +- 4 files changed, 191 insertions(+), 35 deletions(-) create mode 100644 examples/OBDII_PIDs/OBDII_PIDs.ino rename examples/{debug/debug.ino => recv_sd/recv_sd.ino} (51%) diff --git a/examples/OBDII_PIDs/OBDII_PIDs.ino b/examples/OBDII_PIDs/OBDII_PIDs.ino new file mode 100644 index 0000000..baef25f --- /dev/null +++ b/examples/OBDII_PIDs/OBDII_PIDs.ino @@ -0,0 +1,140 @@ +/************************************************************************************************* + OBD-II_PIDs TEST CODE + LOOVEE @ JUN24, 2017 + + Query + send id: 0x7df + dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0 + + Response + From id: 0x7E9 or 0x7EA or 0x7EB + dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option) + + https://en.wikipedia.org/wiki/OBD-II_PIDs + + Input a PID, then you will get reponse from vehicle, the input should be end with '\n' +***************************************************************************************************/ +#include +#include "mcp_can.h" + +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; + +MCP_CAN CAN(SPI_CS_PIN); // Set CS pin + +#define PID_ENGIN_PRM 0x0C +#define PID_VEHICLE_SPEED 0x0D +#define PID_COOLANT_TEMP 0x05 + +#define CAN_ID_PID 0x7DF + +unsigned char PID_INPUT; +unsigned char getPid = 0; + +void set_mask_filt() +{ + /* + * set mask, set both the mask to 0x3ff + */ + CAN.init_Mask(0, 0, 0x7FC); + CAN.init_Mask(1, 0, 0x7FC); + + /* + * set filter, we can receive id from 0x04 ~ 0x09 + */ + CAN.init_Filt(0, 0, 0x7E8); + CAN.init_Filt(1, 0, 0x7E8); + + CAN.init_Filt(2, 0, 0x7E8); + CAN.init_Filt(3, 0, 0x7E8); + CAN.init_Filt(4, 0, 0x7E8); + CAN.init_Filt(5, 0, 0x7E8); +} + +void sendPid(unsigned char __pid) +{ + unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0}; + Serial.print("SEND PID: 0x"); + Serial.println(__pid, HEX); + CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp); +} + +void setup() +{ + Serial.begin(115200); + while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k + { + Serial.println("CAN BUS Shield init fail"); + Serial.println(" Init CAN BUS Shield again"); + delay(100); + } + Serial.println("CAN BUS Shield init ok!"); + set_mask_filt(); +} + + +void loop() +{ + taskCanRecv(); + taskDbg(); + + if(getPid) // GET A PID + { + getPid = 0; + sendPid(PID_INPUT); + PID_INPUT = 0; + } +} + +void taskCanRecv() +{ + unsigned char len = 0; + unsigned char buf[8]; + + if(CAN_MSGAVAIL == CAN.checkReceive()) // check if get data + { + CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf + + Serial.println("\r\n------------------------------------------------------------------"); + Serial.print("Get Data From id: "); + Serial.println(CAN.getCanId(), HEX); + for(int i = 0; i='0' && c<='9') + { + PID_INPUT *= 0x10; + PID_INPUT += c-'0'; + + } + else if(c>='A' && c<='F') + { + PID_INPUT *= 0x10; + PID_INPUT += 10+c-'A'; + } + else if(c>='a' && c<='f') + { + PID_INPUT *= 0x10; + PID_INPUT += 10+c-'a'; + } + else if(c == '\n') // END + { + getPid = 1; + } + } +} +// END FILE \ No newline at end of file diff --git a/examples/receive_Blink/receive_Blink.ino b/examples/receive_Blink/receive_Blink.ino index f37c1c6..3b87e59 100644 --- a/examples/receive_Blink/receive_Blink.ino +++ b/examples/receive_Blink/receive_Blink.ino @@ -2,16 +2,15 @@ // send data coming to fast, such as less than 10ms, you can use this way // loovee, 2014-6-13 - #include #include "mcp_can.h" - // the cs pin of the version after v1.1 is default to D9 // v0.9b and v1.0 is default D10 const int SPI_CS_PIN = 9; -const int LED=8; -boolean ledON=1; +const int LED = 8; +boolean ledON = 1; + MCP_CAN CAN(SPI_CS_PIN); // Set CS pin void setup() @@ -22,7 +21,7 @@ void setup() while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k { Serial.println("CAN BUS Shield init fail"); - Serial.println(" Init CAN BUS Shield again"); + Serial.println("Init CAN BUS Shield again"); delay(100); } Serial.println("CAN BUS Shield init ok!"); @@ -51,21 +50,19 @@ void loop() if(ledON && i==0) { - digitalWrite(LED,buf[i]); - ledON=0; + digitalWrite(LED, buf[i]); + ledON = 0; delay(500); } else if((!(ledON)) && i==4) { - digitalWrite(LED,buf[i]); - ledON=1; + digitalWrite(LED, buf[i]); + ledON = 1; } } Serial.println(); } } -/********************************************************************************************************* - END FILE -*********************************************************************************************************/ \ No newline at end of file +//END FILE \ No newline at end of file diff --git a/examples/debug/debug.ino b/examples/recv_sd/recv_sd.ino similarity index 51% rename from examples/debug/debug.ino rename to examples/recv_sd/recv_sd.ino index 0a1da76..71d71ba 100644 --- a/examples/debug/debug.ino +++ b/examples/recv_sd/recv_sd.ino @@ -1,18 +1,23 @@ -// demo: CAN-BUS Shield, receive data with interrupt mode -// when in interrupt mode, the data coming can't be too fast, must >20ms, -// or else you can use check mode -// loovee, 2014-6-13 +// demo: CAN-BUS Shield, receive data and save to sd card, can.csv +// when in interrupt mode, the data coming can't be too fast, must >20ms, or else you can use check mode +// NOTE: Only CAN Bus Shield V2.0 has SD slot, for other versions, you need an SD card Shield as well +// loovee, Jun 12, 2017 + #include #include "mcp_can.h" +#include + +File myFile; // the cs pin of the version after v1.1 is default to D9 // v0.9b and v1.0 is default D10 -const int SPI_CS_PIN = 9; +const int SPI_CS_CAN = 9; +const int SPI_CS_SD = 4; -MCP_CAN CAN(SPI_CS_PIN); // Set CS pin +MCP_CAN CAN(SPI_CS_CAN); // Set CS pin -unsigned char flagRecv = 0; -unsigned char len = 0; +unsigned char flagRecv = 0; +unsigned char len = 0; unsigned char buf[8]; char str[20]; @@ -23,12 +28,18 @@ void setup() while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k { Serial.println("CAN BUS Shield init fail"); - Serial.println(" Init CAN BUS Shield again"); + Serial.println("Init CAN BUS Shield again"); delay(100); } Serial.println("CAN BUS Shield init ok!"); attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt + + if (!SD.begin(4)) { + Serial.println("SD initialization failed!"); + while(1); + } + Serial.println("SD initialization done."); } void MCP2515_ISR() @@ -36,13 +47,15 @@ void MCP2515_ISR() flagRecv = 1; } -// {id, } void loop() { if(flagRecv) { // check if get data flagRecv = 0; // clear flag + unsigned long id= 0; + + myFile = SD.open("can.csv", FILE_WRITE); // iterate over all pending messages // If either the bus is saturated or the MCU is busy, @@ -51,20 +64,27 @@ void loop() while (CAN_MSGAVAIL == CAN.checkReceive()) { // read data, len: data length, buf: data buf - CAN.readMsgBuf(&len, buf); - - // print the data + CAN.readMsgBufID(&id, &len, buf); + + Serial.print(id); + Serial.print(","); + myFile.print(id); + myFile.print(","); + for(int i = 0; i #include @@ -25,7 +27,6 @@ unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0}; void loop() { // send data: id = 0x00, standrad frame, data len = 8, stmp: data buf - stmp[7] = stmp[7]+1; if(stmp[7] == 100) { @@ -43,6 +44,4 @@ void loop() delay(100); // send data per 100ms } -/********************************************************************************************************* - END FILE -*********************************************************************************************************/ +// END FILE \ No newline at end of file