-->
Page 1 of 1

WiFi Reconnect after wifi router disconnect

PostPosted: Fri Feb 19, 2016 7:47 pm
by patman
I have a fairly simple sketch using ESP8266wifi that reads data from Thingspeak and sets a dry contact relay. In "setup" I use a standard wifi.begin() to connect to a WPA network and it connects just fine. The problem is that in order for me to use the ESP8266 it needs to reconnect to the wifi router again after there is any kind of wifi outage. The problem is it doesn't.

Is it supposed to reconnect automatically after a disconnect?

Re: WiFi Reconnect after wifi router disconnect

PostPosted: Sat Feb 20, 2016 6:11 pm
by markus_b
An easy solution is to check if the connection is still alive, if it is not, then wait a moment , the reboot. When starting it will go again through setup() and connect again. The wait is to make sure it does not too frequently.

Re: WiFi Reconnect after wifi router disconnect

PostPosted: Tue Feb 23, 2016 1:01 pm
by Stévanovich
Hi,

Here is what i use , works for me, maybe not perfect :mrgreen:

Code: Select allvoid WIFI_Connect()
{
  digitalWrite(2,1);
  WiFi.disconnect();
  Serial.println("Booting Sketch...");
  WiFi.mode(WIFI_AP_STA);
  WiFi.begin(ssid, password);
    // Wait for connection
  for (int i = 0; i < 25; i++)
  {
    if ( WiFi.status() != WL_CONNECTED ) {
      delay ( 250 );
      digitalWrite(2,0);
      Serial.print ( "." );
      delay ( 250 );
      digitalWrite(2,1);
    }
  }
  digitalWrite(2,0);
}

...
...
..

void setup(void){
  pinMode(2, OUTPUT);
  Serial.begin(115200);
  Serial.println();
  WIFI_Connect();
...
..
..
}

void loop(void){
    if (WiFi.status() != WL_CONNECTED)
    {
      digitalWrite(2,1);
      WIFI_Connect();
    } else {
      digitalWrite(2,0);
    }
...
...
}



Hope will help.
Best regards

Re: WiFi Reconnect after wifi router disconnect

PostPosted: Sun Aug 20, 2017 4:10 am
by Anekwong
write the WiFi.begin(ssid,pws) and status() in the same setup() in portion of loop(), just work.