How can I see if Wifi connection is still alive?

I have a project that uses a MKR1000 as a simple web server and also as a client to send SMS when certain events occur. And it works fine.

However, sometimes the connection drops. For example maybe the router is reset.

How can I test (in code) to see if the connection has dropped?

And if this detects a drop, is there a clean, reliable way to restart the connection or should I just do a NVIC_SystemReset(); to restart the sketch?

Thanks!

!WL_CONNECTED is the normal way to detect a diconnected WiFi, just do a normal begin. Check the library samples to be sure, but I think that is correct. Here is a code snippet from a WiFi web server. It assumes it gets fixed magically, but you may want to count the number of tries then do a new begin.

I found another thread which seems to indicate that isn't 100% reliable. It suggested the following code that actually pings the gateway to see if it's live:

bool isWifiConnected(){

  //check connected
  if ( WiFi.status() != WL_CONNECTED) {
    return false;
  }

  //test ping the gateway
  IPAddress gip = WiFi.gatewayIP();
  int pingResult = WiFi.ping(gip);
  if (pingResult < 0) {
    return false;
  }  

  //connected
  return true;
  
}

Side note: I did a forum search expecting to find something like this, but apparently didn't search on the correct keywoard. That thread popped up on the side of this one!

Thanks!

Ok, I haven't seen that particular example but it sounds like a good idea. If a failure is detected, I would still recommend restarting only the WiFi, not the entire board.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.