Hello everyone ,
I’m using Arduino one and SparkFun Product
link SparkFun shield https://www.sparkfun.com/products/9555
I am following a tutorial on the SparkFun website and I have some doubts and problems that I can not solve.
my goal is ’ to connect the shield to the machine to perform a diagnostic service
I’m trying the code and ’ attached to the inside of the tutorial .
#include <SoftwareSerial.h>
//Create an instance of the new soft serial library to control the serial Serial
//Note, digital pin 3 of the Arduino should be connected to Rx of the serial Serial.
//This is a character buffer that will store the data from the serial port
char rxData[20];
char rxIndex=0;
SoftwareSerial OBD(5,6);
//Variables to hold the speed and RPM data.
int vehicleSpeed=0;
int vehicleRPM=0;
void setup(){
//Both the Serial Serial and the OBD-II-UART use 9600 bps.
Serial.begin(9600);
OBD.begin(9600);
//Clear the old data from the Serial.
//Put the speed header on the first row.
Serial.print("Speed: ");
//Put the RPM header on the second row.
Serial.print("RPM: ");
//Wait for a little while before sending the reset command to the OBD-II-UART
delay(1500);
//Reset the OBD-II-UART
OBD.println("ATZ");
Serial.println("Risposta ELM327:");
Serial.println(OBD.read());
//Wait for a bit before starting to send commands after the reset.
delay(10000);
//Delete any data that may be in the serial port before we begin.
Serial.flush();
}
void loop(){
//Delete any data that may be in the serial port before we begin.
Serial.flush();
//Set the cursor in the position where we want the speed data.
//Clear out the old speed data, and reset the cursor position.
Serial.print(" ");
//Query the OBD-II-UART for the Vehicle Speed
OBD.println("010D");
//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 Serial
Serial.print(vehicleSpeed);
Serial.print(" km/h");
delay(100);
//Delete any data that may be left over in the serial port.
Serial.flush();
//Move the serial cursor to the position where we want the RPM data.
//Clear the old RPM data, and then move the cursor position back.
Serial.print(" ");
//Query the OBD-II-UART for the Vehicle rpm
OBD.println("010C");
//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 Serial
Serial.print("Giri minuto=");
Serial.print(vehicleRPM);
//Give the OBD bus a rest
delay(1000);
}
//The getResponse function collects incoming data from the UART into the rxData buffer
// and only exits when a carriage return character is seen. Once the carriage return
// string is detected, the rxData buffer is null terminated (so we can treat it as a string)
// and the rxData index is reset to 0 so that the next string can be copied.
void getResponse(void){
char inChar=0;
Serial.println("sono nella getresponde");
//Keep reading characters until we get a carriage return
while(inChar != '\r'){
Serial.println("leggo e aspetto il carattere di escape");
Serial.println(inChar);
//If a character comes in on the serial port, we need to act on it.
if(OBD.available() > 0){
Serial.println("arriva qualcosa");
//Start by checking if we've received the end of message character ('\r').
if(OBD.peek() == '\r'){
//Clear the Serial buffer
inChar=OBD.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 = OBD.read();
//Add the new character to the string, and increment the index variable.
rxData[rxIndex++]=inChar;
}
}
}
}
I slightly modified the code , the main differences are that I did not use an LCD to display data and I do not have the shield connected to the serial arduino but I did a serial communication on pins 5 and 6 through the library SoftwareSerial . Connecting Arduino to the machine I do not get the data sought but I get strange sequences of characters and the response to ’ at command " atz " is -1 or 255 if I change the baud rate . I tried " googling " the problem but have not found anything about it , I just thought that the problems may be due to the use of the software or the serial baud rate .
Have any of you ever encountered problems like this?
thank you all!