XIAO ESP32S3 Deep Sleep Mode - void loop() code not executing

Hello…I’ve inserted code to force my XIAO ESP32S3 into deep sleep mode for 60 secs, followed by 30 secs of awake time. When I run this sketch, the sleep and awake intervals happen as expected, but the code I’ve included in void loop() does not appear to be executing. There are no volts or amps values displayed in serial monitor, nor on the screen of an Android app I created to connect to the ESP32 via Bluetooth LE. Note that the volts and amps display as expected in both serial monitor and in my mobile app when the sleep mode code is commented out. Does anyone have any suggestions for why this is happening?

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <Adafruit_INA260.h>

#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 60          // Time ESP32 will go to sleep (in seconds)
#define WAKE_UP_TIME 30           // Time ESP32 will stay awake (in seconds)

Adafruit_INA260 ina260 = Adafruit_INA260();

BLEServer* pServer = NULL;
BLECharacteristic* pVoltsCharacteristic = NULL;
BLECharacteristic* pAmpsCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

static BLEUUID BLESERVICE_UUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
#define VOLTS_CHARACTERISTIC_UUID "7dd120c3-5d28-4264-88f9-f62233315f45"
#define AMPS_CHARACTERISTIC_UUID "3e37576d-9603-4637-b7b7-1d703e113268"

class MyServerCallbacks : public BLEServerCallbacks {
  void onConnect(BLEServer* pServer) {
    deviceConnected = true;
  }

  void onDisconnect(BLEServer* pServer) {
    deviceConnected = false;
  }
};

void setup() {
  Serial.begin(115200);
  ina260.begin();

  // Create the BLE Device
  BLEDevice::init("XIAO ESP32S3");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Services
  BLEService* pService = pServer->createService(BLESERVICE_UUID, 7, 0);  // Can specify the number of characteristics (minimum 7)

  // Create BLE Characteristics
  pVoltsCharacteristic = pService->createCharacteristic(
    VOLTS_CHARACTERISTIC_UUID,
    BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
  );
  pAmpsCharacteristic = pService->createCharacteristic(
    AMPS_CHARACTERISTIC_UUID,
    BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
  );

  // Create BLE Descriptors
  pVoltsCharacteristic->addDescriptor(new BLE2902());
  pAmpsCharacteristic->addDescriptor(new BLE2902());

  // Start the service
  pService->start();

  // Start advertising
  BLEAdvertising* pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(BLESERVICE_UUID);
  pAdvertising->setScanResponse(false);
  pAdvertising->setMinPreferred(0x0);  // Set value to 0x00 to not advertise this parameter
  BLEDevice::startAdvertising();

  // Sleep configuration
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  delay(WAKE_UP_TIME * 1000);  // Delay for the specified wake-up time
  Serial.flush();
  esp_deep_sleep_start();
}

void loop() {
  // notify changed value
  float voltsValue = (ina260.readBusVoltage() / 1000);
  float ampsValue = (ina260.readCurrent() / 1000);
  Serial.println(voltsValue, 3);
  Serial.println(ampsValue, 3);

  if (deviceConnected) {
    pVoltsCharacteristic->setValue(voltsValue);
    pVoltsCharacteristic->notify();
    pAmpsCharacteristic->setValue(ampsValue);
    pAmpsCharacteristic->notify();
    delay(2000);  // Bluetooth stack will go into congestion if too many packets are sent
  }

  // Disconnecting
  if (!deviceConnected && oldDeviceConnected) {
    delay(500);  // Give the Bluetooth stack the chance to get things ready
    pServer->startAdvertising();  // Restart advertising
    Serial.println("Start advertising...");
    oldDeviceConnected = deviceConnected;
  }

  // Connecting
  if (deviceConnected && !oldDeviceConnected) {
    // Do stuff here on connecting
    oldDeviceConnected = deviceConnected;
  }
}

The last command in setup() is to go into deepsleep ?

When the ESP32S3 comes out of deep sleep it will start at the beginning of setup() again.

Maybe you meant to do light sleep ?

Hi there,
You need the wakeup STUB, afaik. you can add that and the code will resume there.
I have not done it but I read that;'s one way of making it comeback awake and start where you want.
" ESP32-C3 supports running a “deep sleep wake stub” when coming out of deep sleep . This function runs immediately as soon as the chip wakes up - before any normal initialisation, bootloader, or ESP-IDF code has run. After the wake stub runs, the SoC can go back to sleep or continue to start ESP-IDF normally."
HTH
GL :slight_smile: PJ
:v:
Probably another way as well.