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) {

  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.


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

Advertisements
This entry was posted in ESP8266, IoT, Microcontrollers and tagged , , , , , , . Bookmark the permalink.

12 Responses to ESP8266: Connecting to a WiFi Network

  1. Pingback: ESP8266: HTTP GET Requests | techtutorialsx

  2. Pingback: ESP8266: POST Requests | techtutorialsx

  3. Pingback: ESP8266: Setting a simple HTTP webserver | techtutorialsx

  4. Pingback: ESP8266 Webserver: Controlling a LED through WiFi | techtutorialsx

  5. Pingback: ESP8266 Webserver: Resolving an address with mDNS | techtutorialsx

  6. Pingback: ESP8266: Query LinkIt Smart mDNS services | techtutorialsx

  7. Pingback: techtutorialsx

  8. Pingback: ESP8266: Posting JSON data to a Flask server on the cloud | techtutorialsx

  9. Pingback: ESP8266: Scanning WiFi Networks | techtutorialsx

  10. Pingback: ESP8266 Webserver: Accessing the body of a HTTP request | techtutorialsx

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

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s