summaryrefslogtreecommitdiff
path: root/libraries/OBD2UART/examples
diff options
context:
space:
mode:
authorStanley Huang <stanleyhuangyc@live.com>2018-04-11 11:38:05 +1000
committerStanley Huang <stanleyhuangyc@live.com>2018-04-11 11:38:05 +1000
commit47b642a6e06e05d17b70c2369af5acbc2ae9daf2 (patch)
treeb39c3f994a28862463c0ac04f0fddb81cc6f18ba /libraries/OBD2UART/examples
parent26196c5808a0c1dd2fba7e357d9a7ba8aa7037d7 (diff)
parentda6126b1c37ed3aa8e095a1dcc8509d4537c4819 (diff)
download2021-arduino-obd-47b642a6e06e05d17b70c2369af5acbc2ae9daf2.tar.gz
2021-arduino-obd-47b642a6e06e05d17b70c2369af5acbc2ae9daf2.tar.bz2
2021-arduino-obd-47b642a6e06e05d17b70c2369af5acbc2ae9daf2.zip
Merge branch 'master' of https://github.com/stanleyhuangyc/ArduinoOBD
Diffstat (limited to 'libraries/OBD2UART/examples')
-rw-r--r--libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino81
-rw-r--r--libraries/OBD2UART/examples/orientation_demo/orientation_demo.ino97
-rw-r--r--libraries/OBD2UART/examples/rpm_led_uart/rpm_led_uart.ino9
3 files changed, 151 insertions, 36 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
index 9b11eab..4d30b0e 100644
--- a/libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino
+++ b/libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino
@@ -1,25 +1,32 @@
/*************************************************************************
-* Testing sketch for Freematics OBD-II UART Adapter
+* Testing sketch for Freematics OBD-II UART Adapter V1/V2/V2.1
+* Performs AT command-set test
* Reads and prints several OBD-II PIDs value
-* Distributed under GPL v2.0
-* Visit http://freematics.com for more information
-* Written by Stanley Huang <support@freematics.com.au>
+* Reads and prints motion sensor data if available
+* Distributed under BSD
+* Visit https://freematics.com/products for more product information
+* Written by Stanley Huang <stanley@freematics.com.au>
*************************************************************************/
-#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 uses to Serial1
-// On Arduino UNO and those have no Serial1, we use software serial for output
-// as OBD-II UART adapter uses to Serial
+// On Arduino Leonardo, Micro, MEGA or DUE, hardware serial can be used for output as the adapter occupies Serial1
+// On Arduino UNO and those have no Serial1, we use software serial for output as the adapter uses Serial
+#ifdef ARDUINO_AVR_UNO
+#include <SoftwareSerial.h>
SoftwareSerial mySerial(A2, A3);
-//#define mySerial Serial
+#else
+#define mySerial Serial
+#endif
+
+#if defined(ESP32) && !defined(Serial1)
+HardwareSerial Serial1(1);
+#endif
COBD obd;
bool hasMEMS;
-void testOut()
+void testATcommands()
{
static const char cmds[][6] = {"ATZ\r", "ATI\r", "ATH0\r", "ATRV\r", "0100\r", "010C\r", "0902\r"};
char buf[128];
@@ -93,11 +100,12 @@ void readBatteryVoltage()
void readMEMS()
{
- int acc[3];
- int gyro[3];
- int temp;
+ int16_t acc[3] = {0};
+ int16_t gyro[3] = {0};
+ int16_t mag[3] = {0};
+ int16_t temp = 0;
- if (!obd.memsRead(acc, gyro, 0, &temp)) return;
+ if (!obd.memsRead(acc, gyro, mag, &temp)) return;
mySerial.print('[');
mySerial.print(millis());
@@ -117,6 +125,13 @@ void readMEMS()
mySerial.print('/');
mySerial.print(gyro[2]);
+ mySerial.print(" MAG:");
+ mySerial.print(mag[0]);
+ mySerial.print('/');
+ mySerial.print(mag[1]);
+ mySerial.print('/');
+ mySerial.print(mag[2]);
+
mySerial.print(" TEMP:");
mySerial.print((float)temp / 10, 1);
mySerial.println("C");
@@ -127,23 +142,24 @@ void setup()
mySerial.begin(115200);
while (!mySerial);
- // this will begin serial
- byte version = obd.begin();
-
- mySerial.print("Freematics OBD-II Adapter ");
- if (version > 0) {
- mySerial.print("Ver. ");
- mySerial.print(version / 10);
- mySerial.print('.');
- mySerial.println(version % 10);
- } else {
- mySerial.println("not detected");
- for (;;);
+ for (;;) {
+ delay(1000);
+ byte version = obd.begin();
+ mySerial.print("Freematics OBD-II Adapter ");
+ if (version > 0) {
+ mySerial.println("detected");
+ mySerial.print("OBD firmware version ");
+ mySerial.print(version / 10);
+ mySerial.print('.');
+ mySerial.println(version % 10);
+ break;
+ } else {
+ mySerial.println("not detected");
+ }
}
- delay(1000);
// send some commands for testing and show response for debugging purpose
- testOut();
+ testATcommands();
hasMEMS = obd.memsInit();
mySerial.print("MEMS:");
@@ -151,8 +167,9 @@ void setup()
// initialize OBD-II adapter
do {
- mySerial.println("Init...");
+ mySerial.println("Connecting...");
} while (!obd.init());
+ mySerial.println("OBD connected!");
char buf[64];
if (obd.getVIN(buf, sizeof(buf))) {
@@ -160,7 +177,7 @@ void setup()
mySerial.println(buf);
}
- unsigned int codes[6];
+ uint16_t codes[6];
byte dtcCount = obd.readDTC(codes, 6);
if (dtcCount == 0) {
mySerial.println("No DTC");
@@ -176,7 +193,6 @@ void setup()
delay(5000);
}
-
void loop()
{
readPIDSingle();
@@ -186,3 +202,4 @@ void loop()
readMEMS();
}
}
+
diff --git a/libraries/OBD2UART/examples/orientation_demo/orientation_demo.ino b/libraries/OBD2UART/examples/orientation_demo/orientation_demo.ino
new file mode 100644
index 0000000..e04949c
--- /dev/null
+++ b/libraries/OBD2UART/examples/orientation_demo/orientation_demo.ino
@@ -0,0 +1,97 @@
+/*************************************************************************
+* Testing sketch for Freematics OBD-II UART Adapter V2.1
+* Reads and prints motion sensor data and computed orientation data
+* Distributed under BSD
+* Visit https://freematics.com/products for more product information
+* Written by Stanley Huang <stanley@freematics.com.au>
+*************************************************************************/
+
+#include <OBD2UART.h>
+
+// On Arduino Leonardo, Micro, MEGA or DUE, hardware serial can be used for output as the adapter occupies Serial1
+// On Arduino UNO and those have no Serial1, we use software serial for output as the adapter uses Serial
+#ifdef ARDUINO_AVR_UNO
+#include <SoftwareSerial.h>
+SoftwareSerial mySerial(A2, A3);
+#else
+#define mySerial Serial
+#endif
+
+#if defined(ESP32) && !defined(Serial1)
+HardwareSerial Serial1(1);
+#endif
+
+COBD obd;
+
+void setup()
+{
+ mySerial.begin(115200);
+ while (!mySerial);
+
+ // this will begin serial
+
+ for (;;) {
+ delay(1000);
+ byte version = obd.begin();
+ mySerial.print("Freematics OBD-II Adapter ");
+ if (version > 0) {
+ mySerial.println("detected");
+ break;
+ } else {
+ mySerial.println("not detected");
+ }
+ }
+
+ // initialize MEMS with sensor fusion enabled
+ bool hasMEMS = obd.memsInit(true);
+ mySerial.print("Motion sensor is ");
+ mySerial.println(hasMEMS ? "present" : "not present");
+ if (!hasMEMS) {
+ for (;;) delay(1000);
+ }
+}
+
+
+void loop()
+{
+ int16_t acc[3] = {0};
+ int16_t gyro[3] = {0};
+ int16_t mag[3] = {0};
+
+ if (!obd.memsRead(acc, gyro, mag)) return;
+
+ mySerial.print("ACC:");
+ mySerial.print(acc[0]);
+ mySerial.print('/');
+ mySerial.print(acc[1]);
+ mySerial.print('/');
+ mySerial.print(acc[2]);
+
+ mySerial.print(" GYRO:");
+ mySerial.print(gyro[0]);
+ mySerial.print('/');
+ mySerial.print(gyro[1]);
+ mySerial.print('/');
+ mySerial.print(gyro[2]);
+
+ mySerial.print(" MAG:");
+ mySerial.print(mag[0]);
+ mySerial.print('/');
+ mySerial.print(mag[1]);
+ mySerial.print('/');
+ mySerial.print(mag[2]);
+
+ mySerial.println();
+
+ float yaw, pitch, roll;
+ if (obd.memsOrientation(yaw, pitch, roll)) {
+ mySerial.print("Orientation: ");
+ mySerial.print(yaw, 2);
+ mySerial.print(' ');
+ mySerial.print(pitch, 2);
+ mySerial.print(' ');
+ mySerial.println(roll, 2);
+ }
+
+ delay(100);
+}
diff --git a/libraries/OBD2UART/examples/rpm_led_uart/rpm_led_uart.ino b/libraries/OBD2UART/examples/rpm_led_uart/rpm_led_uart.ino
index 3e80ae3..5b7a13e 100644
--- a/libraries/OBD2UART/examples/rpm_led_uart/rpm_led_uart.ino
+++ b/libraries/OBD2UART/examples/rpm_led_uart/rpm_led_uart.ino
@@ -1,8 +1,9 @@
/*************************************************************************
-* 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>
+* Testing sketch for Freematics OBD-II UART Adapter
+* Reads engine RPM data from OBD and triggers Arduino onboard LED
+* Distributed under BSD
+* Visit https://freematics.com/products for more product information
+* Written by Stanley Huang <stanley@freematics.com.au>
*************************************************************************/
#include <OBD2UART.h>