Reputation: 29
I have a simple code for esp32 here. I can only use gpio pin number 35 on the board. So esp32 should go to light sleep and wake up on button press. When I run this program below, my esp32 goes into light sleep, but it doesn't wake up when the button is pressed. What's the problem? Any idea please? Thank you.
#include <esp_sleep.h>
void setup() {
Serial.begin(115200);
delay(2000);
gpio_wakeup_enable(GPIO_NUM_35, GPIO_INTR_HIGH_LEVEL);
esp_sleep_enable_gpio_wakeup();
delay(2000);
Serial.println("Going to sleep now");
delay(2000);
esp_light_sleep_start();
delay(2000);
Serial.println("This will be NOT printed");
}
void loop() {
delay(2000);
Serial.println("Hi after wake up via GPIO Button!");
delay(2000);
}
Upvotes: 0
Views: 1498
Reputation: 804
I was able to find the solution, I've updated the code and the solution, here you go,
Prerequisites:
I've added couple of extra functions:
#include <esp_sleep.h>
#include "esp32/rom/uart.h"
#include "driver/rtc_io.h"
void setup() {
Serial.begin(115200);
gpio_wakeup_enable(GPIO_NUM_15, GPIO_INTR_HIGH_LEVEL);
esp_sleep_enable_timer_wakeup(5000000);
rtc_gpio_pulldown_en(GPIO_NUM_15);
rtc_gpio_pullup_dis(GPIO_NUM_15);
Serial.println("Going to sleep now");
uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
esp_sleep_enable_ext0_wakeup(GPIO_NUM_15, 1);
esp_light_sleep_start();
delay(2000);
Serial.println("This will be printed");
}
void loop() {
if (rtc_gpio_get_level(GPIO_NUM_15) == 1) {
printf("please release button\n");
do
{
vTaskDelay(pdMS_TO_TICKS(10));
} while (rtc_gpio_get_level(GPIO_NUM_15) == 1);
}
Serial.println("Hi after wake up!");
Serial.println("Going to sleep now!");
uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
esp_light_sleep_start();
}
For anyone coming from RTOS background, here's my solution,
#include "driver/rtc_io.h"
#include "stdio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_sleep.h"
#include "esp32/rom/uart.h"
#include "time.h"
#include "sys/time.h"
#include "esp_timer.h"
#define PIN_SWITCH 15
#define PIN_LED 2
void app_main()
{
printf("\nSleep Program Running..\n");
gpio_set_direction(PIN_SWITCH, GPIO_MODE_INPUT);
gpio_wakeup_enable(PIN_SWITCH, GPIO_INTR_HIGH_LEVEL);
rtc_gpio_pulldown_en(PIN_SWITCH);
rtc_gpio_pullup_dis(PIN_SWITCH);
gpio_set_direction(PIN_LED, GPIO_MODE_OUTPUT);
esp_sleep_enable_timer_wakeup(5000000);
while (true)
{
if (rtc_gpio_get_level(PIN_SWITCH) == 1)
{
printf("please release button\n");
do
{
vTaskDelay(pdMS_TO_TICKS(10));
} while (rtc_gpio_get_level(PIN_SWITCH) == 1);
}
printf("Going for a nap\n");
uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
int64_t before = esp_timer_get_time();
gpio_set_level(PIN_LED, 0);
esp_sleep_enable_ext0_wakeup(PIN_SWITCH, 1);
esp_light_sleep_start();
gpio_set_level(PIN_LED, 1);
int64_t after = esp_timer_get_time();
esp_sleep_wakeup_cause_t reason = esp_sleep_get_wakeup_cause();
printf("Waked up Event %d - Sleep for %lld \n", reason, (after - before) / 1000);
vTaskDelay(1);
}
}
Upvotes: 0
Reputation: 29
I've figured out by myself what the trouble was: just change to rtc_io ext0 and set level on 0. Then it will work fine. (Maybe this will be helpful for other users.)
Upvotes: 2