//==============================
// Making LED flash wirelessly + external LED flash
//==============================
#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>
// Define LED pins for Blue and White LEDs
#define BLUE_LED_PIN 2 // Blue LED connected to GPIO 2
#define WHITE_LED_PIN 4 // White LED connected to GPIO 4
// Define UUIDs for the service and characteristics
#define SERVICE_UUID "12345678-1234-1234-1234-123456789012" // UUID for the BLE service
#define CHARACTERISTIC_UUID_BLUE_LED "8765" // UUID for the Blue LED characteristic
#define CHARACTERISTIC_UUID_WHITE_LED "8765" // UUID for the White LED characteristic
// Declare BLE characteristic pointers for both LEDs
BLECharacteristic *pBlueLEDCharacteristic;
BLECharacteristic *pWhiteLEDCharacteristic;
// Connection flag
bool deviceConnected = false;
// Callback class for handling BLE server connection and disconnection events
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *pServer) {
deviceConnected = true;
Serial.println("BLE device connected");
}
void onDisconnect(BLEServer *pServer) {
deviceConnected = false;
Serial.println("BLE device disconnected");
}
};
void setup() {
Serial.begin(115200);
pinMode(BLUE_LED_PIN, OUTPUT); // Set the Blue LED pin as output
pinMode(WHITE_LED_PIN, OUTPUT); // Set the White LED pin as output
pinMode(4, OUTPUT); // External LED on GPIO 4
Serial.print("Hello :)");
// Initialize the BLE device with a name
BLEDevice::init("ESP32_BLE2LEDS");
// Create the BLE Server and set the callbacks
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create the Blue LED characteristic
pBlueLEDCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_BLUE_LED,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
);
// Create the White LED characteristic
pWhiteLEDCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_WHITE_LED,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
);
// Add descriptors
pBlueLEDCharacteristic->addDescriptor(new BLE2902());
pWhiteLEDCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->start();
Serial.println("Waiting for a client to connect...");
}
void loop() {
// BLE LED control
if (deviceConnected) {
String blueLEDValue = pBlueLEDCharacteristic->getValue().c_str();
if (blueLEDValue == "1") {
digitalWrite(BLUE_LED_PIN, HIGH);
} else {
digitalWrite(BLUE_LED_PIN, LOW);
}
String whiteLEDValue = pWhiteLEDCharacteristic->getValue().c_str();
if (whiteLEDValue == "1") {
digitalWrite(WHITE_LED_PIN, HIGH);
} else {
digitalWrite(WHITE_LED_PIN, LOW);
}
}
// External LED flash
digitalWrite(4, HIGH);
delay(1000);
digitalWrite(4, LOW);
delay(1000);
delay(100); // Small delay to avoid excessive CPU usage
}