Chreece / BTHomeV2-ESP32-example

An BTHome v2 example with encryption for ESP32
MIT License
31 stars 7 forks source link

High Power Consumption in Deep Sleep #27

Open n1tr0-5urf3r opened 1 month ago

n1tr0-5urf3r commented 1 month ago

Hi all!

I am building a simple water sensor that reads a water level from a touch PIN, packs the result into a BLEAdvertisement and goes into deep sleep. However, during deep sleep the consumption is at 90mA which is way to high. Without the bthome.begin(DEVICE_NAME, false, "", false); call, I am at 10.5 mA consumption in deep sleep, so initializing the BLE device seems to be the issue. I already tried a few things to disable BLE before going to deep sleep (see below), but nothing seems to work. Any ideas?

Code:

/*
   Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
   Ported to Arduino ESP32 by pcbreflux
   Modified to work with NimBLE
   Modified for V2 by Chreece
   Modified by countrysideboy: Code cleanups, Chop Data, Encryption
   BLE advertisement format from https://bthome.io/

*/

#include "BTHome.h"
#include "NimBLEDevice.h"

#define DEVICE_NAME "Wassersensor"  // The name of the sensor
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 28800

#define NO_WATER 0
#define FEW_WATER 1
#define HALF_FULL_WATER 2
#define FULL_WATER 3

// Pin 32 is close to the bottom of the eimer
// Pin 15 at 50%
// Pin 4 at the top
const int touchPins[] = {4, 15, 32};
const int touchThreshold = 10;

int touchValue = 0;
int waterLevel = 0;
int batteryAnalogValue = 0;
float batteryVoltage = 0;

BTHome bthome;

int getWaterStatus(int pin){
  touchValue = touchRead(pin);
  //voltageMeasured = analogRead(buttonPin);

  if (touchValue <= touchThreshold){
     // Water detected
      return 1;
  } else {
      // Not enough water
     return 0;
  }
}

void sendBLEAdvertisement(int water_level){
  bthome.resetMeasurement();
  bthome.addMeasurement_state(EVENT_BUTTON, water_level);
  bthome.buildPaket();
  bthome.start();
  delay(2000);
  bthome.stop();
}

void setup() {
  bthome.begin(DEVICE_NAME, false, "", false);

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

  for (int pin : touchPins) {
    //Can have up to 4 touch pins. Each value adds +1
     waterLevel += getWaterStatus(pin);
  }

  sendBLEAdvertisement(waterLevel);

  // Trying to disable BT
  BLEDevice::deinit(true);
  btStop();
  esp_bt_controller_disable();
  esp_bt_controller_deinit();
  esp_bt_mem_release(ESP_BT_MODE_BTDM);

  esp_deep_sleep_start();  
}

void loop() {

}

//Object ids by order
#if 0
#define ID_PACKET               0x00
#define ID_BATTERY              0x01
#define ID_TEMPERATURE_PRECISE  0x02
#define ID_HUMIDITY_PRECISE     0x03
#define ID_PRESSURE             0x04
#define ID_ILLUMINANCE          0x05
#define ID_MASS                 0x06
#define ID_MASSLB               0x07
#define ID_DEWPOINT             0x08
#define ID_COUNT                0x09
#define ID_ENERGY               0x0A
#define ID_POWER                0x0B
#define ID_VOLTAGE              0x0C
#define ID_PM25                 0x0D
#define ID_PM10                 0x0E
#define STATE_GENERIC_BOOLEAN   0x0F
#define STATE_POWER_ON          0x10
#define STATE_OPENING           0x11
#define ID_CO2                  0x12
#define ID_TVOC                 0x13
#define ID_MOISTURE_PRECISE     0x14
#define STATE_BATTERY_LOW       0x15
#define STATE_BATTERY_CHARHING  0x16
#define STATE_CO                0x17
#define STATE_COLD              0x18
#define STATE_CONNECTIVITY      0x19
#define STATE_DOOR              0x1A
#define STATE_GARAGE_DOOR       0x1B
#define STATE_GAS_DETECTED      0x1C
#define STATE_HEAT              0x1D
#define STATE_LIGHT             0x1E
#define STATE_LOCK              0x1F
#define STATE_MOISTURE          0x20
#define STATE_MOTION            0x21
#define STATE_MOVING            0x22
#define STATE_OCCUPANCY         0x23
#define STATE_PLUG              0x24
#define STATE_PRESENCE          0x25
#define STATE_PROBLEM           0x26
#define STATE_RUNNING           0x27
#define STATE_SAFETY            0x28
#define STATE_SMOKE             0x29
#define STATE_SOUND             0x2A
#define STATE_TAMPER            0x2B
#define STATE_VIBRATION         0x2C
#define STATE_WINDOW            0x2D
#define ID_HUMIDITY             0x2E
#define ID_MOISTURE             0x2F
#define EVENT_BUTTON            0x3A
#define EVENT_DIMMER            0x3C
#define ID_COUNT2               0x3D
#define ID_COUNT4               0x3E
#define ID_ROTATION             0x3F
#define ID_DISTANCE             0x40
#define ID_DISTANCEM            0x41
#define ID_DURATION             0x42
#define ID_CURRENT              0x43
#define ID_SPD                  0x44
#define ID_TEMPERATURE          0x45
#define ID_UV                   0x46
#define ID_VOLUME1              0x47
#define ID_VOLUME2              0x48
#define ID_VOLUMEFR             0x49
#define ID_VOLTAGE1             0x4A
#define ID_GAS                  0x4B
#define ID_GAS4                 0x4C
#define ID_ENERGY4              0x4D
#define ID_VOLUME               0x4E
#define ID_WATER                0x4F
#endif
Chreece commented 1 month ago

Please try the bthome.stop() before going in deepsleep and report back

n1tr0-5urf3r commented 1 month ago

Hi Chris, thanks for the quick reply! I am already calling bthome.stop() in the sendBLEAdvertisement() function.

Chreece commented 1 month ago

Oh sorry, you are absolute right. I will try to investigate that