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

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.

主站蜘蛛池模板: 建德市| 榆中县| 云龙县| 德江县| 荥经县| 吉木乃县| 九龙城区| 南靖县| 台安县| 涿州市| 东光县| 聂拉木县| 六枝特区| 团风县| 兴化市| 临洮县| 绥阳县| 东平县| 山阴县| 麟游县| 原阳县| 固原市| 乐平市| 南雄市| 吉木乃县| 克东县| 宜黄县| 宝山区| 平江县| 延川县| 灵丘县| 杭州市| 长沙市| 潞西市| 措美县| 呈贡县| 台北市| 喀什市| 历史| 讷河市| 无极县|