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

ESP32 Bluetooth Classic and Android Smartphone BT

We’ll use Bluetooth Classic to control an ESP32 output and send sensor readings to an Android smartphone.

Note: this project is only compatible with Android smartphones.

Bluetooth Classic with ESP32

Bluetooth Classic is currently much easier to use than Bluetooth Low Energy. It is very similar to programming an Arduino with a Bluetooth module like the HC-06. It makes use of standard serial functions and protocols.

hc-05 bluetooth module arduino ESP32

We’ll start with an example that comes with the Arduino IDE in this tutorial. Then, using the ESP32 and your Android smartphone, we’ll create a simple project to exchange data.

We’ll start with an example that comes with the Arduino IDE in this tutorial. Then, using the ESP32 and your Android smartphone, we’ll create a simple project to exchange data.

Parts Required

To follow this tutorial, you need the following parts:

You can use the preceding links to find all the parts for your projects at the best price!

Bluetooth Terminal Application

You’ll need a Bluetooth Terminal application installed on your smartphone to follow along with this tutorial and we recommend using the “Serial Bluetooth Terminal” app which is available in the Google Play Store.

bluetooth serial application esp32

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.

//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 as 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);

}

How the Code Works

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;

setup()

In the setup() initialize serial communication at a baud rate of 115200.

Serial.begin(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

loop()

In the loop(), send and receive data via Bluetooth Serial.

We check if bytes are being received on the serial port in the first if statement. If there is one, send the information to the connected device via Bluetooth.

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

To the ESP32, upload the previous code. Make sure you’re using the correct board and COM port. 

After uploading the code, open the Serial Monitor at a baud rate of 115200.  The ESP32 Enable button should be pressed.

After a few seconds, you should get a message saying: “The device started, now you can pair it with bluetooth!”.

ESP32 Bluetooth Classic and Android Smartphone Demonstration

Open the “Serial Bluetooth Terminal” app on your smartphone. Make sure your smartphone’s Bluetooth is turned on.

You must pair a new device with the ESP32 to connect for the first time.

Go to the Devices tab.

ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal

Select Pair new device from the settings. You should receive a list of available Bluetooth devices, which should include the ESP32test. Pair with the ESP32test.

ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal Pair new device

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 Bluetooth Classic and Android Application Serial Bluetooth Terminal connected

After that, type something in the Serial Bluetooth Terminal app. For example, “Hello”.

ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal Demonstration

You should instantly receive that message in the Arduino IDE Serial Monitor.

ESP32 Bluetooth Classic demonstration serial monitor Arduino IDE

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 Bluetooth Classic demonstration serial monitor Arduino IDE

You should instantly receive that message in the Serial Bluetooth Terminal App.

ESP32 Bluetooth Classic and Android Application Serial Bluetooth Terminal Demonstration

Exchange Data using Bluetooth Serial

You can now modify the previous sketch to make something useful now that you know how to exchange data using Bluetooth Serial. Control the ESP32 outputs when you receive a specific message, for example, or send data to your smartphone, such as sensor readings.

The project we’ll build sends temperature readings to your smartphone every 10 seconds. The DS18B20 temperature sensor will be used.

ds18b20 temperature sensor

Through the Android app, we’ll send messages to control an ESP32 output. When the ESP32 receives the led_on message, we’ll turn the GPIO on, when it receives the led_off message, we’ll turn the GPIO off.

Schematic

Before proceeding with this project, assemble the circuit by following the next schematic diagram.

Connect an LED to GPIO25, and connect the DS18B20 data pin to GPIO32.

Bluetooth Classic ESP32 communication

Code

Install the One Wire library by Paul Stoffregen and the Dallas Temperature library to work with the DS18B20 temperature sensor. If you haven’t already, follow the steps below to install these libraries.

One Wire library

  1. Click here to download the One Wire library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get the OneWire-master folder
  3. Rename your folder from OneWire-master to OneWire
  4. Move the OneWire folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Dallas Temperature library

  1. Click here to download the Dallas Temperature library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get the Arduino-Temperature-Control-Library-master folder
  3. Rename your folder from Arduino-Temperature-Control-Library-master to DallasTemperature
  4. Move the DallasTemperature folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

After assembling the circuit and installing the necessary libraries, copy the following sketch to your Arduino IDE.

/*********

  Rui Santos

  Complete project details at https://randomnerdtutorials.com

*********/

// Load libraries

#include “BluetoothSerial.h”

#include <OneWire.h>

#include <DallasTemperature.h>

// Check if Bluetooth configs are enabled

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)

#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it

#endif

// Bluetooth Serial object

BluetoothSerial SerialBT;

// GPIO where LED is connected to

const int ledPin =  25;

// GPIO where the DS18B20 is connected to

const int oneWireBus = 32;          

// Setup a oneWire instance to communicate with any OneWire devices

OneWire oneWire(oneWireBus);

// Pass our oneWire reference to Dallas Temperature sensor 

DallasTemperature sensors(&oneWire);

// Handle received and sent messages

String message = “”;

char incomingChar;

String temperatureString = “”;

// Timer: auxiliar variables

unsigned long previousMillis = 0;    // Stores last time temperature was published

const long interval = 10000;         // interval at which to publish sensor readings

void setup() {

  pinMode(ledPin, OUTPUT);

  Serial.begin(115200);

  // Bluetooth device name

  SerialBT.begin(“ESP32”);

  Serial.println(“The device started, now you can pair it with bluetooth!”);

}

void loop() {

  unsigned long currentMillis = millis();

  // Send temperature readings

  if (currentMillis – previousMillis >= interval){

    previousMillis = currentMillis;

    sensors.requestTemperatures(); 

    temperatureString = String(sensors.getTempCByIndex(0)) + “C  ” +  String(sensors.getTempFByIndex(0)) + “F”;

    SerialBT.println(temperatureString); 

  }

  // Read received messages (LED control command)

  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 ==”led_on”){

    digitalWrite(ledPin, HIGH);

  }

  else if (message ==”led_off”){

    digitalWrite(ledPin, LOW);

  }

  delay(20);

}

How the Code Works

Let’s take a quick look at the code and see how it works.

Start by including the necessary libraries. The BluetoothSerial library for Bluetooth, and the OneWire and DallasTemperature for the DS18B20 temperature sensor.

Begin by including the required libraries. For Bluetooth, use the BluetoothSerial library, and for the DS18B20 temperature sensor, use the OneWire and DallasTemperature libraries.

#include “BluetoothSerial.h”

#include <OneWire.h>

#include <DallasTemperature.h>

Create a BluetoothSerial instance called SerialBT.

BluetoothSerial SerialBT;

Create a variable called ledPin to hold the GPIO you want to control. In this case, GPIO25 has an LED connected.

const int ledPin =  25;

Define the DS18B20 sensor pin and create objects to make it work. The temperature sensor is connected to GPIO32.

// GPIO where the DS18B20 is connected to

const int oneWireBus = 32;          

// Setup a oneWire instance to communicate with any OneWire devices

OneWire oneWire(oneWireBus);

// Pass our oneWire reference to Dallas Temperature sensor 

DallasTemperature sensors(&oneWire);

Create an empty string called a message to store the received messages.

String message = “”;

Create a char variable called incomingChar to save the characters coming via Bluetooth Serial.

char incomingChar;

The temperatureString variable holds the temperature readings to be sent via Bluetooth.

String temperatureString = “”;

Create auxiliar timer variables to send readings every 10 seconds.

unsigned long previousMillis = 0;   // Stores last time temperature was published

const long interval = 10000;        // interval at which to publish sensor readings

setup()

In the setup(), set the ledPin as an output.

pinMode(ledPin, OUTPUT);

Initialize the ESP32 as a bluetooth device with the “ESP32” name.

SerialBT.begin(“ESP32”); //Bluetooth device name

loop()

In the loop(), send the temperature readings, read the received messages and execute actions accordingly.

The following snippet of code, checks if 10 seconds have passed since the last reading. If it’s time to send a new reading, we get the latest temperature and save it in Celsius and Fahrenheit in the temperatureString variable.

unsigned long currentMillis = millis();

if (currentMillis – previousMillis >= interval) {

    previousMillis = currentMillis;

    sensors.requestTemperatures(); 

    temperatureString = ” ” + String(sensors.getTempCByIndex(0)) + “C  ” +  String(sensors.getTempFByIndex(0)) + “F”;

Then, to send the temperatureString via bluetooth, use SerialBT.println().

SerialBT.println(temperatureString); 

The incoming messages are read in the next if statement. When you receive serial messages, you get one character at a time. You know that the message ended when you receive \n.

So, we check if there’s data available in the Bluetooth serial port.

if (SerialBT.available()) {

If there is, we’ll save the characters in the incomingChar variable.

char incomingChar = SerialBT.read();

If the incomingChar is different than \n, we’ll concatenate that char character to our message.

if (incomingChar!= ‘\n’){

  message += String(incomingChar);

}

When we’re finished reading the characters, we clear the message variable. Otherwise, all received messages would be appended to each other.

message = “”;

After that, we have two if statements to check the content of the message. If the message is led_on, the LED turns on.

if (message ==”led_on”){

   digitalWrite(ledPin, HIGH);

}

If the message is led_off, the LED turns off.

else if (message ==”led_off”){

  digitalWrite(ledPin, LOW);

}

Testing the Project

Upload the previous sketch to your ESP32 board. Then, in the Serial Monitor, press the ESP32 Enable button. You can connect to the ESP32 with your smartphone once you receive the following message.

Testing ESP32 Bluetooth Classic Project Code Example

Then, you can write the“led_on” and “led_off” messages to control the LED.

Testing ESP32 Bluetooth Classic Project Code Example

Several buttons in the application allow you to save default messages. For example, you can associate M1 with the “led_on” message, and M2 with the “led_off” message.

Default messages

Now, you can control the ESP32 GPIOs.

At the same time, you should be receiving the temperature readings every 10 seconds.

Temperature reading

Wrapping Up

In summary, the ESP32 supports BLE and Bluetooth Classic. Using Bluetooth Classic is as simple as using serial communication and its functions. We hope you’ve found this tutorial useful. 

author avatar
Amith G Nair
Experience as a product developer, innovation coach, and electronics lecturer,a seasoned professional driven by passion for designing projects.expertise extends to 3D modelling, hardware designing, and web development using HTML, WordPress, and Django.