Arduino mkr1000 & Blynk freezes when wifi connection is lost

I’m trying to use an Adruino MKR1000 with Blynk to make for a project that is resilient in case of wifi drop out (or similar).

When I run code similar to below (very cookie cutter, sample only), the MKR1000 will hang up at the virtualWrite() function if I restart the wifi router.

How can I make this more resilient to these kind of problems? Can I set up Blynk to timeout when something bad happens? What about the MKR1000? Can I set up a watchdog that will restart everything when needed?

Thanks

#include <BlynkSimpleMKR1000.h>
// other setup stuff here..

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

    void loop()
    {
    float val;
    val=getValFromSomewhere();
    Blynk.virtualWrite(V1, val);
    Blynk.run();
    }

There is a BLYNK_CONNECTED() and BLYNK_DISCONNECTED() function which you can use to achieve a reset. They are standalone functions, so they go outside of other functions. E.g.

void loop()
{
  stuffHere();
}

BLYNK_DISCONNECTED()
{
  doStuffWhenDisconnectHere();
}

I added the following snippet the new function never fired. The Serial.println() never even ran.

Any ideas?

I like the Blynk platform, but the API documentation needs a lot of work. It’s frustrating stabbing in the dark like this.

BLYNK_DISCONNECTED()
{
  Serial.println("disconnected!");

  if(Blynk.connected()!=true)
  {
    Blynk.connect();
  }
}

Well, if there’s no error during compilation, at least it’s right. You got your serial monitor enabled? Also in the setup()?

Yeah, I’m dumping stuff to the serial monitor fine right until I reset the wifi router, then everything dies.

Passing compilation doesn’t tell us enough unfortunately. I could add any function (with any name) and pass compilation. If Blynk is suppose to call that function on the other hand is a different question.

I’ve added a watchdog timer using this Adafruit library: https://github.com/adafruit/Adafruit_SleepyDog

It seems to work well.

You can’t do virtualWrite within loop. Because of flood.

I’ve only posted striped sample code. My real code throttles virtualWrite() to about once per 4 seconds.

Is the Arduino Blynk library able to cope with wifi dropout in a reliable manner? Is there any sample code available?

Ah ok.

At the moment Blynk automatically tries to reconnect. So in general all you need is Blynk.run in loop + BLYNK_CONNECTED as @Lichtsignaal mentioned. Regarding BLYNK_DISCONNECTED… not sure. I’ll call @vshymanskyy here.