include "BluetoothSerial.h"
#include "ELMduino.h"
/*
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif
*/
BluetoothSerial SerialBT;
ELM327 myELM327;
uint32_t rpm = 0;
char CONVERTED_RPM[32];
char CONVERTED_KPH[32];
char CONVERTED_ENGINETEMP[32];
char CONVERTED_INTAKETEMP[32];
/*
String MACadd = "AA:BB:CC:11:22:33";
uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33};
//uint8_t address[6] = {0x00, 0x1D, 0xA5, 0x02, 0xC3, 0x22};
*/
String name = "OBDII";
const char *pin = "1234"; //<- standard pin would be provided by default
bool connected;
void setup() {
Serial.begin(115200);
//SerialBT.setPin(pin);
SerialBT.begin("ESP32-test", true);
myELM327.begin(SerialBT, true, 2000);
//SerialBT.setPin(pin);
Serial.println("The device started in master mode, make sure remote BT device is on!");
// connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs
// to resolve name to address first, but it allows to connect to different devices with the same name.
// Set CoreDebugLevel to Info to view devices bluetooth address and device names
connected = SerialBT.connect(name);
//connected = SerialBT.connect(address);
if(connected) {
Serial.println("Connected Succesfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
// disconnect() may take upto 10 secs max
/*
if (SerialBT.disconnect()) {
Serial.println("Disconnected Succesfully!");
}
*/
// this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
//SerialBT.connect();
}
void loop()
{
float tempRPM = myELM327.rpm();
if (myELM327.nb_rx_state == ELM_SUCCESS)
{
rpm = (uint32_t)tempRPM;
sprintf(CONVERTED_RPM, "%d", rpm);
Serial.print("RPM: "); Serial.println(CONVERTED_RPM);
}
else if (myELM327.nb_rx_state != ELM_GETTING_MSG){
myELM327.printError();
}
int32_t kph = myELM327.kph();
if (myELM327.nb_rx_state == ELM_SUCCESS)
{
sprintf(CONVERTED_KPH, "%d", myELM327.kph());
Serial.print("Km/h: "); Serial.println(CONVERTED_KPH);
}
else if (myELM327.nb_rx_state != ELM_GETTING_MSG){
myELM327.printError();
}
delay(500);
}
serial monitor result
The output on the serial monitor is:
04:40:23.289 -> Service: 1
04:40:23.289 -> PID: 12
04:40:23.289 -> Normal length query detected
04:40:23.289 -> Query string: 010C1
04:40:23.289 -> Clearing input serial buffer
04:40:23.336 -> Sending the following command/query: 010C1
04:40:23.808 -> Received char: 0
04:40:24.321 -> Received char: 1
04:40:24.790 -> Received char: 0
04:40:25.304 -> Received char: C
04:40:25.815 -> Received char: 1
04:40:26.329 -> Received char: \r
04:40:26.796 -> Received char: 4
04:40:27.310 -> Received char: 1
04:40:27.825 -> Received char: _
04:40:28.295 -> Received char: 0
04:40:28.811 -> Received char: C
04:40:29.324 -> Received char: _
04:40:29.792 -> Received char: 1
04:40:30.305 -> Received char: 6
04:40:30.817 -> Received char: _
04:40:31.331 -> Received char: 0
04:40:31.796 -> Received char: A
04:40:32.310 -> Received char: _
04:40:32.821 -> Received char: \r
04:40:33.333 -> Received char: \r
04:40:33.799 -> Received char: >
04:40:33.799 -> Delimiter found.
04:40:33.799 -> All chars received: 010C1410C160A
04:40:33.799 -> Expected response header: 410C
04:40:33.799 -> Single response detected
04:40:33.846 -> 64-bit response:
04:40:33.846 -> responseByte_0: 10
04:40:33.846 -> responseByte_1: 22
04:40:33.846 -> responseByte_2: 0
04:40:33.846 -> responseByte_3: 0
04:40:33.846 -> responseByte_4: 0
04:40:33.846 -> responseByte_5: 0
04:40:33.846 -> responseByte_6: 0
04:40:33.846 -> responseByte_7: 0
04:40:33.846 -> Service: 1
04:40:33.846 -> PID: 13
04:40:33.846 -> Normal length query detected
04:40:33.846 -> Query string: 010D1
04:40:33.846 -> Clearing input serial buffer
04:40:33.846 -> Sending the following command/query: 010D1
04:40:33.846 -> RPM: 1410
04:40:33.846 -> Km/h: 0
04:40:34.359 -> Received char: 0
04:40:34.824 -> Received char: 1
04:40:35.337 -> Received char: 0
04:40:35.834 -> Received char: D
04:40:36.346 -> Received char: 1
04:40:36.860 -> Received char: \r
04:40:37.330 -> Received char: 4
04:40:37.858 -> Received char: 1
04:40:38.364 -> Received char: _
04:40:38.833 -> Received char: 0
04:40:39.345 -> Received char: D
04:40:39.858 -> Received char: _
04:40:40.325 -> Received char: 0
04:40:40.838 -> Received char: 0
04:40:41.358 -> Received char: _
04:40:41.826 -> Received char: \r
04:40:42.339 -> Received char: \r
04:40:42.852 -> Received char: >
04:40:42.852 -> Delimiter found.
04:40:42.852 -> All chars received: 010D1410D00
04:40:42.852 -> Expected response header: 410D
04:40:42.852 -> Single response detected
04:40:42.852 -> 64-bit response:
04:40:42.852 -> responseByte_0: 0
04:40:42.852 -> responseByte_1: 0
04:40:42.852 -> responseByte_2: 0
04:40:42.852 -> responseByte_3: 0
04:40:42.852 -> responseByte_4: 0
04:40:42.852 -> responseByte_5: 0
04:40:42.852 -> responseByte_6: 0
04:40:42.852 -> responseByte_7: 0
04:40:42.852 -> Service: 1
04:40:42.852 -> PID: 13
04:40:42.852 -> Normal length query detected
04:40:42.852 -> Query string: 010D1
04:40:42.899 -> Clearing input serial buffer
04:40:42.899 -> Sending the following command/query: 010D1
04:40:42.899 -> RPM: 0
04:40:42.899 -> Km/h: 0
04:40:43.367 -> Received char: 0
04:40:43.880 -> Received char: 1
04:40:44.395 -> Received char: 0
04:40:44.863 -> Received char: D
04:40:45.377 -> Received char: 1
04:40:45.850 -> Received char: \r
04:40:46.368 -> Received char: 4
04:40:46.884 -> Received char: 1
04:40:47.351 -> Received char: _
04:40:47.864 -> Received char: 0
04:40:48.377 -> Received char: D
04:40:48.889 -> Received char: _
04:40:49.355 -> Received char: 0
04:40:49.870 -> Received char: 0
04:40:50.385 -> Received char: _
04:40:50.855 -> Received char: \r
04:40:51.369 -> Received char: \r
04:40:51.885 -> Received char: >
04:40:51.885 -> Delimiter found.
04:40:51.885 -> All chars received: 010D1410D00
04:40:51.885 -> Expected response header: 410D
04:40:51.885 -> Single response detected
04:40:51.885 -> 64-bit response:
04:40:51.885 -> responseByte_0: 0
04:40:51.885 -> responseByte_1: 0
04:40:51.885 -> responseByte_2: 0
04:40:51.885 -> responseByte_3: 0
04:40:51.885 -> responseByte_4: 0
04:40:51.885 -> responseByte_5: 0
04:40:51.885 -> responseByte_6: 0
04:40:51.885 -> responseByte_7: 0
04:40:51.885 -> Service: 1
04:40:51.885 -> PID: 13
04:40:51.885 -> Normal length query detected
04:40:51.885 -> Query string: 010D1
04:40:51.885 -> Clearing input serial buffer
04:40:51.932 -> Sending the following command/query: 010D1
04:40:51.932 -> RPM: 0
04:40:51.932 -> Km/h: 0
04:40:52.400 -> Received char: 0
04:40:52.913 -> Received char: 1
04:40:53.383 -> Received char: 0
04:40:53.895 -> Received char: D
04:40:54.408 -> Received char: 1
04:40:54.923 -> Received char: \r
04:40:55.389 -> Received char: 4
04:40:55.905 -> Received char: 1
04:40:56.419 -> Received char: _
04:40:56.886 -> Received char: 0
04:40:57.400 -> Received char: D
04:40:57.915 -> Received char: _
04:40:58.383 -> Received char: 0
04:40:58.900 -> Received char: 0
04:40:59.415 -> Received char: _
04:40:59.885 -> Received char: \r
04:41:00.397 -> Received char: \r
04:41:00.911 -> Received char: >
04:41:00.911 -> Delimiter found.
04:41:00.911 -> All chars received: 010D1410D00
04:41:00.911 -> Expected response header: 410D
04:41:00.911 -> Single response detected
04:41:00.911 -> 64-bit response:
04:41:00.911 -> responseByte_0: 0
04:41:00.911 -> responseByte_1: 0
04:41:00.911 -> responseByte_2: 0
04:41:00.911 -> responseByte_3: 0
04:41:00.911 -> responseByte_4: 0
04:41:00.911 -> responseByte_5: 0
04:41:00.911 -> responseByte_6: 0
04:41:00.911 -> responseByte_7: 0
04:41:00.911 -> Service: 1
04:41:00.911 -> PID: 13
04:41:00.911 -> Normal length query detected
04:41:00.958 -> Query string: 010D1
04:41:00.958 -> Clearing input serial buffer
04:41:00.958 -> Sending the following command/query: 010D1
04:41:00.958 -> RPM: 0
04:41:00.958 -> Km/h: 0
04:41:01.427 -> Received char: 0
04:41:01.942 -> Received char: 1
04:41:02.454 -> Received char: 0
04:41:02.923 -> Received char: D
04:41:03.436 -> Received char: 1
04:41:03.951 -> Received char: \r
04:41:04.418 -> Received char: 4
04:41:04.931 -> Received char: 1
04:41:05.444 -> Received char: _
04:41:05.917 -> Received char: 0
04:41:06.431 -> Received char: D
04:41:06.949 -> Received char: _
04:41:07.418 -> Received char: 0
04:41:07.931 -> Received char: 0
04:41:08.449 -> Received char: _
04:41:08.915 -> Received char: \r
04:41:09.431 -> Received char: \r
04:41:09.944 -> Received char: >
04:41:09.944 -> Delimiter found.
04:41:09.944 -> All chars received: 010D1410D00
04:41:09.944 -> Expected response header: 410D
04:41:09.944 -> Single response detected
04:41:09.944 -> 64-bit response:
04:41:09.944 -> responseByte_0: 0
04:41:09.944 -> responseByte_1: 0
04:41:09.944 -> responseByte_2: 0
04:41:09.944 -> responseByte_3: 0
04:41:09.944 -> responseByte_4: 0
04:41:09.944 -> responseByte_5: 0
04:41:09.944 -> responseByte_6: 0
04:41:09.944 -> responseByte_7: 0
04:41:09.944 -> Service: 1
04:41:09.944 -> PID: 13
04:41:09.944 -> Normal length query detected
04:41:09.990 -> Query string: 010D1
04:41:09.990 -> Clearing input serial buffer
04:41:09.990 -> Sending the following command/query: 010D1
04:41:09.990 -> RPM: 0
04:41:09.990 -> Km/h: 0
Although I understand that the km/h should read 0 since the vehicle is stationary, I don't understand why the PID 010D keeps repeating.
(why only just repeat 010D PID?) Is this an issue with the ELM327?
I would appreciate it if you could explain the cause and provide a solution
Please help me