BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

ESP32 WiFi Connection Failure on Alternate Re-starts

I’m back to this after useful advice from Peter. I’ve tried various different ESP32 modules including a M5Stack and my usual ESP32 Dev module. Also tried a different PC with a fresh Arduino install. Always the same issue: Every other re-boot fails to connect. My ESP8266 project works every time. I decided to analyse the pre-amble of both connections, but the 8266 WiFi setup seems very different to the '32. Even the debug settings are quite different, so comparison is next to impossible. I’m beginning to wonder if there’s a bug in the ESP32 WiFi library module - though it’s such an obvious problem, others would have seen it, surely?

I’m sure I can find a way around this, but I do like to fully understand what’s going on.
The sketch is an empty one, consisting of just basic Setup and Loop.

Here are the various debug outputs:
ESP32 Failed:
12:47:44.320 -> ets Jun 8 2016 00:22:57
12:47:44.367 ->
12:47:44.367 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
12:47:44.367 -> configsip: 0, SPIWP:0xee
12:47:44.367 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
12:47:44.367 -> mode:DIO, clock div:1
12:47:44.367 -> load:0x3fff0018,len:4
12:47:44.367 -> load:0x3fff001c,len:1216
12:47:44.367 -> ho 0 tail 12 room 4
12:47:44.367 -> load:0x40078000,len:9720
12:47:44.367 -> ho 0 tail 12 room 4
12:47:44.367 -> load:0x40080400,len:6352
12:47:44.367 -> entry 0x400806b8
12:47:44.601 -> [32] Connecting to DAVETT
12:47:44.695 -> [D][WiFiGeneric.cpp:337] _eventCallback(): Event: 0 - WIFI_READY
12:47:44.695 -> [D][WiFiGeneric.cpp:337] _eventCallback(): Event: 2 - STA_START
12:47:44.789 -> [D][WiFiGeneric.cpp:337] _eventCallback(): Event: 5 - STA_DISCONNECTED
12:47:44.789 -> [W][WiFiGeneric.cpp:353] _eventCallback(): Reason: 202 - AUTH_FAIL

ESP32 Success:
12:47:52.709 -> ets Jun 8 2016 00:22:57
12:47:52.709 ->
12:47:52.709 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
12:47:52.709 -> configsip: 0, SPIWP:0xee
12:47:52.709 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
12:47:52.709 -> mode:DIO, clock div:1
12:47:52.709 -> load:0x3fff0018,len:4
12:47:52.709 -> load:0x3fff001c,len:1216
12:47:52.709 -> ho 0 tail 12 room 4
12:47:52.709 -> load:0x40078000,len:9720
12:47:52.709 -> ho 0 tail 12 room 4
12:47:52.709 -> load:0x40080400,len:6352
12:47:52.709 -> entry 0x400806b8
12:47:52.944 -> [32] Connecting to DAVETT
12:47:53.037 -> [D][WiFiGeneric.cpp:337] eventCallback(): Event: 0 - WIFI_READY
12:47:53.037 -> [D][WiFiGeneric.cpp:337] eventCallback(): Event: 2 - STA_START
12:47:53.225 -> [D][WiFiGeneric.cpp:337] eventCallback(): Event: 4 - STA_CONNECTED
12:47:53.271 -> [D][WiFiGeneric.cpp:337] eventCallback(): Event: 7 - STA_GOT_IP
12:47:53.271 -> [D][WiFiGeneric.cpp:381] eventCallback(): STA IP: 192.168.1.39, MASK: 255.255.255.0, GW: 192.168.1.1
12:47:53.553 -> [609] Connected to WiFi
12:47:53.553 -> [609] IP: 192.168.1.39
12:47:53.553 -> [609]
12:47:53.553 -> ___ __ __
12:47:53.553 -> / _ )/ /
_____ / /

12:47:53.553 -> / _ / / // / _ / '
/
12:47:53.553 -> /
//_, /////_
12:47:53.553 -> /
__/ v0.6.1 on ESP32
12:47:53.553 ->
12:47:53.553 -> [614] Connecting to blynk-cloud.com:80
12:47:53.600 -> [672] <[1D|00|01|00] UlmMOwmt5fGF_wxSS
12:47:53.647 -> [709] >[00|00|01|00|C8]
12:47:53.647 -> [710] Ready (ping: 37ms).
12:47:53.647 -> [710] >[14|00|01|00|08]
12:47:53.647 -> [710] >pm[00]0[00]out
12:47:53.693 -> [777] <[11|00|02|00]Fver[00]0.6.1[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]ESP32[00]build[00]Feb 16 2021 15:56:49[00]
12:47:53.740 -> [811] >[00|00|02|00|C8]
12:48:03.718 -> [10780] <[06|00|03|00|00]
12:48:03.765 -> [10828] >[00|00|03|00|C8]

ESP8266 Always works:
12:48:55.994 -> scandone
12:48:55.994 -> state: 0 -> 2 (b0)
12:48:55.994 -> state: 2 -> 3 (0)
12:48:55.994 -> state: 3 -> 5 (10)
12:48:55.994 -> add 0
12:48:55.994 -> aid 4
12:48:55.994 -> cnt
12:48:56.272 ->
12:48:56.272 -> connected with DAVETT, channel 1
12:48:56.272 -> dhcp client start…
12:48:56.272 -> wifi evt: 0
12:48:58.217 -> ip:192.168.1.35,mask:255.255.255.0,gw:192.168.1.1
12:48:58.217 -> wifi evt: 3
12:48:58.639 -> [5570] Connected to WiFi
12:48:58.639 -> [5570] IP: 192.168.1.35
12:48:58.639 -> [5570]
12:48:58.639 -> ___ __ __
12:48:58.639 -> / _ )/ /_ _____ / /__
12:48:58.639 -> / _ / / // / _ / '/
12:48:58.639 -> /
//_, /////_
12:48:58.639 -> /
__/ v0.6.1 on NodeMCU
12:48:58.639 ->
12:48:58.639 -> [5580] Connecting to blynk-cloud.com:80
12:48:58.639 -> [hostByName] request IP for: blynk-cloud.com
12:48:58.727 -> [hostByName] Host: blynk-cloud.com IP: 139.59.206.133
12:48:58.727 -> [5670] <[1D|00|01|00] UlmMOwmt5fGF_wx
12:48:58.983 -> [5915] >[00|00|01|00|C8]
12:48:58.983 -> [5915] Ready (ping: 244ms).
12:48:58.983 -> [5915] Free RAM: 49408
12:48:58.983 -> [5915] >[14|00|01|00|08]
12:48:58.983 -> [5915] >pm[00]0[00]out
12:48:59.063 -> [5982] <[11|00|02|00]Hver[00]0.6.1[00]h-beat[00]10[00]buff-in[00]1024[00]dev[00]NodeMCU[00]build[00]Feb 16 2021 17:57:18[00]
12:48:59.063 -> [6012] >[00|00|02|00|C8]
12:49:05.959 -> pm open,type:2 0
12:49:09.000 -> [15984] <[06|00|03|00|00]
12:49:09.047 -> [16013] >[00|00|03|00|C8]

Do you want to share the exact sketch (minus credentials) and specific details of your board?

Pete.

Also, have you read this…

Pete.

Well, that link was brilliant. At least it made me realise I was not alone - and that my suspicions about failure to properly disconnect a working link seemed to be confirmed. Also, that others found the ESP8266 variants to work perfectly. It also seems to confirm the issue is not wth Blynk.begin, as the examples in the link were using Wifi.begin. (I presume that Blynk.begin also uses it?)
I can’t believe this issue has been around since 2017!
I’m using a clone of the typical Esspressif ESP32 Dev module, though with a Molex external antenna. It’s an ESP32-WROOM-32U core. Also tried a M5Stack - just the same…

Here’s the sketch. Told you it was basic…
I’ll now try some of the workarounds from your link. Not happy with that though.

#define BLYNK_PRINT Serial
#define BLYNK_DEBUG

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "";
char ssid[] = "";
char pass[] = "";

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();
}

@dave please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Sorry about that.

Okay Pete, the down and dirty fix is here. The negotiation looks messy - see the debug printout compared with a ‘clean’ dialogue in the previous good/bad alternate re-boots.

#define BLYNK_PRINT Serial
#define BLYNK_DEBUG 

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "";

char ssid[] = "DAVETT";
char pass[] = "";

void setup()
{
  Serial.begin(115200);
  delay(10);
  Serial.print("\nConnecting to "); Serial.println(ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(WiFi.status());
    delay(1000);
    WiFi.begin(ssid, pass);
  }
  Serial.print("\nWiFi connected   IP: ");
  //Serial.println(WiFi.localIP());
  Blynk.config(auth);
}

void loop()
{
  Blynk.run();
}

This produces this dialogue:
17:45:47.310 -> Connecting to DAVETT
17:45:47.357 -> [D][WiFiGeneric.cpp:337] eventCallback(): Event: 0 - WIFI_READY
17:45:47.357 -> [D][WiFiGeneric.cpp:337] eventCallback(): Event: 2 - STA_START
17:45:47.497 -> [D][WiFiGeneric.cpp:337] eventCallback(): Event: 5 - STA_DISCONNECTED
17:45:47.497 -> [W][WiFiGeneric.cpp:353] eventCallback(): Reason: 202 - AUTH_FAIL
17:45:47.872 -> 4[D][WiFiGeneric.cpp:337] eventCallback(): Event: 4 - STA_CONNECTED
17:45:49.091 -> [D][WiFiGeneric.cpp:337] eventCallback(): Event: 7 - STA_GOT_IP
17:45:49.091 -> [D][WiFiGeneric.cpp:381] eventCallback(): STA IP: 192.168.1.39, MASK: 255.255.255.0, GW: 192.168.1.1
17:45:49.372 -> 3
17:45:50.389 -> WiFi connected IP: [3123]
17:45:50.389 -> ___ __ __
17:45:50.389 -> / _ )/ /
_____ / /

17:45:50.389 -> / _ / / // / _ / '
/
17:45:50.389 -> /
//_, /////_
17:45:50.389 -> /
__/ v0.6.1 on ESP32
17:45:50.389 -> [D][WiFiGeneric.cpp:337] _eventCallback(): Event: 5 - STA_DISCONNECTED
17:45:50.389 -> [W][WiFiGeneric.cpp:353] _eventCallback(): Reason: 8 - ASSOC_LEAVE
17:45:50.389 -> 1[D][WiFiGeneric.cpp:337] _eventCallback(): Event: 4 - STA_CONNECTED
17:45:50.623 -> [D][WiFiGeneric.cpp:337] _eventCallback(): Event: 7 - STA_GOT_IP
17:45:50.623 -> [D][WiFiGeneric.cpp:381] _eventCallback(): STA IP: 192.168.1.39, MASK: 255.255.255.0, GW: 192.168.1.1
17:45:51.373 -> [4147] <[1D|00|01|00] U
17:45:51.420 -> [4189] >[00|00|01|00|C8]
17:45:51.420 -> [4189] Ready (ping: 41ms).

A ‘clean’ connect (the good one out of the alternate good/bad previous attempts) just shows this. I don’t like the look of the re-negotiation above after the ‘BLYNK’ connection.
17:43:54.988 -> Connecting to DAVETT
17:43:55.080 -> [D][WiFiGeneric.cpp:337] eventCallback(): Event: 0 - WIFI_READY
17:43:55.080 -> [D][WiFiGeneric.cpp:337] eventCallback(): Event: 2 - STA_START
17:43:55.314 -> [D][WiFiGeneric.cpp:337] eventCallback(): Event: 4 - STA_CONNECTED
17:43:55.361 -> [D][WiFiGeneric.cpp:337] eventCallback(): Event: 7 - STA_GOT_IP
17:43:55.361 -> [D][WiFiGeneric.cpp:381] eventCallback(): STA IP: 192.168.1.39, MASK: 255.255.255.0, GW: 192.168.1.1
17:43:55.596 -> 3
17:43:55.596 -> WiFi connected IP: [623]
17:43:55.596 -> ___ __ __
17:43:55.596 -> / _ )/ /
_____ / /

17:43:55.596 -> / _ / / // / _ / '
/
17:43:55.596 -> /
//_, /////_
17:43:55.596 -> /
__/ v0.6.1 on ESP32
17:43:55.596 ->
17:43:55.596 -> [626] Connecting to blynk-cloud.com:80
17:43:55.642 -> [688] <[1D|00|01|00] UlmMOwmt
17:43:55.689 -> [736] >[00|00|01|00|C8]

I can confirm that the original code DOES reconnect correctly if several minutes elapses between connection attempts. I.e. The router closes the connection after a few minutes time delay, so the subsequent reconnection attempt works just fine. The alternate fail issue only occurs on short breaks.