ESP32 - Bluetooth Classic

The ESP32 comes with Wi-Fi, Bluetooth Low Energy and Bluetooth Classic. In this tutorial, you’ll learn how to use ESP32 Bluetooth Classic with Arduino IDE to exchange data between an ESP32 and an Android smartphone.

Video Tutorial

you can watch this video tutorial

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Android Smartphone with Bluetooth
1×Jumper Wires

Bluetooth Terminal Application

To proceed with this tutorial, you need a Bluetooth Terminal application installed in your smartphone.

We recommend using the Android app “Serial Bluetooth Terminal” available in the Play Store.

ESP32 Code

Serial to Serial Bluetooth

We'll program the ESP32 using Arduino IDE, so make sure you have the ESP32 add-on installed before proceeding

Open your Arduino IDE, and go to File > Examples > BluetoothSerial > SerialtoSerialBT.

The following code should load.

Copy

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#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);
}            

Code Explanation

  • This code establishes a two-way serial Bluetooth communication between two devices.
  • The code starts by including the BluetoothSerial library.
  • #include "BluetoothSerial.h"
  • The next three lines check if Bluetooth is properly enabled.
  • #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
    #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
    #endif
  • Then, create an instance of BluetoothSerial called SerialBT:
  • BluetoothSerial SerialBT;
  • In the setup() initialize a serial communication at a baud rate of 115200.
  • Initialize the Bluetooth serial device and pass as an argument the Bluetooth Device name. By default it’s called ESP32test but you can rename it and give it a unique name.
  • SerialBT.begin("ESP32test"); //Bluetooth device name
  • In the loop(), send and receive data via Bluetooth Serial.
  • In the first if statement, we check if there are bytes being received in the serial port. If there are, send that information via Bluetooth to the connected device.
  • if (Serial.available()) {
    SerialBT.write(Serial.read());
    }
  • SerialBT.write() sends data using bluetooth serial.
  • Serial.read() returns the data received in the serial port.
  • The next if statement, checks if there are bytes available to read in the Bluetooth Serial port. If there are, we’ll write those bytes in the Serial Monitor.
  • if (SerialBT.available()) {
       Serial.write(SerialBT.read());
    }

It will be easier to understand exactly how this sketch works in the demonstration.

Testing the Code

  • Upload the previous code to the ESP32. Make sure you have the right board and COM port selected.
  • The code starts by including the BluetoothSerial library.
  • This code establishes a two-way serial Bluetooth communication between two devices.
  • After uploading the code, open the Serial Monitor at a baud rate of 115200. Press the ESP32 Enable button.
  • After a few seconds, you should get a message saying: “The device started, now you can pair it with bluetooth!”.
  • ESP32 Button LED Wiring Diagram
  • Go to your smartphone and open the “Serial Bluetooth Terminal” app. Make sure you’ve enable your smartphone’s Bluetooth.
  • To connect to the ESP32 for the first time, you need to pair a new device. Go to Devices.
  • ESP32 Button LED Wiring Diagram
  • Click the settings icon, and select Pair new device. You should get a list with the available Bluetooth devices, including the ESP32test. Pair with the ESP32test.
  • ESP32 Button LED Wiring Diagram
  • Then, go back to the Serial Bluetooth Terminal. Click the icon at the top to connect to the ESP32. You should get a “Connected” message.
  • ESP32 Button LED Wiring Diagram
  • After that, type something in the Serial Bluetooth Terminal app. For example, “Hello”.
  • ESP32 Button LED Wiring Diagram
  • You should instantly receive that message in the Arduino IDE Serial Monitor.
  • ESP32 Button LED Wiring Diagram
  • You can also exchange data between your Serial Monitor and your smartphone. Type something in the Serial Monitor top bar and press the “Send” button.
  • ESP32 Button LED Wiring Diagram
  • You should instantly receive that message in the Serial Bluetooth Terminal App.
  • ESP32 Button LED Wiring Diagram

Book Tutorial

We are considering to make the book tutorials. If you think the book tutorials are essential, you can download it. download book

References

※ NOTE THAT:

Some components works on 3.3v and others works on 5v!

Now that you know how to exchange data using Bluetooth Serial, you can modify the previous sketch to make something useful. For example, control the ESP32 outputs when you receive a certain message, or send data to your smartphone like sensor readings.