Notifications
Clear all

ESP8266 Deep Sleep - The Darkness Returns

47 Posts
3 Users
4 Likes
1,852 Views
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
Topic starter  

@zander,

I decided to move this part out of Bill's LoRa... we're a little off topic on this part.  I ran the test you suggested.  I'll let the results speak.  

The Code Modified

/*
 * ESP8266 Deep sleep mode example
 * Rui Santos, modified for test by Inq 
 * Complete Project Details  https://randomnerdtutorials.com 
 */
 #include <limits.h>
 
void setup() {
  Serial.begin(115200);
  Serial.setTimeout(2000);
  delay(1000);

  u32 SLEEP = UINT_MAX;
  Serial.printf("\nSleep for %u seconds, %f minutes, %f hours\n",
    SLEEP / 1000000, (float)SLEEP / 6.0E7, (float)SLEEP / 3.6E9);
  ESP.deepSleep(SLEEP); 
  
  // Deep sleep mode until RESET pin is connected to a LOW signal (for example pushbutton or magnetic reed switch)
  //Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal");
  //ESP.deepSleep(0); 
}

void loop() 
{
}

Here is the output

19:26:35.296 -> Sleep for 4294 seconds, 71.582788 minutes, 1.193046 hours
20:34:21.567 -> Sleep for 4294 seconds, 71.582788 minutes, 1.193046 hours

 

As noted in the printf statements, the max u32 should equate to 1.193046 hours.  However, we only got:

  • 20:34:21.567 = 20.57266
  • 19:26:35.296 = 19.44314
  •                           1.12952 hours => 4066272000 µSeconds

Digging out the function in the Espressif ESP8266 Non-OS SDK Version 4.0, we have:

image

cali = system_rtc_clock_cali_proc()

image

Note:  How it says this equation changes with temperature, so RTC timer is not very precise.

 

So at my room's and MPU's temperature, even though I plugged in 2^32, we got a number about 95% of 2^32 before it triggered a wake-up.

Points being:

  • It's not very accurate
  • The max sleep time is only about 1 hour 
  • The max sleep time is NOT anywhere near 2^64 as the variable would lead you to believe.

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6992
 

@inq I am too old and too tired to understand all that fancy code. I showed you what the ESP.deepSleep definition is. You can check me on that by just hovering the cursor over the deepSleep  statement and see it says uint64_t. Tomorrow I will run your code modified to use a 64 bit int, and I will set it to 0xFFFFFFFFFFFFFFFF so I know it is at max. Then tour printf's, and I will add my print so I know what to expect. 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6992
 

 

@inq I copied your sketch above and used u64 since that is what the source code for ESP.deepSleep uses (see pic). I have also attached a pic of the serial output.

Screenshot 2023 11 07 at 10.06.44
Screenshot 2023 11 07 at 10.09.52

 

 #include <limits.h>

void setup() {
  u64 TicksPerSec = 1000000;
  Serial.begin(9600);
  Serial.setTimeout(2000);
  delay(1000);

  // Wait for serial to initialize.
  while(!Serial) { }
  
  u64 SLEEP = 0xFFFFFFFFFFFFFFFF;

  Serial.flush();
  Serial.println(" Start test ");
  Serial.flush();

  Serial.print("Sleep = ");Serial.println(SLEEP, HEX);
  u64 Secs = SLEEP / TicksPerSec;
  Serial.print("Secs ");Serial.println(Secs);
  u64 Mins = Secs / 60;
  Serial.print("Mins = ");Serial.println(Mins);
  u64 Hrs = Mins / 60;
  Serial.print("Hrs = "); Serial.println(Hrs);

  Serial.printf("\nSleep for %lld seconds, %f minutes, %f hours\n",
    SLEEP / (u64)1000000, (float)SLEEP / 6.0E7, (float)SLEEP / 3.6E9);

  ESP.deepSleep(SLEEP); 
  
  // Deep sleep mode until RESET pin is connected to a LOW signal (for example pushbutton or magnetic reed switch)
  //Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal");
  //ESP.deepSleep(0); 
}

void loop() {
}

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
Topic starter  

@zaner,

I see your output only has it going to sleep and not returning.  I look forward to your report in 584,942 years.  🤣 🤣 🤣 

The ESP.deepSleep() is simply wrapper by the Arduino Core developers.  The  Espressif ESP8266 Non-OS SDK Version 4.0 excerpts I showed above are from the actual Espressif code base that ESP.deepSleep() is calling.  It too shows a u64 is the parameter type, but it also discusses the actual maximum value and its determination.  This maximum value definition is very odd to me as it is variable WRT temperature.  As I indicated on my test rig I got a maximum sleep time significantly less than even a u32 maximum value.  I only got:  4,066,272,000 µSeconds

image

I actually look forward to hearing when yours returns.  I wonder how much difference it will show as compared to mine.

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6992
 

@inq What you can't see is the wormhole I have it connected to, it already came back.

I am extremely confused by your findings, and I have never seen the document you are using. I have seen the OFFICIAL Espresif docs but I don't remember what they said about the deep sleep timeout. I will attempt another search today.

HOWEVER, is this even needed when we know how to shut the board down and restart it using a genuine standalone RTC like the Chronodot V2.x or the ZS-042 that has to be modified but is cheap?

A Google search turns up a lot of other RTC boards, but all are more expensive, many by AdaFruit. Of course, you could buy the raw DS3232 chips from Aliexpress. I got 5 for under $30 CDN. I just saw a nice alternative at this link >>> https://www.dfrobot.com/product-2304.html

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
Topic starter  

Posted by: @zander

Espresif docs but I don't remember what they said about the deep sleep timeout.

The excerpts above, show the applicable sections, but here is the full document:

https://www.espressif.com/sites/default/files/documentation/2c-esp8266_non_os_sdk_api_reference_en.pdf

Posted by: @zander

HOWEVER, is this even needed

In your design, you may need that.  I hope you'll share your progress and results.  I certainly would be interested even if I don't need those kind of intervals and battery life.  In my case, I'd rather not have the extra circuitry.  If I get a month or two battery life, I'm fine with that.  If I get more, that's just gravy.  I don't need year-long and for my project, the extra circuitry on many units is not desirable when I can simply use the built-in software functions.  As far as this maximum number, I'll want shorter than 1 hour intervals anyway.  At the moment, I'm gearing for 15 minutes or less.  I merely tested it per your suggestion.  I was curious.

BTW... what kind of things are you wanting to monitor in your garden?  My brown thumb https://forum.dronebotworkshop.com/show-tell/inqgarden-brown-thumbs-rule/ doesn't keep me from being interested for my wife's sake.  Besides if this LoRa pans out, I could monitor the sensors over at the farm 8 miles away.  Although, I'm thinking that's wishful thinking. 🤩

VBR,

Inq

 

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6992
 

@inq I have never seen that manual; I have seen similar manuals but different names. I mostly looked at ESP32 and PICO  and remember there is a lot more to it than meets the eye. Also, I think the ESP8266 is more trouble than the ESP32.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
Topic starter  

Posted by: @zander

Also, I think the ESP8266 is more trouble than the ESP32.

But you'll note this topic is about the ESP8266 Deep Sleep.

That is not a commonly shared opinion in the world.  Even though Espressif is trying their hardest to replace and discontinue the ESP8266 with more expensive ESP32 versions (for their bottom line) they still sell more ESP8266's.  I totally agree the ESP32 in all flavors is a far better MPU, it is not less trouble though.  I have had plenty of troubles with it and the Internet agrees.  Compatibility of Sketches is not assured across even a small fraction of the ESP32 lineup.  Even on my newest ESP32-S3, I am unable to access all the memory on board with the Arduino IDE and it inconsistently will deny uploads.   Even though it is the gold standard Espressif designed and documented development board, many of the ESP32 Arduino IDE Examples do not work.

For the project I'm currently working, even an ESP8266 is overkill.  The WiFi will be disabled.  I need sleep function, I2C and SPI communications ability.  That's all.  Even the lowest Arduino can handle this project.  The difference, I have a grundle of these ESP8266's that I paid less than a $1 apiece for gathering dust that will never get used in anything computationally intense.  I'll be switching to ESP32 for those in the future.  This project is the perfect place to use these old units on sensors out in the elements where I don't care if they get damaged by weather or stolen or trampled or chewed on by animals... it won't hurt.

I appreciate your help (especially on the ESP32) and your opinions on most things... even if we disagree on some. 😉 

VBR,

Inq

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6992
 

@inq When I said more trouble, I only referred to the low-power part. Just to wrap up this discussion, I did just find the ESP8266 Low Power Solutions from Espressif and that API has a 32 bit timer. The API I am testing at the moment has a 64 bit timer. I finished the test loading a max 32 bit value and it timed out at about 70 minutes. I will let the now running 64 bit timer run until +70 and see if sleep has returned, then I will wait another 15 or so minutes to make sure.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6992
 

@inq FYI, I used a 64 bit sleep time set to max and I let the test run 30 mins past the 32 bit time of roughly an hour. As I expected the timer was still running. Just an FYI.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
Topic starter  

@zander 

Which processor are you using for this test?  ESP32-S3, C3, ???

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6992
 

@inq ESP8266 NODE MCU 1.0 and the ESP library.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6992
 

@inq I just realized my test is flawed. The large value MIGHT have been interpreted as zero which is sleep forever. I will test again with 5400 secs which is enough to turn on the 32nd bit. I will get back to ya.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Inq
 Inq
(@inq)
Member
Joined: 2 years ago
Posts: 1900
Topic starter  

@zander

We're both exacting and try to be precise... maybe to excess sometimes.  You know Ron... you were right a couple of posts back.  Maybe we're beating a dead horse that neither of us cares about.  You'll use a ESP32 and possibly external hardware to turn it on or off and I'll never need more than about 15 minutes of sleep.  

I'll be continuing this thread because I want to learn about using sleep effectively on the MPU and be able to calculate the battery life as a function of the Cell capacity and sleep time percentage.  I also want to do it with WiFi doing a MQTT post of sensor data to a RasPi broker and using LoRa to post the same data.  I'll be using the ESP8266 like I said because I've got gobs of them and will save the ESP32's to more interesting, complicated projects.  Checking the temperature/water content in the dirt sounds like a good use of a lowly ESP8266.  I just wish I could get an ESP-01 to talk to the LoRa modules. 😆

Now... if we wish to continue this aspect for academic reasons... here is the ESP code which calls the code I referenced.

void EspClass::deepSleep(uint64_t time_us, WakeMode mode)
{
    system_deep_sleep_set_option(static_cast<int>(mode));
    system_deep_sleep(time_us);
    esp_suspend();
}

BTW... Mine went for 67.77 minutes.  A full u32 is 71.58 minutes.  Again... according to the references I posted, there is some variability due to temperature in what time sleep will end.  However, it also sounds like the behavior is undefined if you put a value greater than the maximum value possible.  That maximum value possible is defined by that awful equation... not... it being a u64.  The fact that they let you enter a u64 suggests that the variability in the maximum value may go over the 71.58 minutes sometimes.  Which is a word we don't like to hear in software development.

Take care Ron.  

Inq

3 lines of code = InqPortal = Complete IoT, App, Web Server w/ GUI Admin Client, WiFi Manager, Drag & Drop File Manager, OTA, Performance Metrics, Web Socket Comms, Easy App API, All running on ESP8266...
Even usable on ESP-01S - Quickest Start Guide


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6992
 

@inq What is the issue with an ESP-01 and LORA?

When I used 0xffffffff I got the expected 1 hr and 10 mins (I unlike you am just doing rough calculations of elapsed time so may not notice the inaccuracy you mention)

When I used 90 minutes that is what I got roughly. The point is it was substantially more than the max 32 bit value which means to me the deepSleep call does expect a 64 bit value.

I wonder if the small error you see is due to rounding etc.

Although I don't plan on using it, it is clear to me the ESP8266 can sleep for a very long time on it's own.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Page 1 / 4