[SOLVED] Blynk freezes the Arduino

Hi all,

I integrated Blynk in an existing project. The project does some pool managment, is on a mega2560 with ethernet shield, also has its webpage for remote control and sends some data to internet server database and also to local wifi connected displays. all works well until the internet connection fails. Then blynk tries to connect to server and blocks ALL up to 32 seconds. This is a 100% nogo for my application.

And Internet fails often in the last month. The provider has no solution.

What can I do?

Post your code would be step 1 presumably?

I know that posting the code is always good idea, but in this case the code is hundreds of lines and has nothing to do with the malfunction. I thought someone else had the same problem and is already solved by adding some code lines to check if there is Internet connection or to change a timeout parameter or so.

Maybe later I can write a small sketch that shows exactly what I mean.

that’s what the search feature is for:

http://community.blynk.cc/search

jeje,
this was the first I did.

I found not really much about it. Some old posts, to code a workaround but no real solution.

Looks strange that a project like Blynk uses blocking routines.

On a system like Arduino I always try to make nonblocking code. And I am only a hobby coder, far away from professional.

Search for isConnected()

Ok… I had the similar problem with one of my project, and was fixed by making the changes in the code like below. Give a try, Good luck!

bool connected = false;

void setup()
{

// Try for 3 time to connect to blynk
int con = 0;
 do {
connected = Blynk.connect();
con = con+1;

} while (!connected && con <=2);

}

void loop()
{

// Runs Blynk.run only if the WiFi connection is made
if (connected) {

 Blynk.run();

}

isConnected() refers to the wifi connection of an ESP8266, or doesn’t?

My Mega is with Ethernet shield ALWAYS conected to the LAN with cable. It’s the internet connection (Router), that fails.

And if I don’t execute Blynk.run(); with whatever workaround, it will no more connect when Router gets internet.
Or I have to make routine to ping google.com and activate then the execution of Blynk.run()
Possible but sure not state of the art.

@ElEspanol I was overlooking the fact you were using an Arduino and Ethernet.

However AFAIK isConnected() relates to being connected to Blynk so it isn’t specific to ESP’s so you should still be able to use it.

Maybe if you describe in more detail what the ‘internet failures’ are we can advise further.

Do you need a fix for when there is no internet when you first connect, your internet connection goes down after the first connection or both?

Obviously there is only so much you can do if the internet is down but there are normally ways available to ensure your system still performs the basics even if it is unable to send data out to the internet.

The Arduino is always in use and should work in local network. The internet connection can be good for days, and then could be down for hours or minutes. If it is down, the Arduino should do his work as foreseen. Of course without internet also can’t send data to thingspeak, but this is coded nonblocking. and not storing the data in this time is bad, but it is as it is. The problem is while no internet is available, the sketch hangs about 32 sec., then makes one loop and hangs again 32 sec.
In this 32 sec, does not recognize a button pressed, absolutly nothing. When internet comes back, all runs again normally.
What I need is that there is no 32 sec. pause if internet is down. Maybe we can change the timeout?
Or I check if loop is more than 10 seconds and if so, not run blink.run() for the next ten minutes?
Or both?
Or the developer of the lib has any idea o can change to non blocking code?

You should write your own ethernet connection routine, and use Blynk.config() instead of Blynk.begin()

@ElEspanol couple of extracts for you to consider:

 void setup(){
   
  // pinMode and other stuff

    Blynk.config(blynk_token);
    Blynk.connect(3333);  // equals 10s timeout as Blynk connect works on 3ms units
    unsigned long timeout = (millis());
    while (Blynk.connect() == false) {
      // wait
    }
  }

 void checkBlynk(){  // called with SimpleTimer
    bool isconnected = Blynk.connected();
    if (isconnected == false){
      Blynk.connect();  // try to reconnect but will try in loop() anyway  
    }
  }

Now I did some tests. Bad result, with
connected = Blynk.connect();
if (connected) Blynk.run();

I have every loop betwenn 5 and 6 sec. blocking, even when Internet is connected. So this seems not to be the solution.

Now I will study Blynk.config()

Hi friends,

Blynk.config() looks like that it brings more or less the solution :slight_smile: :grin:

instead of
Blynk.begin(auth, “blynk-cloud.com”, 8442, ips, dnstest, gateway, subnet, mac);
I have now:
Blynk.config(auth);
bool result = Blynk.connect(300);

When I disconnect now the ethernet cable, I have max 2 sec. delay and a lot of loops between these delays.
And a “Trouble detected: http://docs.blynk.cc/#troubleshooting-flood-error” makes no delay.

Thank you all very much!

Hi @ElEspanol

I am trying to achieve the same result as you. I would like my sketch to continue looping with minimal disruption when connection to the Blynk server is not available. I have the same hardware that you do. Arduino mega with ethernet W5100 sheild.

Can you clarify how you are managing your connection? I am having trouble replicating your results. Do you have this in the setup loop? What do you do with the bool value called ‘result’?

Blynk.config(auth);
bool result = Blynk.connect(300);

The result var returns either 0 or 1 (fail or succes connecting) and you can use that to handle your connection management using blynk.run(), that would be my guess…

In the meantime I learned that this wasn’t the solution. After some changes in the sketch (had nothing to do with the blynk stuff) I had the same problem again.

At the moment the Internet provider there is more stable and I didn’t spend more time on this problem.

There is a solution, check out this thread… Go to the bottom, (post 33)