Serial.println(Wifi.status); Codes Meaning

I'm outputting Serial.println(Wifi.status); from my ESP8266 when it is trying to connect to a WIFI hub.

  • I'm getting a return of 6 when the device is trying to connect.
  • I believe that I'm getting a return of 3 when it connects
  • 1 when it cannot connect (I've modified the SSID to a non existent station in the ESP)
  • 4 when it cannot connect due to a bad password

I have found this reference https://www.arduino.cc/en/Reference/WiFiStatus however it would be handy if there is any official documentation explaining what these return codes mean.

Is there anything available?

...Surfer Tim gives an informative response here:

http://forum.arduino.cc/index.php?topic=162674.0

You can always find these kinds of things in the source code:

typedef enum {
    WL_NO_SHIELD        = 255,   // for compatibility with WiFi Shield library
    WL_IDLE_STATUS      = 0,
    WL_NO_SSID_AVAIL    = 1,
    WL_SCAN_COMPLETED   = 2,
    WL_CONNECTED        = 3,
    WL_CONNECT_FAILED   = 4,
    WL_CONNECTION_LOST  = 5,
    WL_DISCONNECTED     = 6
} wl_status_t;

You could write your own function to print it:

const char* wl_status_to_string(wl_status_t status) {
  switch (status) {
    case WL_NO_SHIELD: return "WL_NO_SHIELD";
    case WL_IDLE_STATUS: return "WL_IDLE_STATUS";
    case WL_NO_SSID_AVAIL: return "WL_NO_SSID_AVAIL";
    case WL_SCAN_COMPLETED: return "WL_SCAN_COMPLETED";
    case WL_CONNECTED: return "WL_CONNECTED";
    case WL_CONNECT_FAILED: return "WL_CONNECT_FAILED";
    case WL_CONNECTION_LOST: return "WL_CONNECTION_LOST";
    case WL_DISCONNECTED: return "WL_DISCONNECTED";
  }
}
Serial.println(wl_status_to_string(WiFi.status()));

Pieter

3 Likes

Thats a nice way of doing it! Thanks.

1 Like

Hey guys,

I just wanted to let you know what I found out how the WiFi.status() behaves in different cases:

  • if wifi is visible:
    WiFi.begin() >> WL_DISCONNECTED >> WL_CONNECTED
    if wifi then disappears:

WL_DISCONNECTED >> WL_NO_SSID_AVAIL

  • if wifi is not visible:
    WiFi.begin() >> WL_DISCONNECTED >> WL_NO_SSID_AVAIL
    if wifi then appears:

WL_DISCONNECTED >> WL_CONNECTED

  • if disconnecting wifi:
    WiFi.disconnect() >> WL_IDLE_STATUS

  • if wifi configuration is wrong (e.g. wrong password):
    WiFi.begin() >> WL_DISCONNECTED >> WL_CONNECT_FAILED

I never found an occasion where WiFi.status() is WL_CONNECTION_LOST.

Here is the code with which I tested this (with ESP8266):

#include <ESP8266WiFi.h>
char ssid[] = "YourSSID";      // your network SSID (name)
char pass[] = "YourPW";   // your network password


void setup() {
	pinMode(BUILTIN_LED, OUTPUT);
	digitalWrite(BUILTIN_LED, LOW);
	Serial.begin(115200);
}

void loop() {
	if (Serial.available() > 0) {
		String input;
		input = Serial.readStringUntil('\n');
		if (input[0] == 'c') {
			WiFi.begin(ssid, pass);
			Serial.println("connecting...");
		}
		else if (input[0] == 'd') {
			WiFi.disconnect();
			Serial.println("disconnecting...");
		}
	}

	
	if (WiFi.status() == WL_IDLE_STATUS) { // when not connected to a network, but powered on 
		digitalWrite(BUILTIN_LED, !digitalRead(BUILTIN_LED));
		Serial.println("WL_IDLE_STATUS");
	}
	else if (WiFi.status() == WL_CONNECTED) {
		digitalWrite(BUILTIN_LED, LOW);
		Serial.print("WL_CONNECTED ");
		Serial.println(WiFi.localIP());
	}
	else if (WiFi.status() == WL_NO_SSID_AVAIL) {
		digitalWrite(BUILTIN_LED, HIGH);
		Serial.println("WL_NO_SSID_AVAIL");
	}
	else if (WiFi.status() == WL_CONNECT_FAILED) {
		digitalWrite(BUILTIN_LED, HIGH);
		Serial.println("WL_CONNECT_FAILED");
	}
	else if (WiFi.status() == WL_CONNECTION_LOST) {
		digitalWrite(BUILTIN_LED, HIGH);
		Serial.println("WL_CONNECTION_LOST");
	}
	else if (WiFi.status() == WL_DISCONNECTED) {
		digitalWrite(BUILTIN_LED, HIGH);
		Serial.println("WL_DISCONNECTED");
	}
	else {
		digitalWrite(BUILTIN_LED, HIGH);
		Serial.print("unknown status: ");
		Serial.println(WiFi.status());
	}
	
	delay(200);
}
1 Like

About WiFi.status() is WL_CONNECTION_LOST, did you try switch off your wifi rooter ?

PieterP:
You could write your own function to print it:

const char* wl_status_to_string(int status) {

Very nice code example. The enum appears to have changed at some point so the function definition needs to replace wl_status_t with int per above.