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

Code isn't working without connected Blynk

The sketch was for ESP. I don’t think Blynk has a config() for Arduino’s.

Oh? That might explain my own issues when I was playing around with it… something I didn’t pursue due to general lack of need.

@GTT with ESP’s you can make the internet connection prior to connecting to Blynk whereas the internet connection is all built in to the blocking Blynk.begin() for Arduino’s.

For Arduino’s you would need to build in a manual timeout to circumvent the blocking routine and ensure loop() contains something like this:

if(Blynk.connected()){
    Blynk.run();
  }

and SimpleTimer to check and reconnect etc.

I’m a little surprised that config() and connect() actually compile on an Arduino.

The other point is that I was under the impression that Blynk.connect(3333); tried for 10s based on a multiplier of 3 in the library. 3 does appear in the library as part of the connection / timeout parameters but I’m assured 3333 is simply 3333ms not 9999ms.

@Spyrosgreece could you change (3333) to () which means the default 10s timeout.

As there is no indication of ESP exclusivity in the DOC’s (which honestly doesn’t guarantee anything :wink: ) I figured it was fully compatible… and I just confirmed that Blynk.config(Serial, auth); not only compiled, but runs fine… however I will have to go back and hook up something to the Arduino to allow “off server” functionality and test again with Blynk.disconnect() & Blynk.connect() on Arduino.

1 Like

I can confirm this works just fine for me on MEGA2560.

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
 if (Ethernet.begin(mac) == 0) {
       Serial.println("Failed to configure Ethernet using DHCP");
       // give the Ethernet shield a second to initialize:
     }
     delay(1000);
     Serial.println("connecting using Ethernet...");
   
     
        Blynk.config(auth_local_server, "192.168.0.251", 8442); 

and I use this logic to check reconnection (note my code is design to allow me to use either ARDUINO MEGA2560 or ESP8266

//===== RE CONNECT IF BLYNK NOT CONNECTED ===== 
// (called every xxx seconds using SimpleTimer)

void reconnectBlynk() {
if (!Blynk.connected()) {
    number_of_times_blynk_lost_connection++;
    
    #ifdef USE_MEGA2650 
      wdt_reset();  // give it as much time as possible to re-check ethernet
    #endif
    
     #ifdef USE_ESP8266
      ESP.wdtFeed();
    #endif
    
    #ifdef USE_MEGA2560
     int return_value = Ethernet.maintain();
    #endif
    
    #ifdef USE_ESP8266
      if (WiFi.status() != WL_CONNECTED)
       {
          WiFi.begin(ssid, pass);                           // Connect to WiFi network
          Serial.println(F("Waiting to connect to Wifi:"));
          long waittime = millis()+ 7000;                  // 7 seconds wait time
          while (WiFi.status() != WL_CONNECTED) {           // Wait for board to connect to WiFi network
             delay(500);                                    
             Serial.print(F("."));  
             if (millis() > waittime)
              { break; }                                   
          }                                                 
           Serial.println(F(" "));  
      }
    #endif

    #ifdef USE_MEGA2560
     #ifdef DISPLAY_DEBUG_ETHERNET
       switch (return_value) {
       case 0 :
        Serial.println (F("0 returned from Ethernet.maintain - i.e. nothing happened"));
        break;
      case 1 :
        Serial.println (F("1 returned from Ethernet.maintain - i.e. renew failed"));
        break;
      case 2 :
        Serial.println (F("2 returned from Ethernet.maintain - i.e. renew success"));
        break;
      case 3 :
        Serial.println (F("3 returned from Ethernet.maintain - i.e. rebind fail"));
        break;
      case 4 :
        Serial.println (F("4 returned from Ethernet.maintain - i.e. rebing success"));
        break;           
    }
     #endif
    #endif
    
    #ifdef USE_MEGA2560
          wdt_disable(); // disable wdt otherwise it will trigger !
     #endif  

      #ifdef USE_ESP8266
          ESP.wdtDisable();
      #endif
      
 
    if(Blynk.connect(3333)) {
 
     
      bool isFirstConnect = true; // assume this is first time we connected to Blynk again :-) i.e. ensure BLYNK_CONNECTED()to executed again, now that we re-connected
      number_of_times_blynk_lost_connection++;
   
       #ifdef USE_MEGA2560
      	wdt_enable(WDTO_8S); // re-enable wdt
         BLYNK_LOG("Essendon MEGA Reconnected to Blynk");
      #endif
     
      #ifdef USE_ESP8266
       ESP.wdtEnable(WDTO_8S);    // enable Watch Dog Time WDT for 8s
         BLYNK_LOG("Essendon Wemos Reconnected to Blynk");
      #endif
    } 
    else {
       
        #ifdef USE_MEGA2560
        wdt_enable(WDTO_8S); // re-enable wdt
        BLYNK_LOG("Essendon Home SCR MEGA Not reconnected to Blynk");
       #endif
    
       #ifdef USE_ESP8266
        ESP.wdtEnable(WDTO_8S);    // enable Watch Dog Time WDT for 8s   // 
        BLYNK_LOG("Reconnected to Blynk");
      #endif
    }
  }
}

The problem might be when the Arduino is using an ESP shield. With Ethernet you can connect to the internet before Blynk. I guess technically you can with an “ESP shield” but it’s not very straightforward and most people use Blynk.begin().

I try to change (3333) to () without any success.

The problem is that if arduino stay offline for two minutes the code reset it.
Thanks to you @Costas !!!

if (disconnects > 12) {
Blynk.begin(auth);
}
if (disconnects > 24) {
if (pinV50 == 0) {
Blynk.virtualWrite(V50, 255);
pinV50 = 1;
resetFunc();
}
}
But sometimes about two or three per month arduino stuks maybe to “blynk.begin(auth)”
and i must to reset it manualy…I try to put “wdt_enable(WDTO_8S);” before “blynk.begin(auth)”
but 8 secs many times isn’t enough το connect…
So i need after specific time to escape from “blynk.begin(auth)” loop and reset again…

Sorry im using ethernet shield…Not esp…

@Spyrosgreece can you post a stripped down version of your sketch that just includes the connect / reconnect / WDT routines (plus the associated Blynk stuff) and I’ll try to take a look at it.

Ethernet is the only connection method I currently use with Blynk and Arduino’s.

Hi Guys,

I am work with ESP8266 and I have the same problem. I have built a smart swich system with local (physical buttons) and remote control (app Blynk),
When the ESP is connected to the WIFI, everything works fine, I can control the lights from my phone and also with the physical buttons.
The problem is, when I turn on the ESP with the router turn off or without internet, the system is not working and I can not use the physical buttons. The system hangs in the setup(), at the Blynk.begin(auth);
If the ESP turn on with wifi signal and internet and after the cable is unplugged of the internet on the router
the code reconnect and work fine. To do this, I use a simple timer for check the reconnection. Sketch of Costas.
Thank for yours helps.

Look up the alternatives for Blynk.begin() in Connection Management section of the docs.

Thanks Costas,
It is convenient to divide the function blynk.begin into 2 parts, 1-connect wifi and 2 connect to the server, both with the break statement after a few seconds. What do you think?

Yes if you want to cover all possibilities and then set timeouts and reconnects separately for both.

Thanks, I will try it

1 Like

Hello friends,

I have the same problem. I have a arduino due and wifi shield esp8266.
When the Internet or Wi-Fi router is active everything works fine, however if restart the arduino at the time when the Internet or router is absent, then the arduino hangs. Arduino hangs in void setup on the line : Blynk.begin(auth, wifi, ssid, pass);
When I try to use “Blynk.connect()” , the arduino hangs in any case (with internet or without).

@kikilich over the last few days I have posted code for:

  1. ESP Blynk no block.
  2. Ethernet Blynk no block.

Unfortunately it’s much more difficult for ESP’s as WiFi shield. See this post for possible solutuons Brown outs or reboots

I read that thread, but I dont understood how it may help me. While I was reading the forum I understood that all users on different device have (or had) a problem with controller hang when the function blynk.begin () is executed while the internet is absent. It is true? When this problem will be solved on the Arduino + ESP ?

In addition, I have a problem with the function blynk.connect (). It dont work. When I call this function my controller is hang ( even in the case of the presence of the Internet). Therefore I can’t reconnect to the internet.

(my controller arduino due + esp , I use library “BlynkSimpleShieldEsp8266.h” version v0.4.8)

Offtopic. In addition, there is Still a stupid question. I built my big project on Arduino DUE. Is it possible to run blynk server on Rasberry and connect to it arduino due via USB or serial port (without network)?

If this means you are using the ESP hooked up to give WiFi to an Arduino the short answer is there is no easy fix.
If you use the ESP for it’s intended purpose, a MCU with WiFi, then I posted the code, again, in the last few days to bypass the Blynk.begin() code blocking routine.
If your seasoned coder looked closely at the Blynk libraries for Blynk.config() and Blynk.connect() they might be able to port them from an ESP to an Arduino.
Alternatively if you really need an Arduino in your project you could go ESP with Arduino shield route, rather than Arduino with ESP shield.

Needs porting to AT format for Arduino with ESP shield.

Dunno but you could add Ethernet to the Due for a couple of dollars and with the right Ethernet board it will recover from internet failures.

A post was split to a new topic: Program not working when device is ofline