Esp32-c3 super mini deepsleep doorsensor

Hi

I am trying to get the ESP32-c3 super mini to wake up from deepsleep when the door contact is closed and open. But so far i am only able to get it to wake up when the sensor is open.

Is there anyway that you can use like:

esp_deep_sleep_enable_gpio_wakeup(1 << 1, ESP_GPIO_WAKEUP_GPIO_BOTH);

now i only have

esp_deep_sleep_enable_gpio_wakeup(1 << 1, ESP_GPIO_WAKEUP_GPIO_LOW);

i have also tried to use

esp_deep_sleep_enable_gpio_wakeup(1 << 1, ESP_GPIO_WAKEUP_GPIO_LOW);

esp_deep_sleep_enable_gpio_wakeup(1 << 1, ESP_GPIO_WAKEUP_GPIO_HIGH);

but it doesnt work.

Any ideas?

Robert

Usually there is a CHANGE option. The API doc should tell you what it has like LOW, HIGH, CHANGE, RISING, FALLING etc.

Very confusing at the best.
You could try something like this (not tested):
(Use RTC pin for WAKE_PIN)

  gpio_wakeup_enable(WAKE_PIN, GPIO_INTR_ANYEDGE);
  esp_deep_sleep_enable_gpio_wakeup();

If it doesn't work, another approach could be to read the state of the pin before sleep and set the wakeup logic according to that.

i have tried this but i get

too few arguments to function ‘esp_err_r esp_deepsleep_gpio_wake_up_mode_t)

here´s the program:

void setup() {
Serial.begin(115200);
pinMode (1,INPUT_PULLUP);

gpio_wakeup_enable((gpio_num_t)1 , GPIO_INTR_ANYEDGE);

void loop() {
int contact=digitalRead(1);
if (contact==HIGH){
esp_deep_sleep_enable_gpio_wakeup();
Serial.println("OPEN");
client.publish(contacttopic,"ON");
delay(100);}
else{
esp_deep_sleep_enable_gpio_wakeup();
Serial.println("CLOSED");
client.publish(contacttopic,"OFF");
delay(100);
}
Serial.println("Going to sleep")
;esp_deep_sleep_start();
}


It's a jungle.
Try if this compiles:
gpio_wakeup_enable(GPIO_NUM_1, GPIO_INTR_ANYEDGE);

That did it. thanks. however it doesn´t seem to wake up when i push the magnet to the sensor.

It seems that once it sleeps it sleeps and doesn´t care about the sensor.

suggestions?

seem to remember that function call only works for light-sleep mode - see GPIO Wakeup (Light-sleep Only)

you marked the topic as solved - is it?

Opps..my bad. now it isn´t

cant figure it out. here´s the present code.

Have i forgotten something?

void setup() {
  Serial.begin(115200);
  pinMode (1,INPUT_PULLUP);

gpio_deep_sleep_wakeup_enable(GPIO_NUM_1, GPIO_INTR_ANYEDGE);
 
}

void loop() {
  int contact=digitalRead(1);
  if (contact==HIGH){
      Serial.println("OPEN");
     client.publish(contacttopic,"ON");
     delay(100);
  }
  else
  {
     
    Serial.println("CLOSED");
   client.publish(contacttopic,"OFF");
   delay(100);
  }
  Serial.println("Going to sleep");
esp_deep_sleep_start();
}

try with:

gpio_wakeup_enable(GPIO_NUM_1, GPIO_INTR_ANYEDGE);
esp_deep_sleep_enable_gpio_wakeup(1ULL << GPIO_NUM_1, ESP_GPIO_WAKEUP_GPIO_ANY);

not tested...
But I'm 100% sure it's doable.

try the following on an ESP32C3 where wakeup from deep sleep is GPIO3 pulled HIGH or GPIO2 pulled LOW

// ESP32-C3 - wakeup from deep sleep GPIO3 pulled HIGH GPIO2 pulled LOW

// adapted from https://forum.arduino.cc/t/esp32c3-not-waking-up-from-deep-sleep-with-gpio-button-press/1358556/4

/*
ESP32 C3  Deep Sleep and wakeup

1. works with RTC pins 2, 3, 4 and 5 but not 0 and 1
   see https://github.com/espressif/arduino-esp32/issues/6656
   and https://github.com/espressif/arduino-esp32/issues/8447 
*/

#include <Arduino.h>
#include "esp_sleep.h"
#include "driver/rtc_io.h"

#define DEBUG 1

#define WAKEUP_PIN 3  // GPIOs taken LOW to HIGH to wakeup
#define WAKEUP_PIN2 2
#define LED_BUILTIN 8

RTC_DATA_ATTR int bootCount;  // stored in RTC memory during deep sleep

esp_err_t errRc;

int esp_deep_sleep_enable_gpio_wakeup(uint64_t, int);

// display wakeup reason
void printWakeupReason() {
  esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
  switch (wakeup_reason) {
    case ESP_SLEEP_WAKEUP_EXT1:
      if (DEBUG) Serial.println("Wakeup caused by external signal using EXT1");
      break;
    case ESP_SLEEP_WAKEUP_EXT0:
      if (DEBUG) Serial.println("Wakeup caused by external signal using EXT0");
      break;
    case ESP_SLEEP_WAKEUP_TIMER:
      if (DEBUG) Serial.println("Wakeup caused by timer");
      break;
    case ESP_SLEEP_WAKEUP_GPIO:
      if (DEBUG) Serial.println("Wakeup caused by GPIO");
      break;
    default:
      if (DEBUG) Serial.printf("Wakeup not caused by deep sleep: %d\n", wakeup_reason);
      break;
  }
}


void setup() {
  if (DEBUG) Serial.begin(115200);
  delay(1000);  // Give time to open the Serial Monitor
  Serial.println();
  pinMode(LED_BUILTIN, OUTPUT);  // blink LED
  for (int i = 0; i < 3; i++) {
    digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);  // turn the LED on (HIGH is the voltage level)
    delay(1000);
  }
  if (DEBUG) Serial.println("starting. . . ");
  bootCount++;  // display bootcount
  if (DEBUG) Serial.printf("Boot number: %d\n", bootCount);

  printWakeupReason();  // display wakeup reason
  Serial.printf("GPIO%d %d  GPIO%d %d\n", WAKEUP_PIN, digitalRead(WAKEUP_PIN),  WAKEUP_PIN2, digitalRead(WAKEUP_PIN2) );
  if(digitalRead(WAKEUP_PIN)) Serial.printf("Wakeup due to GPIO %d LOW>HIGH\n", WAKEUP_PIN);
  if(!digitalRead(WAKEUP_PIN2)) Serial.printf("Wakeup due to GPIO %d HIGH>LOW\n", WAKEUP_PIN2);

  // enable wakeup from deep sleep on two GPIO pins
  errRc = esp_deep_sleep_enable_gpio_wakeup((1ull << WAKEUP_PIN), ESP_GPIO_WAKEUP_GPIO_HIGH);
  // errRc = esp_deep_sleep_enable_gpio_wakeup((1ull << WAKEUP_PIN), ESP_GPIO_WAKEUP_GPIO_HIGH);  
  //errRc = esp_deep_sleep_enable_gpio_wakeup(1ull | 0b11111, ESP_GPIO_WAKEUP_GPIO_HIGH);  // all RTC pins can wake 0 to 5
  if (DEBUG) Serial.printf("errRc  (A): %d\n", (int)errRc);
  errRc = esp_deep_sleep_enable_gpio_wakeup((1ull << WAKEUP_PIN2), ESP_GPIO_WAKEUP_GPIO_LOW);
  if (DEBUG) Serial.printf("errRc  (A): %d\n", (int)errRc);
  if (DEBUG) Serial.printf("Entering deep sleep in 5 seconds."
           " Press the button (GPIO%d pulled HIGH GPIO%d LOW) to wake up.\n", WAKEUP_PIN, WAKEUP_PIN2);  
  delay(5000);
  if (DEBUG) Serial.println("Entering deep sleep now.");
  esp_deep_sleep_start();
}

void loop() {
}

serial monitor output

starting. . . 
Boot number: 1
Wakeup not caused by deep sleep: 0
GPIO3 0  GPIO2 1
errRc  (A): 0
errRc  (A): 0
Entering deep sleep in 5 seconds. Press the button (GPIO3 pulled HIGH GPIO2 LOW) to wake up.

starting. . . 
Boot number: 2
Wakeup caused by GPIO
GPIO3 1  GPIO2 1
Wakeup due to GPIO 3 LOW>HIGH
errRc  (A): 0
errRc  (A): 0
Entering deep sleep in 5 seconds. Press the button (GPIO3 pulled HIGH GPIO2 LOW) to wake up.

starting. . . 
Boot number: 3
Wakeup caused by GPIO
GPIO3 0  GPIO2 0
Wakeup due to GPIO 2 HIGH>LOW
errRc  (A): 0
errRc  (A): 0
Entering deep sleep in 5 seconds. Press the button (GPIO3 pulled HIGH GPIO2 LOW) to wake up..

starting. . . 
Boot number: 4
Wakeup caused by GPIO
GPIO3 0  GPIO2 0
Wakeup due to GPIO 2 HIGH>LOW
errRc  (A): 0
errRc  (A): 0
Entering deep sleep in 5 seconds. Press the button (GPIO3 pulled HIGH GPIO2 LOW) to wake up.

starting. . . 
Boot number: 5
Wakeup caused by GPIO
GPIO3 1  GPIO2 1
Wakeup due to GPIO 3 LOW>HIGH
errRc  (A): 0
errRc  (A): 0
Entering deep sleep in 5 seconds. Press the button (GPIO3 pulled HIGH GPIO2 LOW) to wake up

EDIT: to enable wakeup from deep sleep on two GPIO pins going LOW>HIGH

  errRc = esp_deep_sleep_enable_gpio_wakeup((1ull << WAKEUP_PIN) | (1ull << WAKEUP_PIN2), ESP_GPIO_WAKEUP_GPIO_HIGH);

Was not declared was not declared in this scope

Since i am using a magnet contact i only have 2 pins out. so i want the esp32-c3 super mini to wake up on both low and high signals

the code in post 10 wakes up the ESP32C3 from deep sleep when GPIO3 goes LOW to HIGH or GPIO2 goes HIGH to LOW

can you post the full error message? what identifier was not defined?

what Tools>Board have you selected? I used ESP32C3 Dev Module for code in post 10

i changed the board to Dev module but i still get this:



'ESP_GPIO_WAKEUP_GPIO_ANY' was not declared in this scope; did you mean 'ESP_GPIO_WAKEUP_GPIO_LOW'?


esp32c3 V5.5 api-reference sleep_modes specifies two wakeup condition parameters for esp_deep_sleep_enable_gpio_wakeup(mask, mode) so
mode -- Select logic function used to determine wakeup condition:

  • ESP_GPIO_WAKEUP_GPIO_LOW: wake up when the gpio turn to low.*
  • ESP_GPIO_WAKEUP_GPIO_HIGH: wake up when the gpio turn to high.*

where did you find ESP_GPIO_WAKEUP_GPIO_ANY?

That's correct. ANY is not valid here, my mistake.

Has the code given in post #10 by @horace solved your problem or do you still need help?

I just had a look, how esphome handles that:

#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6)
  if (this->sleep_duration_.has_value())
    esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
  if (this->wakeup_pin_ != nullptr) {
    bool level = !this->wakeup_pin_->is_inverted();
    if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_INVERT_WAKEUP && this->wakeup_pin_->digital_read()) {
      level = !level;
    }
    esp_deep_sleep_enable_gpio_wakeup(1 << this->wakeup_pin_->get_pin(),
                                      static_cast<esp_deepsleep_gpio_wake_up_mode_t>(level));
  }
#endif
  esp_deep_sleep_start();
}

following up on idea by @kmin in post 20
this reads the state of the pin before sleep and sets the wakeup logic to the inverse, e.g. if GPIO is LOW on reset wakeup is for when it goes HIGH

// ESP32-C3 - wakeup from deep sleep GPIO3 
// reads the state of the pin before sleep and sets the wakeup logic to the inverse

// adapted from https://forum.arduino.cc/t/esp32c3-not-waking-up-from-deep-sleep-with-gpio-button-press/1358556/4

/*
ESP32 C3  Deep Sleep and wakeup

1. works with RTC pins 2, 3, 4 and 5 but not 0 and 1
   see https://github.com/espressif/arduino-esp32/issues/6656
   and https://github.com/espressif/arduino-esp32/issues/8447 
*/

#include <Arduino.h>
#include "esp_sleep.h"
#include "driver/rtc_io.h"

#define DEBUG 1

#define WAKEUP_PIN 3  // GPIOs taken LOW or HIGH to wakeup
#define LED_BUILTIN 8

RTC_DATA_ATTR int bootCount;  // stored in RTC memory during deep sleep

esp_err_t errRc;

int esp_deep_sleep_enable_gpio_wakeup(uint64_t, int);

// display wakeup reason
void printWakeupReason() {
  esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
  switch (wakeup_reason) {
    case ESP_SLEEP_WAKEUP_EXT1:
      if (DEBUG) Serial.println("Wakeup caused by external signal using EXT1");
      break;
    case ESP_SLEEP_WAKEUP_EXT0:
      if (DEBUG) Serial.println("Wakeup caused by external signal using EXT0");
      break;
    case ESP_SLEEP_WAKEUP_TIMER:
      if (DEBUG) Serial.println("Wakeup caused by timer");
      break;
    case ESP_SLEEP_WAKEUP_GPIO:
      if (DEBUG) Serial.println("Wakeup caused by GPIO");
      break;
    default:
      if (DEBUG) Serial.printf("Wakeup not caused by deep sleep: %d\n", wakeup_reason);
      break;
  }
}


void setup() {
  if (DEBUG) Serial.begin(115200);
  delay(1000);  // Give time to open the Serial Monitor
  Serial.println();
  pinMode(LED_BUILTIN, OUTPUT);  // blink LED
  for (int i = 0; i < 3; i++) {
    digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);  // turn the LED on (HIGH is the voltage level)
    delay(1000);
  }
  if (DEBUG) Serial.println("starting. . . ");
  bootCount++;  // display bootcount
  if (DEBUG) Serial.printf("Boot number: %d\n", bootCount);

  printWakeupReason();  // display wakeup reason
  if (digitalRead(WAKEUP_PIN)) { // if GPIO is HIGH set for HIGH to LOW wakeup
    Serial.printf("GPIO%d is HIGH setting wakeup to GPIO_LOW\n", WAKEUP_PIN);
    errRc = esp_deep_sleep_enable_gpio_wakeup((1ull << WAKEUP_PIN), ESP_GPIO_WAKEUP_GPIO_LOW);
    if (DEBUG) Serial.printf("errRc  (A): %d\n", (int)errRc);
    if (DEBUG) Serial.printf("Entering deep sleep in 5 seconds. GPIO%d pulled LOW to wake up\n",  WAKEUP_PIN);  
  } else {                        // if GPIO is LOW set for LOW to HIGH wakeup
    Serial.printf("GPIO%d is LOW setting wakeup to GPIO_HIGH\n", WAKEUP_PIN);
    errRc = esp_deep_sleep_enable_gpio_wakeup((1ull << WAKEUP_PIN), ESP_GPIO_WAKEUP_GPIO_HIGH);
    if (DEBUG) Serial.printf("errRc  (A): %d\n", (int)errRc);
    if (DEBUG) Serial.printf("Entering deep sleep in 5 seconds. GPIO%d pulled HIGH to wake up\n",  WAKEUP_PIN);  
  }
  delay(5000);
  if (DEBUG) Serial.println("Entering deep sleep now.");
  esp_deep_sleep_start();
}

void loop() {
}

serial monitor output

starting. . . 
Boot number: 1
Wakeup not caused by deep sleep: 0
GPIO3 is HIGH setting wakeup to GPIO_LOW
errRc  (A): 0
Entering deep sleep in 5 seconds. GPIO3 pulled LOW to wake up

starting. . . 
Boot number: 2
Wakeup caused by GPIO
GPIO3 is LOW setting wakeup to GPIO_HIGH
errRc  (A): 0
Entering deep sleep in 5 seconds. GPIO3 pulled HIGH to wake up
Entering deep sleep now.

starting. . . 
Boot number: 3
Wakeup caused by GPIO
GPIO3 is HIGH setting wakeup to GPIO_LOW
errRc  (A): 0
Entering deep sleep in 5 seconds. GPIO3 pulled LOW to wake up

starting. . . 
Boot number: 4
Wakeup caused by GPIO
GPIO3 is LOW setting wakeup to GPIO_HIGH
errRc  (A): 0
Entering deep sleep in 5 seconds. GPIO3 pulled HIGH to wake up