ESP32 BLE on Arduino IDE with UART Test
Introduction
The ESP32 is a powerful microcontroller that supports both Wi-Fi and Bluetooth functionalities. One of the key features of the ESP32 is Bluetooth Low Energy (BLE), which is widely used for short-range wireless communication in IoT applications. Using BLE in combination with UART (Universal Asynchronous Receiver-Transmitter) allows seamless wireless communication between devices such as smartphones, tablets, and computers.
In this comprehensive guide, we’ll walk through setting up ESP32 BLE with UART communication using the Arduino IDE. We’ll create a BLE server on the ESP32 that transmits and receives data over UART, demonstrating a practical BLE-to-UART test setup.
Prerequisites
Before proceeding, ensure you have the following:
- ESP32 Development Board
- Arduino IDE (latest version recommended)
- ESP32 Board Package installed in Arduino IDE
- Smartphone or Tablet with a BLE scanner app (e.g., nRF Connect, BLE Scanner)
Step 1: Install the ESP32 Board Package
If you haven’t installed the ESP32 board package in Arduino IDE yet:
- Open Arduino IDE.
- Go to File → Preferences.
- In the “Additional Board Manager URLs” field, add the following link:
https://dl.espressif.com/dl/package_esp32_index.json
- Go to Tools → Board → Board Manager.
- Search for ESP32 and click Install.
Step 2: ESP32 BLE Code for UART Communication
Create a new sketch in Arduino IDE and paste the following code:
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#define SERVICE_UUID "12345678-1234-5678-1234-56789abcdef0"
#define CHARACTERISTIC_UUID "abcdef01-1234-5678-1234-56789abcdef0"
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE UART Service...");
// Initialize BLE
BLEDevice::init("ESP32_BLE_UART");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create BLE Characteristic for UART Communication
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pServer->getAdvertising()->start();
Serial.println("BLE UART Service is now active");
}
void loop() {
if (deviceConnected) {
// Check for incoming data from BLE and send it to UART
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.print("Received Value: ");
Serial.println(rxValue.c_str());
pCharacteristic->setValue("Data Received: " + rxValue);
pCharacteristic->notify();
}
}
delay(1000);
}
Step 3: Upload Code to ESP32
- Connect your ESP32 board to your computer via USB.
- Go to Tools → Board and select ESP32 Dev Module.
- In Tools → Port, select the correct COM port for your ESP32.
- Click the Upload button to flash the code.
Step 4: Testing BLE Communication
To test the BLE UART communication:
- Install a BLE scanner app like nRF Connect or BLE Scanner on your smartphone.
- Power up your ESP32 board. It will appear as ESP32_BLE_UART in the BLE scanner app.
- Connect to ESP32_BLE_UART and locate the characteristic with UUID
abcdef01-1234-5678-1234-56789abcdef0
. - Send a string (e.g., “Hello ESP32”) from the BLE scanner app.
- The string should appear on the Serial Monitor in Arduino IDE.
- The ESP32 will respond back with “Data Received: [your message]” via BLE.
Step 5: UART Communication Test
To extend this BLE communication to UART:
- Connect the TX and RX pins of the ESP32 to another microcontroller or UART device.
- Use
Serial2.begin(baud_rate, SERIAL_8N1, RX_PIN, TX_PIN);
in the ESP32 code to initialize UART communication. - Forward received BLE data to UART using:
Serial2.print(rxValue.c_str());
- Data sent via UART will now be broadcasted over BLE.
Step 6: Troubleshooting Common Issues
If you encounter issues, consider the following solutions:
- ESP32 Not Visible on BLE Scanner: Ensure BLE is initialized correctly in
setup()
and verify BLE services are active. - Data Not Received: Verify the BLE characteristic UUID is correctly defined in the code and matches the scanner app.
- Serial Monitor Not Displaying Data: Ensure your Serial Monitor baud rate is set to 115200.
- Connection Drop Issues: Verify your smartphone is within range and Wi-Fi/BLE interference is minimized.
Step 7: Applications of ESP32 BLE UART
The ESP32 BLE UART functionality is useful in various scenarios such as:
- IoT Device Control (e.g., smart home automation)
- Wireless Sensor Data Transmission
- BLE-Based Data Logging Systems
- Communication Between Multiple ESP32 Devices
Conclusion
Using ESP32 BLE with UART on Arduino IDE provides a powerful wireless communication solution for IoT projects. This setup simplifies data transmission between the ESP32 and other devices without complex wiring. By combining BLE and UART, you gain flexibility, reliability, and scalability in your ESP32-based applications.
You may like also:
Pingback: ESP32 BLE with DHT11 - How To - IoTbyHVM
Pingback: Android Things - OS for IoT and embedded devices - IoTbyHVM
Pingback: Johnny Five : The JavaScript Robotics & IoT Platform
Pingback: How to Fix Broken Packages in Ubuntu - IoTbyHVM - Bits & Bytes of IoT
Pingback: How to Fix Ubuntu Update Errors - IoTbyHVM - Bits & Bytes of IoT
Pingback: Managing Files with NodeMCU | NodeMCU Examples
Pingback: IoT Sensors and Actuators - IoTbyHVM - Bits & Bytes of IoT
Pingback: IoT Protocols and Communication APIs - IoTbyHVM - Bits & Bytes of IoT
Pingback: ATOMIC Pi - A high power alternative to RPi - IoTbyHVM
Pingback: Symphony Link LPWAN IoT Network - IoTbyHVM - Bits & Bytes of IoT
Pingback: Interface MQ135 (Gas Sensor) with NodeMCU - IoTbyHVM
Pingback: Top 10 IoT Cloud Platforms - CompileIoT
Pingback: IoT OS and RTOS for Internet of Things Devices - CompileIoT
Pingback: Interface LDR module with NodeMCU - IoTbyHVM - Bits & Bytes of IoT
Pingback: Physical and Logical Design of IoT - IoTbyHVM