Activate an LED when classic bluetooth is connected

Hey everyone, just trying to turn ON an LED when there is a connection to classic bluetooth on esp32, can anyone tell me whats wrong with this code? Not my code however

#include "BluetoothSerial.h"

#define LED2  2 // Built-in LED

// Bluetooth Serial object
BluetoothSerial SerialBT;


// Bt_Status callback function
void Bt_Status (esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {

  if (event == ESP_SPP_SRV_OPEN_EVT) {
    Serial.println ("Client Connected");
    digitalWrite (LED2, HIGH);
    // Do stuff if connected
  }

  else if (event == ESP_SPP_CLOSE_EVT ) {
    Serial.println ("Client Disconnected");
    digitalWrite (LED2, LOW);
    // Do stuff if not connected
  }
}


void setup() {
  Serial.begin (115200);
  pinMode (LED2, OUTPUT);

  // Define the Bt_Status callback
  SerialBT.register_callback (Bt_Status);

  SerialBT.begin ("ESP32");

  Serial.println ();
  Serial.println ("Client Disconnected");
}


void loop() {
  // put your main code here, to run repeatedly:

}

What does the Serial Monitor output look like? Does it tell you a client has been connected?
Did you try the basic blink sketch to see if your ESP32 actually has an LED and it blinks?

I have an esp 32 development board, and my led2 is blue and on pin 19
#define LED2  19 // Built-in LED

The led turns on when LOW and off when HIGH.

When I modify your sketch these changes, the led goes on and off with connect/disconnect.

What exp32 arduino core are you using? There was an issue with 1.05 and BT connect and disconnect without restarting the esp 32. Use 1.0.6 from the board manager.

No the serial doesn't say client connected when I connect a phone to it

I've even tried changing the pin to pin 0 and tested pin 0 with a voltmeter and pin 0 never goes high when connected.

Yes my esp32 board does have an LED on pin 2, its not very common board, the looks like this:

Add thing I find is that when I upload this sketch ESP32 asks me for a security PIN to connect to it but this sketch has nothing on it about a security PIN in the code. My other last sketch that was on it does have a security PIN though...?

Your sketch works fine for me on a WEMOS D1 MINI ESP32
I changed it to use LED_BUILTIN but that is defined as pin 2 anyway

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.