Is it possible to wakeup esp8266 from sleep periodically and by the button press?

homedweller
Posts: 1
Joined: Tue Apr 17, 2018 7:42 pm

Is it possible to wakeup esp8266 from sleep periodically and by the button press?

Postby homedweller » Thu Apr 26, 2018 10:46 pm

I making the device that needs to wake up periodically and by the button press.
I am trying to implement this functionality but I have some problems with combining this functional. The button is going to sleep but wakes up only on button press. Then it goes to sleep and wake up immediately for a few times and goes to sleep until I press the button again.
Are there some solutions for this issue?

Code: Select all

#include "driver/uart.h"
#include "gpio.h"
#include "osapi.h"
#include "user_interface.h"

LOCAL os_timer_t taskTimer;
LOCAL os_timer_t sleepTimer;

void ICACHE_FLASH_ATTR fpm_wakup_cb_func(void)
{
  uint8_t pin_state = GPIO_INPUT_GET(5);
  os_printf("\n\n\nin wakeup function %d\n", system_get_time());
  os_printf("pin %d\n", pin_state);
  wifi_fpm_close(); // disable force sleep function

  // wifi_set_sleep_type(NONE_SLEEP_T);
  // wifi_fpm_set_sleep_type(NONE_SLEEP_T);
  app_init();
}

void ICACHE_FLASH_ATTR go_to_sleep(void)
{
  os_printf("sleep for 20 s\n");
  wifi_set_opmode(NULL_MODE);

  wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);

  PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U, FUNC_GPIO5);
  // PIN_PULLUP_DIS(BUTTON_PIN_MUX);
  gpio_output_set(0, GPIO_ID_PIN(5), 0, GPIO_ID_PIN(5));
  wifi_enable_gpio_wakeup(5, GPIO_PIN_INTR_LOLEVEL);

  wifi_fpm_open();

  wifi_fpm_set_wakeup_cb(fpm_wakup_cb_func); // Set wakeup callback

  // wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME);
  wifi_fpm_do_sleep(20 * 1000000); // Sleep for 300s, then publish again to test
  // os_printf("sleep done...\n");
}

void ICACHE_FLASH_ATTR turn_off()
{
  os_printf("going to sleep %d\n", system_get_time());

  os_printf("disconnecting from wifi\n");
  wifi_station_disconnect();

  os_printf("disarming timers\n");
  os_timer_disarm(&taskTimer);

  os_timer_disarm(&sleepTimer);
  os_timer_setfn(&sleepTimer, (os_timer_func_t *)go_to_sleep, NULL);
  os_timer_arm(&sleepTimer, 1000, 0);
}

void ICACHE_FLASH_ATTR set_turn_off_after(uint8_t sec)
{
  os_printf("turn off after %d seconds\n", sec);
  os_timer_disarm(&taskTimer);
  os_timer_setfn(&taskTimer, (os_timer_func_t *)turn_off, NULL);
  os_timer_arm(&taskTimer, sec * 1000, 0);
}

void ICACHE_FLASH_ATTR app_init(void)
{
  set_turn_off_after(10);
}

void ICACHE_FLASH_ATTR user_init(void)
{
  //tur on the led
  gpio_init();
  PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4);
  gpio_output_set((1 << 4), 0, (1 << 4), 0);

  uart_init(BIT_RATE_115200, BIT_RATE_115200);
  uint8_t i;
  for (i = 0; i < 10; i++)
  {
    os_printf(".");
    os_delay_us(10000);
  }

  system_init_done_cb(app_init);
}


Could someone help me?

Cheers,
homedweller

Who is online

Users browsing this forum: Bing [Bot] and 4 guests