Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Leveraging Bluetooth in ESP32: Creating Smart IoT Devices

May 7, 2024

Leveraging Bluetooth in ESP32: Creating Smart IoT Devices

Bluetooth technology has revolutionized how we connect and communicate with devices wirelessly. With the advent of IoT and the growing demand for smart devices, understanding and leveraging Bluetooth capabilities have become essential for developers. In this blog post, we will explore how to use Bluetooth in ESP32 microcontrollers to create smart IoT devices.

Introduction to ESP32 and Bluetooth

ESP32 is a powerful microcontroller that includes built-in Wi-Fi and Bluetooth capabilities. It is widely used in IoT projects due to its versatility, low power consumption, and a large community support. Bluetooth, on the other hand, enables devices to communicate with each other and exchange data wirelessly over short distances.

Setting Up the Environment

To get started, you’ll need an ESP32 development board, such as the ESP32 DevKitC. Install the required software tools:

Once the installation is complete, you can now start leveraging Bluetooth in your IoT project.

ESP32 Bluetooth Library

The ESP32 comes with a Bluetooth library that simplifies Bluetooth-related operations. To make use of this library, you need to import the BluetoothSerial library in your Arduino sketch:

1
#include <BluetoothSerial.h>

Bluetooth Serial Communication

The BluetoothSerial class in the ESP32 library provides methods to perform serial communication over Bluetooth. Here’s an example that demonstrates how to send and receive data between an ESP32 and a smartphone using Bluetooth:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <BluetoothSerial.h>

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32");

  Serial.println("Bluetooth device is ready to pair.");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read()); // Forward data from Serial to Bluetooth
  }

  if (SerialBT.available()) {
    Serial.write(SerialBT.read()); // Forward data from Bluetooth to Serial
  }
}

In this example, the ESP32 receives data from the computer’s serial port (via USB) and sends it to the smartphone over Bluetooth. Similarly, it receives data from the smartphone and sends it back to the computer’s serial port.

Bluetooth Low Energy (BLE)

Bluetooth Low Energy (BLE) is a power-efficient version of Bluetooth that enables devices to operate for extended periods with limited battery capacity. ESP32 supports BLE and can act as a BLE peripheral device or a central device.

Here’s an example of how to create a simple BLE peripheral device using the ESP32:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>

BLEServer *pServer;
BLECharacteristic *pCharacteristic;

void setup() {
  BLEDevice::init("ESP32 BLE Device");
  pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(BLEUUID((uint16_t)0x180F));
  pCharacteristic = pService->createCharacteristic(
                      BLEUUID((uint16_t)0x2A19),
                      BLECharacteristic::PROPERTY_READ
                    );
  pCharacteristic->setValue("Hello, BLE!");

  pService->start();
  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();
}

This example creates a Bluetooth Low Energy peripheral device advertising a custom service and characteristic. When connected, the device can be queried to get the value of the characteristic.

Conclusion

Bluetooth in ESP32 opens up a world of possibilities for creating wireless IoT devices. In this blog post, we explored the basics of leveraging Bluetooth capabilities in ESP32 microcontrollers. We covered Bluetooth serial communication and how to create a simple BLE peripheral device. With this knowledge, you can now build your own smart IoT devices that communicate wirelessly using Bluetooth.

Remember to refer to the official documentation and explore more advanced features provided by the ESP32 library to fully utilize Bluetooth capabilities.

Happy coding!


➡️ Linux 'pwd' command


⬅️ Linux 'cd' command


Go back to Posts.