Simple Bluetooth with ESP32 - Help

Submitted by Violet on Tue, 10/09/2018 - 13:58

Hi,

I have used ESP32 ling time back and now I have a chance to use it again. My project requires both Wi-Fi and Bluetooth to work to gather so I thought ESP32 would be a great choice. But only after digging deep inside I understood that ESP32 has BLE and it is very different from normal Serial Bluetooth Hardware like HC-05 or HC-06.

I just want a simple program using which I can toggle an LED from ESP32, I should be able to built on top of it from there. Can someone guide me on getting started with Bluetooth using ESP32

Hi Violet,

Do not worry, you do not have to use BLE if your project does not require it. ESP32 supports both Bluetooth Low Energy (BLE) and Classic Bluetooth.

The one that you use with HC-05 is classic Bluetooth, so to use classic Bluetooth with ESP32 you can make use of the BluetoothSerial library from espressif itself. Once you include the library you can code it similar to using HC-05. I am pasting the example sketch below 

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}

If you have installed the ESP32 support for Arduino recently then this library should have been added by default 

  Joined August 16, 2016      998
Tuesday at 12:29 AM

Hi Aswinth, Thanks for the quick reply.

I have added the board details to Arduino long before, so I did not have that library installed by default. So I downloaded the folder from GIT and pasted in my lib location and I still get errors. 

The error occurs with BluetoothSerial Library only other example programs like BLE_notify BLE_server etc are working properly and I am even able to connect and pair with the ESP32 from my phone

  Joined August 16, 2018      42
Thursday at 11:31 AM

Hi Violet,

The reason for the problem is that you are using the old board details for ESP32. Now Espriff has added its support to Arduino IDE so you can directly install the board from Arduino Board Manager. 

I suppose that you have used GIT to install ESP32 board earlier

  Joined August 16, 2016      998
Tuesday at 12:29 AM

Hi Aswinth Thanks a ton...

Yes I did install using the GIT software. So, I removed the espriff folder from hardware folder in Arduino directory and installed the ESP32 board from Board manager in Arduino.

Then after selecting the ESP32 Dev Board using Toold menu, I found that there was an example program with BluetoothSerial Library I just uploaded and viola it works like a charm.

I am also happy that ESP32 can send and receive strings on the whole unlike HC-05 which can send only one char at a time the library is awesome. Also the uploading time has also improved it is much faster now than the old GIT style.

Thanks again..

  Joined August 16, 2018      42
Thursday at 11:31 AM