Hello readers, hope you all are doing well. This is our 6th tutorial of the ESP32 programming series. In the previous tutorial, we discussed the ESP32 BLE (Bluetooth Low Energy), its protocol, and testing with ESP32 and android phones. Bluetooth Classic is the basic version of BLE or you can also say that the Bluetooth classic is the predecessor of Bluetooth Low Energy.
ESP32 is equipped with multiple wireless communication features like Wi-Fi, Bluetooth low energy, Bluetooth classic etc. In this tutorial, we will discuss Bluetooth Classic in ESP32 and how to communicate data between ESP32 and smartphone using Arduino IDE.
What is Bluetooth Classic?
Bluetooth is a short-range communication (wireless) technology which makes wireless electronic device like mobile phones, computers, TVs, headphones, speakers etc. to communicate with each other over a short distance, approximately 15m. Bluetooth operates at 2.4GHz ISM band.
Bluetooth uses low energy radio waves are used for data communication between Bluetooth-enabled devices.
Bluetooth Evolution
- Bluetooth Classic was introduced in the late 1990s. The initial Bluetooth version (V1.0) was riddled with bugs and limitations.
- Bluetooth 2.0 was created as a result of various modifications and improvements to the basic version 1.0. Bluetooth 2.0’s most notable feature was the enhanced data rate (EDR). Fast modulation technology and a data rate of up to 3Mbps are used in Enhanced Data Rate mode.
- Despite improvements in the basic version, Bluetooth 2.0 lacks a security feature. Bluetooth 2.1 added a security feature called “Pairing” as well as a faster data rate.
- Another updated version, Bluetooth 3.0, included a Wi-Fi feature, but it was rarely used, and when it was, the features were similar to the Bluetooth 2.1 version.
- Bluetooth 4.0 was the first version to include the Bluetooth low energy feature (BLE). It is compatible with both BLE and Bluetooth classic.
- Finally, the most recent Bluetooth version is v5.2, which supports both Classic Bluetooth and BLE. Which consists of the following features:
- EATT (enhanced attribute protocol)
- LE (Low energy) power control feature (LEPCF)
- LE Audio
Bluetooth Network topology
- Classic Bluetooth forms a piconet. A piconet has single master and multiple(max 7) salves. Each piconet has its own hopping sequence.
Fig.: Classic Bluetooth Network topology
- Classic Bluetooth can operate on point to point as well point to multi-point network topology. In traditional Bluetooth, maximum 7 slave devices can be connected with the master Bluetooth at a time. Though, classic Bluetooth an connect with multiple nodes/slave devices at a time but it can exchange data with only single node at a time.
Bluetooth Clock
In classic Bluetooth the piconets are not synchronized.
The clock is one of the most important aspects of Bluetooth. In a Bluetooth connection, the master device has a clock that is used to split the time on each physical channel. Clocks on all slaves in a connection are synchronized to the master clock.
Bluetooth clock synchronization is essential because the radios must agree on when to transmit. Because Bluetooth uses precise timeslots for transmissions with devices alternating, if the clocks are not synchronized, there may be issues with devices transmitting at the incorrect time.
Classic Bluetooth Transmit power
It is defined in multiple classes:
- +20dBm maximum in class 1
- Up to +4dBm in Class 2.
- Up to + 0dBm in Class 3
Classic Bluetooth Data transmission modes
Generally, there are two data transmission modes:
- Basic Rate (BR) : BR is the first Bluetooth protocol which is implemented in Bluetooth v1.0. Is uses one of the FSK (frequency shift keying) modulation technique known as Gaussian frequency-shift keying (GFSK) and communicates data at 2.4 GHz ISM band.
Fig. 3: Bluetooth data transmission modes
- Enhanced Data Rate (EDR): It’s a Bluetooth specification that allows for a higher data rate or speed. It is not available in all Bluetooth versions, and its availability is dependent on Bluetooth version and profile. EDR uses pi/4-DQPSK (differential quadrature phase-shift keying) and 8DPSK (differential phase-shift keying) modulation techniques with data rate of 2Mbps and 3Mbps respectively.
Bluetooth packet format
- When two devices communicate data over Classic Bluetooth, then they use SPP (Serial Port Profile)
Fig. Bluetooth packet format
Enhanced data rate packet sends the Access code and header using the basic rate and this process uses GFSK (Gaussian Frequency Shift Keying). The guard gives the time to change the modulation to EDR modulation and then the synch word (64 bits), payload, Trailer (4 bits) bits are sent using EDR (enhanced data rate) modulation.
BLE vs Bluetooth Classic
Fig: BLE vs Classic Bluetooth
- Bandwidth: Bluetooth can send large amount of data than BLE which is capable of sharing only small chunks of data.
- Compatibility: The classic Bluetooth and BLE are not compatible with each other. Like if a Bluetooth device can discover or communicate with Bluetooth classic then the same device can’t communicate with BLE supported device.
But, a device having BT V4 (Bluetooth version 4) can discover both BLE and Classic Bluetooth devices.
- Power consumption: The classic Bluetooth consumes more power than BLE.
- Pairing: In Bluetooth classic pairing is necessary before sharing data between Bluetooth devices for security purpose. On the other hand BLE technology doesn’t ask for pairing before data transmission.
- Number of active devices or devices: In traditional Bluetooth, maximum 7 slave devices can be connected with the master Bluetooth at a time. Though classic Bluetooth an connect with multiple nodes/slave devices at a time but it can exchange data with only single node at a time.
Code for ESP32 Bluetooth classic
- We are using Arduino IDE as a compiler and upload into ESP32 module. To know more about Arduino IDE and how to use it, follow our previous tutorial i.e., on ESP32 programming series.
- This code will make ESP32’s Bluetooth to exchange data with android phone app serially.
#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(“TEP_ESP32_BT”); //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);
}
Code Description
- First of all, we need to add the required library from where we will call the functions required to enable Bluetooth and to make devices communicate.
- Next we need to check whether the Bluetooth configurations are enabled in SDK or not and if it is not then we need to recompile the SDK.
Next we need to create an object of class BluetoothSerial to initialize the Bluetooth stack and communicate serially over Bluetooth.
Arduino Setup() Function
- Inside the setup function first you have to initialize the serial monitor with a baud rate of 115200.
- Initialize the serial Bluetooth by calling SerialBT.begin() function. You also need to pass an argument inside the function as a name to ESP32’s Bluetooth or can use the default name.
- After initializing the device print the status on serial monitor.
Arduino Loop() Function
- Inside the loop function you can communicate data.
- If there is any serial input from the serial monitor [Serial.read()] then transfer that data over Bluetooth to mobile phone using SerialBT.write() function.
- If there is any data input over Bluetooth then write that data on Serial Monitor using Serial.write(SerialBT.read());
Testing/Working
For testing purpose go to the play store and download the app which supports classic Bluetooth communication. We are using the Serial Bluetooth Terminal app available on Google play store.
- Upload the code into the ESP32 module using Arduino IDE.
- After successfully the code open the Serial monitor with 115200 baud rate.
- Open the Serial Bluetooth Terminal app in your mobile phone.
- Turn on the Bluetooth.
- Mobile phone’s Bluetooth will scan for available Bluetooth (classic) devices.
- Then it will ask for pairing. Click on pair.
Fig: Pairing
- Connect to your ESP32 Bluetooth device (in our case it is named as “TEP_ESP32_BT”.
- Go to Devices in mobile phone’s Bluetooth app menu and select “TEP_ESP32_BT”.
- Now you are all set to read and write data over Bluetooth classic (serially).
You can read the data on serial monitor which has been transmitted from mobile phone.
This concludes today’s tutorial. Hope you find it helpful. In next tutorial we will discuss about ESP23’s interrupts and timers.
JLCPCB – Prototype 10 PCBs for $2 (For Any Color)
China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w
The post ESP32 Bluetooth Classic appeared first on The Engineering Projects.
No comments:
Post a Comment