1

With my ESP8266, I need to make a simple GET request to a server, and then go to deep sleep (the goal is 1 year battery powered)... until a signal comes on RST and then it starts again.

The following code works, but it takes 6 to 10 seconds on each RST to get connected to my home WiFi using "WPA2 Personal" (when it stops blinking in my code).

Is this 6 to 10 seconds delay normal, do you have the same order of magnitude? Or can we go down to 1 second, after a wake-up from deep sleep?

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
WiFiClient client;
HTTPClient http;
void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
    if (WiFi.SSID() != WIFI_SSID) {     // don't do begin if not necessary, see tutorial link after
        WiFi.begin("MySSID", "MyPassword");
        WiFi.persistent(true);
        WiFi.setAutoConnect(true);
        WiFi.setAutoReconnect(true);
    }
    while(WiFi.status() != WL_CONNECTED) {
      digitalWrite(LED_BUILTIN, LOW); delay(10); digitalWrite(LED_BUILTIN, HIGH); delay(200);  // blinking
    }
    http.begin(client, "http://example.com/request.php");
    http.GET();
    http.end();
    digitalWrite(LED_BUILTIN, LOW); delay(500); digitalWrite(LED_BUILTIN, HIGH); delay(500);    
    ESP.deepSleep(0);
}
void loop() {
}

Note: I have read and carefully respected this useful information about low power / battery-powered ESP8266:
Power Saving tips for the ESP8266 (especially this paragraph):

Do not call WiFi.begin() in setup().
The ESP8266 chip saves the last known wifi settings. Calling WiFi.begin() wipes those out. By calling WiFi.begin() only when it was needed, I shaved more than 2 seconds from the average amount of time it takes to associate with the AP. WiFi.begin() should be called only if the saved SSID does not match the configured SSID. You can check the configured SSID with the WiFi.SSID() call. Also, it is good practice to clear the saved WiFi settings if the timer expires. (See next section "Use watchdog timers")

2
  • and does it use now the automatic connection and is it faster?
    – Juraj
    Commented Dec 2, 2022 at 13:24
  • It is in general take around 6s for the ESP to scan all the 2.4GHz Wi-Fi channels to find the SSID and the associated BSSID (i.e. Mac Address), you can eliminate the scanning time by connecting using BSSID. See one of my project (under the "ESP8266 WiFi - How to speed up connection" section) where I cut the connection time from 6s down to about 2s.
    – hcheung
    Commented Dec 2, 2022 at 16:02

1 Answer 1

4

It is in general take around 6s for the ESP to scan all the 2.4GHz Wi-Fi channels to find the SSID and the associated BSSID (i.e. Mac Address), you can eliminate the scanning time by connecting using BSSID.

Using a static IP instead of rely on DHCP server to assign an IP would further reduce the WiFi connecting time by another 500ms.

See one of my project (under the "ESP8266 WiFi - How to speed up connection" section) where I cut the connection time from 6s down to about 2s.

Code signature for WiFi.begin with BSSID here:

    const byte WIFI_BSSID[] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC};
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD, 0, WIFI_BSSID);

For calculating IoT battery consultion, use IoT battery life calculator.

5
  • Thank you @hcheung for your answer. Do you know how much power it takes to do wake up + connection to Wifi (2 secondes) + send a single HTTP request (a few bytes to send) + go to deep sleep ? Do you think 200 mA * 3 seconds = 0.17 mAh is the right order of magnitude ? So it could be done ~6000 times on 3 AA batteries ? (I don't have equipment to do more precision measurement)
    – Basj
    Commented Dec 10, 2022 at 9:25
  • You need to do some measurement and calculate it yourself. As different boards may use different components, particularly the on-board LDO may not be designed for deep sleep application and has leakage current during deep sleep. If you can't design your own board, then look for those specially designed for battery-operation.
    – hcheung
    Commented Dec 10, 2022 at 9:38
  • Thanks @hcheung. What is "LDO"? Do you know standard consumer level ESP32 (easy to find on popular shops) that have good battery-operation?
    – Basj
    Commented Dec 10, 2022 at 9:53
  • 1
    LDO stands for Low Dropout Output, refer to the regulator that convert the input voltage to 3.3v on your board. Typical Chinese made ESP32 boards or ESP8266 boards do not have good LDO, they designed for powering via USB.
    – hcheung
    Commented Dec 10, 2022 at 10:00
  • Thanks @hcheung. PS: I edited to add the code + link to doc about function signature (not easy to find!)
    – Basj
    Commented Dec 10, 2022 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.