Author Topic: Light Sleep with GPIO wakeup  (Read 18607 times)

zemborg

  • Newbie
  • *
  • Posts: 1
Light Sleep with GPIO wakeup
« on: March 15, 2016, 06:20:50 am »
I'm trying to send my oak into light sleep (or deep sleep) to save power. My current setup is i have a input that will be toggled every 1 second to 6 hours, i therefore would like to have the oak sleep to save power in between input toggles. Once the input is toggled (detected by an interrupt) the oak sends a GET request to a webpage.

From my googleing, i would like to use functions like:
However they dont seem to be implemented on the oak.

Code: [Select]
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);
gpio_pin_wakeup_enable(GPIO_ID_PIN(2), GPIO_PIN_INTR_LOLEVEL); // GPIO_ID_PIN(2) corresponds to GPIO2 on ESP8266-01 , GPIO_PIN_INTR_LOLEVEL for a logic low, can also do other interrupts, see gpio.h above
wifi_fpm_open(); // Enables force sleep
wifi_fpm_do_sleep(0xFFFFFFF); // Sleep for longest possible time

Looking through the oak git repository i have found the functions:

Code: [Select]
// gpio.h
void gpio_pin_wakeup_enable(uint32 i, GPIO_INT_TYPE intr_state);

// Esp.h
void deepSleep(uint32_t time_us, RFMode mode = RF_DEFAULT);

// user_interface.h
bool system_deep_sleep_set_option(uint8 option);
void system_deep_sleep(uint32 time_in_us);
bool wifi_set_sleep_type(enum sleep_type type);
enum sleep_type wifi_get_sleep_type(void);

I have tried calling wifi_set_sleep_type(LIGHT_SLEEP_T), however i find no difference in the current draw. My oak uses 70-80mA constantly. Is there a way to for it to go into light sleep until a pin interrupt?
Also would deep sleep be an option for this? I think my input would need to be connected to RST, but since my input toggles it would miss half of the events?

References:
ESP Sleep Doc -
http://espressif.com/sites/default/files/documentation/9b-esp8266_sleep_function_description_en_v1.0.pdf
ESP Force Sleep Example -
https://github.com/esp8266/Arduino/issues/1381

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Light Sleep with GPIO wakeup
« Reply #1 on: March 16, 2016, 10:50:29 pm »
I haven't used the ESP8266 with Arduino before the Oak, but I would be looking at the ESP8266 Arduino IDE API calls instead of  Expressif docs for this sort of thing. If you have a look at the ESP specific API calls, there is a specific deep sleep call you can use. Hope that gets you somewhere!

Pete

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Light Sleep with GPIO wakeup
« Reply #2 on: March 17, 2016, 12:59:27 am »
I have successfully used ESP.deepSleep(sleepTimeS*1000000, WAKE_RF_DEFAULT) on the Oak to reduce power usage for a sketch that reads a DS18B20 thermometer. The arg sleepTimeS is the number of seconds to sleep.

If you use this method, you will have to connect the WAKE pin to the RST pin. But it seems that you want to cause a reset via an external trigger. So as you suggested you could connect your external trigger to reset which I believe is "active LOW" and normally pulled up on the Oak. So the trigger would have to pull reset LOW. If you wanted to have a reset occur on state CHANGE, you may have to create some external digital logic circuit to pulse RST low on the state CHANGE. I would also wonder if at the 1 sec interval if there would be enough time for the wireless connection to come back.

You may already know this but reset will cause your sketch to start from the beginning and enter setup().

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Light Sleep with GPIO wakeup
« Reply #3 on: March 18, 2016, 03:06:22 am »
Hey, thanks for the exeng... I suppose it helps when you read the labels on the Oak!!!! I have now re-programmed my own DS18B20-enabled Oak to deepSleep 60 seconds between updates, and it is behaving well. So well that I need to get some better USB power meters (I only have ones that have 0.xxA resolution) as they're showing me 0.00A when the Oak is asleep... but there will still be a couple of milliamps flowing due to the LED, etc). Even so, no comparision to ~70ma when 'working'!.

Now I have need of the P1 safe mode for programming, as now that it deepSleeps, it won't program OTA as it is either asleep, or waking up, and not listening!

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Light Sleep with GPIO wakeup
« Reply #4 on: March 18, 2016, 10:52:48 am »
Oooops... Yes forgot about the inability to update when sleeping. I ran into this too and should have mentioned it. Here is what it did at the end of setup().

Code: [Select]
  // Check to see if sleep mode selected
  if (digitalRead(Sleep_pin) == HIGH) {
     Particle.publish("Oak Setup", "Entering Deep Sleep", 60, PRIVATE);
     ESP.deepSleep(sleepTimeS*1000000, WAKE_RF_DEFAULT); // Sleep
  }
  else {
     Particle.publish("Oak Setup","Entering Non Sleep Mode",60,PRIVATE);
  }
} //END setup()

Basically I defined a Sleep_pin to be INPUT_PULLUP (normally HIGH) which indicates to the sketch that I want to sleep (suppose I could have done the opposite as the idea is less power). Then when I want to run normally and enter loop(), I pull the Sleep_pin to GND. This allows me to get out of sleep mode so that I can upload new sketches.

Hope this helps.

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Light Sleep with GPIO wakeup
« Reply #5 on: March 18, 2016, 06:39:41 pm »
Nice idea exeng! And yes, I get what you mean about less power - however, in the end, you either need to pull up or pull down to make sure the pin doesn't float, so you might as well take the easy way out and use the internal pullups so you can just use a jumper to GND as the switch.

I couldn't be bothered. if I want to program it again, I'll just pull the power, jumpter P1 to ground, and reprogram from config mode. But this is certainly a valid point to raise and a simple and elegant solution, as someone will do the deepSleep and be going 'now my Oak won't program!!!' :)

zeiv

  • Newbie
  • *
  • Posts: 2
Re: Light Sleep with GPIO wakeup
« Reply #6 on: April 20, 2016, 02:12:22 pm »
So is deep sleep the only low-power mode that the Oak supports?  I have a battery-powered application where I would like to quickly perform an HTTP request after pressing a button.  Is there any kind of sleep or low-power mode where the Oak can stay connected to the Wifi?  Or at least "connected enough" that it can wake up, connect to the wifi, connect to Blynk, and perform the action all in less than a second?

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Light Sleep with GPIO wakeup
« Reply #7 on: April 22, 2016, 03:28:07 am »
The deepSleep function accepts one of four mode parameters (WAKE_RF_DEFAULT, WAKE_RFCAL, WAKE_NO_RFCAL, WAKE_RF_DISABLED). However, I would think that anything that turns off the radio will then require renegotiation with the access point, and consequently take more than a second to process. Funnily enough, I haven't seen any code that specifically switches on or off the radio - only manually connect if you have the Oak in a different mode. This thread on github may be of some interest on that topic, but I can't say how much applies to the Oak.

NotNot

  • Newbie
  • *
  • Posts: 4
Re: Light Sleep with GPIO wakeup
« Reply #8 on: May 02, 2016, 03:26:31 pm »
Hi,
I'm trying to use deep sleep mode too, but I can't wake up the module once it goes in DSM. I wrote a simple code based on your example:
Code: [Select]
#include "stdlib.h"
#include <ESP8266WiFi.h>

#define SLEEP_PIN   5
int sleepTimeS = 20;


void setup() {               
  // initialize the digital pin as an output.
  pinMode(1, OUTPUT); //LED on Oak
  pinMode(0, INPUT);
  pinMode(2, INPUT);
  pinMode(6, INPUT);
 
 
}

// the loop routine runs over and over again forever:
void loop() {
  short i;
 
  for(i=0; i<20; i++)
  {
    digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);               // wait for a second
    digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);               // wait for a second
  }

  // Check to see if sleep mode selected
  if (digitalRead(SLEEP_PIN) == HIGH) {
     Particle.publish("Oak Setup", "Entering Deep Sleep", 60, PRIVATE);
     ESP.deepSleep(sleepTimeS*1000000, WAKE_RF_DEFAULT); // Sleep
  }
  else
  {
     Particle.publish("Oak Setup","Entering Non Sleep Mode",60,PRIVATE);
  }

}

Powered via USB.
I've connected pin 6 to GND, pin 0 and 2 to Vcc via a 47K resistor and I use pin 5 to enter or not DSM.

The Oak doesn't wake up.  :-[

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Light Sleep with GPIO wakeup
« Reply #9 on: May 02, 2016, 06:08:48 pm »
You need to connect pin 10 (WAKE) to the reset pin (RST) in order to wakeup. If you enter deep sleep without this connection (WAKE to RESET) you will not wake up. The pin 5 example that you are using only provides a means to not enter deep sleep so that you can upload a new sketch, it has nothing to do with waking up. It's really not necessary as you can enter safe mode by connecting pin 1 to GND and cycling power, then remove the pin 1 to GND connection after a few seconds.

Make sure pin 10 (WAKE) is connected to the reset pin (RST) and your sketch should wake up.
« Last Edit: May 02, 2016, 07:39:00 pm by exeng »

NotNot

  • Newbie
  • *
  • Posts: 4
Re: Light Sleep with GPIO wakeup
« Reply #10 on: May 03, 2016, 02:23:11 pm »
Ok. exeng thank you for your help. It works.

Can you point me to some docs about this HW... where this kind of information can usually be found? Is there some data sheet?

Anyway I owe you one.
Thanks.

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Light Sleep with GPIO wakeup
« Reply #11 on: May 03, 2016, 04:47:37 pm »
Does this help? Wiki links for the OAK hardware:
http://digistump.com/wiki/oak

NotNot

  • Newbie
  • *
  • Posts: 4
Re: Light Sleep with GPIO wakeup
« Reply #12 on: May 04, 2016, 02:02:19 pm »
:)
I know this page. But I think you can't find this info there (connect RST to WAKE) or simply I'm to noob to find it. :P

Thanks again.

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Light Sleep with GPIO wakeup
« Reply #13 on: May 04, 2016, 02:46:18 pm »
You are correct and there has been a few that have tripped on this. Perhaps I will add info to the appropriate wiki page and post the link here. Stay Tuned.

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Light Sleep with GPIO wakeup
« Reply #14 on: May 04, 2016, 03:13:20 pm »
Added text re: pin 10 [WAKE] connection to reset pin [RST] requirement to facilitate wake up after entering a sleep mode in http://digistump.com/wiki/oak/tutorials/pinout#general_notes