AC_Protect.aia (191.8 KB)
I am learning how to use bluetoothBLE to exchange information with ESP32.
I followed what I found in my searches but the bluetoothBLE.stringsReceived is not displaying the data sent from ESP32.
When I press Read(meant "send to ESP32" ) I receive the message on the ESP32.
When I connect using nrf connect for mobile, I do see the message sent from the ESP32.
the esp32 code:
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb52ae1-36e1-4688-b7f5-ea07361b26a8"
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
Serial.print("New value: ");
for (int i = 0; i < value.length(); i++)
Serial.print(value[i]);
Serial.println();
}
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("MyESP32");
pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
uint32_t value=0;
void loop() {
// put your main code here, to run repeatedly:
char toSend[100];
sprintf(toSend,"%s:%d","Value: ",value);
pCharacteristic->setValue((char *)toSend);
pCharacteristic->notify();
Serial.println(toSend);
delay(5000);
value++;
}