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.
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...?