#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs: // Server code
// https://www.uuidgenerator.net/
#define SERVICE_UUID "79c79196-51cb-4f67-9520-9bc63e6597c1"
#define CHARACTERISTIC_UUID "e3038128-ca03-4f22-9f0d-b6547426dc6a"
#define led_Left 2
#define led_Right 4
BLEServer *pServer;
BLEService *pService;
BLECharacteristic *pCharacteristic;
void setup()
{
Serial.begin(115200);
Serial.println("Starting BLE Server!");
BLEDevice::init("ESP32-BLE-Server");
pServer = BLEDevice::createServer();
pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pinMode(led_Left,OUTPUT);
pinMode(led_Right,OUTPUT);
pCharacteristic->setValue("Waiting for signal!");
pService->start();
//BLEAdvertising *pAdvertising = pServer->getAdvertising();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
//pAdvertising->start();
Serial.println("Characteristic defined! Now you can read it in the Client!");
}
void loop()
{
std::string value = pCharacteristic->getValue(); // string type
Serial.print("The new characteristic value is: ");
Serial.println(value.c_str()); // This array includes the same sequence of characters that make up the value
// of the basic_string object plus an additional terminating null-character at the end.
if(value == "left" ) // comparison must be string to string, that's why it works
{
digitalWrite(led_Left,HIGH);
delay(10);
digitalWrite(led_Left,LOW);
delay(10);
digitalWrite(led_Left,HIGH);
delay(10);
digitalWrite(led_Left,LOW);
delay(10);
digitalWrite(led_Left,HIGH);
delay(10);
digitalWrite(led_Left,LOW);
delay(10);
};
if(value == "right") // comparison must be string to string, that's why it works
{
digitalWrite(led_Right,HIGH);
delay(10);
digitalWrite(led_Right,LOW);
delay(10);
digitalWrite(led_Right,HIGH);
delay(10);
digitalWrite(led_Right,LOW);
delay(10);
digitalWrite(led_Right,HIGH);
delay(10);
digitalWrite(led_Right,LOW);
delay(10);
};
delay(50);
}