Embedded System Project 7: ESP32 Bluetooth with Android

Fernaldi Fauzie
5 min readMar 19, 2021

Alright! This time, just like the title of this story, we will do experimentation with ESP32 Bluetooth. More precisely, we will try to exchange data between ESP32 and an Android smartphone. We will try many things, like sending data from BMP280 sensor to Android smartphone, turning on/off led by using the message from Android smartphone, or greet the ESP32 with Android smartphone (although this one is actually pretty simple)

Bluetooth?

In short, Bluetooth is a wireless technology Personal Area Network (PAN) standard used for exchanging data between fixed and mobile devices over short distances. For your information, Bluetooth operates in the 2.4 GHz band.

Let’s Start!

For this story, I’m using this randomnerdtutorials link as a reference!

In this story, we will need:

  • ESP32
  • Micro USB cable
  • Breadboard
  • Android smartphone
  • LED
  • 330 Ohm resistor
  • BMP280 sensor

We need a Bluetooth Terminal application installed on our Android smartphone. To make it easy, I recommend this Serial Bluetooth Terminal application that available in the Play Store

Before we continue, we have to assemble the circuit by following the next schematic diagram.

Connect an LED to GPIO17, BMP280 VCC to ESP32 3V3, BMP280 GND to ESP32 GND, BMP280 SCL to GPIO22, and BMP280 SDA to GPIO21.

Schematic Diagram
My Simple Circuit

Next, we have to use this code to make use of ESP32 Bluetooth and Android’s Bluetooth:

// Load libraries
#include "BluetoothSerial.h"
#include <Wire.h>
#include <Adafruit_BMP280.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 = 17;
Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
// 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!");
if (!bmp.begin(0x76)) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}
/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}
void loop() {
unsigned long currentMillis = millis();
// Send temperature readings
if (currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
SerialBT.print(F("Temperature = "));
SerialBT.print(bmp.readTemperature());
SerialBT.print(" *C\n");
SerialBT.print(F("Pressure = "));
SerialBT.print(bmp.readPressure());
SerialBT.print(" Pa\n");
SerialBT.print(F("Approx altitude = "));
SerialBT.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
SerialBT.print(" m\n");
}
// Read received messages
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);
}
else if (message =="hello"){
SerialBT.print("hello, Fernaldi!");
}
delay(20);
}

In short, here is the steps to use Serial Bluetooth Terminal:

  • Turn on your smartphone’s Bluetooth. Then, open Serial Bluetooth Terminal application
  • Go to Devices
  • Click the settings icon, then select Pair new device. Pair with ESP32
  • Finally, go back to Terminal

Also, I would like to point out some parts of the code, like:

// Bluetooth Serial object
BluetoothSerial SerialBT;

This code is to create an instance of BluetoothSerial called SerialBT:

SerialBT.begin("ESP32"); //Bluetooth device name

Initialize the ESP32 as a Bluetooth device with the name of “ESP32”

SerialBT.print(F("Temperature = "));
SerialBT.print(bmp.readTemperature());
SerialBT.print(" *C\n");
SerialBT.print(F("Pressure = "));
SerialBT.print(bmp.readPressure());
SerialBT.print(" Pa\n");
SerialBT.print(F("Approx altitude = "));
SerialBT.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
SerialBT.print(" m\n");
}

I think this one actually pretty straightforward. So this is the code to send the temperature, pressure, and approximate altitude using sea-level pressure (1013.25 mbar) to the Android smartphone.

// Read received messages
if (SerialBT.available()){
char incomingChar = SerialBT.read();
if (incomingChar != '\n'){
message += String(incomingChar);
}
else{
message = "";
}
Serial.write(incomingChar);
}

This one is to read received messages, so that we know what is the purpose of the messages, like turning on/off LED or a greeting!

// Check received message and control output accordingly
if (message =="led_on"){
digitalWrite(ledPin, HIGH);
}
else if (message =="led_off"){
digitalWrite(ledPin, LOW);
}
else if (message ="hi"){
SerialBT.print("hi, Fernaldi!");
}
delay(20);

This one is to check the received message and control output accordingly. If the message is “led_on”, turn on the LED. Else if the message is “led_off”, turn off the LED. Else if the message is “hi”, send greeting “hi, Fernaldi!”.

Now, It’s Time to Have Fun!

After connecting the Android with ESP32, we can get temperature, pressure, and approximate altitude from ESP32!

Temperature, Pressure, and Approximate Altitude

Next, we can send a greeting (“hello”) to ESP32. ESP32 will send the message “hello, Fernaldi” as the result!

Greet the ESP32

The “hello” message from Android Smartphone can be seen in the serial monitor in Arduino IDE. Just make sure that you use the correct baud (in my case it’s 115200 baud) so that we can get the accurate message.

We can also turn on LED with sending “led_on” message and turn off LED with sending “led_off” message!

Turn On and Off the LED

In conclusion, by using ESP32 Bluetooth, we can do many things with the help of an Android smartphone, like send the BMP280 sensor reading to an Android smartphone, or turn on/off an LED with a message from an Android smartphone. Moreover, we also can do simple chatting with Serial Bluetooth Terminal application (Android smartphone) with the serial monitor in Arduino IDE (PC) and make ESP32 respond to some specific message like “hello”.

It seems that our story has come to the end. I hope that this story can be informative to you guys. Remember to stay curious because there are still many things in ESP32 that we can explore! See you guys in the next story! Good luck and don’t forget to have fun!

--

--