ESP32 with ELM327

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!

To see if it's the same because you suspect the 'meter' is bad ?

Is an ELM327 BT or BLE ? (Maybe your module does both?)

Yes, I know that the 'meter' in my car is "broken" because I can read the right coolant temperature when I connect the ELM327 to my phone with an OBDII app. My plan is to also show the engine rpm and some other sensors on the display, but I am seeing that as the next step.
I can't tell if the module can do both, BT and BLE but I think BLE is working fine.

I am at the same point as you trying to do it with BLE and I get the connection with the equipment but the data is not correct when reading, any news?

Hi,
I have not had the time yet to work further on it. But I will let you know when I am achieving any progress. At least I am not the only one struggling with sending and receiving.
I think I will buy an OBD2 simulator wich makes it easier to get a result and I don’t have to get in my car every time.
I can recommend you this video for some advice.
https://youtu.be/L3ufJTFX2v0?si=OISUzcPEOP1tAXQQ

Let me know if you’re making progress…

How were you able to get the uuid? The issue might be related to trying to acess an incorrect service. I've found in other forums that they were trying to acess 00001101-0000-1000-8000-00805F9B34FB, but when I tried to connect with my phone using a bluetooth terminal was only able to find FFF1, and FFF2. Not exactly sure which one should I use

Hey @Mattei were you able to solve that riddle? I'm also trying to connect my ELM but only see FFF1 and FFF2, same as you. Banging my head on the wall, since I don't understand it.