- Mon Apr 17, 2017 9:09 am
#194412
Hi guys, Can someone check my code or maybe give me some insight on what I'm doing wrong.
I have a SparkFun OBD-II UART paired with a red board. I got the sample script to work with a I2C display and then moved to what I was looking to use this for, read engine coolant temp.
from here it is supposed to be 1 byte and the conversion is A - 40. Engine is about 150-160 F and I'm reading 13C and 55 F. The vehicle is a Chevy 2007 Trailblazer
Anyway, any pointers would be appreciated. I assume the problem is going to be in my string to long conversion.
Thanks!
I have a SparkFun OBD-II UART paired with a red board. I got the sample script to work with a I2C display and then moved to what I was looking to use this for, read engine coolant temp.
from here it is supposed to be 1 byte and the conversion is A - 40. Engine is about 150-160 F and I'm reading 13C and 55 F. The vehicle is a Chevy 2007 Trailblazer
Anyway, any pointers would be appreciated. I assume the problem is going to be in my string to long conversion.
Thanks!
Code: Select all
/*
OBD-II-UART Quickstart Sketch
Written by Ryan Owens for SparkFun Electronics 7/5/2011
Updates for Arduino 1.0+ by Toni Klopfenstein
Released under the 'beerware' license
(Do what you want with the code, but if we ever meet then you buy me a beer)
This sketch will grab RPM and Vehicle Speed data from a vehicle with an OBD port
using the OBD-II-UART board from SparkFun electronics. The data will be displayed
on a serial 16x2 LCD. See the tutorial at https://www.sparkfun.com/tutorials/294
to learn how to hook up the hardware:
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//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 enginecoolant = 0;
int enginecoolantF = 0;
void setup() {
//Both the Serial LCD and the OBD-II-UART use 9600 bps.
Serial.begin(9600);
//Wait for a little while before sending the reset command to the OBD-II-UART
delay(1500);
//Reset the OBD-II-UART
Serial.println("ATZ");
//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.
Serial.flush();
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
display.clearDisplay();
// text display tests
display.setTextSize(1);
display.setTextColor(WHITE);
display.display();
}
void loop() {
//Delete any data that may be left over in the serial port.
Serial.flush();
//Query the OBD-II-UART for the Vehicle rpm
Serial.println("0105");
//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
enginecoolant = (strtol(&rxData[6],0,16))-40;
enginecoolantF = (enginecoolant * 9/5) + 32;
delay(100);
display.clearDisplay();
display.setCursor(0, 8);
display.println(enginecoolant);
display.setCursor(0, 20);
display.println(enginecoolantF);
display.display();
}
//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;
//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 (Serial.available() > 0) {
//Start by checking if we've received the end of message character ('\r').
if (Serial.peek() == '\r') {
//Clear the Serial buffer
inChar = Serial.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 = Serial.read();
//Add the new character to the string, and increment the index variable.
rxData[rxIndex++] = inChar;
}
}
}
}