deep sleep wake up cause is not tue
Posted: Tue Apr 24, 2018 7:15 pm
The code at the end of this post working perfect. It gives the exact wake up reason when it wakes up from deep sleep. For power consumption efficiency I want to close the peripherals with a GPIO pin ( pin 4) connected to a MOSFET circuit . So I decided to add a
statement before going to sleep . After wake up I add the following statements to the starting off the program.
after adding these statements the wake up reason becomes undefined. What will be the possible cause of this behavior?
Code: Select all
mgos_gpio_write(4,0);
Code: Select all
mgos_gpio_set_mode(4, MGOS_GPIO_MODE_OUTPUT);
mgos_gpio_write(4,1);
Code: Select all
#include "mgos.h"
#include "rom/rtc.h"
#include "driver/rtc_io.h"
#include "esp_sleep.h"
#include "mgos_gpio.h"
#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex - pin 33 is used for wakeup
// DEEP SLEEP HELPER FUNCTION:
//
static void gotosleep(){
const int wakeup_time_sec = 60;
printf("Enabling timer wakeup, %ds\n", wakeup_time_sec);
esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000);
esp_err_t pdConfigError = esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
if (pdConfigError != ESP_OK) {
printf("pdConfigError");
}
esp_err_t enableWakeupError = esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK, ESP_EXT1_WAKEUP_ANY_HIGH);
if (enableWakeupError != ESP_OK) {
printf("enableWakeupError");
}
printf("All done. Going to sleep in ");
for(int i=5; i>0; --i){
printf("%d...\n", i);
sleep(2);
}
printf("Good night.\n");
//mgos_gpio_write(4,0);
esp_deep_sleep_start();
}
// HELPER FUNCTION: Decode the reason for resetting.
// Refer to:
// https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/ResetReason/ResetReason.ino
// https://github.com/espressif/esp-idf/blob/master/components/esp32/include/rom/rtc.h
//
void why_reset(){
int reset_reason = rtc_get_reset_reason(0);
printf("Reset Reason (%d): ", reset_reason);
switch (reset_reason) {
case 1 : printf("Vbat power on reset");break;
case 3 : printf("Software reset digital core");break;
case 4 : printf("Legacy watch dog reset digital core");break;
case 5 : printf("Deep Sleep reset digital core");break;
case 6 : printf("Reset by SLC module, reset digital core");break;
case 7 : printf("Timer Group0 Watch dog reset digital core");break;
case 8 : printf("Timer Group1 Watch dog reset digital core");break;
case 9 : printf("RTC Watch dog Reset digital core");break;
case 10 : printf("Instrusion tested to reset CPU");break;
case 11 : printf("Time Group reset CPU");break;
case 12 : printf("Software reset CPU");break;
case 13 : printf("RTC Watch dog Reset CPU");break;
case 14 : printf("for APP CPU, reseted by PRO CPU");break;
case 15 : printf("Reset when the vdd voltage is not stable");break;
case 16 : printf("RTC Watch dog reset digital core and rtc module");break;
default : printf("NO_MEAN");
}
printf("\n");
}
// HELPER FUNCTION: Decode our reason for waking.
//
void why_wake(){
int wake_cause = esp_sleep_get_wakeup_cause();
printf("Wake Cause (%d): ", wake_cause);
switch (wake_cause) {
case 1 : printf("Wakeup caused by external signal using RTC_IO");break;
case 2 : printf("Wakeup caused by external signal using RTC_CNTL");break;
case 3 : printf("Wakeup caused by timer");break;
case 4 : printf("Wakeup caused by touchpad");break;
case 5 : printf("Wakeup caused by ULP program");break;
default : printf("Undefined. In case of deep sleep, reset was not caused by exit from deep sleep.");
}
printf("\n");
}
// Read the sensor via timer
//
static void sensor_timer_cb(void *arg){
if(mgos_gpio_read(2)){
printf("Door is Open! Do stuff....\n");
} else {
printf("Door is closed, going to sleep.\n");
gotosleep();
}
}
enum mgos_app_init_result mgos_app_init(void) {
printf("-------------- STARTING APPLICATION -------------\n");
why_reset();
why_wake();
int sleep_pin = 2;
rtc_gpio_deinit(sleep_pin);
mgos_gpio_set_mode(sleep_pin, MGOS_GPIO_MODE_INPUT);
mgos_gpio_set_pull(sleep_pin, MGOS_GPIO_PULL_UP);
//mgos_gpio_set_mode(4, MGOS_GPIO_MODE_OUTPUT);
//mgos_gpio_write(4,1);
mgos_set_timer(2000, MGOS_TIMER_REPEAT, sensor_timer_cb, NULL);
return MGOS_APP_INIT_SUCCESS;
}