// Garage Door Controller - ESP32 Firmware written by BRAIDEN S. PSIUK #include #include #include // Initialize all pointers BLEServer* pServer = NULL; // Pointer to the server BLECharacteristic* pCharacteristic = NULL; // Pointer to Characteristic //BLE2902 *pBLE2902; // Pointer to BLE2902 of Characteristic // Some variables to keep track on device connected bool deviceConnected = false; bool oldDeviceConnected = false; // Variable that will continuously be increased and written to the client uint32_t value = 0; // Define UUIDs #define SERVICE_UUID "e45f0000-e483-5bc5-7532-95ae2f7fffcb" #define CHARACTERISTIC_UUID "e45f0001-e483-5bc5-7532-95ae2f7fffcb" #define DELAY_VAL 10000 // Callback function that is called whenever a client is connected or disconnected class MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { deviceConnected = true; }; void onDisconnect(BLEServer* pServer) { deviceConnected = false; } }; void toggleDoor(const String doorNumber) { switch(doorNumber.toInt()) { case 4041: digitalWrite(21, LOW); delay(DELAY_VAL); digitalWrite(21, HIGH); // delay(DELAY_VAL); break; case 4042: digitalWrite(22, LOW); delay(DELAY_VAL); digitalWrite(22, HIGH); // delay(DELAY_VAL); break; default: digitalWrite(LED_BUILTIN, HIGH); delay(DELAY_VAL); digitalWrite(LED_BUILTIN, LOW); // delay(DELAY_VAL); } } class CharacteristicCallBack: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pChar) override { std::string pchar_value_stdstr = pChar -> getValue(); String pChar_value_string = String(pchar_value_stdstr.c_str()); Serial.println("Received: "+pChar_value_string); toggleDoor(pChar_value_string); } }; void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(21, OUTPUT); pinMode(22, OUTPUT); digitalWrite(21, HIGH); digitalWrite(22, HIGH); Serial.begin(115200); // Create the BLE Device BLEDevice::init("Garage Door Controller"); // Create the BLE Server pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); // Create the BLE Service BLEService *pService = pServer->createService(SERVICE_UUID); pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, // BLECharacteristic::PROPERTY_READ | // BLECharacteristic::PROPERTY_WRITE | // BLECharacteristic::PROPERTY_NOTIFY BLECharacteristic::PROPERTY_WRITE ); // pBLE2902 = new BLE2902(); // pBLE2902->setNotifications(true); // pCharacteristic->addDescriptor(pBLE2902); // add callback functions here: pCharacteristic->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..."); Serial.println("Waiting a client connection..."); } void loop() { // 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; } }