Grabbing the content from a web page

As the last project in this chapter, we are finally going to use the Wi-Fi connection of the chip to grab the content of a page. We will simply use the www.example.com page, as it's a basic page largely used for test purposes.

This is the complete code for this project:

// Import required libraries
#include <ESP8266WiFi.h>

// WiFi parameters
constchar* ssid = "your_wifi_network";
constchar* password = "your_wifi_password";

// Host
constchar* host = "www.example.com";

void setup() {
// Start Serial
Serial.begin(115200);

// 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());
}

int value = 0;

void loop() {

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;
  }

// This will send the request to the server
client.print(String("GET /") + " HTTP/1.1
" +
"Host: " + host + "
" + "Connection: close

");
  delay(10);

// Read all the lines of the reply from server and print them to Serial
while(client.available()){
    String line = client.readStringUntil('
');
Serial.print(line);
  }

Serial.println();
Serial.println("closing connection");
  delay(5000);

}

The code is really basic: we first open a connection to the example.com website, and then send a GET request to grab the content of the page. Using the while(client.available()) code, we also listen for incoming data, and print it all inside the Serial monitor.

You can now copy this code and paste it into the Arduino IDE. Then, upload it to the board using the instructions from Chapter 1, Getting Started with the ESP8266, in the section Connecting Your Module to Your Wi-Fi Network. This is what you should see in the Serial monitor:

Grabbing the content from a web page

This is basically the content of the page, in pure HTML code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.36