Go Down

Topic: OBDII to UART Issues (Read 7 times) previous topic - next topic

matinzk

This is what I tested first:

Code: [Select]
#include <LiquidCrystal.h>

LiquidCrystal lcd(26,28,30,32,34,36);

//This is a character buffer that will store the data from the serial port
char rxData[20];
char rxIndex=0;

//Variables to hold the speed and RPM data.
int vehicleSpeed=0;
int vehicleRPM=0;

void setup(){
  //Both the Serial LCD and the OBD-II-UART use 9600 bps.
  lcd.begin(4, 20);   

  Serial1.begin(9600);
//  Serial.begin(9600);
 
  //Clear the old data from the LCD.
  lcd.clear();
 
  //Put the speed header on the first row.

  lcd.print("Speed: ");

  //Put the RPM header on the second row.
  lcd.setCursor(0,1);
  lcd.print("RPM: ");

  //Wait for a little while before sending the reset command to the OBD-II-UART
  delay(1500);
  //Reset the OBD-II-UART
  Serial1.print("ATZ\r");
  //Wait for a bit before starting to send commands after the reset.
 
  delay(2000);
 
  //Delete any data that may be in the serial port before we begin.
  Serial1.flush();
}


void loop(){
  //Delete any data that may be in the serial port before we begin. 
  Serial1.flush();
  //Set the cursor in the position where we want the speed data.

  //Clear out the old speed data, and reset the cursor position.
//  lcd.setCursor(10,0);
//  lcd.print("   ");

  //Query the OBD-II-UART for the Vehicle Speed
  Serial1.print("010D\r");
  //Get the response from the OBD-II-UART board. We get two responses
  //because the OBD-II-UART echoes the command that is sent.
  //We want the data in the second response.
  getResponse();
  getResponse();
  //Convert the string data to an integer
  vehicleSpeed = strtol(&rxData[6],0,16);
  //Print the speed data to the lcd
  lcd.setCursor(10,0);
  lcd.print(vehicleSpeed);
 

 
  lcd.setCursor(16,0);
  lcd.print("km/h");
  delay(100);
 
  //Delete any data that may be left over in the serial port.
  Serial1.flush();


  //Query the OBD-II-UART for the Vehicle rpm
  Serial1.print("010C\r");
  //Get the response from the OBD-II-UART board
  getResponse();
  getResponse();
  //Convert the string data to an integer
  //NOTE: RPM data is two bytes long, and delivered in 1/4 RPM from the OBD-II-UART
  vehicleRPM = ((strtol(&rxData[6],0,16)*256)+strtol(&rxData[9],0,16))/4;
  //Print the rpm data to the lcd
  lcd.setCursor(10,1);
  lcd.print(vehicleRPM);

  //Give the OBD bus a rest
  delay(100);
 

}



void getResponse(void){
  char inChar=0;
  //Keep reading characters until we get a carriage return
  while(inChar != '\r'){
    //If a character comes in on the serial port, we need to act on it.
    if(Serial1.available() > 0){
      //Start by checking if we've received the end of message character ('\r').
      if(Serial1.peek() == '\r'){
        //Clear the Serial buffer
        inChar=Serial1.read();
        //Put the end of string character on our data string
        rxData[rxIndex]='\0';
        //Reset the buffer index so that the next character goes back at the beginning of the string.
        rxIndex=0;
      }
      //If we didn't get the end of message character, just add the new character to the string.
      else{
        //Get the new character from the Serial port.
        inChar = Serial1.read();
        //Add the new character to the string, and increment the index variable.
        rxData[rxIndex++]=inChar;
      }
    }
  }
}

matinzk

This is what I tried next:

Code: [Select]
#include <LiquidCrystal.h>

LiquidCrystal lcd(26,28,30,32,34,36);

//This is a character buffer that will store the data from the serial port
char rxData[20];
char rxIndex=0;

//Variables to hold the speed and RPM data.
int vehicleSpeed=0;
int vehicleRPM=0;

void setup(){
 //Both the Serial LCD and the OBD-II-UART use 9600 bps.
 lcd.begin(4, 20);    

 Serial1.begin(9600);
//  Serial.begin(9600);
 
 //Clear the old data from the LCD.
 lcd.clear();
 
 //Put the speed header on the first row.

 lcd.print("Speed: ");

 //Put the RPM header on the second row.
 lcd.setCursor(0,1);
 lcd.print("RPM: ");

       lcd.setCursor(0,2);
       lcd.print("Coolant");
 
 //Wait for a little while before sending the reset command to the OBD-II-UART
 delay(1500);
 //Reset the OBD-II-UART
 Serial1.print("ATWS/r");
 //Wait for a bit before starting to send commands after the reset.
 delay(1000);
 
 Serial1.print("ATDPN/r");  //Shows the protocol
 delay(1000);
 
 Serial1.print("0100/r");  
 delay(2000);
 
 //Delete any data that may be in the serial port before we begin.
 Serial1.flush();
}


void loop(){
 //Delete any data that may be in the serial port before we begin.  
 Serial1.flush();
 //Set the cursor in the position where we want the speed data.

 //Clear out the old speed data, and reset the cursor position.
//  lcd.setCursor(10,0);
//  lcd.print("   ");

 //Query the OBD-II-UART for the Vehicle Speed
 Serial1.print("010D\r");
 //Get the response from the OBD-II-UART board. We get two responses
 //because the OBD-II-UART echoes the command that is sent.
 //We want the data in the second response.
 getResponse();
 getResponse();
 //Convert the string data to an integer
 vehicleSpeed = strtol(&rxData[6],0,16);
 //Print the speed data to the lcd
 lcd.setCursor(10,0);
 lcd.print(vehicleSpeed);
 

 
 lcd.setCursor(16,0);
 lcd.print("km/h");
 delay(100);
 
 //Delete any data that may be left over in the serial port.
 Serial1.flush();

 //Query the OBD-II-UART for the Vehicle rpm
 Serial1.print("010C\r");
 //Get the response from the OBD-II-UART board
 getResponse();
 getResponse();
 //Convert the string data to an integer
 //NOTE: RPM data is two bytes long, and delivered in 1/4 RPM from the OBD-II-UART
 vehicleRPM = ((strtol(&rxData[6],0,16)*256)+strtol(&rxData[9],0,16))/4;
 //Print the rpm data to the lcd
 lcd.setCursor(10,1);
 lcd.print(vehicleRPM);

 //Give the OBD bus a rest
 delay(100);

}



void getResponse(void){
 char inChar=0;
 //Keep reading characters until we get a carriage return
 while(inChar != '\r'){
   //If a character comes in on the serial port, we need to act on it.
   if(Serial1.available() > 0){
     //Start by checking if we've received the end of message character ('\r').
     if(Serial1.peek() == '\r'){
       //Clear the Serial buffer
       inChar=Serial1.read();
       //Put the end of string character on our data string
       rxData[rxIndex]='\0';
       //Reset the buffer index so that the next character goes back at the beginning of the string.
       rxIndex=0;
     }
     //If we didn't get the end of message character, just add the new character to the string.
     else{
       //Get the new character from the Serial port.
       inChar = Serial1.read();
       //Add the new character to the string, and increment the index variable.
       rxData[rxIndex++]=inChar;
     }
   }
 }
}

pylon

Insert debugging statements that put out everything you get on the Serial1 to the Serial. This way you see the raw data and you can distinguish between irregular data you get from the engine and errors in interpreting it. At least your BMW says it supports the speed and RPM PIDs (010D and 010C).

matinzk

I won't be able to test until I find another car close to me, that's OBDII compatible.

Does the code look fine to you? Also will you be kind enough to test the code for me if you have access to the board/car? If not, don't worry.

Thanks.

pylon

If you change the code to put out to the PC serial instead of the LCD, I will test it for you. I have a graphical LCD on my OBD-Arduino, so your code will not work the way it currently is.

Also keep in mind, that different cars are different, especially in this regard. My car uses the CAN bus, your brother's BMW uses ISO 9141. Just reading the RPMs and speed values should work on most OBD-II compatible cars though.

Go Up