esp8266

ESP8266 Tutorial Part 2 – Connect to WiFi and Download from the Internet





You can download our e-book ‘Learn Arduino’ from this link

In part one of this series, we gave an introduction to the ESP8266, setting the IDE and the circuitry needed to get started. In this post, we will go through some basic software capabilities particularly connecting to a WiFi network, and establishing a connection to a web server to download data.

The ESP8266 is not an Arduino, it is only WiFi to serial device turned into a powerful Arduino compatible device. Thanks to a great number of people, this device can now be programmed using the Arduino IDE and almost all of its basic functionality is readily available through a library. In principle, the ESP8266 cannot replace the Arduino board for all projects, primarily due to higher power consumption and the lack of an advanced ADC. The ESP board has only 1 ADC with values from 0 to 1 volt. This pin is NOT available on the ESP-01.

Let me introduce you to the ESP board using some actual code. :)

The device can act as a WiFi station (like your laptop) or as a Access Point. And if you want to make things really fun, try mode 3… In mode 3 the esp8266 turns into both an AP and a Station. That means, the ESP can be connected to an AP while listening for WiFi clients at the same time.

Thanks to the ESP8266 library, these features are exposed as normal Arduino library functions. Lets begin by establishing a connection to an AP using our little ESP.

We will use a modified version of the example called WifiClient.ino in the examples provided by the library. In our example we will access an RSS feed. Any RSS feed you want :)

#include 

const char* ssid     = "your-ssid";
const char* password = "your-password";

const char* host = "feeds.reuters.com"; ///reuters/topNews

First we begin by called the library definition so we can use the functions provided by the ESP8266 Android library. The next two lines are your WiFi SSID (the name of your WiFi) and the password. The host is the hostname you intend to get the feed from, just the hostname; the path to the page will be requested later.

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}





The first few lines are normal Serial.println. The interesting part begins at line WiFi.begin(ssid, password); Here we are instructing the ESP to connect to the network. Next we wait for a connection. We check the WiFi.status for the connection status. When the result is equal to

‘WL_CONNECTED’ we stop searching.

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

then we print the obtained IP address with Serial.println(WiFi.localIP());

Great!! We are now connected to the network. Now lets download some data.

void loop() {
  delay(5000);

  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

In the loop() we create the connection, send the HTTP headers and retrieves data from the server. We create the connection by invoking

client.connect(host, httpPort);

The connection is placed inside an if statement because the connect function returns boolean. If a connection is established the if statement is skipped.

// We now create a URI for the request
  String url = "/reuters/topNews"

  Serial.print("Requesting URL: ");
  Serial.println(url);

 // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }

The function client.print sends the HTTP request to the server (very simple request but it should work 😛 ). We then check if the client sends some data if not we wait 5000 milliseconds; if no data is sent after 5000 milliseconds the connection is broken.

// Read all the lines of the reply from server and print them to Serial

  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("closing connection");
}





Finally, we check if the client object (the connection) is still available, then we read the response line by line

So that was a very basic method of how to download some data from a website via TCP and HTTP protocol and port 80

Stay tuned for next article as we will be running a simple web server, serving a website on our ESP8266 :)

Happy Coding

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *