/* Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp Ported to Arduino ESP32 by Evandro Copercini updates by chegewara */ #include #include #include #include int lux; int mini; // See the following for generating UUIDs: // https://www.uuidgenerator.net/ #define SERVICE_UUID "f608bae5-6eec-4be7-aafb-2bd43ce942bf" #define POWER_READ_UUID "06c79a69-54b1-46e9-9b1d-afb77e6fad15" #define POWER_CHECK_UUID "f54a0bdf-afa3-4d18-a96c-770ba5bc3577" #define SEUILS_UUID "e7588746-cde4-44c4-aec6-8ef838a71480" bool estConnecte = false; char msgTOsend[30]; //char rxValue [255]; class MyCallback : public BLECharacteristicCallbacks { void onRead(BLECharacteristic* pCharacteristic) { // Do something before the read completes int lux=random(0, 9999); sprintf(msgTOsend,"Puissance : %d",lux); pCharacteristic->setValue(msgTOsend); Serial.printf("Hello : %d \n",lux); //Serial.print('\n'): } void onWrite(BLECharacteristic* pCharacteristic) { // Do something because a new value was written. std::string rxValue=pCharacteristic->getValue(); //ex tel envoi 5000 mini = atoi(rxValue.c_str()); Serial.print("message received :"); Serial.println(rxValue.c_str()); // si valeur numérique : //uint8_t* received_data = pCharacteristic->getData(); // Serial.println(*received_data,HEX); } }; class EtatServeur : public BLEServerCallbacks { void onConnect(BLEServer* pServer) { estConnecte = true; Serial.println("Connecté"); } void onDisconnect(BLEServer* pServer) { estConnecte = false; BLEDevice::startAdvertising(); Serial.println("Déconnecté"); } }; class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { void onResult(BLEAdvertisedDevice advertisedDevice) { Serial.printf("Advertised Device: %s", advertisedDevice.toString().c_str()); if (advertisedDevice.haveRSSI()){ Serial.printf("Rssi: %d \n", (int)advertisedDevice.getRSSI()); } else { Serial.printf("\n"); } } }; BLECharacteristic *pCharacteristic; void setup() { Serial.begin(115200); Serial.println("Starting BLE "); BLEDevice::init("PomPom"); BLEServer *pServer = BLEDevice::createServer(); pServer->setCallbacks(new EtatServeur()); BLEService *pService = pServer->createService(SERVICE_UUID); //BLECharacteristic *pCharacteristic = pService->createCharacteristic( pCharacteristic = pService->createCharacteristic( POWER_READ_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE ); pCharacteristic->setValue("Hello ESP32 "); //BLECharacteristic::setCallbacks(new MyCallback()) pCharacteristic->setCallbacks(new MyCallback()); 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);*/ //pAdvertising->setCallbacks(new MyAdvertisedDeviceCallbacks()); BLEDevice::startAdvertising(); Serial.println("Characteristic defined! Now you can read it in your phone!"); } void loop() { //float h = getRssi(); // put your main code here, to run repeatedly: if (estConnecte) { lux=random(0, 9999); if(luxsetValue(msgTOsend); pCharacteristic->notify(); } } delay(2000); //BLEDevice::startAdvertising(); }