官术网_书友最值得收藏!

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\r\n" +
"Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
  delay(10);

// 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");
  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:

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

主站蜘蛛池模板: 育儿| 花莲市| 蒙城县| 南投市| 九龙县| 林周县| 谢通门县| 弥勒县| 方城县| 乾安县| 玉田县| 扶风县| 吴江市| 苏尼特左旗| 龙里县| 鄢陵县| 鄂托克前旗| 正宁县| 五莲县| 铜川市| 陈巴尔虎旗| 襄樊市| 历史| 五指山市| 兴国县| 罗平县| 崇明县| 萨嘎县| 厦门市| 孝感市| 遵义县| 平山县| 株洲市| 华亭县| 陆河县| 伊吾县| 农安县| 宜城市| 梅河口市| 莱阳市| 潜江市|