Hi, I'm new with esp32. I want to make a project when my ESP32 bluetooth disconnected from my handphone my buzzer will be ring, but when my handphone connected to ESP32 bluetooth it stop ringing. How to make it so?
Heres my code before
#include "BluetoothSerial.h"
#include "esp_gap_bt_api.h"
BluetoothSerial ESP_BT; //Object for Bluetooth
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run make menuconfig
to and enable it
#endif
int incoming;
int pinbuzzer = 18;
void setup() {
Serial.begin(115200); //Start Serial monitor in 9600
ESP_BT.begin("Demensia"); //Name of your Bluetooth Signal
Serial.println("Bluetooth Device is Ready to Pair");
ledcSetup(0,1E5,12);
ledcAttachPin(18,0);
// esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
// esp_bt_pin_code_t pin_code;
// pin_code[0] = '1';
// pin_code[1] = '1';
// pin_code[2] = '1';
// pin_code[3] = '1';
// esp_bt_gap_set_pin(pin_type, 4, pin_code);
}
void loop() {
if (ESP_BT.available()) //Check if we receive anything from Bluetooth
{
label_1:
incoming = ESP_BT.read(); //Read what we recevive
Serial.print("Received:"); Serial.println(incoming);
if (incoming == 49)
{
ledcWriteTone(0,1000);
ESP_BT.println("Buzzer ON");
goto label_1;
}
if (incoming == 48)
{
ledcWriteTone(0,0);
ESP_BT.println("Buzzer OFF");
goto label_1;
}
}
else{
ledcWriteTone(0,1000);
}
delay(20);
}
First you need to read this about how to post your code on the forum
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966
If I understand your question correctly, you will need to add something like this to your loop().
if (ESP_BT.connected())
{
//code to turn buzzer off
}
else
{
//code to turn buzzer on
}
first of all im sorry i didnt know how to post correctly
when im using your code, class bluetoothserial has no member named 'connected' why is it ?
Please post the complete code you tried.
A review of the library certainly indicates that .connected() is a supported function
https://github.com/espressif/arduino-esp32/tree/master/libraries/BluetoothSerial
Heres my complete code i tried
void setup() {
Serial.begin(115200); //Start Serial monitor in 9600
ESP_BT.begin("Demensia"); //Name of your Bluetooth Signal
Serial.println("Bluetooth Device is Ready to Pair");
ledcSetup(0,1E5,12);
ledcAttachPin(18,0);
// esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
// esp_bt_pin_code_t pin_code;
// pin_code[0] = '1';
// pin_code[1] = '1';
// pin_code[2] = '1';
// pin_code[3] = '1';
// esp_bt_gap_set_pin(pin_type, 4, pin_code);
}
void loop() {
if(ESP_BT.connected()){
if (ESP_BT.available()) //Check if we receive anything from Bluetooth
{
incoming = ESP_BT.read(); //Read what we recevive
Serial.print("Received:"); Serial.println(incoming);
if (incoming == 49)
{
ledcWriteTone(0,1000);
ESP_BT.println("Buzzer ON");
}
if (incoming == 48)
{
ledcWriteTone(0,0);
ESP_BT.println("Buzzer OFF");
}
}
}
else{
ledcWriteTone(0,1000);
}
delay(20);
}
my current version of esp32 is 1.0.1
Heres my complete code
You are missing he #include for the library, the object instantiation, and some variables. This is what you you had in the improperly posted code. hen I add it to what you posted, it compiles.
You need to learn how to read the error message the compiler gives you. In "Preferences" turn on the verbose output during compilation.
#include "BluetoothSerial.h"
#include "esp_gap_bt_api.h"
BluetoothSerial ESP_BT; //Object for Bluetooth
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
int incoming;
int pinbuzzer = 18;
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.