Hello Guys,
I am currently trying to connect an ESP32 with my car and therefore using the ELM327 Bluetooth Adapter.
My cars coolant temperature gauge is not working as it's supposed to and thats why I want to display it on a small 128x64 display. I can get the esp32 to connect to the ELM327 but no matter what command I try to send I will always get the same answer from the ELM327 wich is " 4F 42 44 49 49
" and can be translated to "OBDII". Can someone try to help me find out why I get this response and how I can improve my code to run properly?
The PID for the coolant temperature is 01 05 .
I am new to microcontrollers and do a lot of coding with ChatGPT.
Here is my code.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
BLEClient* pClient;
BLERemoteService* pRemoteService;
BLERemoteCharacteristic* pRemoteCharacteristic;
BLEScan* pBLEScan;
BLEAdvertisedDevice* myDevice = nullptr;
void initializeDisplay()
{
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Initializing BLE...");
display.display();
}
void initializeBLE()
{
Serial.println("Initializing BLE...");
BLEDevice::init("");
pClient = BLEDevice::createClient();
pBLEScan = BLEDevice::getScan();
pBLEScan->setActiveScan(true);
Serial.println("BLE client initialized.");
}
void connectToELM327()
{
Serial.println("Starting scan for BLE devices...");
display.clearDisplay();
display.setCursor(0, 0);
display.println("Scanning for BLE...");
display.display();
BLEScanResults* foundDevices = pBLEScan->start(5);
bool elm327Found = false;
for (int i = 0; i < foundDevices->getCount(); i++)
{
BLEAdvertisedDevice device = foundDevices->getDevice(i);
if (device.getAddress().toString() == "1c:a1:35:69:8d:c5")
{
Serial.println("ELM327 found!");
display.println("ELM327 found!");
display.display();
myDevice = new BLEAdvertisedDevice(device);
elm327Found = true;
break;
}
}
if (!elm327Found)
{
Serial.println("ELM327 not found. Retrying in 2 seconds...");
display.println("ELM327 not found.");
display.display();
delay(2000);
return;
}
Serial.println("Trying to connect to ELM327...");
display.println("Connecting to ELM327...");
display.display();
if (!pClient->connect(myDevice))
{
Serial.println("Connection to ELM327 failed.");
display.println("Connection failed");
display.display();
delay(5000);
return;
}
Serial.println("Connected to ELM327!");
display.println("Connected!");
display.display();
pRemoteService = pClient->getService(BLEUUID("00001800-0000-1000-8000-00805f9b34fb"));
if (!pRemoteService)
{
Serial.println("OBD-II Service not found. Disconnecting.");
display.println("Service not found.");
display.display();
pClient->disconnect();
return;
}
pRemoteCharacteristic = pRemoteService->getCharacteristic(BLEUUID("00002a00-0000-1000-8000-00805f9b34fb"));
if (!pRemoteCharacteristic)
{
Serial.println("Characteristic not found. Disconnecting.");
display.println("Characteristic not found.");
display.display();
pClient->disconnect();
return;
}
Serial.println("Characteristic connected!");
display.println("Characteristic connected!");
display.display();
}
void sendCommand(String command)
{
Serial.print("Sending command: ");
Serial.println(command);
pRemoteCharacteristic->writeValue(command.c_str(), command.length());
delay(500);
if (pRemoteCharacteristic->canRead())
{
String response = pRemoteCharacteristic->readValue();
Serial.print("Response received: ");
Serial.println(response);
}
else
{
Serial.println("The characteristic cannot be read.");
}
}
void setup()
{
Serial.begin(115200);
initializeDisplay();
initializeBLE();
connectToELM327();
}
void loop()
{
if (!pClient->isConnected())
{
Serial.println("No connection to ELM327. Restarting...");
connectToELM327();
delay(5000);
return;
}
sendCommand("AT Z\r"); // Reset command
sendCommand("AT DP\r"); // Query protocol
String obdCommand = "01 05";
sendCommand(obdCommand);
delay(5000);
}
Best regards!