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

arforgethedev
Posts: 1
Joined: Sun Jan 07, 2018 12:42 am

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

Postby arforgethedev » Sun Jan 07, 2018 4:20 am

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?
I am using non_os_sdk 2.1.0

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);
}



the serial output:

Code: Select all

r����I8���..........mode : softAP(62:01:94:31:3c:0b)
add if1
dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
bcn 100
turn off after 10 seconds
going to sleep 10262005
disconnecting from wifi
disarming timers
sleep for 20 s
bcn 0
del if1
usl
mode : null
force slp enable,type: 1
fpm open,type:1 0
list_null



in wakeup function 11323243
pin 1
fpm close 3
turn off after 10 seconds
going to sleep 21323557
disconnecting from wifi
disarming timers                                               �W��Y]��function 23078164
pin 0 for 20 s             ��z����������j��+k�嫱�5Rj��H�H(�
fpm close 3 �����������j�d
turn off after 10 seconds
going to sleep 33078477
disconnecting from wifi
disarming timers
sleep for 20 s
force slp enable,type: 1
fpm open,type:1 0
list_null



in wakeup function 34081802
pin 1
fpm close 3
turn off after 10 seconds
going to sleep 44082116
disconnecting from wifi
disarming timers
sleep for 20 s
force slp enable,type: 1
fpm open,type:1 0
list_null



in wakeup function 45085446
pin 1
fpm close 3
turn off after 10 seconds
going to sleep 55085764
disconnecting from wifi
disarming timers
sleep for 20 s
force slp enable,type: 1
fpm open,type:1 0
list_null



in wakeup function 56089095
pin 1
fpm close 3
turn off after 10 seconds
going to sleep 66089412
disconnecting from wifi
disarming timers
sleep for 20 s
force slp enable,type: 1
fpm open,type:1 0
list_null



in wakeup function 67092759
pin 1
fpm close 3
turn off after 10 seconds
going to sleep 77093073
disconnecting from wifi
disarming timers                                              �W��Y]��function 78097880
pin 1 for 20 s            ��z����������j��+k�嫱�5Rj��H�H(�
fpm close 3 ���������j�d
turn off after 10 seconds
going to sleep 88098200
disconnecting from wifi
disarming timers
sleep for 20 s
force slp

Her Majesty
Posts: 233
Joined: Mon Oct 27, 2014 11:09 am

Re: Is it possible to wakeup esp8266 from light sleep periodically and by the button press.

Postby Her Majesty » Mon Jan 08, 2018 5:33 pm

If there is a timer active, the ESP cannot sleep, you can refer to the ESP8266 non OS API Guide, which provided an example of light sleep.

Who is online

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