#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)
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP* uS_TO_S_FACTOR);
Serial.flush();
esp_deep_sleep_start();
The device does enter deep sleep and then awakes on its own after the 60 second time interval. However, the awake time is very brief before it returns to deep sleep. I tried introducing delay(30000) at various points in my code to try to control the awake time, but I suspect that's not how to properly accomplish my goal. Can anyone provide some guidance as to where the Serial.flush(); and esp_deep_sleep_start(); commands should be inserted, and how to control the awake time? Here's my entire sketch:
Code: Select all
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <Adafruit_INA260.h>
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 SERVICE_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(SERVICE_UUID);
BLEService* pService = pServer->createService(BLESERVICE_UUID, 7, 0); //Can specify the number of characteristics (minimum 7)
// Create a 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);
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
// Create a BLE Descriptor
pVoltsCharacteristic->addDescriptor(new BLE2902());
pAmpsCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
BLEAdvertising* pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
//Serial.println("Waiting for client connection...");
}
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, in 6 hours test i was able to go as low as 3ms
}
// 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;
}
}