 
 #define INPUT_PIN 0
 #define DEBOUNCE_TIME_MILLIS 50
+#define SLEEP_TIME_MILLIS 10000
 
 BLEServer* pServer = NULL;
 BLECharacteristic* pCharacteristic = NULL;

bool connected = false;
bool pendingValue = false;


class BLECallback: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      Serial.println("Connected");
      connected = true;
    };


}


void go_to_sleep(){
  esp_sleep_enable_ext0_wakeup(GPIO_NUM_0,0);
  Serial.println("Sleeping...");
  delay(10);
  esp_light_sleep_start();
  Serial.println("Woken up");
  connected = false;
  newValue();
}


void loop() {
  while(pendingValue == true){
    // Wait for pin to stabilize
    delay(100);
    if(notify()){
      pendingValue = false;
    }
  }
  if(connected == true){
    if(millis() - lastEvent > SLEEP_TIME_MILLIS){
      go_to_sleep();
    }
  }
}
