Long time ESP8266/Blynk user - Blynk now disconnects upon AP change

Hello. I know there must be a great answer to my question, but I’m stumped - this had been working very well and consistently previously. So … it goes to you the experts!

I have a network of a few ESP8266-12E modules that connect perfectly via SoftAP commands. I’ve never had any trouble connecting, disconnecting and then reconnecting over the SoftAP network. One (and only one) of the modules connects to my router after disconnecting from the SoftAP network and then back to the router and then back again etc on a schedule (a few seconds here then a few seconds there). The first time it connects to the router it also connects to Blynk successfully as you’ll see in my console dump below. But every time after when connected to the router it does not connect to Blynk.

I’ve updated everything but the ESP8266s. New Arduino IDE and Blynk libraries. Could the issue be with the ESP8266 instead of the router?

I’m including my code here as well as the results at bottom. Again, every connect attempt to SoftAP and router is always successful, but only the first attempt to connect to Blynk works as you can see. Am I having a router conflict issue? Is the IP address of my ESP8266 not really changing as the results shows it to be?

Thanks guys!!

#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>

IPAddress ipAddressR(192,168,0,29);
IPAddress ipAddressS(192,168,4,27);
IPAddress gatewayR(192,168,0,1);
IPAddress gatewayS(192,168,4,9);
IPAddress softAPMultiIP(192,168,4,255);
IPAddress routerMultiIP(192,168,0,255);
IPAddress subnet(255,255,255,0);
IPAddress myMultiIP;

const char auth[] = "RECENTLY REFRESHED";
const char ssid2[] = "CenturyLink0395";
const char sspwd2[] = "18 CHARS LONG PWD"; //pwd same for SoftAP

boolean connectToRouter = false;
boolean isBlynkConnected = false;
boolean isSoftAPConnected = false;

void setup() {
    Serial.begin(115200);
    delay(100L);
    establishConnection(); 
}

void establishConnection(){
    if (!isSoftAPConnected && Blynk.connected() == 1) {
        Blynk.disconnect();

        Serial.printf("Blynk disconnecting ...");
        while (Blynk.connected() == 1) {
            delay(100L);
            Serial.print(".");
        }
        Serial.printf("\nBLYNK DISCONNECTED\n");
    }
        
    WiFi.disconnect(true);
    WiFi.softAPdisconnect(true);
    
    Serial.printf("WiFi disconnecting \n");
    while (WiFi.status() == WL_CONNECTED) {
        delay(100L);
        Serial.print(".");
    }
    
    WiFi.mode(WIFI_STA);
    connectToAP();
}

void connectToAP() {
    isBlynkConnected = false;
    isSoftAPConnected = false;
    
    if (connectToRouter) {
        myMultiIP = routerMultiIP;
        Serial.print("ROUTER ");
        WiFi.config(ipAddressR, gatewayR, subnet);
        WiFi.begin(ssid2, sspwd2);

    } else {
        myMultiIP = softAPMultiIP;
        Serial.print("softAPSSID1 ");
        WiFi.config(ipAddressS, gatewayS, subnet);
        WiFi.begin("SoftAPSSID1", sspwd2);
    }
    
    IPAddress espIP = WiFi.localIP(), espGateway = WiFi.gatewayIP();
    Serial.printf("local values espIP=%s espGateway=%s\n", espIP.toString().c_str(), espGateway.toString().c_str() );
    
    boolean tooLong = false;
    unsigned long wifiConnectionTestMillis = millis();
    while (WiFi.status() != WL_CONNECTED) {
        delay(200);
        Serial.print(".");
        if (millis() - wifiConnectionTestMillis > 20000L) { // try for less than 20 secs then switch to another AP
            tooLong = true;
            yield();
            break;
        }
    }
    
    if (tooLong) {
        //try again...
        connectToAP();
    } else {
        if (connectToRouter){
            isBlynkConnected = manageBlynkConnection();
            
            if (isBlynkConnected) {
                Serial.println("Blynk connected!");
            } else {
                Serial.println("Blynk NOT connected!");
            }
        }else{
            isSoftAPConnected = true;
            Serial.println("SoftAP connection successful!");
        }
    }
}

void manageConnections() {
    static unsigned long wifiSwitchTimerStart = millis();
    unsigned long WIFI_SWITCH_TIMEOUT = connectToRouter ? 4000L : 3000L;
    
    if (millis() - wifiSwitchTimerStart > WIFI_SWITCH_TIMEOUT) {
        connectToRouter = !connectToRouter;
        establishConnection();
        wifiSwitchTimerStart = millis();
    }
}

boolean manageBlynkConnection(){
    Blynk.config(auth);
    Blynk.connect();

    unsigned long blynkConnectTestMillis = millis();
    while (Blynk.connected() == 0) {
        delay(35);//use time to do background stuff - prevent resets!
        if (millis() - blynkConnectTestMillis > 7000L) { // try for 7 seconds to connect to Blynk
            break;
        }
    }
    return Blynk.connected() == 1;
}

void loop() {
     manageConnections();
     
     if (isBlynkConnected){
        Blynk.run();
        delay(500);
    }else{
        delay(10);
    }
}
WiFi disconnecting 
.softAPSSID1 local values espIP=192.168.4.27 espGateway=192.168.4.9
.......SoftAP connection successful!
WiFi disconnecting 
ROUTER local values espIP=192.168.0.29 espGateway=192.168.0.1
............[10952] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.1 on NodeMCU

[10953] Connecting to blynk-cloud.com:80
[11170] Ready (ping: 86ms).
Blynk connected!
[15239] Disconnected
Blynk disconnecting ...
BLYNK DISCONNECTED
WiFi disconnecting 
softAPSSID1 local values espIP=192.168.4.27 espGateway=192.168.4.9
............SoftAP connection successful!
WiFi disconnecting 
ROUTER local values espIP=192.168.0.29 espGateway=192.168.0.1
............[26013] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.1 on NodeMCU

[26015] Connecting to blynk-cloud.com:80
[31016] Connecting to blynk-cloud.com:80
Blynk NOT connected!
WiFi disconnecting 
.softAPSSID1 local values espIP=192.168.4.27 espGateway=192.168.4.9
............SoftAP connection successful!
WiFi disconnecting 
ROUTER local values espIP=192.168.0.29 espGateway=192.168.0.1
............[54863] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.1 on NodeMCU

[54864] Connecting to blynk-cloud.com:80
[59865] Connecting to blynk-cloud.com:80
Blynk NOT connected!

I forgot to add a couple things …

  1. The order of WiFi.config() & WiFi.begin() makes no difference to the result. It’s the same whether config happens first or second.
  2. Every part of the code is updated. The ESP8266 itself is not though. I’ll try a couple other chips to see if it’s a hardware issue. Possible I guess?

Feels like the lwIP bugs in the ESP8266 core.

Which version of the core and lwIP are you using?

Thanks Costas.

Board is updated to v 2.4.0. Core is 2.4

IwIP is v2 Prebuilt (Mss = 536)

hmm … I’m not sure the hardware folder is where it should be, but how could everthing build correctly otherwise? Here’s where it is now …

packages\esp8266\hardware\esp8266\2.4.0

let me move files to Arduino/hardware and see what happens. Be right back …

Buggy don’t use until Core 2.5.0 is released or you pick up the master with Git.
Use v1.4 Prebuilt.

I’ve been using 1.4 also. No difference.

Moved 2.4 hardware files over to be under Arduino dir. No change. Nothing seems to make any difference which is why I’m suspecting the router. But what would the router be doing to cause this?

I’ll change the switching times to be 90 seconds in between router connects. Maybe the router doesn’t like something about the quick switching?

No difference. Only connects to Blynk that first time.

Oh! If I comment out the Wifi.config() statements it works perfectly! I don’t get it!, do you?

I’m not sure I can do without the assigned IPs though. Hmmm

I was just looking at that.
You have WiFi.conifg() after WiFi.begin() but normally they need to be the other way around or are you setting up the WiFi.config() for the NEXT connection?

Hmm… already tried with downgraded Blynk library? I’m still on 0.4.8 currently. Downgraded from 0.5.1 (other problems with wifi) and currently I’m happy :slight_smile:
The ESP core is 2.3.0rc2 FYI, so it might be, the recent version messes things up too. :anguished:

sorry paul, if you look at my comments it doesn’t matter which orientation of those two. i always put config first, but was testing it when i posted that code. wish that was it!

@ marvin7 oh yeah? no, i’m on the latest blynk, but I doubt it matters. i had a much earlier blynk version and there was no difference. this really looks like a router + esp8266 issue. i can’t figure out which, but it’s clear now that it’s a wifi config issue. so … esp8266WiFi might be retaining the wrong info during config?

awesome advice to look at earlier esp8266 cores. thanks!!

@marvin7 are you sure you don’t mean 2.4.0rc2 and not 2.3.0rc2?

Yes @hazyj , I guess so :wink:
Schowek-1

And correction: Never used 1.5.1. The last I was using, which were then downgraded to 0.4.8 was 0.5.0 (due to looong data sending time- thanks @Gunner :stuck_out_tongue: )

Thanks @martin7 but since 2.3.0-rc2 isn’t an option provided to me when selecting a board, I went with 2.3.0. There’s no difference.

Great advice regarding a change to Blynk 0.4.8 - thanks!

Anyone else have ideas for what might be happening here? Is there someone in particular I should be asking?

Go back to basics.
I’d try erasing the flash memory of the ESP completely, in case it’s corrupted in some way.
Then I’d try using a basic Blynk sketch to check that everything’s working as it should.

Pete.

One more thing came into my mind: as a last resort I’d try to flash some basic wifi connection example, even without Blynk itself. Alternatively flash some ready, prebuilt firmware (EspEasy, Espurna, etc) and check if connection issues still exists. That might give you some clue, at least what chances you do have :wink:

And speaking about chances: I was forced once to totally abandon one of my so called “routers”. I will not point it’s brand, but will NEVER ever again buy that **#*.

thanks - all good ideas. yeah, i’m leaning more and more toward considering the router as the cause. but I can’t make this project router specific so I need to work around the challenge of a bad router. so far so good as long as I don’t absolutely need static IPs.