ESP does not run when connected to Wifi but no internet connection

I’m having a problem, hope someone can help. I’m using the Blynk Edgent sketch in the example. It works normally. Without Wifi the program still runs.
But there is one problem: If the ESP is running and loses its network connection (still connected to Wifi), the program will continue to run normally. But if the ESP has just turned on (or after restarting), is connected to Wifi but has no connection to the network (for example, the network cable is broken), the program crashes and cannot run.
The problem here is that if on the first run, the ESP is able to connect to the internet, then even if there is no network connection or no Wifi connection, the ESP will still run. But if in the first run, ESP connects to Wifi but no internet connection, ESP will not run until internet connection is available.
I am using ESP8266 and blynk library V1.1.0.

This topic has been discussed many times in this forum. Please search the forum.

It doesn’t crash.
The code in the ConfigMode.h file sets:

blynkstate::set(MODE_ERROR);

and when that occurs, the MCU is restarted…


void enterError() {
  BlynkState::set(MODE_ERROR);

  unsigned long timeoutMs = millis() + 10000;
  while (timeoutMs > millis() || g_buttonPressed)
  {
    delay(10);
    app_loop();
    if (!BlynkState::is(MODE_ERROR)) {
      return;
    }
  }
  DEBUG_PRINT("Restarting after error.");
  delay(10);

  restartMCU();
}

Pete.

I have searched for similar problems on the forum but still no solution. It didn’t restart, I did a bit of editing in the ConfigMode.h file:

        void enterConnectCloud()
       {
      BlynkState::set(MODE_CONNECTING_CLOUD);

      Blynk.config(configStore.cloudToken, configStore.cloudHost, configStore.cloudPort);
      Blynk.connect(0);

      unsigned long timeoutMs = millis() + WIFI_CLOUD_CONNECT_TIMEOUT;
      while ((timeoutMs > millis()) &&
             (WiFi.status() == WL_CONNECTED) &&
             (!Blynk.isTokenInvalid()) &&
             (Blynk.connected() == false))
      {
        delay(10);
        BLYNK_LOG1(BLYNK_F(BLYNK_NEWLINE
                           "a" BLYNK_NEWLINE));
        Blynk.run();
        BLYNK_LOG1(BLYNK_F(BLYNK_NEWLINE
                           "b" BLYNK_NEWLINE));
        app_loop();
        if (!BlynkState::is(MODE_CONNECTING_CLOUD))
        {
          Blynk.disconnect();
          return;
        }
      }

BlynkProtocol.h file

 if (tconn && (t - lastLogin > BLYNK_TIMEOUT_MS))
    {
        BLYNK_LOG1(BLYNK_F("Login timeout"));
        conn.disconnect();
        state = CONNECTING;
        return false;
    }
    else if (!tconn && (t - lastLogin > 5000UL))
    {
        conn.disconnect();
        BLYNK_LOG1(BLYNK_F(BLYNK_NEWLINE
                           "abc" BLYNK_NEWLINE));
        if (!conn.connect())
        {
            BLYNK_LOG1(BLYNK_F(BLYNK_NEWLINE
                               "abcd" BLYNK_NEWLINE));
            lastLogin = t;
            return false;
        }
        BLYNK_LOG1(BLYNK_F(BLYNK_NEWLINE
                           "abcde" BLYNK_NEWLINE));
        msgIdOut = 1;
        sendCmd(BLYNK_CMD_HW_LOGIN, 1, authkey, strlen(authkey));
        lastLogin = lastActivityOut;
        return true;
    }

here are the results:

     Device:    Blynk zxc-5C54
     Firmware:  1.0.1 (build Aug 30 2022 14:05:55)
     Token:     SBqL - •••• - ••••- ••••
     Platform:  ESP8266 @ 160MHz
     Boot ver:  6
     SDK:       2.2.2-dev(38a443e)
     ESP Core:  3.0.1
     Flash:     4096K
     Free mem:  29072
     ----------------------------------------------------

     >[769] INIT => CONNECTING_NET
       [5799] Connecting to WiFi: net1

       [11656] Using Dynamic IP: 192.168.43.240
       [11656] CONNECTING_NET => CONNECTING_CLOUD
       [11667]
       a

       [11667]
       abc

On first startup, if ESP connects to Wifi but can’t connect to Cloud, the program seems to have stopped at “if (!conn.connect())” command until it connects to the cloud.

Without seeing the code, its hard to assist. Post your code properly formatted.

I’m using the BlynkEdgent.h sketch in the example. The program runs normally. The problem I am having is that on first run if ESP connects to wifi but fails to connect to blynk.cloud the program stops. But after connecting to the cloud, then if the connection is lost, the program will still run.

Sorry, I don’t speak English, so I use Google translate, so maybe you don’t understand what I mean.
Like I said it doesn’t reboot, it just restarts after 500 failed connections. So that’s not the problem.
My question is, why can’t the program run if the first time running the ESP can’t connect to the cloud. But if the ESP is already connected to the cloud, then even if the connection is lost, the program will still run.

We are not facing any such issues. Atleast i am not !!
May be you have changed something in your code/header files. Not sure.

I am not able to think if any solutions for your problem without much details.

I didn’t edit anything in the files at all.
The problem only occurs when, ESP connects to Wifi but the modem has no internet connection. (ie can connect to wifi but not connect to blynk.cloud)
And if there is no wifi connection (or no wifi), the program will still run normally.

My problem is not finding a way to reconnect when the connection is lost, but finding a way to keep the program running when the cloud connection is lost

The part of the code that you need to keep running when no internet/ no wifi, should be called by an ISR.

So it keeps running what ever the connection status is.

1 Like

Those two statements appear to contradict each other.

Pete.

1 Like

I don’t think some print commands will crash the program. And with or without those print commands the program still won’t run.

Good luck with your project.

1 Like

Thank you for the help. I think I’ll keep using Blynksimple instead of Edgent. With Blynksimple I just use Blynk.config and the problem is solved.