NodeMCU DeepSleep on Interrupt

Howdy folks!

I’m in the very early stages of a project. My end goal is to make a cheap, flexible, battery operated nodemcu based mqtt switch.

Since it will be battery operated, I need to use the deepsleep function. I’m trying to create a test sketch that checks the state of a pushbutton, serial prints the state, and then goes to sleep until the button is pressed again. Based on relentless googling I’ve gotten to the point where the nodemcu sleeps unless the button is pushed, which is perfect. But! It goes back into deep sleep as soon as I release the pushbutton, which is no good because in the final version it will need to connect to wifi / mqtt, and I don’t want to have to hold the button the whole time. (I can tell this is the case because I can hold the button long enough for the state to be printed, but if I release it shortly after that it doesn’t print the sleeping line. But if I hold it longer, it does).

In fact, because the sketch stops executing as soon as the button is released, I think I’m not even using deepsleep right now, although my tests with using deepsleep on a timer (e.g. 20e6 us, or 20 seconds) were successful.

Anyways, here’s my sketch and circuit. Do any of you masters know how I can use the deepsleep function and have it only wake based on the button press? Thank you!

int buttonPin = D6;     // the number of the pushbutton pin

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  Serial.begin(74880);
  
  
  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  
  // Wait for serial to initialize.
  while (!Serial) { }
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    Serial.println("High");
  } else {
    Serial.println("Low");
  }
  delay(1000);
  naptime();
}

void naptime()  {
  Serial.println("Going into deep sleep until interrupt");
  // sleeps until RST High
  ESP.deepSleep(0); 
}

image

it looks like you are pulsing the reset pin with the button, which is probably what’s actually waking it up and as soon as you release the button it goes back to deepsleep after 1s. The thing to remember about deepsleep is that the NMCU will start from scratch every time it wakes up, as if you powered it up again, so anything you need to preserve over deepsleep needs storing in the flash. Basically don’t call naptime until you have done all you need to do.

This thread might help you…
http://www.esp8266.com/viewtopic.php?f=6&t=13893&start=0

I may have missed the connection, but from the node_mcu dsleep documentation, you have to connect certain pins together to get deep sleep

Caution
This function can only be used in the condition that esp8266 PIN32(RST) and 
PIN8(XPD_DCDC aka GPIO16) are connected together. Using sleep(0) will set no 
wake up timer, connect a GPIO to pin RST, the chip will wake up by a falling-edge 
on pin RST.
1 Like

Have a look here

@keithh666 and @gpbenton thank you both very much for your quick replies!

When I was using deepsleep on a timer yesterday, I did indeed have to tie D0 and RST together per the graphic in the link keith shared:

Based on what you two have said, today I tried tying RST, GP0, and the switch together (rather unscientifically). I did this both on the ground side and the high side of the switch to test both. In one case the nodemcu just seemed dead, and in the other it just constantly rebooted rapid-fire.

Any further recommendations for me? I suspect I’m close but missing something fundamental!

Here’s one way I tried today:

image

And here’s the other:

image

Sorry to divert the topic slightly but is it possible to turn on the esp8266 powered by a battery from deep sleep using HA and not a physical switch as designed by @Dolores?

Yep use the timer version i.e. ESP.deepSleep(value_in_milisecs);, You can’t do it via HA as it can’t respond to anything in deepsleep.

OK, I think what you need is rst pulled up to 3.3v and the switch on the rst to ground as what you need is a reset. D0 - rst is not active when its not on a timer, so I don’t think that will work.

Hi @keithh666 can you explain abit more please?

You do what ever you need to do, read temp, send a msg, then put the esp to sleep using ESP.deepSleep(value_in_milisecs); In value_in_milisecs time the esp wakes up and starts from the beginning of the sketch, you do what you need to do and call deepsleep again. In this mode you will need the GPIO16 (D0) connected to rst.

Well all I was trying to do was to turn on and off a light which is powered by battery. So the switch of this light is operated by rf and a relay. The rf eats all the battery in no time when its connected via esp8266 to the Internet.

What I think you want is something like this project, but with modified code to do something other than talk to iftt
https://www.hackster.io/noelportugal/ifttt-smart-button

This requires to press the push button to turn the esp on or off… which is the same issue I presented earlier.

I need something which could turn dumb lights on or off and are battery powered without a physical button.

This would require the radio to be on all the time to receive the signal. Using wifi for this isn’t practical, as it takes too much power for battery use.

You might try a mysensors node, the radios for that take less power, but even then you will be changing the battery very frequently.

1 Like