# MystBin ! - Server_Test_Code.ino #include #include #include #include #define ble_max_conn 4 BLEServer* pServer = NULL; BLECharacteristic* pCharacteristic = NULL; BLECharacteristic* pCharacteristic2 = NULL; BLECharacteristic* pCharacteristic3 = NULL; bool deviceConnected = false; bool oldDeviceConnected = false; uint32_t value = 0; // Array to store client characteristic configuration descriptors BLE2902* clientConfigDescriptors[4]; bool buttonPressed = false; const int buttonPin = 6; int buttonState = LOW; int lastButtonState = LOW; unsigned long lastDebounceTime = 0; unsigned long debounceDelay = 50; #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" #define CHARACTERISTIC_UUID_2 "b3a543cc-f414-4fca-a107-0202a62443f8" #define CHARACTERISTIC_UUID_3 "c9e99fbb-c287-4acb-bcf6-16082b84b593" class MyServerCallbacks : public BLEServerCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; delay(100); pCharacteristic->notify(); BLEDevice::startAdvertising(); } void onDisconnect(BLEServer* pServer) { deviceConnected = false; } }; void setup() { Serial.begin(115200); // Create the BLE Device BLEDevice::init("ESP32"); BLEDevice::setMTU(500); // Allow multiple connections BLEDevice: // 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( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE ); pCharacteristic2 = pService->createCharacteristic( CHARACTERISTIC_UUID_2, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE ); pCharacteristic3 = pService->createCharacteristic( CHARACTERISTIC_UUID_3, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE ); // Create a BLE Descriptor pCharacteristic->addDescriptor(new BLE2902()); // Start the service pService->start(); // Start advertising BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(false); pAdvertising->setMinPreferred(0x0); BLEDevice::startAdvertising(); // Set up the button as an input pinMode(buttonPin, INPUT_PULLUP); } void loop() { int reading = digitalRead(buttonPin); if (reading != lastButtonState) { lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; if (buttonState == HIGH) { Serial.println("Button pressed"); if (deviceConnected) { uint8_t data = 0xFF; pCharacteristic->setValue(&data, 1); // Set the value as a byte array pCharacteristic->notify(); Serial.println("Data updated!"); } buttonPressed = true; } else { Serial.println("Button released"); if (deviceConnected) { uint8_t data = 0x00; pCharacteristic->setValue(&data, 1); // Set the value as a byte array pCharacteristic->notify(); Serial.println("Data updated!"); } buttonPressed = false; } } } lastButtonState = reading; if (!deviceConnected && oldDeviceConnected) { pServer->startAdvertising(); Serial.println("Start advertising"); oldDeviceConnected = deviceConnected; } if (deviceConnected && !oldDeviceConnected) { // Actions to perform on connecting oldDeviceConnected = deviceConnected; } }