I am a beginner in ESP32 and I am learning how to connect LDR and Led on ESP32 connected to Web via Bluetooth. I found the tutorial from the following site: Control ESP32 GPIO Pins using Web Bluetooth
But why does the sketch in the tutorial display an error when uploaded ? :
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
// BLE UUIDs
#define SERVICE_UUID "12345678-1234-1234-1234-123456789abc"
#define LED_CHAR_UUID "abcdef01-2345-6789-0123-456789abcdef"
#define LDR_CHAR_UUID "abcdef02-2345-6789-0123-456789abcdef"
// GPIO Pins
#define LED_PIN 2
#define LDR_PIN 34
BLECharacteristic *pLedCharacteristic;
BLECharacteristic *pLdrCharacteristic;
bool deviceConnected = false;
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("Device connected");
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("Device disconnected");
pServer->getAdvertising()->start(); // Restart advertising
}
};
class LedCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
Serial.print("LED command: ");
Serial.println(value.c_str());
if (value == "1") digitalWrite(LED_PIN, HIGH);
else if (value == "0") digitalWrite(LED_PIN, LOW);
}
}
};
void setup() {
Serial.begin(115200);
// Initialize GPIO
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // LED off by default
// Initialize BLE
BLEDevice::init("ESP32_GPIO_Control");
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// LED Characteristic (writeable)
pLedCharacteristic = pService->createCharacteristic(
LED_CHAR_UUID,
BLECharacteristic::PROPERTY_WRITE
);
pLedCharacteristic->setCallbacks(new LedCallbacks());
// LDR Characteristic (readable, notifying)
pLdrCharacteristic = pService->createCharacteristic(
LDR_CHAR_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
);
pLdrCharacteristic->addDescriptor(new BLE2902());
// Start service and advertising
pService->start();
pServer->getAdvertising()->start();
Serial.println("BLE server started. Waiting for connection...");
}
void loop() {
if (deviceConnected) {
// Read LDR value (0-4095) and map to percentage (0-100)
int ldrValue = analogRead(LDR_PIN);
int ldrPercent = map(ldrValue, 0, 4095, 0, 100);
String ldrString = String(ldrPercent);
// Update LDR characteristic and notify
pLdrCharacteristic->setValue(ldrString.c_str());
pLdrCharacteristic->notify();
delay(500); // Update every 0.5 seconds
}
}
And here is the error that appears when uploading:
Web_BLE_2.ino:33:50: error: conversion from 'String' to non-scalar type 'std::string' {aka 'std::__cxx11::basic_string<char>'} requested
33 | std::string value = pCharacteristic->getValue();
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~
exit status 1
Compilation error: conversion from 'String' to non-scalar type 'std::string' {aka 'std::__cxx11::basic_string<char>'} requested