1

I have a very simple code of door sensor (magnetic) which gives results in 1 & 0 format. But I want to run the NodeMCU only when a door is open or close. Rest of time I want to keep it in deep sleep mode. Here is my code looks like :

int doorClosed = 1;

#define REED_SWITCH D5

void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);
  pinMode(BUILTIN_LED, OUTPUT);
  digitalWrite(BUILTIN_LED, HIGH);

  pinMode(REED_SWITCH, INPUT_PULLUP); 
}

void loop() {
  // put your main code here, to run repeatedly

   Serial.println(digitalRead(REED_SWITCH));

    if ((digitalRead(REED_SWITCH) == HIGH) && (doorClosed == 1))
    {
      Serial.println("DOOR OPEN!!");
      doorClosed = 0;
      digitalWrite(BUILTIN_LED, LOW);
      ESP.deepSleep(0); 
    } 
    else if ((digitalRead(REED_SWITCH) == LOW) && (doorClosed == 0))
    {
      Serial.println("DOOR CLOSED!!");
      doorClosed = 1;
      digitalWrite(BUILTIN_LED, HIGH);
      ESP.deepSleep(0); 
    }
    delay(500);
}

Here I am able to put the NodeMCU in deep sleep. But it never wakes up. How can I wake up my chip. After attaching the D0 with RST, I don't event get Serial Logs. I am not sure what kind of behaviour is this. If someone can help me to fix this code will be great!

Thank you!

8
  • 1
    According to this guide (randomnerdtutorials.com/esp8266-deep-sleep-with-arduino-ide) the pin to be connected to RST is D0 and not A0.
    – leoc7
    Jan 9, 2019 at 21:49
  • I am sorry it was a typo mistake. I just fixed it. Jan 10, 2019 at 5:33
  • After going in sleep mode, check D0 voltage value with a multimeter. Should be approximately to 0V.
    – leoc7
    Jan 10, 2019 at 7:25
  • If D0 is not 0V, connect RST to GND and check if microcontroller wakes up.
    – leoc7
    Jan 10, 2019 at 7:28
  • I just check the D0 voltage, but seems like its not going to 0V its showing me 3.25V. Which means its not going to deep sleep? in multimeter I am touching D0 and GND pin to check the voltage. Jan 10, 2019 at 9:41

2 Answers 2

0

According to this guide:

The RST pin of the ESP8266 is always HIGH while the ESP8266 is running. However, when the RST pin receives a LOW signal, it restarts the microcontroller.

If you set a Deep Sleep timer with the ESP8266, once the timer ends, GPIO 16 sends a LOW signal. That means that GPIO 16 when connected to RST pin can wake up the ESP8266 every time the timer ends.

The only way to wakes up ESP8266 from deep sleep mode without timer is set a LOW signal on RST pin.

Example without timer:

ESP.deepSleep(0); 

Connect D0 (GPIO 16) to RST is available only when ESP8266 is in sleep mode with timer.

Example with 30sec timer:

ESP.deepSleep(30e6); 

In your case, you can use deep sleep with timer and connect Door sensor to D0 and will act as switch between the RST and D0.

9
  • can i do that programmatically? Jan 11, 2019 at 5:06
  • also if its in deep sleep mode. Then how can i send a signal to it? i am using door sensor, will it be able to read its value in deep sleep? Jan 11, 2019 at 5:07
  • You can’t do it programmatically! During deep sleep esp8266 is switched off and can’t do any operation. An approach can be to set a deep sleep mode with timer (e.g. 30sec) and connect D0 to RST. Every 30sec esp8266 wakes up, restart program and check door sensor.
    – leoc7
    Jan 11, 2019 at 7:37
  • interrupt will also not work in this case? Jan 11, 2019 at 7:58
  • No, if you use Deep Sleep, ESP8266 is off and the only way to wake up it is trigger physically RST pin. Consider to use other type of sleep mode (for example light-sleep) Take a look to this guide: losant.com/blog/making-the-esp8266-low-powered-with-deep-sleep
    – leoc7
    Jan 11, 2019 at 8:33
0

My response is late but herewith my experience that may be of help to others.

NodeMcu in deep sleep will wake-up from external source on receiving a pulse from high to low on the RST pin.

An ADXL345 accelerometer raises an interrupt HIGH on sensing motion and remains high until (I2C) READ to prevent multiple devices from interfering with each other

The following post was instrumental in my finding a solution

https://electronics.stackexchange.com/questions/158015/how-to-turn-continuous-signal-into-a-short-pulse-using-logic-gates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.