ESP8266NodeMCU - how to restart wifi after a disconnect

I have a Lolin NodeMCU set up to repeatedly ping devices on my local network, and on the web.
It uses the ESP8266WiFi library.

Until there is a disconnect it works fine; however I cant see how to get it to reconnect.

The full project is described here

and the relevant bits of the sketch - I've attached the full sketch below for context but its quite big.

#include <ESP8266WiFi.h>            //WiFi functions

..

WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;

..

and in setup()

 //monitor Wifi connection and handle D0

  gotIpEventHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP & event)
  {
    Serial.print("Station connected, IP: ");
    Serial.println(WiFi.localIP());
    digitalWrite(dPin[1], 0); //blue LED
  });

  disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected & event)
  {
    Serial.println("Station disconnected");
    digitalWrite(dPin[1], 1); //blue LED
  });

  bool stationConnected = WiFi.begin(ssid, pass);
  delay(200); //allow time to connect
  // Wait until connection completed
  Serial.print("Connecting to AP...");
  while (WiFi.status() != WL_CONNECTED)  {
    delay(200);
    Serial.print(".");
  }
  Serial.println("Ok\n");
  digitalWrite(dPin[0], 0); //indicate wifi connected -  white LED

I've googled & read the documentation but to me its advanced gobbledegook.

netmonitor-good.ino (18.2 KB)

Put the code starting the Wifi in a function of its own. Call that function from setup but also from the code, in loop, where the failure is detected.

Thanks RailRoader, AFAIK the failure is detected by the disconnectedEventHandler - not in the loop().

Here's a code that I use to maintain a WiFi and MQTT connection, perhaps it will give a few hints.

Also, if you have the ESP8266 with OS you can set the keep alive as a separate task.

void MQTTkeepalive( void *pvParameters )
{
  // setting must be set before a mqtt connection is made
  MQTTclient.setKeepAlive( 90 ); // setting keep alive to 90 seconds makes for a very realiable connection, must be set before the 1st connection is made.
  for (;;)
  {
    //check for a is connected and if the WiFi thinks its connected, found checking on both is more reliable than just a single check
    if ( (wifiClient.connected()) && (WiFi.status() == WL_CONNECTED) )
    {
      xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY ); // whiles loop() is running no other mqtt operations should be in process
      MQTTclient.loop();
      xSemaphoreGive( sema_MQTT_KeepAlive );
    }
    else {
      log_i( "MQTT keep alive found MQTT status %s WiFi status %s", String(wifiClient.connected()), String(WiFi.status()) );
      if ( !(WiFi.status() == WL_CONNECTED) )
      {
        connectToWiFi();
      }
      connectToMQTT();
    }
    vTaskDelay( 250 ); //task runs approx every 250 mS
  }
  vTaskDelete ( NULL );
}

You can have your WiFi feedback routine either try a reconnect or a reset of the ESP8266 to start all over.

Also, you might want to issue a WiFi.disconnect(); before making the connection:
As shown below:

void connectToWiFi()
{
  byte mac[6];
  log_i( "connect to wifi" );
  while ( WiFi.status() != WL_CONNECTED )
  {
    WiFi.disconnect();
    WiFi.begin( SSID, PASSWORD );
    log_i(" waiting on wifi connection" );
    vTaskDelay( 4000 );
  }
  log_i( "Connected to WiFi" );
  WiFi.macAddress(mac);
  log_i( "mac address %d.%d.%d.%d.%d", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]  );
  WiFi.onEvent( WiFiEvent );
  GetTheTime();
  printLocalTime();
}

johnerrington:
Thanks RailRoader, AFAIK the failure is detected by the disconnectedEventHandler - not in the loop().

Call a restart from there then?