Simple Bluetooth with ESP32 - Help

Violet's picture

Offline

Joined: Aug 16, 2018

Replies: 10
Simple Bluetooth with ESP32 - Help
October 9, 2018 - 1:58pm

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

Aswinth Raj's picture

Offline

Joined: Aug 16, 2016

Replies: 778
Use BluetoothSerial Library
October 9, 2018 - 2:03pm

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 

Violet's picture

Offline

Joined: Aug 16, 2018

Replies: 10
BluetoothSerial Library nor working for ESP32
October 9, 2018 - 2:11pm

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

Aswinth Raj's picture

Offline

Joined: Aug 16, 2016

Replies: 778
Remove ESP32 board from Arduino and install again
October 9, 2018 - 2:18pm

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

Violet's picture

Offline

Joined: Aug 16, 2018

Replies: 10
Installing though board manager solved the problem
October 9, 2018 - 2:22pm

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