Sleep mode help please

#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.

Try adding serial debug prints, to narrow it down to the lines in the code where this is happening.

That is a complete syntax correct while statement, is that what you meant?

a7

i might be wrong on there. what do you suggest i do? use event handling maybe?

i did add couple serial prints but it executes them and then goes to sleep right away.

Then add another one right after the last one you put in. Rinse and repeat. :slight_smile:

that is not helping. i did it many times. do you know why it might be triggering to sleep right after the execution?

Post the entire sketch, with the latest additions you have made.

I asked for the entire sketch.

Now post the serial output, literally copy and paste.

#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);
    Serial.println("its happenning");
    Serial.println("it really is!!!!!!!");
  }
  //Go to sleep now
  Serial.println("Going to sleep now");
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}

void loop(){

}

output is :
Devices found: 62

Scan done!

its happenning

it really is!!!!!!!

Going to sleep now

  while(esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,1)); //1 = High, 0 = Low


  blescan();
  blescanresult();
  startBluetooth();
  Serial.setRxBufferSize(BUFFER_SIZE);
  Serial.setTimeout(10);
  Serial.println("its happenning");
  Serial.println("it really is!!!!!!!");

That's your code, formatted with the unnecessary braces removed. Because this

while(esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,1)); //1 = High, 0 = Low

Is a complete syntax correct while statement. Controlling the execution of the empty statement which is the semicolon.

Also ReadBgTask() appears to have an infinite loop?

a7

Are you telling me that this is the complete serial output? No more? I suspect you didn't copy and paste the entire serial output. I asked you not to paraphrase it, to post the entire message literally.

I don't disbelieve you. I just need to see the exact messages in order to extract the maximum information from it...

Advertised Device: Name: , Address: 2e:87:82:af:79:84, serviceUUID: 0000fd6f-0000-1000-8000-00805f9b34fb, rssi: -97, serviceData: �C�~�����5

Advertised Device: Name: , Address: 41:09:96:33:3b:ff, manufacturer data: 4c000719010e2022f58f000004d72dbe714ff2e78ae039413424931776, rssi: -95

Advertised Device: Name: , Address: fd:bd:b5:e5:ef:db, manufacturer data: ffff07020204310603320185bc, rssi: -95

Advertised Device: Name: , Address: f0:50:b0:5b:07:0b, manufacturer data: 4c0012021400, rssi: -97

Advertised Device: Name: , Address: f2:1d:6f:e0:17:13, manufacturer data: 4c0012020003, rssi: -95

Advertised Device: Name: , Address: c5:fd:c0:ed:be:f6, manufacturer data: ffff080300042b07ff11001242, rssi: -95

Advertised Device: Name: , Address: ef:5a:94:82:b2:f6, manufacturer data: 4c0012020000, rssi: -94

Advertised Device: Name: , Address: cc:6d:fe:cf:a1:ae, manufacturer data: ffff070202043103d53202d106, rssi: -10

Advertised Device: Name: , Address: f7:eb:76:45:d8:f3, manufacturer data: ffff070202043103333202e976, rssi: -96

Advertised Device: Name: , Address: 4b:08:4d:48:b0:30, manufacturer data: 4c0010063a1e2cb1feb9, txPower: 7, rssi: -94

Advertised Device: Name: , Address: ef:12:a5:84:2d:ae, manufacturer data: 4c0012020002, rssi: -99

Devices found: 62

Scan done!

its happenning

it really is!!!!!!!

Going to sleep now

Ln 132, Col 40

ESP32-WROOM-DA Module

on COM12

2

I told you, I don't disbelieve you.

What is this part?

Ln 132, Col 40

ESP32-WROOM-DA Module

on COM12

2

It doesn't look like a serial output...

when i copied and pasted it got included for some reason. but the serial output end after going to sleep now!

is there maybe an event handler i can use. i am just struggling on it.

Get some sleep, then read the forum instructions on the most effective way to communicate in the forum. It's a 24 hour forum, maybe someone will post a solution while you sleep...

When the ESP32 is in deepsleep the GPIO API is disabled. BUT, the RTC_GPIO can be used or the API can be set to not turn off certain GPIO pins.

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/gpio.html

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/sleep_modes.html?highlight=deep%20sleep

some research may be required by the OP.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.