ESP8266: Connecting to a WiFi Network

The objective of this post is to explain how to connect the ESP8266 to a WiFi network, using the ESP8266 libraries for the Arduino IDE.

First of all, we include the ESP8266WiFi library, that will make available a global variable named WiFi, which is an object of class ESP8266WiFiClass. This class has the methods needed to connect to a WiFi network using the ESP8266. You can check the header file here.

#include <ESP8266WiFi.h>

In order to make the code more easily readable, we declare 2 global variables to hold the credentials to register in the network. The first one corresponds to the SSID (Service Set IDentifier), which is the name associated with the wireless network [1] we want to connect to. Naturally, to be able to connect, we also need to specify the network password.

const char* ssid = "yourSSID";
const char* password = "yourPASSWORD";

We will handle the whole connection to the WiFi network on the setup function. We start by creating a Serial connection to print the outputs of the program.

Then, we call the begin method on the WiFi object, passing as arguments the SSID and password variables specified early. This will start the connection to the network.

WiFi.begin(ssid, password);

Then, we call the status method to check when the connection is performed. This is typically done in a loop since the connection process can take some time. The status method will return a variable of type wl_status_t, which is an enumeration. The types defined for this enumeration can be seen here. In this case, we want to check for WL_CONNECTED, which indicate a successful connection to the network.

while (WiFi.status() != WL_CONNECTED) {



After the connection is successfully established, we can check the IP assigned to the ESP8266 by calling the localIP method. This is useful if we want to send data to the ESP8266, from inside this network. Keep in mind that this corresponds to a local IP and thus we cannot contact the device from outside this network using it [2].


The complete setup function is shown below.

void setup (){

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {





This covers just the basics of connecting to a WiFi network. Next posts will explain how to do more advanced procedures, such as sending HTTP requests.



Technical details

ESP8266 libraries: v2.3.0


15 Replies to “ESP8266: Connecting to a WiFi Network”

  1. Pingback: techtutorialsx
    1. Hi! Sorry, unfortunately never used thingsspeak, so I cannot be of much assistance.

      Nonetheless, I think it is a very popular platform and I’ve already stumbled across many articles around the web about it, I’m sure you will be able to find a lot of information.

      If in the future I have some time to explore it, I will make sure to share a post explaining how to do it.

      Best regards,
      Nuno Santos


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s