Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By bastian.h.jaeger
#89115 Hi,

I am implementing some ESP8266 device with deep sleep + wifi. I want the device to sleep long, wake up, measure, send out data and sleep again. The standard IoT use case, I thing. :)

But the WiFi connection takes very long. Sometimes even more than 10 seconds.

Hardware:
I tested
- an Sparkfun ESP8266 Thing Dev and
- an ESP12f
with two different WiFis (one FritzBox and one Smartphone Hotspot).

Both ESP devices are more or less equal in the connection time. The smartphone WiFi replies the IP faster.

Can anyone tell my why I read a lot about ESP8266 devices connect within 2-3 seconds but my devices are so much slower???

Code here:
Code: Select all#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <AsyncMqttClient.h>

#define logf(msg, ...) \
  Serial.printf("%d ", millis()); \
  Serial.printf("[%s] ", __FUNCTION__); \
  Serial.printf(msg, __VA_ARGS__); \
  Serial.printf("\n");
#define log(msg) \
  Serial.printf("%d ", millis()); \
  Serial.printf("[%s] ", __FUNCTION__); \
  Serial.printf(msg); \
  Serial.printf("\n");

WiFiEventHandler wifiDiconnectEventHandler;
time_t lastSend = 0;

void OnWiFiEvent(WiFiEvent_t event) {
  switch(event) {
  case WiFiEvent_t::WIFI_EVENT_STAMODE_CONNECTED:
    log("WIFI connected.");
    break;
  case WiFiEvent_t::WIFI_EVENT_STAMODE_DISCONNECTED:
    break;
  case WiFiEvent_t::WIFI_EVENT_STAMODE_GOT_IP:
    logf("WIFI got ip '%s'.", WiFi.localIP().toString().c_str());
    break;
  default:
    printf("WIFI unhandled event %d", event);
  }
}

void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
  logf("WIFI disconnected. Reason %d", event.reason);
}

void setup() {
  Serial.begin(115200);

  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);

  WiFi.onEvent(OnWiFiEvent, WiFiEvent_t::WIFI_EVENT_ANY);
  wifiDiconnectEventHandler = WiFi.onStationModeDisconnected(
          [](const WiFiEventStationModeDisconnected& event) { onStationModeDisconnected(event); });

  log("WIFI connecting.");
  WiFi.begin("myWifi", "SoSafePassword");
}

void loop () {
  if ( (millis()-lastSend) > 5000) {
    lastSend = millis();
    logf("Heap %d, Wifi %d", ESP.getFreeHeap(), WiFi.isConnected());
  }

  delay(1);
}


Sample printout here:
Code: Select all
␀r␘␂␀l��r�␀�#␂�n�␄��␌␘�␌␜��␜p�<���␀�8␂��ǒ��l␜p␌␘␌�nn�␂�;�nĒ��␌␛�␌b�#l`␛$`␛␄nn��܀␌␜���l␜����␌␜�␌b�␄n��n�䎒␌␟�␌b��>~�n�␃����␀l`␛��␒�#�n�␄␎r␘␂␎␂nr���;␂��␌?␌␂�;�␂␎r�ےn�␄␎�b��l`␛Ğ�␌␟��␜␂��␃r�;�␂��n�b�
72 [setup] WIFI connecting.
3811 [onStationModeDisconnected] WIFI disconnected. Reason 203
5001 [loop] Heap 49640, Wifi 0
7683 [OnWiFiEvent] WIFI connected.
9350 [OnWiFiEvent] WIFI got ip '10.42.178.28'.
10002 [loop] Heap 48896, Wifi 1
␀r␘␂␀l��r�␀�#␂�n�␄��␌␘�␌␜��␜p�<���␀�8␂��ǒ��l␜p␌␘␌�nn�␂�;�nĒ��␌␛�␌b�#l`␛$`␛␄nn��܀␌␜���l␜����␌␜�␌b�␄n��n��␎�␌␟�␌b�ľ~�n�␃����␀l`␛��␒�#�n�␄�r␘␂␎␂nr���;␂��␌?␌␂�;�␂␎r�ےn�␄��b��l`␛Ğ�␌␟��␜␂��␃r�␂��n�b�
71 [setup] WIFI connecting.
3809 [onStationModeDisconnected] WIFI disconnected. Reason 203
5001 [loop] Heap 49640, Wifi 0
7681 [OnWiFiEvent] WIFI connected.
9354 [OnWiFiEvent] WIFI got ip '10.42.178.28'.
10002 [loop] Heap 48896, Wifi 1
␀r␘␂␀l��r�␀�#␂�n�␄��␌␘�␌␜��␜p�<���␀�8␂��ǒ��l␜p␌␘␌�nn�␂�;�nĒ��␌␛�␌b�#l`␛$`␛␄nn���␀␌␜���l␜����␌␜�␌b�␄n��n�䎒␌␟�␌b�ľ~�n�␃����␀l`␛��␒�#�n�␄�r␘␂␎␂nr���;␂��␌?␌␂�␂␎r�ےn�␄��b��l`␛Ğ�␌␟��␜␂��␃r�;�␂��n�b�
68 [setup] WIFI connecting.
3809 [onStationModeDisconnected] WIFI disconnected. Reason 203
5002 [loop] Heap 49640, Wifi 0
7678 [OnWiFiEvent] WIFI connected.
9326 [OnWiFiEvent] WIFI got ip '10.42.178.28'.
10003 [loop] Heap 48896, Wifi 1
␀r␘␂␀l��r�␀�#␂�n�␄��␌␘�␌␜��␜p�<���␀�8␂��ǒ��l␜p␌␘␌�nn�␂�;�nĒ��␌␛�␌b�#l`␛$`␛␄nn��܀␌␜���l␜����␌␜�␌b�␄n��n�䎒␌␟�␌b�ľ~�n�␃����␀l`␛��␒�#�n�␄�r␘␂␎␂nr���;␂��␌?␌␂�;�␂␎r�ےn�␄��b��l`␛Ğ�␌␟��␜␂��␃r�;�␂��n�b�
68 [setup] WIFI connecting.
3807 [onStationModeDisconnected] WIFI disconnected. Reason 203
5002 [loop] Heap 49640, Wifi 0
7676 [OnWiFiEvent] WIFI connected.
␀r␘␂␀l��r�␀�#␂�n�␄��␌␘�␌␜��␜p�<���␀�8␂��ǒ��l␜p␌␘␌�nn�␂�;�nĒ��␌␛�␌b�#l`␛$`␛␄nn��܀␌␜���l␜����␌␜�␌b�␄n��n�䎒␌␟�␌b��>~�n�␃����␀l`␛��␒�#�n�␄␎r␘␂␎␂nr���;␂��␌?␌␂�␂␎r�ےn�␄␎�b��l`␛Ğ�␌␟��␜␂��␃r�␂��n�b�
70 [setup] WIFI connecting.
3809 [onStationModeDisconnected] WIFI disconnected. Reason 203
5001 [loop] Heap 49640, Wifi 0
7677 [OnWiFiEvent] WIFI connected.
9352 [OnWiFiEvent] WIFI got ip '10.42.178.28'.
10002 [loop] Heap 48896, Wifi 1
␀r␘␂␀l��r�␀�#␂�n��␌␘�␌␜��␜p�<���␀�8␂��ǒ��l␜p␌␘␌�nn�␂�;�nĒ��␌␛�␌b�#l`␛$`␛␄nn��܀␌␜���l␜����␌␜�␌b�␄n��n�䎒␌␟�␌b�ľ~�n�␃����␀l`␛��␒�#�n�␄␎r␘␂␎␂nr���;␂��␌?␌␂�;�␂␎r�ےn�␄␎�b��l`␛Ğ�␌␟��␜␂��␃r�␂��n�b�
68 [setup] WIFI connecting.
3809 [onStationModeDisconnected] WIFI disconnected. Reason 203
5001 [loop] Heap 49640, Wifi 0
7677 [OnWiFiEvent] WIFI connected.
9350 [OnWiFiEvent] WIFI got ip '10.42.178.28'.
10002 [loop] Heap 48896, Wifi 1


Other network:
Code: Select all�n�␄�r␘␂␎␂nr���;␂��␌?␌␂�;�␂␎r�ےn�␄��b��l`␛Ğ�␌␟��␜␂��␃r�;�␂��n�b�
67 [setup] WIFI connecting.
3825 [onStationModeDisconnected] WIFI disconnected. Reason 2
4979 [OnWiFiEvent] WIFI connected.
5001 [loop] Heap 48968, Wifi 0
6857 [OnWiFiEvent] WIFI got ip '192.168.43.91'.
␀r␘␂␀l��r�␀�#␂�n��␌␘�␌␜��␜p�<���␀�8␂��ǒ��l␜p␌␘␌�nn�␂�;�nĒ��␌␛�␌b�#l`␛$`␛␄nn��܀␌␜���l␜����␌␜�␌b�␄n��n�䎒␌␟�␌b�ľ~�n�␃����␀l`␛��␒�#�n�␄�r␘␂␎␂nr���;␂��␌?␌␂�;�␂␎r�ےn�␄␎�b��l`␛Ğ�␌␟��␜␂��␃r�;�␂��n�b�
70 [setup] WIFI connecting.
4815 [onStationModeDisconnected] WIFI disconnected. Reason 202
5001 [loop] Heap 49640, Wifi 0
7689 [OnWiFiEvent] WIFI connected.
9359 [OnWiFiEvent] WIFI got ip '192.168.43.91'.
␀r␘␂␀l��r�␀�#␂�n�␄��␌␘�␌␜��␜p�<���␀�8␂��ǒ��l␜p␌␘␌�nn�␂�;�nĒ��␌␛�␌b�#l`␛$`␛␄nn��܀␌␜���l␜����␌␜�␌b�␄n��n�䎒␌␟�␌b��>~�n�␃����␀l`␛��␒�#�n�␄�r␘␂␎␂nr���;␂��␌␡␌␂�␂␎r�ےn�␄��b��l`␛Ğ�␌␟��␜␂��␃r�;�␂��n�b�
70 [setup] WIFI connecting.
3832 [onStationModeDisconnected] WIFI disconnected. Reason 2
4982 [OnWiFiEvent] WIFI connected.
5001 [loop] Heap 48968, Wifi 0
6838 [OnWiFiEvent] WIFI got ip '192.168.43.91'.
␀r␘␂␀l��r�␀�#␂�n��␌␘�␌␜��␜p�<���␀�8␂��ǒ��l␜p␌␘␌�nn�␂�;�nĒ��␌␛�␌b�#l`␛$`␛␄nn���␀␌␜���l␜����␌␜�␌b�␄n��n��␎�␌␟�␌b�ľ~�n�␃����␀l`␛��␒�#�n�␄�r␘␂␎␂nr���;␂��␌?␌␂�;�␂␎r�ےn�␄��b��l`␛Ğ�␌␟��␜␂��␃r�;�␂��n�b�
68 [setup] WIFI connecting.
3822 [onStationModeDisconnected] WIFI disconnected. Reason 2
4967 [OnWiFiEvent] WIFI connected.
5001 [loop] Heap 48968, Wifi 0
6831 [OnWiFiEvent] WIFI got ip '192.168.43.91'.
␀r␘␂␀l��r�␀�#␂�n�␄��␌␘�␌␜��␜p�<���␀�8␂��ǒ��l␜p␌␘␌�nn�␂�;�nĒ��␌␛�␌b�#l`␛$`␛␄nn��܀␌␜���l␜����␌␜�␌b�␄n��n��␎�␌␟�␌b��>~�n�␃����␀l`␛��␒�#�n�␄�r␘␂␎␂nr���;␂��␌?␌␂�␂␎r�ےn�␄␎�b��l`␛Ğ�␌␟��␜␂��␃r�;�␂��n�b�
68 [setup] WIFI connecting.
4853 [OnWiFiEvent] WIFI connected.
5001 [loop] Heap 48968, Wifi 0
6834 [OnWiFiEvent] WIFI got ip '192.168.43.91'.
User avatar
By StanJ
#89195 The 3 second connection time is if you're using a static IP address.
With DHCP it takes at least 6 seconds, generally 8 to 13 for me.

If you have less than 20 units, ESP-NOW is a heck of a lot quicker as it doesn't use the standard WiFi stack. TornTrousers (Anthony Elder) did a good break down on it here:
https://github.com/HarringayMakerSpace/ESP-Now
You can boot, send a short packet, and go back to sleep in under a second. However, ESP-NOW can only handle 20 unencrypted peers, or 10 encrypted peers. If you have more remote sensors than that, you'll have to live with the 3 second connection time with static IP.

edit: I forgot to mention, the default RF output power of the ESP may be overdriving your router; I drop my by 4 to 6dB and it generally connects a bit faster. Your mileage may vary.