ESP32 Essentials: Exploring the Power of Bluetooth Classic

by AKP in Circuits > Arduino

112 Views, 1 Favorites, 0 Comments

ESP32 Essentials: Exploring the Power of Bluetooth Classic

esp32-bluetooth-classic-tutorial.png

The ESP family of microcontrollers is commonly linked with WiFi, and this association is logical given their reputation as the preferred choice for swiftly and effortlessly connecting projects to the internet. Despite the spotlight being on WiFi functionality, it’s important to note that the ESP32 also comes equipped with Bluetooth. However, Bluetooth usage on the ESP32 is not as prevalent, even though it is a feature available. If you wish to explore Bluetooth capabilities on the ESP32, this tutorial serves as an excellent starting guide.

Regarding ESP32 Bluetooth

The ESP32 is equipped with dual-mode Bluetooth, supporting both Bluetooth Classic and Bluetooth Low Energy (BLE). Despite sharing certain fundamental aspects such as architecture and operating in the 2.4 GHz ISM (Industrial, Scientific, and Medical) band, these two protocols differ significantly.

Bluetooth Classic

Bluetooth Classic, known for its use in connecting smartphones to wireless earbuds, is the original Bluetooth technology. If you’ve ever utilized Bluetooth-to-Serial-Bridge modules like the HC-05 or HC-06 with an Arduino, you’ve inadvertently employed Bluetooth Classic.

Designed for continuous two-way data transfer with high application throughput (up to 3 Mbps), Bluetooth Classic is effective but limited to short distances. Operating over 79 channels in the 2.4 GHz ISM band, it is commonly employed in projects requiring continuous data streaming, such as audio streaming or file transfer.

This tutorial provides guidance on utilizing Bluetooth Classic on the ESP32.

Bluetooth Low Energy (BLE)

Bluetooth LE, initially marketed as Bluetooth Smart and commonly referred to as BLE, prioritizes very low power operation while maintaining a comparable communication range. However, BLE is more than just a low-power version of Bluetooth Classic. Operating in the same radio spectrum range but utilizing a different set of 40 channels at a lower data rate (up to 1 Mbps), BLE is well-suited for IoT projects where power consumption is a primary concern, such as projects requiring periodic device wake-up, sensor data gathering, Bluetooth transmission, and return to sleep mode.

As using ESP32 BLE is somewhat more intricate than using Bluetooth Classic, we’ve covered it in a separate tutorial.

Bluetooth Classic vs. BLE

While BLE is primarily utilized to conserve power, there are several crucial differences.

Power Consumption: Bluetooth Classic typically consumes more power (around 1 W), while BLE is designed for low power consumption (typically between 0.01 and 0.5 W), making it suitable for battery-powered devices requiring extended operation.

Data Transfer Rates: Bluetooth Classic provides higher data rates than BLE, making it suitable for projects requiring continuous and high-speed data transmission, while BLE is optimized for short bursts of data transmission.

Range: Both BLE and Classic can cover up to 100 meters, with the exact distance varying based on the environment and implementation.

Latency: Bluetooth Classic connections have a latency of up to 100 ms, while BLE connections have a latency of 6 ms, with lower values considered better.

Compatibility: Bluetooth Classic is more prevalent in older devices, while BLE is commonly used in modern smartphones and other gadgets.

Refer to the table below for a detailed comparison between BLE and Bluetooth Classic.

ESP32 Bluetooth Protocol Stack

ESP32-Bluetooth-Protocol-Stack.png

Before we move forward, it’s essential to comprehend the Bluetooth protocol stack, as it sheds light on the reasoning behind incorporating specific functions into our code.

In essence, the Bluetooth protocol stack is divided into two components: a “host stack” and a “controller stack.”

The host serves as the main CPU responsible for executing a Bluetooth stack. ESP-IDF currently supports two host stacks. The default BLUEDROID-based stack accommodates both Bluetooth Classic and BLE. Conversely, the Apache NimBLE-based stack is exclusively designed for BLE.

The controller stack manages the Bluetooth radio, essentially operating as a data pipeline: serial data received is transmitted through the Bluetooth connection, and data incoming from the Bluetooth side is forwarded to the host.

The VHCI interface (software-implemented virtual HCI interface) plays a pivotal role in facilitating communication between the host and the controller.

For more in-depth information, you can consult Espressif’s Bluetooth architecture description.

Bluetooth Serial Sketch

ESP32-Bluetooth-Classic-Output-Screenshot1.png

Now, we’ll create the code for the ESP32 to establish communication with our smartphone and enable data exchange.

To program the ESP32, we’ll use the Arduino IDE, so make sure you have the ESP32 add-on installed before proceeding:

  1. Open your Arduino IDE.
  2. Go to File > Examples > BluetoothSerial > SerialtoSerialBT.
  3. The relevant code should load.
#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

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#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);
}

Once you have uploaded the sketch, open the serial monitor at baud rate 115200. You should see a message saying: “The device started, now you can pair it with bluetooth!”.


ESP32 Project: Bluetooth-Controlled Relay

Wiring-Relay-Module-to-ESP32.jpg

Let’s create a simple project that lets you control relays wirelessly using Bluetooth. This can be useful for home automation, smart lighting, security systems, and other similar applications.

Wiring

Connect the relay module to the ESP32 as depicted in the following diagram. The diagram shows the wiring for a 2-channel relay module; wiring for a different number of channels is similar.

ESP32 Code

Once you are done with the wiring, try the below sketch.

#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

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

// GPIO where relay is connected to
const int relayPin = 5;

// Handle received messages
String message = "";

// Create BluetoothSerial object
BluetoothSerial SerialBT;

void setup() {
// Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(115200);

// Initialize relayPin as an output
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);

// Initialize the Bluetooth stack
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
if (SerialBT.available()){
char incomingChar = SerialBT.read();
if (incomingChar != '\n'){
message += String(incomingChar);
}
else{
message = "";
}
Serial.write(incomingChar);
}
// Check received message and control output accordingly
if (message == "on"){
digitalWrite(relayPin, LOW);
}
else if (message == "off"){
digitalWrite(relayPin, HIGH);
}
delay(20);
}



See Full Article Here