Categories: ESP32 Firmware Guide

BLE UART on ESP32 Controller

UART is a Universal Asynchronous Receiver/Transmitter, The main purpose is to transmit and receive serial data. The serial communication stands for sequential transmission or reception of data of one bit at a time through the bus or communication channel. And in ESP32 microcontroller, there are some UART controllers are exist and which are compatible with different UART devices. The UART controllers are UART0, UART1, and UART2. In this tutorial, the rxValue is the data received and txValue is the data to be sent. In this example, just a byte is incremented in every second.

The ESP32 controller has features such as WiFi, Bluetooth, and BLE. BLE ie Bluetooth Low Energy which mainly used for short distance communication of low width small amount of data and its make very less power.BLE on ESP32 has a serial communication property so this  UART feature can be used to exchange sequence of data between the ESP32 controller and connected device.
The libraries used here are,

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

Define service UUID and characteristics UUID for Tx and Rx.

#define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" 
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

The transmitted data on one end is the received data on the other end and the RX UUID characteristics in the app are the TX UUID characteristics of the ESP32 and vice versa. Next, let’s look at the callback function that handles the Bluetooth connection status:

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

To do all these set the “deviceConnected” flag as true or false when you connect or disconnect from the ESP32. Similarly, there is a callback function that handles the receiving data sent by the client (phone)

class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string rxValue = pCharacteristic->getValue();

Initialize the ESP32 as a BLE device and set its name and create BLE server.

 BLEDevice::init("UART Service");
 BLEServer *pServer = BLEDevice::createServer();
   pServer->setCallbacks(new MyServerCallbacks());

Create BLE service using the service UUID.

BLEService *pService = pServer->createService(SERVICE_UUID);

Add characteristics.PROPERTY_WRITE, PROPERTY_NOTIFY are the properties of the characteristics.

 pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID_TX,
                      BLECharacteristic::PROPERTY_NOTIFY
                    );
                      
  pCharacteristic->addDescriptor(new BLE2902());

  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID_RX,
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setCallbacks(new MyCallbacks());

Start BLE service

pService->start();

Start advertising

 pServer->getAdvertising()->start();

Sent the txValue in every second after the device connected.

if (deviceConnected) { 
Serial.printf("*** Sent Value: %d ***\n", 
txValue);
 pCharacteristic->setValue(&txValue, 1); 
pCharacteristic->notify(); 
txValue++; 
}

Share