SunFounder ESP32 Starter Kit
(continued from previous page)
if (pServer == nullptr) {
Serial.println("Error creating BLE server");
return;
}
pServer->setCallbacks(new MyServerCallbacks()); // Set the BLE server␣
˓→callbacks
// Create the BLE service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Print the error message if the BLE service creation fails
if (pService == nullptr) {
Serial.println("Error creating BLE service");
return;
}
// Create the BLE characteristic for sending notifications
pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX,
˓→ BLECharacteristic::PROPERTY_NOTIFY);
pCharacteristic->addDecodeor(new BLE2902()); // Add the decodeor
// Create the BLE characteristic for receiving data
BLECharacteristic *pCharacteristicRX = pService->
˓→createCharacteristic(CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_
˓→WRITE);
pCharacteristicRX->setCallbacks(new MyCharacteristicCallbacks()); //␣
˓→Set the BLE characteristic callbacks
pService->start(); //␣
˓→Start the BLE service
pServer->getAdvertising()->start(); //␣
˓→Start advertising
Serial.println("Waiting for a client connection..."); //␣
˓→Wait for a client connection
}
Please note that this code allows for bidirectional communication - it can send and receive data via BLE. However, to
interact with specific hardware like turning on/off an LED, additional code should be added to process the received
strings and act accordingly.
1.39 7.2 Bluetooth Control RGB LED
This project is an extension of a previous project(7.1 Bluetooth), adding RGB LED configurations and custom com-
mands such as “led_off”, “red”, “green”, etc. These commands allow the RGB LED to be controlled by sending
commands from a mobile device using LightBlue.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name ITEMS IN THIS KIT LINK
ESP32 Starter Kit 320+
1.39. 7.2 Bluetooth Control RGB LED 141