How to get your ESP32 development board to communicate with another device over Bluetooth Serial

Since a ESP32 development board comes with Bluetooth, we can get it to communicate with another device over Bluetooth Serial.

With this in mind, let's look at how we can get your ESP32 development board to communicate with another device over Bluetooth Serial.

Enabling ESP32 Development on Arduino IDE

At this point in time, you are ready to flash a program into your ESP32 board to test out communication via Bluetooth Serial. In order to do so, we need to use a tool to write our program into the flash memory of the development board.

Since it is easy to write code and flash programs with Arduino IDE, we can use it to serve our purpose.

In order to use Arduino IDE for this guide, be sure to enable ESP32 development on Arduino IDE before continuing.

Sample Arduino Sketch to get your ESP32 development board to communicate with another device over Bluetooth Serial

After you had enabled ESP32 development on Arduino IDE, you can then proceed with uploading a sample Arduino Sketch that demonstrate Bluetooth Serial communication:

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;
 
void setup()
{
  SerialBT.begin("ESP32test");
  delay(1000);
}
 
void loop()
{
  String inputFromOtherSide;
  if (SerialBT.available()) {
    inputFromOtherSide = SerialBT.readString();
    SerialBT.println("You had entered: ");
    SerialBT.println(inputFromOtherSide);
  }
}

So what does this Arduino sketch do?

Firstly, inside the setup function, we create a BluetoothSerial object with ESP32test as the device name. After that we send ESP32 BluetoothSerial echo test to the device that connects to it via Bluetooth.

Next, inside the loop function, we try to read a String for the other device. Whenever the other device sends something, we simply echo the input back to the sender.

Testing the ESP32 Bluetooth connection with Arduino IDE

Once you had uploaded the Arduino sketch onto the ESP32 development board, you can then proceed to test it with Arduino IDE.

First, pair your computer with ESP32 via Bluetooth. For example, this is how to connect an ESP32 development board via Bluetooth on your Mac. In case you are using Windows 10, this is how to connect to an ESP32 development board via Bluetooth on Windows 10.

Once you had paired your computer with ESP32, the Bluetooth connection will appear as a port inside your Arduino IDE.

Mac Arduino 1.8.9 showing bluetooth connection as port

After you had selected the Bluetooth connection as the port to use, start Serial Monitor.

When Serial Monitor appears, you should see the name of the Bluetooth connection as the title.

Mac Arduino 1.8.9 serial monitor showing utilization of ESP32test bluetooth connection as the port

Given that, you can use the Serial Monitor to send data to your ESP32 development board via the Bluetooth connection.

When you do so, you should see what you had entered being echoed back inside the text area.

Mac Arduino 1.8.9 serial monitor showing utilization of ESP32test bluetooth connection as the port and some sample input

At this point in time, you have validated that Bluetooth connection of your ESP32 development board is working.

Given these points, you can then extend the code to instruct ESP32 to send sensor information to the other device via Bluetooth Serial. For example, you may wish to get your ESP32 to read temperature and humidity from a DHT11 sensor and send the readings to the other device through Bluetooth Serial.

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.

Udemy.com Homepage 300x250 Domain names for just 88 cents!

Find a domain starting at $0.88

powered by Namecheap