Arduino Forum

Using Arduino => Networking, Protocols, and Devices => Topic started by: rjbuckley on Feb 11, 2019, 05:15 pm

Title: MKR1000 WiFi101.h and WiFi.disconnect()
Post by: rjbuckley on Feb 11, 2019, 05:15 pm
Can't seem to disconnect...


void loop() {
  while ( status != WL_CONNECTED )
  {
    Serial.println("Obviously not connected...");
    status = WiFi.begin(ssid);
  }
  Serial.println("Connected. Trying to disconnect");
  WiFi.disconnect();
  delay(30000);   
}

Produces:
11:14:19.204 -> Obviously not connected...
11:14:21.742 -> Connected. Trying to disconnect
11:14:51.757 -> Connected. Trying to disconnect

ad infinitum
Title: Re: MKR1000 WiFi101.h and WiFi.disconnect()
Post by: pert on Feb 15, 2019, 06:53 am
The first time the loop runs, status is set to WL_CONNECTED. After that, there is no way in your code for status to ever get set to anything other than WL_CONNECTED since status is only updated in the while loop and the while loop can only run if status != WL_CONNECTED. So even though it was disconnected by the call to WiFi.disconnect(), the while loop will never run again.

In order to run as you expect, you would want to change the while loop to look like this:
Code: [Select]
 while (WiFi.status() != WL_CONNECTED )
  {
    Serial.println("Obviously not connected...");
    WiFi.begin(ssid);
  }