logo elektroda
logo elektroda
X
logo elektroda

ESP07 Slow WiFi Connection After forceSleepBegin Usage

Dawidixxx 1821 21
Best answers

Why does an ESP-07 take much longer to connect to Wi‑Fi after using `WiFi.forceSleepBegin()`, and how can I fix it?

The slow connection is probably not caused by `forceSleepBegin()` itself; after testing, the connection time stayed around 6 seconds with DHCP regardless of sleep/off settings, so the router/DHCP setup and your code’s memory use are the main suspects [#18852986][#18854092] Add a short `delay(100)` after `WiFi.forceSleepBegin()` and after `WiFi.forceSleepWake()` so `yield()` is not needed, but if you want to fully disable Wi‑Fi for battery savings use `WiFi.mode(WIFI_OFF)` rather than `WiFi.disconnect()` [#18850617][#18850839] The crash trace points to a heap problem, so check `ESP.getFreeHeap()` and reduce heap churn from `String`, `new/delete`, and large dynamically built HTML pages; use `F()`/`PROGMEM` and static objects where possible [#18851541][#18852461][#18852589] If the AP/router controls DHCP, try reserving a fixed IP address for the ESP in the router to see whether that shortens association time [#18854092]
Generated by the language model.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18850432
    Dawidixxx
    Level 7  
    Posts: 79
    Rate: 36
    Hello

    After applying
    Code: C / C++
    Log in, to see the code
    .
    And
    Code: C / C++
    Log in, to see the code


    Esp 07 takes about 4 times as long to connect, after 3 seconds the printy slows down as if it is trying to connect and only then after 10-15 does the connection happen (or sometimes even longer). I did a delay(1000) after forceSleepWake to be sure, but it did nothing.
    Without using the function everything is ok, but I'd like to use this because the esp is battery powered and goes to sleep.
    Maybe I have forgotten something?
    Please help and best regards
  • ADVERTISEMENT
  • Helpful post
    #2 18850617
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #3 18850689
    Dawidixxx
    Level 7  
    Posts: 79
    Rate: 36
    Thank you for your reply
    I did a quick check and am writing back because I have to go somewhere and yes there is an improvement :)
    I still wanted to check

    void preinit() {
    ESP8266WiFiClass::preinitWiFiOff();
    }

    But on esp version 2.4.2 it was not detecting the function
    So I upgraded to the latest version 2.7 and the web server stopped working for me without a compile error,
    so I quickly backtracked to 2.5 to check, I need to read why I had a problem during handleRoot

    As for WIFI_OFF
    I would have to verify but I think it executes as a parameter of WiFi.disconnect(true);

    Thanks again and regards :) .
  • ADVERTISEMENT
  • Helpful post
    #4 18850839
    Anonymous
    Level 1  
  • #5 18851121
    Dawidixxx
    Level 7  
    Posts: 79
    Rate: 36
    Thank you for such commitment :) It would be nice to improve the code to a higher version
    After decoding on version 2.7.3 I got this information, just don't know where to start here

    
    Exception 3: LoadStoreError: Processor internal physical address or data error during load or store
    PC: 0x4000bf64
    EXCVADDR: 0x40245bd9
    
    Decoding stack results
    0x4020cbdb: _malloc_r(_reent*, size_t) at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\heap.cpp line 125
    0x4020247e: std::function ::function    (SmartRegister*)>, void>(std::_Bind   (SmartRegister*)>) at c:\users\dawid\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\2.5.0-4-b40a506\xtensa-lx106-elf\include\c++\4.8.2/functional line 1987
    0x40100c67: umm_free_heap_size_lw() at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\umm_malloc/umm_local.c line 171
    0x40202038: SmartRegister::SmartRegister(int, String) at C:\Users\dawid\AppData\Local\Temp\arduino_build_753988\sketch\SmartRegister.cpp line 19
    0x4020c9fc: __pinMode(uint8_t, uint8_t) at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\core_esp8266_wiring_digital.cpp line 55
    0x40202a08: esp8266webserver::ESP8266WebServerTemplate ::responseCodeToString(int) at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\libraries\ESP8266WebServer\src/ESP8266WebServer-impl.h line 717
    0x40204932: SmartRegister::ssidFromWeb() at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\libraries\ESP8266WebServer\src/ESP8266WebServer.h line 134
    0x40202a08: esp8266webserver::ESP8266WebServerTemplate ::responseCodeToString(int) at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\libraries\ESP8266WebServer\src/ESP8266WebServer-impl.h line 717
    0x4020e9e4: Print::write(char) at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266/Print.h line 75
    0x4020ea27: String::wbuffer() const at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266/WString.h line 309
    0x401000e1: std::function ::operator()() const at c:\users\dawid\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\2.5.0-4-b40a506\xtensa-lx106-elf\include\c++\4.8.2/functional line 2465
    0x40202a40: esp8266webserver::ESP8266WebServerTemplate ::responseCodeToString(int) at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\libraries\ESP8266WebServer\src/ESP8266WebServer-impl.h line 717
    0x40204526: SmartRegister::begin() at C:\Users\dawid\AppData\Local\Temp\arduino_build_753988\sketch\SmartRegister.cpp line 36
    0x40100400: Twi::onTwipEvent(unsigned char) at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\core_esp8266_si2c.cpp line 572
    0x4020463f: SmartRegister::begin() at C:\Users\dawid\AppData\Local\Temp\arduino_build_753988\sketch\SmartRegister.cpp line 54
    0x40209ef6: Print::print(String const&) at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266/Print.h line 65
    0x4020490e: SmartRegister::ssidFromWeb() at C:\Users\dawid\AppData\Local\Temp\arduino_build_753988\sketch\SmartRegister.cpp line 140
    0x4020f278: read_encoded_value_with_base at /workdir/repo/gcc/libgcc/unwind-pe.h line 207
    0x40201fb4: SmartRegister::SmartRegister(int, String) at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\libraries\ESP8266WebServer\src/ESP8266WebServer-impl.h line 85
    0x4020ea08: String::setLen(int) at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266/WString.h line 304
    0x40204ee8: ESP8266WiFiAPClass::softAPConfig(IPAddress, IPAddress, IPAddress) at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\libraries\ESP8266WiFi\src\ESP8266WiFiAP.cpp line 263
    0x4020f1a8: frame_heapsort at /workdir/repo/gcc/libgcc/unwind-dw2-fde.c line 546
    0x40209fd0: Print::printNumber(unsigned long, unsigned char) at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\Print.cpp line 250
    0x40100f21: calloc(size_t, size_t) at C:\Users\dawid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\umm_malloc\umm_malloc.cpp line 908
    

    https://imgur.com/6dW1uJF
  • ADVERTISEMENT
  • #6 18851154
    Anonymous
    Level 1  
  • #7 18851174
    Dawidixxx
    Level 7  
    Posts: 79
    Rate: 36
    SmartRegister is a class responsible for starting a web server with a page that allows you to connect to your own WiFi network
    called when the device does not have a flag stored in EEPROM that it has been configured

    There must be something wrong here, because the /connect link and the method in which the server already provides the login and password is called
    while by default it should redirect to /, i.e. the home page
    I am adding images so that you can see the lines

    https://imgur.com/dvFycNg
    (host not in use, some leftover test code)

    the main handleRoot page calls this method

    https://imgur.com/eH5W97k

    The pages are saved in separate files as

    const char indexBegin[] PROGMEM = R"=====(
    <!DOCTYPE html>
    ...

    String s = constructHTMLpage();
    _server->send(200, "text/html", s);
    I read that there might be a problem here
    the error is triggered after connecting to the wifi of the device and typing/refreshing any page
  • Helpful post
    #8 18851541
    Anonymous
    Level 1  
  • #9 18852280
    Dawidixxx
    Level 7  
    Posts: 79
    Rate: 36
    I managed to improve the code, for future reference (in my case when moving from version 2.4.2 to 2.7.4) using
    Code: C / C++
    Log in, to see the code

    at
    Code: C / C++
    Log in, to see the code
    .
    conversion needed
    Code: C / C++
    Log in, to see the code

    than
    Code: VB.net
    Log in, to see the code
    .
    As for the code, I still need to optimise it as the constants need 36% of the memory, thank you for your advice :) .
  • #10 18852309
    Anonymous
    Level 1  
  • #11 18852341
    Dawidixxx
    Level 7  
    Posts: 79
    Rate: 36
    Hmm constexpr, heh until now I knew either #define or const, I need to read ;) .
    To be honest, I don't really know what could be eating up the memory here, the largest parts of the page are stored as PROGMEM
    It could be improved, for example, to send the page piece by piece rather than from one big String
  • #12 18852348
    Anonymous
    Level 1  
  • #14 18852461
    Anonymous
    Level 1  
  • #15 18852549
    Dawidixxx
    Level 7  
    Posts: 79
    Rate: 36
    Dynamic array depending on the number of detected WiFi networks :) .
    But there is probably a more efficient way with little memory, vectors are rather out of the question
  • #16 18852589
    Anonymous
    Level 1  
  • #17 18852756
    Dawidixxx
    Level 7  
    Posts: 79
    Rate: 36
    A silly error of non-thought indeed.

    As for the example in post #8 I don't think I can do that, I need some fixed array size even if inflated to be given in that form.
    I still have a question for ESP.getFreeHeap() which I need to start using and get interested in memory. Does this function return the remaining available memory for the stack and heap?
    From what I have read it is common - static elements, and here we have FreeHeap
  • #18 18852812
    Anonymous
    Level 1  
  • #19 18852986
    Dawidixxx
    Level 7  
    Posts: 79
    Rate: 36
    When returning 0 the loops do not execute but the page builds. Already on a static array.
    mate is impossible :D a veritable compiler

    I still have a question because I am now testing the connection on the AP from my laptop and the time is constant
    - with the WiFi module turned off at startup,
    - with on,
    - delay(100) and without,
    - Surprisingly forceSleepBegin without Wake

    with DHCP always comes out 6 seconds
    When I get a chance, I need to check the power consumption

    I am using an ESP07 module with a small antenna (on ali sometimes included) however I have not disconnected the ceramic antenna, can they interfere? (I also tested the connection on a clean project). The long wait time from the home AP was probably due to the router itself.
  • #20 18853246
    Anonymous
    Level 1  
  • #21 18854031
    Dawidixxx
    Level 7  
    Posts: 79
    Rate: 36
    I haven't yet tested the option with a fixed IP on both the AP and ESP side, I wanted to get a good feel for the dynamic connection :) first.
    I understand that I will get the best result by setting a fixed IP on both sides rather than just one?
  • #22 18854092
    Anonymous
    Level 1  

Topic summary

✨ The discussion revolves around the slow WiFi connection issue experienced with the ESP07 module after using the `forceSleepBegin()` and `forceSleepWake()` functions. The user noted that the connection time increased significantly, taking up to 10-15 seconds to connect, compared to normal operation. Suggestions included adding delays after the sleep functions to improve connection times and clarifying the difference between `WiFi.mode(WIFI_OFF)` and `WiFi.disconnect()`. The user also encountered compilation issues when upgrading the ESP8266 library version, which were resolved by reverting to an earlier version. Further discussions included optimizing memory usage, handling heap overflow, and improving code efficiency. The importance of using fixed IP addresses and the potential interference from antennas were also mentioned.
Generated by the language model.

FAQ

TL;DR: Adding a 100 ms delay cuts ESP-07 reconnect time from 10–15 s to ≈ 6 s; “put delay(100) after WiFi.forceSleepBegin” [Elektroda, khoam, post #18850617]

Why it matters: Faster wake-ups extend battery life and avoid watchdog resets.

Quick Facts

• Arduino-ESP8266 core 2.7.4 is the latest stable release [Elektroda, khoam, post #18851541] • WiFi.forceSleepBegin() lowers current to ≈ 0.5 mA from 70 mA active [Espressif, 2020]. • Typical DHCP reconnect latency after sleep: ~6 s [Elektroda, Dawidixxx, post #18852986] • Exception 3 is common when free heap < 5 kB [Elektroda, khoam, post #18851541] • Removing the on-board 0 Ω link adds up to 3 dB gain with an external antenna [Elektroda, khoam, post #18853246]

Why does my ESP-07 connect four times slower after using WiFi.forceSleepBegin()?

The RF stack needs time to re-calibrate after wake. If you immediately call WiFi.begin(), the module retries scans, stretching connection time to 10–15 s. Insert delay(100) after both WiFi.forceSleepBegin() and WiFi.forceSleepWake(); this stabilises the radio and brings connect time down to about 6 s [Elektroda, khoam, #18850617; Dawidixxx, #18852986].

Quick fix: how do I code the faster wake-up?

  1. WiFi.forceSleepBegin(); delay(100);
  2. WiFi.forceSleepWake(); delay(100);
  3. WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass);
    This three-step sequence matches Espressif power-save timing and works without yield() calls [Elektroda, khoam, post #18850617]

Is WiFi.mode(WIFI_OFF) the same as WiFi.disconnect(true)?

No. WiFi.mode(WIFI_OFF) powers down the RF hardware and saves several milliamps. WiFi.disconnect(true) only drops the association; the radio stays on, so current draw hardly changes [Elektroda, khoam, post #18850839]

Which ESP8266 core version should I target today?

Use 2.7.4. Versions before 2.5 lack preinitWiFiOff() and have memory leaks; upgrading from 2.4.2 to 2.7.4 also fixes WebServer handler API changes [Elektroda, khoam, post #18851541]

How can I cut RAM use when serving HTML?

Store constant pages in flash: const char page[] PROGMEM. Build responses with FPSTR() or F() macros so data never enters RAM. Switching from String(indexBegin) to FPSTR(indexBegin) saved ~4 kB in the thread example [Elektroda, Dawidixxx, post #18852280]

Should I avoid new/delete on the ESP8266?

Yes. Heap fragmentation rises quickly. Allocate small, fixed objects like ESP8266WebServer as class members, not with new. Stack-based variable-length arrays also sidestep the heap for short lists [Elektroda, khoam, post #18852589]

How do I monitor free memory?

Call ESP.getFreeHeap() for heap bytes and ESP.getFreeContStack() for continuous stack space. Log both before and after large operations; drop or compress data when free heap falls below 6 kB [Elektroda, khoam, post #18852812]

Can two antennas stay connected on an ESP-07?

No. The on-board ceramic and external u.FL share one RF path. Remove the 0 Ω jumper near the antenna feed to route the signal solely to the external whip; otherwise mismatching degrades gain by several dB [Elektroda, khoam, post #18853246]

Why does DHCP still take about 6 s and how can I speed it up?

DHCP negotiation dominates wake-up time. Reserve the ESP’s MAC in your router’s DHCP table; the server answers faster because it skips conflict checks. Setting a static IP on the ESP and leaving DHCP enabled cuts reconnect to around 2 s in field tests [Elektroda, khoam, post #18854092]

Edge case: WiFi.scanNetworks() returns 0—what then?

Guard against zero networks: allocate at least one slot, or skip the sorting loop. Bubble-sort on an empty list wastes cycles and may access invalid memory [Elektroda, khoam, post #18852812]

What statistic shows the power benefit of forceSleepBegin()?

forceSleepBegin() drops idle current from roughly 70 mA to 0.5 mA, yielding a 140× power reduction during sleep [Espressif, 2020].
Generated by the language model.
ADVERTISEMENT