I have a OBD II project the uses the sparkfun UART OBD II (https://www.sparkfun.com/products/9555) device, a 1.8" tft (similar to the one sold at by adafruit), a button and an accelerometer. Everything is configured via a file in an SD card.
A bit of warning, the code block is long and I don't like how it is organized (one large loop of reading through the config file), but that part works for the moment.
Here is the larger, graphical 1.8" tft version: https://github.com/stirobot/arduinoModularTFTgauges/blob/master/modularTFTgauge.ino
I also have a smaller more readable version of this project that uses 3 buttons, the OBD II UART device and a serial 7 segment display (SparkFun 7-Segment Serial Display - Blue - COM-11442 - SparkFun Electronics).
Here is the code for that: https://github.com/stirobot/arduinoModularTFTgauges/blob/master/brzclockgauge.ino
Some other notes:
-Both sketches use roughly the same OBD II code. I've included the second one so you don't have to wade through the long project code listing of the first version. It has a lot of graphics code and stuff for slogging through the config file (in an inefficient manner...it is like I did the whole thing forgetting to use arrays
).
-The BRZ (also called the FRS/GT86) is a car I own that use a special OBD II CAN header to access the 2101 pid that gives oil temperature.
-Even though some of the code applies the header right before getting an oil temp reading, it isn't necessary. Resetting when switching back to standard PIDs is also not necessary.
So, what is my issue...
-I have something wrong with the OBD II serial flow of things. I will oftentimes get no data or partial data from some PID calls. And this is dependent on what PID I query for first
-I suspect this has something to do with stuff still left in the rxData[] array or maybe with how I initialized the connection to the UART device and make the first call.
There are two versions of getResponse() that are responsible for getting the serial stuff back from the UART device. I've gotten them from others in other forums. What are your thoughts on the two of them?
//from: https://forum.sparkfun.com/viewtopic.php?f=14&t=32457&start=60 and https://forum.sparkfun.com/viewtopic.php?f=14&t=38253
/*void getResponse(void){
char c;
// int start=millis();
//If nothing is currently available do nothing and break after 3 seconds
//while(Serial1.available()==0){if(millis()-start>3000){break;}}
do {
if (Serial1.available() > 0)
{
c = Serial1.read();
if ((c != '>') && (c != '\r') && (c != '\n')) //Keep these out of our buffer
{
rxData[rxIndex++] = c; //Add whatever we receive to the buffer
}
}
}
while (c != '>'); //The ELM327 ends its response with this char so when we get it we exit out.
rxData[rxIndex++] = '\0'; //Converts the array into a string
Serial.print("rxData(in getResponse): ");
Serial.println(rxData);
rxIndex = 0; //Set this to 0 so next time we call the read we get a "clean buffer
}
and
void getResponse(void){
char obdIn=0;
int i=0;
int start=millis();
//If nothing is currently available do nothing and break after 3 seconds
while(Serial1.available()==0){if(millis()-start>3000){break;}}
while(Serial1.available()){
//check to see if end of line/message
if (Serial1.peek()=='\r'){
obdIn=Serial1.read();
rxData[i]='\0';
Serial.println(rxData);
i=0;
}
// The prompt is sometimes the only thing recieved so this needs to be taken care of
else if(Serial1.peek()=='>'){
obdIn=Serial1.read();
Serial.write(obdIn);
}
// Add next character to string
else{
obdIn=Serial1.read();
rxData[i++]=obdIn;
}
}
Serial.print("rxData(in getResponse): ");
Serial.println(rxData);
rxIndex=0;
}
Sorry for the word fort/wall of text. It isn't the most straightforward question and I don't expect a silver bullet answer (though that would be pretty neat). I would like to know what to try next. I'm stumped.