Light- or Modem Sleep

Posted on
  • Hi!

    Does Espruino on the ESP8266 support triggering light- or modem sleep (see here)?

    Thanks in advance!

  • Sleep is not yet supported, espruino on esp8266 is still in a beta state.

  • thank you for your fast response!

    Is there any other option to reduce power consumption? Can I e.g. turn off wifi completely? Or is there any easy way to "hack" C calls to ESP8266 SDK functions into Espruino?

    I am currently evaluating platforms for a battery-powered display controller that gets its information using remote API calls.

  • The code to add deep sleep is not that much:­69ef56759e3d04215bcc49b
    I haven't committed it to my fork nor built a binary with it, however.

  • IMO that code should really go into jshSleep - if you look at the STM32 port it basically does:

    void jshSleep(time) {
      if (deepSleepEnabled) {
      } else {

    I know it's a bit different in ESP8266, but it should still be possible to sleep in there - and then it behaves the same as other Espruino devices. You just call setDeepSleep(1) and from then on it does deep sleep whenever it can.

  • That would be appropriate for "light sleep" in which memory is preserved. Deep sleep does not preserve memory. When it wakes up it's as if someone had pressed the reset button. (That's actually literally what happens.)
    I know how to implement light sleep, but haven't gotten to it yet. It's a bit more complicated.

  • Oh wow, yeah. That's going to be painful - not really suitable then :)

    To be honest this is something I should expose in Espruino on STM32 as well - it could be extremely useful for some things. Do IRQs and RTC still wake up the ESP8266?

  • in light sleep it's either a timer or a gpio transition, but not an interrupt. so can't wake-up from serial input.

    in deep sleep it's either the rtc timer or a reset...

  • I did not find a firmware built that has this included so I built my own version that contains deep and modem sleep (see here ), firmware is also attached to this post.

    1 Attachment

  • Wow, awesome!

    Is the source code for this in github anywhere?

    If it works, maybe you could create a pull request so we could get this functionality into main releases? A number of people have expressed interest in this functionality.

  • @urbiman: used 1.85, added code, compiled, flashed, tested and lost control ....

    In deepSleep()
    sleepTime: 1000000
    >rLL¼°rBÂN<LÌ<p|¾Ìp²¾²¼ÌLpp~òN¾LNN¼2BB<pÌ<BL@rL2¾ò²Î22NNBpü 
  • @MaBe : You need to make sure that you connect the reset pin and gpio16 with each other otherwise the chip won't wake up again. Other than that 1000*1000 is just one second.

    @DrAzzy : I sure can do that but I do not know if pulls are accepted? The code can also be seen in my blog (see the link in my post before).

  • +Gordon is always encouraging people who expand or enhance Espruino to contribute it back to the repo. How does your implementation interact with setTimeout?

    I think Gordon could advise on how to best integrate it with Espruino. The way deepsleep is implemented on the "normal" Espruinii (ones based on STM32) is that you just "turn on" deep sleep, and the then it wakes up every few seconds (was it 3? 8? some single digit number of seconds) very briefly at that interval, checking for things to execute.

  • You could a actually implement the sleeping in the jshSleep function - and then as @DrAzzy says, it would work automatically after being turned on with setDeepSleep(1) (assuming this doesn't reset the whole device?).

    Pull requests would be great and are definitely accepted - as long as they don't break things that worked before :)

  • Actually I do not interact with setTimeout. In the case of deepsleep it wouldn't make too much sense either I think, as after deepsleep the chip is resetted and therefor all timers are lost. In case of modemsleep it should be uneccessary as the CPU runs normal.

    I am not sure about lightsleep (I did not implement that) but my current understanding is, that lightsleep needs an external trigger to wakeup:

    Under the light-sleep mode, the CPU will not respond to the signal and interrupt from the peripheral hardware interface under the pause state. Therefore, the ESP8266 need to be waked up via external GPIO, and the wake process is less than 3 ms.

    To me currently it seems that there is no way where the ESP8266 could interact with setTimeout (however I am not completely sure when it comes to lightsleep as RTC is running according to the document above).

  • Ok, yeah - if it resets then I think exactly what you've done is the right way to go.

  • Ok, good to hear :) I will do a pull in the next days first adding deepsleep and modemsleep. However it seems to be possible to implement what you have done for the STM boards by using lightsleep. I found the following code (using light sleep and a timer):

    //sleep over.
    void fpm_wakup_cb_func1(void)
       wifi_fpm_close();   //disable sleep function
       wifi_set_opmode(STATION_MODE);      //set wifi mode to station mode
       wifi_station_connect();         //connect ap
    void user_func(...)
    wifi_set_opmode(NULL_MODE);      //set wifi mode to null mode.
    wifi_fpm_set_sleep_type(LIGHT_SLEEP_T);      //set force sleep type, clsoe rf&cpu
    wifi_fpm_open();            //enable force sleep fucntion
    wifi_fpm_set_wakeup_cb(fpm_wakup_cb_func­1);   //Set fpm wakeup callback function 
    wifi_fpm_do_sleep(10*1000);         // do sleep


    I think that would be the next step then...

  • Yes, that looks like it'd be good - however you'd only want to do that when Wifi is off?

  • I did a pull now to merge the deepSleep and modemSleep stuff (#825).

    Regarding the lightSleep it seems that this is only possible with wifi off if you want to enforce the sleep (which is what is supposed as far as I understand):

    Yes, wifi_station_disconnect(); and wifi_set_opmode_current(NULL_MODE); are needed before calling force sleep function. We will add more description on this in the documentation.

  • Are you a spammer?
    Page show only facebook.

  • Thanks - deleted. He spam posted something else too...

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Light- or Modem Sleep

Posted by Avatar for user61930 @user61930