Sleep mode on timer help

#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP  5

RTC_DATA_ATTR int bootCount = 0;
#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];

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;

uint32_t value = 0;
void onConnect(BLEServer *pServer);
void onDisconnect(BLEServer *pServer);

bool bleConnected;

void print_wakeup_reason(){
   esp_sleep_wakeup_cause_t wake_up_source;

   wake_up_source = esp_sleep_get_wakeup_cause();

   switch(wake_up_source){
      case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wake-up from external signal with RTC_IO"); break;
      case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wake-up from external signal with RTC_CNTL"); break;
      case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wake up caused by a timer"); break;
      case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wake up caused by a touchpad"); break;
      default : Serial.printf("Wake up not caused by Deep Sleep: %d\n",wake_up_source); break;
   }
}
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 setup(){
    Serial.begin(115200);

    ++bootCount;
    Serial.println("----------------------");
    Serial.println(String(bootCount)+ "th Boot ");

    // Displays the reason for the wake up
    print_wakeup_reason();

    //Timer Configuration
    esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
    Serial.println("ESP32 wake-up in " + String(TIME_TO_SLEEP) + " seconds");
    Serial.setRxBufferSize(BUFFER_SIZE);
    Serial.setTimeout(10);

    //Start BLE
    startBluetooth();
    if(SerialBT.available()){
        if(bleConnected == true)
        {
        //Disable watchdog timers
        disableCore0WDT();
        disableCore1WDT();
        disableLoopWDT();
        esp_task_wdt_delete(NULL);
        rtc_wdt_protect_off();
        rtc_wdt_disable();
        Serial.println("ble is connected");

        //Start tasks
        xTaskCreate(ReadSerialTask, "ReadSerialTask", 10240, NULL, 1, NULL);
        xTaskCreate(ReadBtTask, "ReadBtTask", 10240, NULL, 1, NULL);
        }
        delay(100000);

        // Go in Deep Sleep mode
        if(deviceConnected == false)
        {
        Serial.println("Goes into Deep Sleep mode");
        Serial.println("----------------------");
        delay(100);
        esp_deep_sleep_start();
        Serial.println("This will never be displayed");
        }
    }
}

void loop(){
}

why is my if statement not working here. m i doing something wrong? what i want is when ble is connected == true then it will start the serial communication but when it is not connected then it will go back to sleep.

here is my output:
ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)

configsip: 0, SPIWP:0xee

clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00

mode:DIO, clock div:1

load:0x3fff0030,len:1344

load:0x40078000,len:13924

ho 0 tail 12 room 4

load:0x40080400,len:3600

entry 0x400805f0


1th Boot

Wake up not caused by Deep Sleep: 0

ESP32 wake-up in 5 seconds
and it stops there due to the delay.

  if (SerialBT.available()) {
    if (bleConnected == true)
...

your connection test is inside your available test. What makes you think there will be any serial data immediately available when you start it up? If nothing is available, none of that code gets executed.

Also, nowhere in your code do you assign a value to bleConnected so it will never be true.

Side note. This statement

sprintf(deviceName, "Serial Communication", unitMACAddress[4], unitMACAddress[5]);

is the same as

strcpy(deviceName, "Serial Communication");

since you don't provide any format specifiers.

what would you suggest then? please help me i have been struggling on this.

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