/* Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp Ported to Arduino ESP32 by Evandro Copercini updated by chegewara and MoThunderz */ #include #include #include #include #define led_pin 27 uint32_t counterpointer=3; BLEServer* pServer = NULL; BLECharacteristic* pCharacteristic = NULL; BLECharacteristic* pCharacteristic_2 = NULL; BLEDescriptor *pDescr; BLE2902 *pBLE2902; bool deviceConnected = false; bool oldDeviceConnected = false; uint32_t value = 0; // See the following for generating UUIDs: // https://www.uuidgenerator.net/ #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #define CHAR1_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" #define CHAR2_UUID "e3223119-9445-4e96-a4a1-85358c4046a2" class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; }; void onDisconnect(BLEServer* pServer) { deviceConnected = false; } }; class CharacteristicCallBack: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pChar) override { std::string pChar2_value_stdstr = pChar->getValue(); String pChar2_value_string = String(pChar2_value_stdstr.c_str()); int pChar2_value_int = pChar2_value_string.toInt(); counterpointer=pChar2_value_int; Serial.println("pChar2: " + String(pChar2_value_int)); } }; void setup() { Serial.begin(115200); pinMode(led_pin,OUTPUT); // Create the BLE Device BLEDevice::init("ESP32"); // Create the BLE Server pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); // Create the BLE Service BLEService *pService = pServer->createService(SERVICE_UUID); // Create a BLE Characteristic pCharacteristic = pService->createCharacteristic( CHAR1_UUID, BLECharacteristic::PROPERTY_NOTIFY ); pCharacteristic_2 = pService->createCharacteristic( CHAR2_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE ); // Create a BLE Descriptor pDescr = new BLEDescriptor((uint16_t)0x2901); pDescr->setValue("A very interesting variable"); pCharacteristic->addDescriptor(pDescr); pBLE2902 = new BLE2902(); pBLE2902->setNotifications(true); // Add all Descriptors here pCharacteristic->addDescriptor(pBLE2902); pCharacteristic_2->addDescriptor(new BLE2902()); // After defining the desriptors, set the callback functions pCharacteristic_2->setCallbacks(new CharacteristicCallBack()); // Start the service pService->start(); // Start advertising BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(false); pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter BLEDevice::startAdvertising(); Serial.println("Waiting a client connection to notify..."); } void loop() { // notify changed value if (deviceConnected) { pCharacteristic->setValue(value); pCharacteristic->notify(); value++; delay(1000); if (counterpointer==1){ digitalWrite(led_pin,HIGH); Serial.println("helooooooooooooooo"); // Serial.println(digitalRead(led_pin)); // tone(piezo, 1000); // Send 1KHz sound signal... // delay(1000); // ...for 1 sec // noTone(piezo); // Stop sound... // delay(1000); // ...for 1sec }else{ digitalWrite(led_pin,LOW); } } // disconnecting if (!deviceConnected && oldDeviceConnected) { delay(500); // give the bluetooth stack the chance to get things ready pServer->startAdvertising(); // restart advertising Serial.println("start advertising"); oldDeviceConnected = deviceConnected; } // connecting if (deviceConnected && !oldDeviceConnected) { // do stuff here on connecting oldDeviceConnected = deviceConnected; } }