Implementation of Bluetooth & BLE on ESP32

Objective

To establish Bluetooth Low Energy (BLE) communication using ESP32, where ESP32 acts as a BLE server broadcasting sensor data, and a smartphone or another ESP32 acts as a BLE client.

Methodology

  • Set up ESP32 as a BLE server to advertise sensor data.
  • Connect a smartphone or another ESP32 as a BLE client to receive the data.
  • Use Arduino IDE with the ESP32 BLE library for implementation.

Implementation Steps

Step 1: Install ESP32 Board in Arduino IDE

1Open Arduino IDE.
2Go to File → Preferences, and add the ESP32 board URL:
3https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
4Install the ESP32 board from Boards Manager.

Step 2: Install the Required BLE Library

1Go to Sketch → Include Library → Manage Libraries, and install:
2"ESP32 BLE Arduino" by Neil Kolban.

Step 3: Write Code for ESP32 BLE Server

This ESP32 BLE server continuously advertises temperature sensor data.

1#include <BLEDevice.h>
2  #include <BLEUtils.h>
3  #include <BLEServer.h>
4  
5  #define SERVICE_UUID "12345678-1234-5678-1234-56789abcdef0"
6  #define CHARACTERISTIC_UUID "87654321-4321-6789-4321-abcdef012345"
7  
8  BLEServer* pServer = NULL;
9  BLECharacteristic* pCharacteristic = NULL;
10  
11  void setup() {
12      Serial.begin(115200);
13      BLEDevice::init("ESP32_BLE_Sensor");
14  
15      pServer = BLEDevice::createServer();
16      BLEService *pService = pServer->createService(SERVICE_UUID);
17      
18      pCharacteristic = pService->createCharacteristic(
19                          CHARACTERISTIC_UUID,
20                          BLECharacteristic::PROPERTY_READ | 
21                          BLECharacteristic::PROPERTY_NOTIFY
22                        );
23  
24      pCharacteristic->setValue("Temperature: 25°C");
25      pService->start();
26  
27      BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
28      pAdvertising->addServiceUUID(SERVICE_UUID);
29      pAdvertising->setScanResponse(true);
30      BLEDevice::startAdvertising();
31      
32      Serial.println("BLE Server is running...");
33  }
34  
35  void loop() {
36      int temp = random(20, 35);  // Simulate temperature data
37      String tempValue = "Temperature: " + String(temp) + "°C";
38      pCharacteristic->setValue(tempValue.c_str());
39      pCharacteristic->notify();
40      Serial.println(tempValue);
41      delay(5000);  // Update every 5 seconds
42  }

Step 4: Write Code for ESP32 BLE Client

This ESP32 BLE client scans for BLE devices and receives temperature data.

1#include <BLEDevice.h>
2  #include <BLEUtils.h>
3  #include <BLEScan.h>
4  #include <BLEAdvertisedDevice.h>
5  
6  #define SERVICE_UUID "12345678-1234-5678-1234-56789abcdef0"
7  
8  void setup() {
9      Serial.begin(115200);
10      BLEDevice::init("");
11  
12      BLEScan* pBLEScan = BLEDevice::getScan();
13      pBLEScan->setActiveScan(true);
14      
15      Serial.println("Scanning for BLE devices...");
16      pBLEScan->start(10, false);
17  }
18  
19  void loop() {
20      BLEScanResults foundDevices = BLEDevice::getScan()->getResults();
21      for (int i = 0; i < foundDevices.getCount(); i++) {
22          BLEAdvertisedDevice device = foundDevices.getDevice(i);
23          if (device.haveServiceUUID() && device.isAdvertisingService(BLEUUID(SERVICE_UUID))) {
24              Serial.print("Found ESP32 BLE Server: ");
25              Serial.println(device.getName().c_str());
26          }
27      }
28      delay(5000);
29  }

Applications

  • Remote Sensor Monitoring: View ESP32 sensor values via BLE apps.
  • Health & Fitness Devices: BLE for wearable sensor data transmission.
  • Smart Home: Inter-device communication using low power BLE.

Future Research Concepts

  • Integrate BLE with cloud platforms for IoT dashboards.
  • Use BLE Mesh networking for broader smart home coverage.
  • Combine BLE with AI for proximity-based automation.