I cleaned up my code (remove the BLE connection) and here is the complete one
using namespace std;
const uint8_t pinWakeUp = D4;
const uint8_t pinMotContVCC = D5;
const uint8_t pinSerRX = D7;
const uint8_t pinSerTX = D6;
const uint8_t pinBeep = D9;
//const uint64_t WAKEUP_PINS = 0b11111111111111111111111111111111;
const uint64_t WAKEUP_PINS = 0b00111110;
#define sleepTime 5000 //in milisec
unsigned long gTimer = 0;
unsigned long gTimer2 = 0;
uint8_t gWrite = false;
void handleTimer(bool pGoSleep) {
if (HIGH == digitalRead(pinWakeUp)) {
gTimer = millis();
}
if(pGoSleep && millis() - gTimer> sleepTime) {
Serial.println("Sleep…");
digitalWrite(pinBeep,LOW);
esp_sleep_enable_timer_wakeup(10000);
esp_deep_sleep_enable_gpio_wakeup(WAKEUP_PINS,ESP_GPIO_WAKEUP_GPIO_HIGH);
// esp_deep_sleep_start();
}
if (millis() - gTimer2 > 500) {
digitalWrite(pinBeep,LOW);
}
}
void intVibr(void) {
//ESP.restart();
gTimer = millis();
gTimer2 = gTimer;
gWrite = true;
}
void setup() {
pinMode(D4, INPUT);
pinMode(pinWakeUp, INPUT);
pinMode(pinBeep, OUTPUT);
digitalWrite(pinBeep, HIGH);
delay(300);
digitalWrite(pinBeep, LOW);
delay(300);
digitalWrite(pinBeep, HIGH);
delay(300);
digitalWrite(pinBeep, LOW);
delay(300);
digitalWrite(pinBeep, HIGH);
gTimer2 = millis();
attachInterrupt(pinWakeUp, intVibr, CHANGE);
handleTimer(false);
Serial.begin(115200);
Serial.println("Starting Arduino BLE Client application...");
} // End of setup.
//*********************** This is the Arduino main loop function.
void loop() {
handleTimer(true);
Serial.write(".");
while (Serial.available()) {
gTimer = millis();
gWrite = true;
Serial.write(Serial.read());
}
if (gWrite) {
digitalWrite(pinBeep,HIGH);
Serial.write("+");
gWrite = false;
}
delay(200); // Delay between loops.
} // End of loop