Skip to content

Sleep Mode for ESP8266

schlammbad edited this page Oct 23, 2019 · 2 revisions

The ESP8266 sleep support in this library will allow you to enter a low power mode by calling EnterSleep().

    void EnterSleep(uint32_t microSeconds, 
        void* state = NULL, 
        uint16_t sizeofState = 0, 
        WakeMode mode = WAKE_RF_DEFAULT);

microSeconds - the time in microseconds to sleep. If this is 0, then the ESP8266 will sleep until the RST pin is pulsed LOW. If this is greater than 0, then the RST pin must be connected to the WAKE (GPIO16) pin so that when the internal RTC being used pulses the 'WAKE' pin, it will pulse the RST pin. state - a buffer of data that is stored in the RTC_MEM that can be retrieved with the RestartedFromSleep. sizeofState - the size of the passed in state buffer in bytes. mode - The state of the WiFi radio after the ESP8266 wakes up. The default should be good for most sketches.

  • WAKE_RF_DEFAULT (0) : RF_CAL or not after deep-sleep wake up, depends on init data byte 108.
  • WAKE_RF_CAL (1): RF_CAL after deep-sleep wake up, there will be large current spike at startup
  • WAKE_RF_NO_CAL (2): no RF_CAL after deep-sleep wake up, there will only be small current draw at startup
  • WAKE_RF_DISABLED (4): disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.

The ESP8266 will wake up by restarting the complete sketch. Due to this, all running state is lost unless it saved and then checked and retrieved in setup().

Simple sleep and wake

This is a simple example of how to enter sleep and react when the Arduino wakes up from sleep. 60 seconds after the call to EnterSleep() the ESP8266 will restart the sketch; where the check for RestartedFromSleep() can be used to do specific work as needed.

TaskManager taskManager;

void setup() {
  if (taskManager.RestartedFromSleep()) {
    // wake from sleep
  }
  else {
    // normal boot
  }
}

// somewhere in code, enter sleep for 60 seconds or external interrupt
  taskManager.EnterSleep(60 * 1000000); // 60 seconds

Sleep with saved state

This example demonstrates how to enter sleep and save some state so that when you wake, you are given context to apply when the ESP8266 wakes up from sleep. 60 seconds after the call to EnterSleep() the ESP8266 will restart the sketch; where the check for RestartedFromSleep() can be used to do specific work as needed.

TaskManager taskManager;

struct SleepState {
  // replace the contents with what you need
  // avoid the String class and use char[#] instead
  uint16_t someValue; 
  uint8_t anotherValue;
};

void setup() {
  SleepState state;
  if (taskManager.RestartedFromSleep(static_cast<void*>(&state), sizeof(state))) {
    // wake from sleep
    // state now contains what ever you stored in it when you called EnterSleep()
    if (state.anotherValue == 0) {
      // sleep called from second location
    }
    else {
      // sleep called from first location
    }
  }
  else {
    // normal boot
  }
}

// somewhere in code, enter sleep for 60 seconds or external interrupt
  SleepState state;
  state.someValue = 3333; 
  state.anotherValue = 42;
  taskManager.EnterSleep(60 * 1000000, &state, sizeof(state)); // 60 seconds

// another somewhere in code, enter sleep for 60 seconds or external interrupt
  SleepState state;
  state.someValue = 3333; 
  state.anotherValue = 0;
  taskManager.EnterSleep(60 * 1000000, &state, sizeof(state)); // 60 seconds
Clone this wiki locally