ESP8266: Connecting to a WiFi Network

Introduction

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

Being able to connect the device to a WiFi network is a very powerful feature, which opens up the possibility of using Internet protocols to communicate with other devices. For example, once we are connected to a network, we can perform HTTP requests to a remove server, allowing us to publish, for example, sensor measurements collected by the ESP8266.

This makes the ESP8266 a very good microcontroller to implement Internet of Things applications. Other important factor associate with the WiFi connectivity is the cheap cost of the device.

The code

First of all, we include the ESP8266WiFi.h 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 will define two global variables to hold the credentials necessary 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 Arduino 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 defined 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) {
 
  delay(1000);
  Serial.println("Connecting..");
 
}

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].

Serial.println(WiFi.localIP());

The complete setup function is shown below.

void setup (){
 
  Serial.begin(115200);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
 
    delay(1000);
    Serial.println("Connecting..");
 
  }
 
  Serial.println(WiFi.localIP());
 
}

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.

Suggested ESP8266 readings

References

[1] http://compnetworking.about.com/cs/wireless/g/bldef_ssid.htm
[2] http://www.computerhope.com/jargon/i/internip.htm

Technical details

ESP8266 libraries: v2.3.0

15 thoughts on “ESP8266: Connecting to a WiFi Network”

  1. Pingback: ESP8266: Connecting to MQTT broker | techtutorialsx

  2. Pingback: ESP8266: Connecting to MQTT broker | techtutorialsx

  3. Pingback: ESP8266: Adding Swagger UI to REST API | techtutorialsx

  4. Pingback: ESP8266: Adding Swagger UI to REST API | techtutorialsx

  5. Pingback: Arduino i IoT: Connectar-se a una xarxa WiFi amb ESP8266 – INSTILAB.cat

  6. Pingback: Arduino i IoT: Connectar-se a una xarxa WiFi amb ESP8266 – INSTILAB.cat

    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

    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

%d bloggers like this: