ivmarkov / rust-esp32-std-demo

Rust on ESP32 STD demo app. A demo STD binary crate for the ESP32[XX] and ESP-IDF, which connects to WiFi, Ethernet, drives a small HTTP server and draws on a LED screen.
Apache License 2.0
784 stars 105 forks source link

Possible to use old wifi stack, after sleeping? #74

Closed S3j5b0 closed 2 years ago

S3j5b0 commented 2 years ago

Hi, have, based on this example, made a little tcp client that runs on mty esp32 wroom, and exchanges some messages with my computer. The device sits there and pings my computer once in a while, with a few minutes between every message. I'm using the functions that I grabbed from the example to initialize the wifi connection, this is the wifi and pingfunction.

and then before I make the tcp connection, I run these (also as given by the example)

    #[allow(unused)]
    let netif_stack = Arc::new(EspNetifStack::new()?);
    #[allow(unused)]
    let sys_loop_stack = Arc::new(EspSysLoopStack::new()?);
    #[allow(unused)]
    let default_nvs = Arc::new(EspDefaultNvs::new()?);

    #[allow(clippy::redundant_clone)]
    #[cfg(not(feature = "qemu"))]
    #[allow(unused_mut)]
    let mut wifi = wifi(
        netif_stack.clone(),
        sys_loop_stack.clone(),
        default_nvs.clone(),
    )?;

In order to save power I thought I would try to let the device sleep in between the messages it sends, so I tried adding the esp_deep_sleep function, like so:

    unsafe {
        esp_idf_sys::esp_deep_sleep(Duration::from_secs(60).as_micros() as u64);
    }
return Ok(())

This of course gives me some issues with the wifi, since it is not woken up again, and I end up with this output:

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0048,len:12
ho 0 tail 12 room 4
load:0x3fff0054,len:4800
load:0x40078000,len:17448
load:0x4007c428,len:4840
entry 0x4007c6a0
I (426) cpu_start: Pro cpu up.
I (426) cpu_start: Starting app cpu, entry point is 0x40082794
....
I (2800) wifi:Deinit lldesc rx mblock:10
Error: Unexpected Wifi status: Status(Started(Connecting), Starting)

Is there any way that I can set the device to sleep, and then when it wakes up, re-access the initialized wifi?

I also tried adding the wifi initialization code below the sleep, to see if it would just recnnect:

    unsafe {
        esp_idf_sys::esp_deep_sleep(Duration::from_secs(60).as_micros() as u64);
    }
    #[allow(unused)]
    let netif_stack = Arc::new(EspNetifStack::new()?);
    #[allow(unused)]
    let sys_loop_stack = Arc::new(EspSysLoopStack::new()?);
    #[allow(unused)]
    let default_nvs = Arc::new(EspDefaultNvs::new()?);

    #[allow(clippy::redundant_clone)]
    #[cfg(not(feature = "qemu"))]
    #[allow(unused_mut)]
    let mut wifi = wifi(
        netif_stack.clone(),
        sys_loop_stack.clone(),
        default_nvs.clone(),
    )?;

The full code snippet is here.

So is this possible? to keep using the wifi stack after waking up from sleeping?

ivmarkov commented 2 years ago

You can read on what deep sleep means here: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/sleep_modes.html

Basically, once you are awoken from a deep sleep, your app starts over completely from the beginning, so it is not possible to re-use anything. There are alternatives to deep sleep, but they are not as efficient. Also, not all chips support deep sleep - the riscv ones (c3) don't.

By the way, these questions would likely be addressed more efficiently by posting them in the matrix channel, as they often end up not to be issues, but a need for more info.