#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex
RTC_DATA_ATTR int bootCount = 0;
/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 10; //In seconds
BLEScan* pBLEScan;
#include <BleSerial.h>
#include <esp_attr.h>
#include <esp_task_wdt.h>
#include <driver/rtc_io.h>
#include "soc/rtc_wdt.h"
const int BUFFER_SIZE = 8192;
BleSerial SerialBT;
uint8_t unitMACAddress[6]; // Use MAC address in BT broadcast and display
char deviceName[20]; // The serial string that is broadcast.
uint8_t bleReadBuffer[BUFFER_SIZE];
uint8_t serialReadBuffer[BUFFER_SIZE];
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};
void blescan(){
Serial.println("Scanning...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void blescanresult(){
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}
void startBluetooth() {
// Get unit MAC address
esp_read_mac(unitMACAddress, ESP_MAC_WIFI_STA);
unitMACAddress[5] += 2;
//Create device name
sprintf(deviceName, "Serial Communication", unitMACAddress[4], unitMACAddress[5]);
//Init BLE Serial
SerialBT.begin(deviceName);
SerialBT.setTimeout(10);
}
//Task for reading Serial Port
void ReadSerialTask(void *e) {
while (true) {
if (Serial.available()) {
auto count = Serial.readBytes(serialReadBuffer, BUFFER_SIZE);
SerialBT.write(serialReadBuffer, count);
}
delay(20);
}
}
//Task for reading BLE Serial
void ReadBtTask(void *e) {
while (true) {
if (SerialBT.available()) {
auto count = SerialBT.readBytes(bleReadBuffer, BUFFER_SIZE);
Serial.write(bleReadBuffer, count);
}
delay(20);
}
}
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
void setup(){
Serial.begin(115200);
delay(1000); //Take some time to open up the Serial Monitor
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
//Print the wakeup reason for ESP32
print_wakeup_reason();
while(esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,1)); //1 = High, 0 = Low
{
blescan();
blescanresult();
startBluetooth();
Serial.setRxBufferSize(BUFFER_SIZE);
Serial.setTimeout(10);
}
//Go to sleep now
Serial.println("Going to sleep now");
esp_deep_sleep_start();
Serial.println("This will never be printed");
}
void loop(){
}
I want to wake up the esp32 with a push button and let the bluetooth work and go to sleep when bluetooth is disconnected. It does the BLE scan but goes to bed right way without starting the ble connection.Any help would be greatly appreciated.