From 26e353765bd0ecc419f79531a28a562466acf925 Mon Sep 17 00:00:00 2001 From: Stanley Huang Date: Thu, 12 Nov 2015 19:42:43 +1100 Subject: Update OBD library and examples --- libraries/OBD/OBD.h | 2 +- .../OBD/examples/obd_i2c_test/obd_i2c_test.ino | 6 +- .../OBD/examples/obd_uart_test/obd_uart_test.ino | 89 ++++++++++++++++++++++ 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 libraries/OBD/examples/obd_uart_test/obd_uart_test.ino diff --git a/libraries/OBD/OBD.h b/libraries/OBD/OBD.h index 84e8446..e5ca2f9 100644 --- a/libraries/OBD/OBD.h +++ b/libraries/OBD/OBD.h @@ -10,7 +10,7 @@ #define OBD_MODEL_UART 0 #define OBD_MODEL_I2C 1 -#define OBD_TIMEOUT_SHORT 3000 /* ms */ +#define OBD_TIMEOUT_SHORT 1000 /* ms */ #define OBD_TIMEOUT_LONG 10000 /* ms */ #define OBD_TIMEOUT_GPS 200 /* ms */ #define OBD_SERIAL_BAUDRATE 38400 diff --git a/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino b/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino index d73e207..2a53a08 100644 --- a/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino +++ b/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino @@ -17,7 +17,7 @@ MPU6050 accelgyro; void testOut() { - static const char PROGMEM cmds[][6] = {"ATZ\r", "ATL1\r", "ATRV\r", "0100\r", "010C\r", "0902\r"}; + static const char PROGMEM cmds[][6] = {"ATZ\r", "ATL1\r", "ATH0\r", "ATRV\r", "0100\r", "010C\r", "0902\r"}; char buf[OBD_RECV_BUF_SIZE]; for (byte i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) { @@ -41,7 +41,7 @@ void testOut() } else { Serial.println("Timeout"); } - delay(500); + delay(1000); } Serial.println(); } @@ -94,7 +94,7 @@ void readPID() void setup() { delay(500); Serial.begin(115200); - Wire.begin(); + obd.begin(); accelgyro.initialize(); readMEMS(); diff --git a/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino b/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino new file mode 100644 index 0000000..b746cdd --- /dev/null +++ b/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino @@ -0,0 +1,89 @@ +/************************************************************************* +* Sample sketch for Freematics OBD-II UART Adapter +* Reads and prints several OBD-II PIDs value and MEMS sensor data +* Distributed under GPL v2.0 +* Visit http://freematics.com for more information +* (C)2012-2015 Stanley Huang +*************************************************************************/ + +#include +#include +#include +#include + +SoftwareSerial mySerial(A2, A3); +COBD obd; + +void testOut() +{ + static const char PROGMEM cmds[][6] = {"ATZ\r", "ATL1\r", "ATH0\r", "ATRV\r", "0100\r", "010C\r", "0902\r"}; + char buf[OBD_RECV_BUF_SIZE]; + + for (byte i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) { + char cmd[6]; + memcpy_P(cmd, cmds[i], sizeof(cmd)); + mySerial.print("Sending "); + mySerial.println(cmd); + if (obd.sendCommand(cmd, buf)) { + char *p = strstr(buf, cmd); + if (p) + p += strlen(cmd); + else + p = buf; + while (*p == '\r') p++; + while (*p) { + mySerial.write(*p); + if (*p == '\r' && *(p + 1) != '\r') + mySerial.write('\n'); + p++; + } + } else { + mySerial.println("Timeout"); + } + delay(1000); + } + mySerial.println(); +} + +void readPID() +{ + static const byte PROGMEM pidlist[] = {PID_ENGINE_LOAD, PID_COOLANT_TEMP, PID_RPM, PID_SPEED, PID_TIMING_ADVANCE, PID_INTAKE_TEMP, PID_THROTTLE, PID_FUEL_LEVEL}; + for (byte i = 0; i < sizeof(pidlist) / sizeof(pidlist[0]); i++) { + byte pid = pgm_read_byte(pidlist + i); + bool valid = obd.isValidPID(pid); + mySerial.print('0'); + mySerial.print((int)pid | 0x100, HEX); + mySerial.print('='); + if (valid) { + int value; + if (obd.read(pid, value)) { + byte n = mySerial.println(value); + } + } else { + mySerial.println('X'); + } + } +} + +void setup() { + delay(500); + mySerial.begin(9600); + obd.begin(); + + do { + testOut(); + mySerial.println("Init..."); + } while (!obd.init()); + + char buf[OBD_RECV_BUF_SIZE]; + if (obd.getVIN(buf)) { + mySerial.print("VIN:"); + mySerial.println(buf); + } + delay(1000); +} + +void loop() { + readPID(); + delay(500); +} -- cgit v1.2.3