Bluetooth Serial Library

/*
To test connection, type the following into the serial monitor:
  AT Z
  AT E0
  AT S0
  AT AL
  AT TP A0
*/

#include "BluetoothSerial.h"


BluetoothSerial SerialBT;


#define DEBUG_PORT Serial
#define ELM_PORT   SerialBT


void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  DEBUG_PORT.begin(115200);
  ELM_PORT.begin("ESP32test", true);
  ELM_PORT.setPin("1234");

  DEBUG_PORT.println("Attempting to connect to ELM327...");

  if (!ELM_PORT.connect("OBDII"))
  {
    DEBUG_PORT.println("Couldn't connect to OBD scanner");
    while(1);
  }

  DEBUG_PORT.println("Connected to ELM327");
  DEBUG_PORT.println("Ensure your serial monitor line ending is set to 'Carriage Return'");
  DEBUG_PORT.println("Type and send commands/queries to your ELM327 through the serial monitor");
  DEBUG_PORT.println();
}


void loop()
{
  if(DEBUG_PORT.available())
  {
    char c = DEBUG_PORT.read();

    DEBUG_PORT.write(c);
    ELM_PORT.write(c);
  }

  if(ELM_PORT.available())
  {
    char c = ELM_PORT.read();

    if(c == '>')
      DEBUG_PORT.println();

    DEBUG_PORT.write(c);
  }
}
C:\Users\nbryant\AppData\Local\Temp\.arduinoIDE-unsaved2024712-10340-1154hjz.u4v6\ESP32_test\ESP32_test.ino: In function 'void setup()':
C:\Users\nbryant\AppData\Local\Temp\.arduinoIDE-unsaved2024712-10340-1154hjz.u4v6\ESP32_test\ESP32_test.ino:27:12: error: 'class BluetoothSerial' has no member named 'setPin'
   27 |   ELM_PORT.setPin("1234");
      |            ^~~~~~

exit status 1

Compilation error: 'class BluetoothSerial' has no member named 'setPin'

I am having trouble connecting my generic ELM327 to my ESP32. I can connect to my laptop and send AT commands to the ELM device. However I do need to input a PIN "1234" to connect the laptop to the ELM. I have not been able to connect the ELM327 to my ESP32 at all. I have tried all the baud rates with the ESP32_test example with no success. I have also tried changing the "OBDII" to my ELM's mac address, I was able to attain my MAC address and device name with the BluetoothSerial.h example DiscoverConnect. I know the ESP32 is seeing the Bluetooth device but something is interfering with the paring process.

Welcome to the forum

The setPin() function definition of BluetoothSerial is as follows

  bool setPin(const char *pin, uint8_t pin_code_len);

Try adding the second parameter to your call to setPin()

I cannot guarantee that it will work, but worth a try

/*
To test connection, type the following into the serial monitor:
  AT Z
  AT E0
  AT S0
  AT AL
  AT TP A0
*/

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;
const char *pin = "1234";
uint8_t pin_code_len = strlen(pin);

#define DEBUG_PORT Serial
#define ELM_PORT   SerialBT


void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  DEBUG_PORT.begin(115200);
  ELM_PORT.begin("ESP32test", true);
  bool setPin(const char *pin, uint8_t pin_code_len); //ELM_PORT.setPin("1234");

  DEBUG_PORT.println("Attempting to connect to ELM327...");

  if (!ELM_PORT.connect("OBDII"))
  {
    DEBUG_PORT.println("Couldn't connect to OBD scanner");
    while(1);
  }

  DEBUG_PORT.println("Connected to ELM327");
  DEBUG_PORT.println("Ensure your serial monitor line ending is set to 'Carriage Return'");
  DEBUG_PORT.println("Type and send commands/queries to your ELM327 through the serial monitor");
  DEBUG_PORT.println();
}


void loop()
{
  if(DEBUG_PORT.available())
  {
    char c = DEBUG_PORT.read();

    DEBUG_PORT.write(c);
    ELM_PORT.write(c);
  }

  if(ELM_PORT.available())
  {
    char c = ELM_PORT.read();

    if(c == '>')
      DEBUG_PORT.println();

    DEBUG_PORT.write(c);
  }
}

still no connection to my device with this update. I have tried all the baud rates and clearing the paired Bluetooth devices from my esp32. I have opened up the case and am not opposed to hardwiring into the RX and TX but I cannot find a data sheet for the IC it is stamped AB23PB1U243-69A2.

Have you got a clean compile yet?

Yes, I am able to upload my sketches to my esp32. I have included my code in my reply to [UKHeliBob]

The ELM is BT "slave".
I believe ESP32 is too.
Check to see if it's configurable as "master" (perhaps with another library).

Ok, I got a clean compile, can see Serial output, but we can't test since you seem to have special devices. You need to replicate the problem using only readily available tools the volunteers have like Serial output, various boards, but I have no idea what an ELM327 is. Here is as far as I got, I assume you do too, so it looks like you are stuck if you can't simplify the problem.

1 Like

I have opened up the case and am not opposed to hardwiring into the RX and TX but I cannot find a data sheet for the IC it is stamped AB23PB1U243-69A2. As far as getting you more information on my ELM327 chipset I have only gotten as far as connecting to it with a laptop and sending the "AT I" command I get the return ELM327 v1.5, I have found https://www.sparkfun.com/datasheets/Widgets/ELM327_AT_Commands.pdf datasheet with the commands

The error msg is no such member in BluetoothSerial

#include "BluetoothSerial.h"
 
#define USE_NAME // Comment this to use MAC address instead of a slaveName
const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device
 
#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;
 
#ifdef USE_NAME
  String slaveName = "OBDII"; // Change this to reflect the real name of your slave BT device
#else
  String MACadd = "1c:a1:35:69:8d:c5"; // This only for printing
  uint8_t address[6]  = {0x1c, 0xa1, 0x35, 0x69, 0x8d, 0xc5}; // Change this to reflect real MAC address of your slave BT device
#endif
 
String myName = "ESP32-BT-Master";
 
void setup() {
  bool connected;
  Serial.begin(115200);
 
  SerialBT.begin(myName, true);
  Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
 
  #ifndef USE_NAME
    SerialBT.setPin(pin);
    Serial.println("Using PIN");
  #endif
 
  // connect(address) is fast (up to 10 secs max), connect(slaveName) is slow (up to 30 secs max) as it needs
  // to resolve slaveName 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
  #ifdef USE_NAME
    connected = SerialBT.connect(slaveName);
    Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
  #else
    connected = SerialBT.connect(address);
    Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd);
  #endif
 
  if(connected) {
    Serial.println("Connected Successfully!");
  } 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 up to 10 secs max
  if (SerialBT.disconnect()) {
    Serial.println("Disconnected Successfully!");
  }
  // This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
  SerialBT.connect();
  if(connected) {
    Serial.println("Reconnected Successfully!");
  } else {
    while(!SerialBT.connected(10000)) {
      Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
    }
  }
}
 
void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

I was not able to get a connection to my device with this example either.

I have an ELM327 BT device - but I'm not bold enough to plug it in.
(No guts, no glory, I guess, but if the sob took my car down - that'd suck.)

If you do not show the entire output, we are left to guess where it may have failed.

esp32 3.x seems to be troublesome in many ways (BLE, for one, that I know).
This isn't BLE, but going to Boards Manager and rolling back to 2.0.17 might be worthwhile.

You editing a code in #3 is completely incorrect. Why did you inserted a method prototype in the setup() ?

Do you understand the using an object methods in C++?

You only needed to add a second parameter to the method's call:

Was not the error message that the method setPin did not exist?

I set up a little test with an include, an object, and her is what happens when I start typing setPin. As you can see setPin does NOT exist!
Screenshot 2024-08-12 at 23.30.46

You should be correct. I can't test it myself because I'm on phone now.

In that case I wondered where @UKHeliBob found the method with two arguments as he stated in #2

setPin() exists conditionally. From arduino-esp32/libraries/BluetoothSerial/src/BluetoothSerial.h at master · espressif/arduino-esp32 · GitHub

#ifdef CONFIG_BT_SSP_ENABLED
  void enableSSP();
  void enableSSP(bool inputCapability, bool outputCapability);
  void disableSSP();
#else
  bool setPin(const char *pin, uint8_t pin_code_len);
#endif

There seems to have been a change; I've checked my installation of core 2.0.6 and it's not conditional and only has one argument

bool setPin(const char *pin);

I guess that the above answered that question :wink:

1 Like

Correct

That's nasty. What is CONFIG_BT_SSP_ENABLED ?