Page 1 of 1

Cannot get a Lolin32 to low-power upon deep sleep.

Posted: Wed Apr 04, 2018 3:42 pm
by rin67630
My aim is to get a Lolin32 module to low power.

The following code is supposed to do these actions:
Upon first run: connect to WiFi, synchronize the RTC with NTP, get the time into variables, print the time and reset reason.
Then go to deep sleep for 35 seconds.
Upon retun from deep sleep, detect that state, do not reconnect to WiFi, do not resynchronize the RTC, get the time into variables from the RTC, print the time and reset reason.
Then go to deep sleep again for 35 seconds and so on.

According to the serial Monitor, it does what I want.

The only problem is that it still draws 72mA @ 4,1V from the LiPo, that isnt exactly what I expected from deep sleep!

Code:

Code: Select all

#include <WiFi.h>
#include "time.h"
#include <rom/rtc.h> //needed to get reset reason

#define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  35       /* ESP32 will go to sleep for (x seconds) */

const char* ssid       = "SSID";
const char* password   = "PASS";
const char* ntpServer = "de.pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;
int second;
int minute;
int hour;
int day;
int month;
int year;
int weekday;
struct tm timeinfo;
int resetReason0;
int resetReason1;


RTC_DATA_ATTR int bootCount = 0; //Boot count should be stored in the non volatile RTC Memory

//Parameters see: http://www.cplusplus.com/reference/ctime/tm/

void setup()
{
  Serial.begin(115200);
  resetReason0 = rtc_get_reset_reason(0);
  resetReason1 = rtc_get_reset_reason(1);
  esp_sleep_wakeup_cause_t wakeup_reason;
  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch (wakeup_reason)
  {
    case 1  : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case 2  : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case 3  : Serial.println("Wakeup caused by timer");
      WiFi.disconnect(true);
      WiFi.mode(WIFI_OFF);
      break;
    case 4  : Serial.println("Wakeup caused by touchpad"); break;
    case 5  : Serial.println("Wakeup caused by ULP program"); break;
    default : Serial.println("Wakeup was not caused by deep sleep");
      if (!getLocalTime(&timeinfo))
      {
        //connect to WiFi
        Serial.printf("Connecting to %s ", ssid);
        WiFi.begin(ssid, password);
        while (WiFi.status() != WL_CONNECTED) {
          delay(500);
          Serial.print(".");
        }
        Serial.println(" CONNECTED");

        //init and get the time
        configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
        printLocalTime();

        //disconnect WiFi as it's no longer needed
        WiFi.disconnect(true);
        WiFi.mode(WIFI_OFF);
      }
      break;
  }
  bootCount++;
}

void loop()
{
  //printLocalTime();
  getTimeValues();
  Serial.print("Time from variables:  ");
  Serial.print(day);
  Serial.print(".");
  Serial.print(month);
  Serial.print(".");
  Serial.print(year);
  Serial.print(" --- ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.print(second);
  Serial.print("  Boot count: ");
  Serial.print(bootCount);
  Serial.print("  Reset reason: ");
  Serial.println(resetReason0);  
  
  Serial.println("Setup ESP32 to sleep for " + String(TIME_TO_SLEEP) + " Seconds");
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  Serial.println("Going to sleep now");
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}


void print_wakeup_reason()
{
  esp_sleep_wakeup_cause_t wakeup_reason;
  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch (wakeup_reason)
  {
    case 1  : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case 2  : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case 3  : Serial.println("Wakeup caused by timer"); break;
    case 4  : Serial.println("Wakeup caused by touchpad"); break;
    case 5  : Serial.println("Wakeup caused by ULP program"); break;
    default : Serial.println("Wakeup was not caused by deep sleep"); break;
  }
}

void getTimeValues()
{
  getLocalTime(&timeinfo);
  second = timeinfo.tm_sec;
  minute = timeinfo.tm_min;
  hour = timeinfo.tm_hour;
  day = timeinfo.tm_mday;
  month = timeinfo.tm_mon + 1;
  year = timeinfo.tm_year + 1900;
  weekday = timeinfo.tm_wday + 1;
}

void printLocalTime()
{

  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %d %B %Y %H:%M:%S");
  //Parameters see: http://www.cplusplus.com/reference/ctime/strftime/
}
Serial Monitor:

Code: Select all

clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:812
load:0x40078000,len:0
load:0x40078000,len:11404
entry 0x40078a28
Wakeup was not caused by deep sleep
Connecting to GW-FM-MA .... CONNECTED
Wednesday, 04 April 2018 17:18:10
Time from variables:  4.4.2018 --- 17:18:10  Boot count: 1
  Reset reason: 1
Setup ESP32 to sleep for 35 Seconds
Going to sleep now
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_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:2
load:0x3fff0018,len:4
load:0x3fff001c,len:812
load:0x40078000,len:0
load:0x40078000,len:11404
entry 0x40078a28
Wakeup caused by timer
Time from variables:  4.4.2018 --- 15:18:45  Boot count: 2
  Reset reason: 5
Setup ESP32 to sleep for 35 Seconds
Going to sleep now
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_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:2
load:0x3fff0018,len:4
load:0x3fff001c,len:812
load:0x40078000,len:0
load:0x40078000,len:11404
entry 0x40078a28
Wakeup caused by timer
Time from variables:  4.4.2018 --- 15:19:21  Boot count: 3
  Reset reason: 5
Setup ESP32 to sleep for 35 Seconds
Going to sleep now
ets Jun  8 2016 00:22:57

Re: Cannot get a Lolin32 to low-power upon deep sleep.

Posted: Wed Apr 04, 2018 9:15 pm
by rin67630
The near zero consumption upon deep sleep works upon powering by +5V, but NOT upon powering from the LiPo connector ???

Re: Cannot get a Lolin32 to low-power upon deep sleep.

Posted: Thu Apr 05, 2018 1:49 am
by WiFive
Should be 150uA

Re: Cannot get a Lolin32 to low-power upon deep sleep.

Posted: Thu Apr 05, 2018 7:29 am
by rin67630
WiFive wrote:Should be 150uA
That's the quiescent current of a bare-metal ESP32.
The rest of the LoLin32 board electronics (regulator, usb-chip...) draw also current.

Re: Cannot get a Lolin32 to low-power upon deep sleep.

Posted: Thu Apr 05, 2018 8:47 am
by WiFive

Re: Cannot get a Lolin32 to low-power upon deep sleep.

Posted: Fri Apr 06, 2018 7:32 pm
by rin67630
I have discovered that one shoud avoid to initialize Serial.begin (115200) if never connected to USB.
That saves permanently 5mA for the USB Modem.

With Serial.begin (115200) and later disconnection from USB the ESP32 detects the disconnection and powers off the USB Modem, but if you never connect USB the modem remains powered and wastes 5mA.

With a D-Duino and an OLED Display with 128*48Pixels the additional permanent consumption is around 2-4mA depending on the amount of pixels lit.

Re: Get a Lolin32 to low-power upon deep/light sleep.

Posted: Sat Apr 07, 2018 8:28 am
by rin67630
I must correct myself. Even with no serial initialized, the power consumption is higher than it should when the Lolin32 is starting wo USB, jut powered by +5V. Just plug the usb in a computer and unplug it and the current drops by 6mA.

By the way: deep sleep is not efficient for pauses around 1 sec, since the reboot procedure draws too much current. Light sleep is better.

Re: Cannot get a Lolin32 to low-power upon deep sleep.

Posted: Wed Jun 23, 2021 6:45 pm
by nutthee
Not sure that do you already solve this problem. I have the same problem with Lolin32 that take not less than 60mA in deep sleep mode.
If you already solve that, can you share your way?

Re: Cannot get a Lolin32 to low-power upon deep sleep.

Posted: Sat Jul 03, 2021 2:24 pm
by al1fch
Hi

Il your Lolin32 a Lolin32Lite one ?
I solved such problem with adding a pull up up resistor on SPI Flash CS (pin 1)
During deep-sleep scope show floating Flash SPI CS pin (driven from GPIO11) without this resistor and then SPI Flash continue to
consume some current.

Lolin32Lite schematic attached