Connecting ESP8266 to WiFi

Hi all.
I’m connecting my Adadfruit ESP8266 Huzzah to a WiFi network with the following code:

#include <ESP8266WiFi.h>

#define WAIT_FOR_WIFI 5000

void setup()
{
  uint32_t startTime;
  char ssid[] = "mySSID";
  char pass[] = "myPassword";
  Serial.begin(115200);

  startTime = millis();
  WiFi.begin(ssid, pass);                             
  while (WiFi.status() != WL_CONNECTED) { 
    delay(500);
    Serial.print(".");   
    if (millis()-startTime >=  WAIT_FOR_WIFI) {
      break;
    }
  }
  Serial.println(); Serial.print("Status = "); Serial.println(WiFi.status());
  Serial.print("SSID: "); Serial.println(ssid);
  Serial.print(IP Address: "); Serial.println(WiFi.localIP()); 
}

void loop() {
  ;  
}

For easy reference, here are the definitions from ‘wl_definitions.h’:

typedef enum {
 WL_NO_SHIELD = 255,
        WL_IDLE_STATUS = 0,
        WL_NO_SSID_AVAIL,
        WL_SCAN_COMPLETED,
        WL_CONNECTED,
        WL_CONNECT_FAILED,
        WL_CONNECTION_LOST,
        WL_DISCONNECTED
} wl_status_t;

So, if I run the code from a fresh power-up, it works as expected and the value returned by the call to ‘WiFi.status()’ is 3 (i.e. ‘WL_CONNECTED’).

But, if the code runs from a processor reset (or firmware download followed by a processor reset), then the ‘while’ loop times out and the value returned by ‘WiFi.status()’ is 6 (i.e. ‘WL_DISCONNECTED’).

However, in BOTH cases, the ESP successfully connects to the WiFi. It gets an IP address, I can ping it, and other code (not shown) can do whatever network stuff it’s supposed to.

Anyone else see this? Wonder if there’s some state in the WiFi engine that’s not reset by processor reset? Maybe there’s a Wifi reset call available that I don’t know about?

Thanks.

Anyone else see this?

Lots of other people, as you could have found for yourself. It's a common complaint.

Please provide a link. My search did not show up this particular problem.

gfvalvo:
Please provide a link. My search did not show up this particular problem.

Still haven’t seen any other reports of this particular issue. But, a work around appears to be calling ‘WiFi.disconnect()’ before ‘WiFi.begin()’:

#include <ESP8266WiFi.h>

#define WAIT_FOR_WIFI 5000

void setup()
{
  uint32_t startTime;
  char ssid[] = "mySSID";
  char pass[] = "myPassword";
  Serial.begin(115200);

  startTime = millis();
  WiFi.disconnect();
  WiFi.begin(ssid, pass);                             
  while (WiFi.status() != WL_CONNECTED) { 
    delay(500);
    Serial.print(".");   
    if (millis()-startTime >=  WAIT_FOR_WIFI) {
      break;
    }
  }
  Serial.println(); Serial.print("Status = "); Serial.println(WiFi.status());
  Serial.print("SSID: "); Serial.println(ssid);
  Serial.print(IP Address: "); Serial.println(WiFi.localIP()); 
}

void loop() {
  ;  
}