And I don’t understand how to write a sketch on the ESP32 client so that he would do the same without a smartphone.
Who understands please write how to do it.
Thank you very much in advance!
Code ESP32 Server:
- #include <BLEDevice.h>
- #include <BLEUtils.h>
- #include <BLEServer.h>
- // See the following for generating UUIDs:
- // https://www.uuidgenerator.net/
- #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
- #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
- #define ELEM_UUID "cc46b944-003e-42b6-b836-c4246b8f19a0" // ID
- void setup() {
- Serial.begin(115200);
- Serial.println("Starting BLE work!");
- BLEDevice::init("Long name works now");
- BLEServer *pServer = BLEDevice::createServer();
- BLEService *pService = pServer->createService(SERVICE_UUID);
- BLECharacteristic *pCharacteristic = pService->createCharacteristic(
- CHARACTERISTIC_UUID,
- BLECharacteristic::PROPERTY_READ |
- BLECharacteristic::PROPERTY_WRITE
- );
- BLECharacteristic *pElem;
- class ElemCallbacs : public BLECharacteristicCallbacks {
- void onWrite(BLECharacteristic *pCharacteristic) {
- std::string value1 = pCharacteristic->getValue();
- Serial.println(value1.c_str());
- }
- };
- pElem = pService->createCharacteristic(ELEM_UUID,BLECharacteristic::PROPERTY_READ| BLECharacteristic::PROPERTY_WRITE);
- pElem->setCallbacks(new ElemCallbacs());
- pCharacteristic->setValue("Hello World says Neil");
- pService->start();
- // BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
- 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();
- Serial.println("Characteristic defined! Now you can read it in your phone!");
- }
- void loop() {
- // put your main code here, to run repeatedly:
- delay(2000);
- }