DanielOgorchock / ST_Anything

ST_Anything is an Arduino library, sketch, and Device Type that works with your SmartThings ThingShield to create an all-in-one SmartThings device.
GNU General Public License v3.0
438 stars 448 forks source link

Feature Request: Deep sleep for ESP8266 #244

Closed zhen-bot closed 3 years ago

zhen-bot commented 3 years ago

Feature request to add deep sleep mode for ESP8266: https://diyi0t.com/how-to-reduce-the-esp8266-power-consumption/#:~:text=The%20ESP8266%20power%20consumption%20is,depending%20on%20different%20use%20cases.&text=I%20live%20in%20Germany%2C%20where,year%2C%20which%20is%20nearly%20nothing.

Would be really cool and allow it to be battery powered.

ogiewon commented 3 years ago

Deep Sleep mode is really only useful for devices that only transmit data to the hub, on a slow periodic interval, like a temperature sensor, for example. Other uses have achieved this all within the setup() routine of the sketch. If you want the microcontroller to receive WiFi data, then it cannot go to sleep. If you want to use a digital input to wake up the microcontroller and send a status update, that is possible, however it performs way too slow for something like a door/window contact sensor being used to turn on lights.

But, something like a temperature sensor that transmits data once an hour, would be possible. What application do you have in mind?

zhen-bot commented 3 years ago

Thanks for the quick reply I am working on a remote battery voltage monitor device. The battery is charged by a solar panel, but I'd like to monitor the voltage. I was going to suggest that a battery monitoring device, but I noticed a past bug saying that you can use PS_Voltage, which I did, with a voltage divider and it's working great. The only problem is that it's consuming about 70mA, which is quite a bit. I only need to monitor maybe once an hour, so the application would be perfect for deep sleep.

ogiewon commented 3 years ago

To try deep sleep on an ESP8266 simply wire GPIO 16 (D0 on most NodeMCU boards) to the RST pin on the board. (make sure your sketch does not use pin GPIO 16 (DO) for anything else, of course.

Then, as the very last line in your ST_Anything sketch's setup() routine add the following 1 line of code.

ESP.deepSleep(3600000000); /* 1 hour of time, in units of microseconds - adjust as you see fit */

When the board powers on, it will connect to WiFi, and then 'init' each device you have defined, which in turn will cause an update to be sent to the hub immediately. Then, it will enter deepSleep and wait until 3600 seconds passes, and then start again as if the board was just powered on. The 'loop()' will never be run.

zhen-bot commented 3 years ago

I tried that and it didn't seem to work. D0 staying low for some reason.

On Mon, Feb 1, 2021 at 8:12 PM ogiewon notifications@github.com wrote:

To try deep sleep on an ESP8266 simply wire GPIO 16 (D0 on most NodeMCU boards) to the RST pin on the board. (make sure your sketch does not use pin GPIO 16 (DO) for anything, of course.

Then, as the very last line in your ST_Anything sketch's setup() routine add the following 1 line of code.

ESP.deepSleep(3600000000); / 1 hour of time, in units of microseconds - adjust as you see fit /

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/DanielOgorchock/ST_Anything/issues/244#issuecomment-771343627, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARNSXJA5TI3YH3BNT3FXS33S453TDANCNFSM4W53GA5Q .

ogiewon commented 3 years ago

What ESP8266 board are you using?

ogiewon commented 3 years ago

You can try this very simple deepSleep example sketch...

/*
 * ESP8266 Deep sleep mode example
 * Rui Santos 
 * Complete Project Details https://randomnerdtutorials.com
 */

void setup() {
  Serial.begin(115200);
  Serial.setTimeout(2000);

  // Wait for serial to initialize.
  while(!Serial) { }

  // Deep sleep mode for 30 seconds, the ESP8266 wakes up by itself when GPIO 16 (D0 in NodeMCU board) is connected to the RESET pin
  Serial.println("");
  Serial.println("I'm awake, but I'm going into deep sleep mode for 10 seconds");
  ESP.deepSleep(10e6); 

  // 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() {
}

FUYI - Once you have a board that is in deepSleep, you have to manually wake it up and put it into programming mode in order to the Arduino IDE to be able to write a new program to it.

ogiewon commented 3 years ago

Also, after the Arduino IDE programs the board, you may need to manually start the process by clicking the reset button, or simply cycling power. At least that is how my NodeMCU behaves, but once it is running, it wakes up periodically.

zhen-bot commented 3 years ago

Thanks for the help. It looks like I got a faulty board that awakes into flash mode. I'll try again after getting new, hopefully working boards.

On Mon, Feb 1, 2021 at 9:11 PM ogiewon notifications@github.com wrote:

Also, after the Arduino IDE programs the board, you may need to manually start the process by cliking the reset button, or simply cycling power. At least that is how my NodeMCU behaves, but once it is running, it wakes up periodically.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/DanielOgorchock/ST_Anything/issues/244#issuecomment-771366331, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARNSXJHVHF6YFYKCQYVB3NDS46CPFANCNFSM4W53GA5Q .

zhen-bot commented 3 years ago

Just wanted to let you know that I switched to a D1 Mini board and it's been working flawlessly. Thanks again.

On Mon, Feb 1, 2021 at 11:13 PM Zhen zhen85@gmail.com wrote:

Thanks for the help. It looks like I got a faulty board that awakes into flash mode. I'll try again after getting new, hopefully working boards.

On Mon, Feb 1, 2021 at 9:11 PM ogiewon notifications@github.com wrote:

Also, after the Arduino IDE programs the board, you may need to manually start the process by cliking the reset button, or simply cycling power. At least that is how my NodeMCU behaves, but once it is running, it wakes up periodically.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/DanielOgorchock/ST_Anything/issues/244#issuecomment-771366331, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARNSXJHVHF6YFYKCQYVB3NDS46CPFANCNFSM4W53GA5Q .

ogiewon commented 3 years ago

Great news! Thank you for the follow-up message.