Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ben Rockwood
committed
Jan 5, 2018
0 parents
commit d2454c1
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
author: Ben Rockwood <benr@cuddletech.com> | ||
description: C++ example of the ESP32 Deep Sleep | ||
version: 1.0 | ||
|
||
libs_version: ${mos.version} | ||
modules_version: ${mos.version} | ||
mongoose_os_version: ${mos.version} | ||
|
||
tags: | ||
- c | ||
|
||
sources: | ||
- src | ||
|
||
filesystem: | ||
- fs | ||
|
||
libs: | ||
- origin: https://github.com/mongoose-os-libs/ca-bundle | ||
- origin: https://github.com/mongoose-os-libs/rpc-service-config | ||
- origin: https://github.com/mongoose-os-libs/rpc-service-fs | ||
- origin: https://github.com/mongoose-os-libs/rpc-uart | ||
- origin: https://github.com/mongoose-os-libs/rpc-service-gpio | ||
- origin: https://github.com/mongoose-os-libs/rpc-mqtt | ||
- origin: https://github.com/mongoose-os-libs/mqtt | ||
- origin: https://github.com/mongoose-os-libs/wifi | ||
|
||
- origin: https://github.com/mongoose-os-libs/mjs | ||
- origin: https://github.com/mongoose-os-libs/esp32-touchpad | ||
|
||
manifest_version: 2017-05-18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#include "mgos.h" | ||
#include "rom/rtc.h" | ||
#include "driver/rtc_io.h" | ||
#include "esp_sleep.h" | ||
|
||
|
||
void my_main(int pin){ | ||
|
||
mgos_gpio_set_mode(pin, MGOS_GPIO_MODE_INPUT); | ||
mgos_gpio_set_pull(pin, MGOS_GPIO_PULL_UP); | ||
|
||
printf("Door sensor reads: %s\n", (mgos_gpio_read(pin)) ? "Open" : "Closed"); | ||
|
||
if(mgos_gpio_read(pin)){ | ||
printf("Door is open, lets do some stuff.\n"); | ||
printf("OK, all done, sleepy time."); | ||
} else { | ||
printf("Door is closed. Lets go to sleep.\n"); | ||
} | ||
sleep(10); | ||
} | ||
|
||
static void gotosleep(int pin, void *arg){ | ||
// Register the wakeup | ||
esp_sleep_enable_ext0_wakeup(pin, 1); // 1== HIGH (Sensor Open) | ||
// Go to sleep. | ||
printf("All done. Going to sleep in "); | ||
for(int i=5; i>0; --i){ | ||
printf("%d...\n", i); | ||
sleep(2); | ||
} | ||
printf("Good night.\n"); | ||
rtc_gpio_pullup_en(pin); // Fix for strange GPIO wakeup behavior. | ||
rtc_gpio_pulldown_dis(pin); | ||
esp_deep_sleep_start(); | ||
} | ||
|
||
void why_reset(){ | ||
int reset_reason = rtc_get_reset_reason(0); | ||
printf("Reset Reason (%d): ", reset_reason); | ||
|
||
// 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 | ||
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"); | ||
} | ||
|
||
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"); | ||
case 2 : printf("Wakeup caused by external signal using RTC_CNTL"); | ||
case 3 : printf("Wakeup caused by timer"); | ||
case 4 : printf("Wakeup caused by touchpad"); | ||
case 5 : printf("Wakeup caused by ULP program"); | ||
default : printf("Undefined. In case of deep sleep, reset was not caused by exit from deep sleep."); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
static void sensor_timer_cb(void *arg){ | ||
if(mgos_gpio_read(13)){ | ||
printf("Door is Open! Do stuff....\n"); | ||
} else { | ||
printf("Door is closed, going to sleep.\n"); | ||
gotosleep(13, NULL); | ||
} | ||
|
||
|
||
|
||
(void) arg; // Return the args | ||
} | ||
|
||
enum mgos_app_init_result mgos_app_init(void) { | ||
printf("-------------- STARTING APPLICATION -------------\n"); | ||
why_reset(); | ||
why_wake(); | ||
|
||
|
||
int pin = 13; | ||
rtc_gpio_deinit(pin); | ||
mgos_gpio_set_mode(pin, MGOS_GPIO_MODE_INPUT); | ||
mgos_gpio_set_pull(pin, MGOS_GPIO_PULL_UP); | ||
printf("MGOS GPIO13 read: %d\n", mgos_gpio_read(13)); | ||
printf("RTC GPIO13 read: %d\n", rtc_gpio_get_level(13)); | ||
|
||
mgos_set_timer(2000, MGOS_TIMER_REPEAT, sensor_timer_cb, NULL); | ||
|
||
/* | ||
int pin = 13; | ||
mgos_gpio_set_int_handler(pin, MGOS_GPIO_INT_LEVEL_LO, door_closed_cb, NULL); | ||
mgos_gpio_enable_int(pin); | ||
my_main(pin); | ||
*/ | ||
|
||
return MGOS_APP_INIT_SUCCESS; | ||
} |