|
| 1 | +#include "mgos.h" |
| 2 | +#include "rom/rtc.h" |
| 3 | +#include "driver/rtc_io.h" |
| 4 | +#include "esp_sleep.h" |
| 5 | + |
| 6 | + |
| 7 | +void my_main(int pin){ |
| 8 | + |
| 9 | + mgos_gpio_set_mode(pin, MGOS_GPIO_MODE_INPUT); |
| 10 | + mgos_gpio_set_pull(pin, MGOS_GPIO_PULL_UP); |
| 11 | + |
| 12 | + printf("Door sensor reads: %s\n", (mgos_gpio_read(pin)) ? "Open" : "Closed"); |
| 13 | + |
| 14 | + if(mgos_gpio_read(pin)){ |
| 15 | + printf("Door is open, lets do some stuff.\n"); |
| 16 | + printf("OK, all done, sleepy time."); |
| 17 | + } else { |
| 18 | + printf("Door is closed. Lets go to sleep.\n"); |
| 19 | + } |
| 20 | + sleep(10); |
| 21 | +} |
| 22 | + |
| 23 | +static void gotosleep(int pin, void *arg){ |
| 24 | + // Register the wakeup |
| 25 | + esp_sleep_enable_ext0_wakeup(pin, 1); // 1== HIGH (Sensor Open) |
| 26 | + // Go to sleep. |
| 27 | + printf("All done. Going to sleep in "); |
| 28 | + for(int i=5; i>0; --i){ |
| 29 | + printf("%d...\n", i); |
| 30 | + sleep(2); |
| 31 | + } |
| 32 | + printf("Good night.\n"); |
| 33 | + rtc_gpio_pullup_en(pin); // Fix for strange GPIO wakeup behavior. |
| 34 | + rtc_gpio_pulldown_dis(pin); |
| 35 | + esp_deep_sleep_start(); |
| 36 | +} |
| 37 | + |
| 38 | +void why_reset(){ |
| 39 | + int reset_reason = rtc_get_reset_reason(0); |
| 40 | + printf("Reset Reason (%d): ", reset_reason); |
| 41 | + |
| 42 | + // https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/ResetReason/ResetReason.ino |
| 43 | + // https://github.com/espressif/esp-idf/blob/master/components/esp32/include/rom/rtc.h |
| 44 | + switch (reset_reason) { |
| 45 | + case 1 : printf("Vbat power on reset");break; |
| 46 | + case 3 : printf("Software reset digital core");break; |
| 47 | + case 4 : printf("Legacy watch dog reset digital core");break; |
| 48 | + case 5 : printf("Deep Sleep reset digital core");break; |
| 49 | + case 6 : printf("Reset by SLC module, reset digital core");break; |
| 50 | + case 7 : printf("Timer Group0 Watch dog reset digital core");break; |
| 51 | + case 8 : printf("Timer Group1 Watch dog reset digital core");break; |
| 52 | + case 9 : printf("RTC Watch dog Reset digital core");break; |
| 53 | + case 10 : printf("Instrusion tested to reset CPU");break; |
| 54 | + case 11 : printf("Time Group reset CPU");break; |
| 55 | + case 12 : printf("Software reset CPU");break; |
| 56 | + case 13 : printf("RTC Watch dog Reset CPU");break; |
| 57 | + case 14 : printf("for APP CPU, reseted by PRO CPU");break; |
| 58 | + case 15 : printf("Reset when the vdd voltage is not stable");break; |
| 59 | + case 16 : printf("RTC Watch dog reset digital core and rtc module");break; |
| 60 | + default : printf("NO_MEAN"); |
| 61 | + } |
| 62 | + printf("\n"); |
| 63 | +} |
| 64 | + |
| 65 | +void why_wake(){ |
| 66 | + int wake_cause = esp_sleep_get_wakeup_cause(); |
| 67 | + printf("Wake Cause (%d): ", wake_cause); |
| 68 | + switch (wake_cause) { |
| 69 | + case 1 : printf("Wakeup caused by external signal using RTC_IO"); |
| 70 | + case 2 : printf("Wakeup caused by external signal using RTC_CNTL"); |
| 71 | + case 3 : printf("Wakeup caused by timer"); |
| 72 | + case 4 : printf("Wakeup caused by touchpad"); |
| 73 | + case 5 : printf("Wakeup caused by ULP program"); |
| 74 | + default : printf("Undefined. In case of deep sleep, reset was not caused by exit from deep sleep."); |
| 75 | + } |
| 76 | + printf("\n"); |
| 77 | +} |
| 78 | + |
| 79 | +static void sensor_timer_cb(void *arg){ |
| 80 | + if(mgos_gpio_read(13)){ |
| 81 | + printf("Door is Open! Do stuff....\n"); |
| 82 | + } else { |
| 83 | + printf("Door is closed, going to sleep.\n"); |
| 84 | + gotosleep(13, NULL); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + (void) arg; // Return the args |
| 90 | +} |
| 91 | + |
| 92 | +enum mgos_app_init_result mgos_app_init(void) { |
| 93 | + printf("-------------- STARTING APPLICATION -------------\n"); |
| 94 | + why_reset(); |
| 95 | + why_wake(); |
| 96 | + |
| 97 | + |
| 98 | + int pin = 13; |
| 99 | + rtc_gpio_deinit(pin); |
| 100 | + mgos_gpio_set_mode(pin, MGOS_GPIO_MODE_INPUT); |
| 101 | + mgos_gpio_set_pull(pin, MGOS_GPIO_PULL_UP); |
| 102 | + printf("MGOS GPIO13 read: %d\n", mgos_gpio_read(13)); |
| 103 | + printf("RTC GPIO13 read: %d\n", rtc_gpio_get_level(13)); |
| 104 | + |
| 105 | + mgos_set_timer(2000, MGOS_TIMER_REPEAT, sensor_timer_cb, NULL); |
| 106 | + |
| 107 | +/* |
| 108 | + int pin = 13; |
| 109 | + mgos_gpio_set_int_handler(pin, MGOS_GPIO_INT_LEVEL_LO, door_closed_cb, NULL); |
| 110 | + mgos_gpio_enable_int(pin); |
| 111 | + my_main(pin); |
| 112 | +*/ |
| 113 | + |
| 114 | + return MGOS_APP_INIT_SUCCESS; |
| 115 | +} |
0 commit comments