summaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorStanley Huang <stanleyhuangyc@live.com>2016-06-26 20:53:03 +0800
committerStanley Huang <stanleyhuangyc@live.com>2016-06-26 20:53:03 +0800
commit243b5fa4ddd06b845a68abccb0a109584cf0fa38 (patch)
tree853ff2c83711e6ef0b55b919ec54b195aa49a739 /libraries
parent80112f02d05aed34035534b342b320854d445cba (diff)
download2021-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')
-rw-r--r--libraries/OBD2UART/OBD2UART.cpp (renamed from libraries/OBDUART/OBDUART.cpp)63
-rw-r--r--libraries/OBD2UART/OBD2UART.h (renamed from libraries/OBDUART/OBDUART.h)4
-rw-r--r--libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino105
-rw-r--r--libraries/OBD2UART/examples/rpm_led_uart/rpm_led_uart.ino30
4 files changed, 176 insertions, 26 deletions
diff --git a/libraries/OBDUART/OBDUART.cpp b/libraries/OBD2UART/OBD2UART.cpp
index f91c7ea..78954c4 100644
--- a/libraries/OBDUART/OBDUART.cpp
+++ b/libraries/OBD2UART/OBD2UART.cpp
@@ -5,7 +5,7 @@
* (C)2012-2016 Stanley Huang <stanleyhuangyc@gmail.com>
*************************************************************************/
-#include "OBDUART.h"
+#include "OBD2UART.h"
//#define DEBUG Serial
@@ -54,14 +54,14 @@ byte hex2uint8(const char *p)
* OBD-II UART Adapter
*************************************************************************/
-byte COBDUART::sendCommand(const char* cmd, char* buf, byte bufsize, int timeout)
+byte COBD2UART::sendCommand(const char* cmd, char* buf, byte bufsize, int timeout)
{
write(cmd);
dataIdleLoop();
return receive(buf, bufsize, timeout);
}
-void COBDUART::sendQuery(byte pid)
+void COBD2UART::sendQuery(byte pid)
{
char cmd[8];
sprintf(cmd, "%02X%02X\r", dataMode, pid);
@@ -71,7 +71,7 @@ void COBDUART::sendQuery(byte pid)
write(cmd);
}
-bool COBDUART::readPID(byte pid, int& result)
+bool COBD2UART::readPID(byte pid, int& result)
{
// send a query command
sendQuery(pid);
@@ -79,14 +79,14 @@ bool COBDUART::readPID(byte pid, int& result)
return getResult(pid, result);
}
-byte COBDUART::readPID(const byte pid[], byte count, int result[])
+byte COBD2UART::readPID(const byte pid[], byte count, int result[])
{
// send a multiple query command
char buffer[128];
char *p = buffer;
byte results = 0;
for (byte n = 0; n < count; n++) {
- p += sprintf(p, "%02X%02X\r\n", dataMode, pid[n]);
+ p += sprintf(p, "%02X%02X\r", dataMode, pid[n]);
}
write(buffer);
// receive and parse the response
@@ -98,19 +98,19 @@ byte COBDUART::readPID(const byte pid[], byte count, int result[])
return results;
}
-void COBDUART::clearDTC()
+void COBD2UART::clearDTC()
{
char buffer[32];
write("04\r");
receive(buffer, sizeof(buffer));
}
-void COBDUART::write(const char* s)
+void COBD2UART::write(const char* s)
{
OBDUART.write(s);
}
-int COBDUART::normalizeData(byte pid, char* data)
+int COBD2UART::normalizeData(byte pid, char* data)
{
int result;
switch (pid) {
@@ -194,7 +194,7 @@ int COBDUART::normalizeData(byte pid, char* data)
return result;
}
-char* COBDUART::getResponse(byte& pid, char* buffer, byte bufsize)
+char* COBD2UART::getResponse(byte& pid, char* buffer, byte bufsize)
{
while (receive(buffer, bufsize) > 0) {
char *p = buffer;
@@ -213,7 +213,7 @@ char* COBDUART::getResponse(byte& pid, char* buffer, byte bufsize)
return 0;
}
-bool COBDUART::getResult(byte& pid, int& result)
+bool COBD2UART::getResult(byte& pid, int& result)
{
char buffer[64];
char* data = getResponse(pid, buffer, sizeof(buffer));
@@ -226,7 +226,7 @@ bool COBDUART::getResult(byte& pid, int& result)
return true;
}
-bool COBDUART::setProtocol(OBD_PROTOCOLS h)
+bool COBD2UART::setProtocol(OBD_PROTOCOLS h)
{
char buf[32];
if (h == PROTO_AUTO) {
@@ -241,22 +241,37 @@ bool COBDUART::setProtocol(OBD_PROTOCOLS h)
return false;
}
-void COBDUART::sleep()
+void COBD2UART::sleep()
{
char buf[32];
sendCommand("ATLP\r", buf, sizeof(buf));
}
-float COBDUART::getVoltage()
+char* COBD2UART::getResultValue(char* buf)
+{
+ char* p = buf;
+ for (;;) {
+ if (isdigit(*p)) {
+ return p;
+ }
+ p = strchr(p, '\r');
+ if (!p) break;
+ if (*(++p) == '\n') p++;
+ }
+ return 0;
+}
+
+float COBD2UART::getVoltage()
{
char buf[32];
if (sendCommand("ATRV\r", buf, sizeof(buf)) > 0) {
- return atof(buf);
+ char* p = getResultValue(buf);
+ if (p) return atof(p);
}
return 0;
}
-bool COBDUART::getVIN(char* buffer, byte bufsize)
+bool COBD2UART::getVIN(char* buffer, byte bufsize)
{
if (sendCommand("0902\r", buffer, bufsize)) {
char *p = strstr(buffer, "0: 49 02");
@@ -276,7 +291,7 @@ bool COBDUART::getVIN(char* buffer, byte bufsize)
return false;
}
-bool COBDUART::isValidPID(byte pid)
+bool COBD2UART::isValidPID(byte pid)
{
if (pid >= 0x7f)
return true;
@@ -286,7 +301,7 @@ bool COBDUART::isValidPID(byte pid)
return pidmap[i] & b;
}
-void COBDUART::begin()
+void COBD2UART::begin()
{
OBDUART.begin(OBD_SERIAL_BAUDRATE);
#ifdef DEBUG
@@ -295,7 +310,7 @@ void COBDUART::begin()
recover();
}
-byte COBDUART::receive(char* buffer, byte bufsize, int timeout)
+byte COBD2UART::receive(char* buffer, byte bufsize, int timeout)
{
unsigned char n = 0;
unsigned long startTime = millis();
@@ -328,13 +343,13 @@ byte COBDUART::receive(char* buffer, byte bufsize, int timeout)
return n;
}
-void COBDUART::recover()
+void COBD2UART::recover()
{
char buf[16];
sendCommand("AT\r", buf, sizeof(buf));
}
-bool COBDUART::init(OBD_PROTOCOLS protocol)
+bool COBD2UART::init(OBD_PROTOCOLS protocol)
{
const char *initcmd[] = {"ATZ\r","ATE0\r","ATL1\r","0100\r"};
char buffer[64];
@@ -391,13 +406,13 @@ bool COBDUART::init(OBD_PROTOCOLS protocol)
return true;
}
-void COBDUART::end()
+void COBD2UART::end()
{
m_state = OBD_DISCONNECTED;
OBDUART.end();
}
-bool COBDUART::setBaudRate(unsigned long baudrate)
+bool COBD2UART::setBaudRate(unsigned long baudrate)
{
OBDUART.print("ATBR1 ");
OBDUART.print(baudrate);
@@ -411,7 +426,7 @@ bool COBDUART::setBaudRate(unsigned long baudrate)
#ifdef DEBUG
-void COBDUART::debugOutput(const char *s)
+void COBD2UART::debugOutput(const char *s)
{
DEBUG.print('[');
DEBUG.print(millis());
diff --git a/libraries/OBDUART/OBDUART.h b/libraries/OBD2UART/OBD2UART.h
index eb9fad5..618b8c6 100644
--- a/libraries/OBDUART/OBDUART.h
+++ b/libraries/OBD2UART/OBD2UART.h
@@ -95,10 +95,10 @@ typedef enum {
uint16_t hex2uint16(const char *p);
uint8_t hex2uint8(const char *p);
-class COBDUART
+class COBD2UART
{
public:
- COBDUART():dataMode(1),errors(0),m_state(OBD_DISCONNECTED) {}
+ COBD2UART():dataMode(1),errors(0),m_state(OBD_DISCONNECTED) {}
// begin serial UART
virtual void begin();
// initialize OBD-II connection
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);
+ }
+}