In the previous post we’ve seen how to program ESP8266 using Arduino IDE by installing the Boards Manager.
This post is on using the powerful ESP8266 WiFi Library which is automatically installed while using the Boards Manager.When you proceed with the board package for the ESP8266,as explained in previous post, the ESP8266 WiFi library is automatically installed for you.
Now, any time you want to use the classes and the functions from that ESP8266 WiFi library, use the Include statement at the top of the program first, followed by the header name
at the starting of Arduino code & start using the Classes associated with this library.
ESP8266 WiFi library is designed after the the standard Arduino WiFi library but has even more functionality than the standard Arduino library .The standard Arduino WiFi library is used for the WiFi shield or with the Arduino boards like YUN with inbuilt WiFi.
With the introduction of ESP8266 , the WiFi functionality has become much cheaper & easy to use.
The WiFi library has many classes that you can use. Each class has functions in it that are specific to that class. There’s the WiFi class, the IP address class, the server class, the client class and the UDP class.
In this post we’re going to explore the WiFi class & its functions.
The WiFi class allows to establish a connection with an access point.
What’s cool about the WiFi class is that we don’t have to create an instance of this class. We can go ahead and start using it. It’s like the serial library of Arduino. We don’t need to create an instance of the serial library.
First Function of the library is WiFi.begin().
The usage of this function is similar to our Serial.begin().
Just like Serial.begin() , we use WiFi.begin() .WiFi.begin() requires 2 strings as arguments. You need to pass the SSID and the password of the Access point you wish to connect.You need to pass the arguments as character arrays or strings with a lower case s.
The possible return values are
WL_CONNECTED after successful connection is established with the Access Point
WL_IDLE_STATUS when Wi-Fi is in process of changing between statuses
WL_NO_SSID_AVAIL in case configured SSID cannot be reached
WL_CONNECT_FAILED if password is incorrect
WL_DISCONNECTED if module is not configured in station mode
The status function in the WiFi class, doesn’t take any arguments but it returns stuff depending on the status of the network that you’re connected to.
Usually, first, you call WiFi.begin, you pass the SSID and the password because you’re trying to establish a connection with the network. Then, what you do is you wait in a loop until WiFi.status returns the value WL_CONNECTED.
while (WiFi.status() != WL_CONNECTED)
WiFi.SSID doesn’t take any arguments but it returns the name of the SSID that you’re connected to.
Returns the current signal strength in dB
Returns the number of discovered networks.It will not list the network names , but returns the number of networks.
Returns the MAC address of the ESP device
There is a specific function available to print out key Wi-Fi diagnostic information:
Here is a sample sketch to demonstrate the WiFi library.
//disconnect any previous connections
//scan for number of nearby networks & print SSIDs
Serial.print("Nearby networks found :");
Serial.println("List of surrounding Network SSIDs…:");
int n = WiFi.scanNetworks();
for (int i = 0; i < n; i++)
//connect to preferred SSID
while (WiFi.status() != WL_CONNECTED)
// print SSID name,local IP allotted ,MAC address & signal strength
Serial.print("Connected to SSID : ");
Serial.print("IP address allotted to ESP : ");
Serial.print("MAC Address of ESP : ");
Serial.print("Signal strength is : ");
The screenshot shows the result in the serial monitor of Arduino IDE
The Client & Server classes & their methods are shown in screen shots below :
Download PDF tutorial file for the WiFi library from