Copyright ©

Mongoose OS Forum


JS API for deep sleep

blublablubla United States

Hey, how's it going guys?

I see that there's usleep on the js Sys API. I reckon that's not deep sleep, right? (because deep sleep needs reset to wake up)
If so, how can you use deep sleep?



  • kennytkennyt Japan

    i am also looking for this. how can we use deep sleep? :#

  • There is an outstanding pull request:

    Not sure on the process or status of it though.

  • blublablubla United States

    That would be a nice add, especially for devices with non-replaceable batteries or any device were battery is critical!

  • I really need the uAmps deep sleep right now...
    about this pull request: is the deep sleep already in the core implemented. can we simply use something like
    let deepsleep = ffi('void system_deep_sleep(int)');

  • SergeySergey Dublin, Ireland

    Ah, let me integrate that pr, apologies for a delay. Will update this thread when a default firmware download get that in.

  • blublablubla United States
  • SergeySergey Dublin, Ireland

    Update your firmware. there is api_esp8266.js

    For deep sleep, you'd need RTC functions too - they are not there yet.

  • Any chance we can get deep sleep for ESP32 too? Thanks!

  • SergeySergey Dublin, Ireland

    Use ESP-IDF SDK for now.

    If you feel brave, send a PR for FFI ESP-IDF C functions to JS , akin to

  • kennytkennyt Japan

    How do you go about using ESP-IDF on mongoose? can we add the deepsleep lib as a mos lib?

  • SergeySergey Dublin, Ireland
    1. In C/C++, you can use ESP-IDF API as-is. Just include the header you want and call a function.
    2. In JS, ffi the required ESP-IDF SDK call, and rebuild the app.
  • kennytkennyt Japan

    Is this how it is done?

    GPIO.set_button_handler(23, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
      let test = {
        deepSleep:  ffi('void system_deep_sleep(int)'),
    }, null);
  • Hello,

    New to mgos and IoT development in general, any help would be much appreciated.

    I have the option of using either an esp8266 or an esp32 to implement a deep sleep mode, and I'm also struggling to figure out exactly how to do so.
    As far as I can tell the deep sleep JS hooks in the pull request mentioned earlier in this thread were all eventually folded into the main source in api_esp8266.js, but have subsequently been removed in this commit a few days ago, after which I can't find any mention of them, presumably because the developers wish for us to define architecture-specific ffi's in our own code. (Tell me if this is incorrect?)
    I've tried to ffi the system_deep_sleep method as described in kennyt's post, expecting it to work on the 8266 but not the 32, but I get the same error message on both platforms: "MJS callback error: bad ffi signature".

    Sergey's post above says that to properly ffi things, I have to "include the correct header" - does that correspond to the esp-idf file that defines the system_deep_sleep method? I can't find that definition anywhere in the mgos source files.

    Thank you!

  • rojerrojer Dublin, Ireland

    there is a second part to that commit, but it's to a different repo. here it is:

  • edited July 14

    Thank you! - for anyone wondering, I got 10 second deep sleep on button press working by including the mjs library and making the following modifications to the default init.js on esp8266:


    // Helper C function get_led_gpio_pin() in src/main.c returns built-in LED GPIO
    // ffi() returns a callbale object for the specified C function.
    // As parsing the signature has non-trivial overhead, it's a good practice to
    // store the value for later reuse.
    let get_led_gpio_pin = ffi('int get_led_gpio_pin()');
    // Now call the function to obtain the LED pin number.
    let led = get_led_gpio_pin();

    let getInfo = function() {
    return JSON.stringify({total_ram: Sys.total_ram(), free_ram: Sys.free_ram()});

    // Blink built-in LED every second
    GPIO.set_mode(led, GPIO.MODE_OUTPUT);
    Timer.set(1000 /* 1 sec /, true / repeat */, function() {
    let value = GPIO.toggle(led);
    print(value ? 'Tick' : 'Tock', 'uptime:', Sys.uptime(), getInfo());
    }, null);

    // Publish to MQTT topic on a button press. Button is wired to GPIO pin 0
    GPIO.set_button_handler(0, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
    let deepSleep = ffi('int mgos_system_deep_sleep_d(double)');
    }, null);

    I suspect that hacking this in on the esp32 will be much harder...

  • rojerrojer Dublin, Ireland
    edited July 14

    it will actually be exactly the same, except for the deep sleep call, which we don't have on esp32 yet (but soon will - i sent out a change to add it).

  • Thanks a lot for this.

    I am hoping to use the code in that pull request as a template to access more advanced deep sleep functions like esp_deep_sleep_enable_ext0_wakeup.
    I'm accomplishing this by patching two files: esp32_mjs.c:

    void mgos_esp_deep_sleep_enable_ext0_wakeup_d(int gpio_num, int level) {
      esp_deep_sleep_enable_ext0_wakeup(gpio_num, int level);

    and api_esp32.js:

    deepSleepExt0: ffi('void mgos_esp_deep_sleep_enable_ext0_wakeup_d(gpio_num, level)'),

    Then from init.js I try to call ESP32.deepSleepExt0(0,0) on button press.
    From there I get: "MJS error: failed to exec file "api_esp32.js": bad ffi signature: "void esp_deep_sleep_enable_ext0_wakeup_d()": dlsym('esp_deep_sleep_enable_ext0_wakeup_d') failed"

    Is there something else I need to do or any obvious mistake here?

  • rojerrojer Dublin, Ireland
    edited July 22

    ffi signature declares arg types, not names. this should do it:

    deepSleepExt0: ffi('void mgos_esp_deep_sleep_enable_ext0_wakeup_d(int, int)'),

    Thanked by 1wormyrocks
  • Any plans to incorporate deep sleep functionality for the CC3200?

  • rojerrojer Dublin, Ireland

    nothing concrete at the moment, please file a feature request here.

  • edited July 26


    Got deep sleep/wake toggle working on button press, thanks much.
    The next challenge is setting up the button as a GPIO after the button is pressed to bring the module out of deep sleep, as esp_deep_sleep_ext0 configures pin 0 with the RTC peripheral.
    I am trying to use rtc_gpio_deinit to configure the pin as a gpio again, as described in this tutorial and this one. However, the firmware won't build when I try to include rtc_io.h.

    Thanks for any help folks.

Sign In or Register to comment.