ESP32 Chapter 6: ESP32 Bluetooth Classic
Hello and Welcome!
“It is better to fail in originality than to succeed in imitation”
The quote above from Herman Melville perfectly sums up the process of the projects we have been through. In the previous projects, mostly, it was all about “duplication”, as we needed only to copy codes and circuit schema exactly the same from the module sources. Starting from last week, Mr. Kusprasapta stated that we stepped into a new phase, “development”, as creativity and improvisation skills are highly needed.
This week, we were introduced to another interestingly challenging mode, the Bluetooth module, which includes Bluetooth Classic and Bluetooth Low Energy as parts of this project.
Bluetooth Classic
The first step was to download Serial Bluetooth Terminal Application so that we could send messages from Serial Bluetooth Terminal to Serial Monitor on Arduino IDE.

To test whether it works, use this code 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
#endifBluetoothSerial 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);
}
Then, we need to set up the application by following these steps.
Verify and upload the code above, then open the Serial Monitor and set the baud rate to 115200 baud. After pressing the EN button on the ESP32 board, the message “The device started, now you can pair it with bluetooth!” should appear on the Serial Monitor.

Enable the phone’s Bluetooth, open the Serial Bluetooth Terminal, go to Devices, Pair new device, and pair with the “Uji_Coba_ESP32”.

Go to the Serial Bluetooth Terminal, tap the “connect” button. Type a message in the Serial Bluetooth Terminal, and it should be shown in the Arduino IDE Serial Monitor straight away.


If you want to send a message from Serial Monitor to your phone, type a message on the Serial Monitor top bar and press the “Send” button.


We were also instructed to make the LCD show a message sent from the Serial Bluetooth Terminal, also send the output of the DHT11 sensor measurement to the Serial Bluetooth Terminal. For just effectiveness matter only, we combined the code so that both actions would take place at the same time.
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11 // DHT 11
#include <LiquidCrystal_I2C.h>
#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
#endifBluetoothSerial SerialBT;DHT dht(DHTPIN, DHTTYPE);
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);void setup() {
Serial.begin(115200);
SerialBT.begin("Uji_Coba_ESP32");
lcd.println("The device started, now you can pair it with bluetooth!");
Serial.println(F("DHT11 test!"));dht.begin();
lcd.begin(16,2);
lcd.init();
lcd.backlight();
}void loop() {
lcd.clear();
String message;
if (SerialBT.available()) {
lcd.clear();
while(SerialBT.available()){
char incomingChar = SerialBT.read();
if (incomingChar != '\n'){
message += String(incomingChar);
}
}
}
lcd.setCursor(0,0);
lcd.print(message);
Serial.println(message);
delay(5000);
lcd.clear();
lcd.clear();
delay(20);
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);if (isnan(h) || isnan(t) || isnan(f)) {
lcd.println(F("Failed to read from DHT sensor!"));
return;
}SerialBT.print(F("Humidity: "));
SerialBT.print(h);
SerialBT.print(F("%"));
delay(1000);
SerialBT.print('\n');
SerialBT.print(F("Suhu: "));
SerialBT.print(t);
SerialBT.print(F(" C"));
SerialBT.print('\n');
delay(1000);
}
We adopted the schematic circuit from the previous project in ESP32 Chapter 5.

Here below is the proof of how all the process happening.
Also, I attached the screen recorder of the phone as the Serial Bluetooth Terminal point of view.
At first, we found a couple of problems, one of which being the format of temperature and humidity written to the Serial Bluetooth Terminal, as they kept coming out without any spaces and enters between lines, which was confusing. We figured this problem out by adding “SerialBT.print(“\n”)” resulting on a more easy-to-read display on the Terminal. Also, whenever we sent a message from our phone to the LCD, especially when the number of characters of the message was less than 16 — the number of maximum character that can be shown in LCD per line — it would show a strange character at the end of the message, a problem we weren’t able to solve, but other than those, they all worked fine.
Christovito Hidajat
18218043