diff options
author | Stanley Huang <stanleyhuangyc@live.com> | 2016-06-26 20:53:03 +0800 |
---|---|---|
committer | Stanley Huang <stanleyhuangyc@live.com> | 2016-06-26 20:53:03 +0800 |
commit | 243b5fa4ddd06b845a68abccb0a109584cf0fa38 (patch) | |
tree | 853ff2c83711e6ef0b55b919ec54b195aa49a739 /libraries/OBD2UART/examples | |
parent | 80112f02d05aed34035534b342b320854d445cba (diff) | |
download | 2021-arduino-obd-243b5fa4ddd06b845a68abccb0a109584cf0fa38.tar.gz 2021-arduino-obd-243b5fa4ddd06b845a68abccb0a109584cf0fa38.tar.bz2 2021-arduino-obd-243b5fa4ddd06b845a68abccb0a109584cf0fa38.zip |
Library and examples for OBD-II UART Adapter MK2
Diffstat (limited to 'libraries/OBD2UART/examples')
-rw-r--r-- | libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino | 105 | ||||
-rw-r--r-- | libraries/OBD2UART/examples/rpm_led_uart/rpm_led_uart.ino | 30 |
2 files changed, 135 insertions, 0 deletions
diff --git a/libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino b/libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino new file mode 100644 index 0000000..bd1c618 --- /dev/null +++ b/libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino @@ -0,0 +1,105 @@ +/************************************************************************* +* Testing sketch for Freematics OBD-II UART Adapter +* Reads and prints several OBD-II PIDs value +* Distributed under GPL v2.0 +* Visit http://freematics.com for more information +* Written by Stanley Huang <stanleyhuangyc@gmail.com> +*************************************************************************/ + +#include <SoftwareSerial.h> +#include <OBD2UART.h> + +// On Arduino Leonardo, Micro, MEGA or DUE, hardware serial can be used for output +// as OBD-II UART adapter connects to Serial1, otherwise we use software serial +SoftwareSerial mySerial(A2, A3); +//#define mySerial Serial + +COBD obd; + +void testOut() +{ + static const char cmds[][6] = {"ATZ\r", "ATL1\r", "ATH0\r", "ATRV\r", "0100\r", "010C\r", "0902\r"}; + char buf[128]; + + for (byte i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) { + const char *cmd = cmds[i]; + mySerial.print("Sending "); + mySerial.println(cmd); + if (obd.sendCommand(cmd, buf, sizeof(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 readPIDSingle() +{ + int value; + mySerial.print('['); + mySerial.print(millis()); + mySerial.print(']'); + mySerial.print("RPM="); + if (obd.readPID(PID_RPM, value)) { + mySerial.print(value); + } + mySerial.println(); +} + +void readPIDMultiple() +{ + static const byte pids[] = {PID_SPEED, PID_ENGINE_LOAD, PID_THROTTLE, PID_COOLANT_TEMP, PID_INTAKE_TEMP}; + int values[sizeof(pids)]; + if (obd.readPID(pids, sizeof(pids), values) == sizeof(pids)) { + for (byte i = 0; i < sizeof(pids) ; i++) { + mySerial.print('['); + mySerial.print(millis()); + mySerial.print(']'); + mySerial.print((int)pids[i] | 0x100, HEX); + mySerial.print('='); + mySerial.println(values[i]); + } + } +} + +void setup() +{ + delay(500); + mySerial.begin(115200); + // this will begin serial + obd.begin(); + + // send some commands for testing and show response + testOut(); + + // initialize OBD-II adapter + do { + mySerial.println("Init..."); + } while (!obd.init()); + + char buf[64]; + if (obd.getVIN(buf, sizeof(buf))) { + mySerial.print("VIN:"); + mySerial.println(buf); + } + delay(1000); +} + +void loop() +{ + readPIDSingle(); + readPIDMultiple(); +} diff --git a/libraries/OBD2UART/examples/rpm_led_uart/rpm_led_uart.ino b/libraries/OBD2UART/examples/rpm_led_uart/rpm_led_uart.ino new file mode 100644 index 0000000..3e80ae3 --- /dev/null +++ b/libraries/OBD2UART/examples/rpm_led_uart/rpm_led_uart.ino @@ -0,0 +1,30 @@ +/************************************************************************* +* Sample sketch based on OBD-II library for Arduino +* Distributed under GPL v2.0 +* Visit http://freematics.com for more information +* (C)2012-2014 Stanley Huang <stanleyhuangyc@gmail.com> +*************************************************************************/ + +#include <OBD2UART.h> + +COBD obd; + +void setup() +{ + // we'll use the debug LED as output + pinMode(13, OUTPUT); + // start communication with OBD-II UART adapter + obd.begin(); + // initiate OBD-II connection until success + while (!obd.init()); +} + +void loop() +{ + int value; + if (obd.readPID(PID_RPM, value)) { + // RPM is successfully read and its value stored in variable 'value' + // light on LED when RPM exceeds 3000 + digitalWrite(13, value > 3000 ? HIGH : LOW); + } +} |