- Internet of Things with ESP8266
- Marco Schwartz
- 313字
- 2021-07-14 10:58:55
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.
- 演進(jìn)式架構(gòu)(原書(shū)第2版)
- Learning Chef
- 高效微控制器C語(yǔ)言編程
- Raspberry Pi Networking Cookbook(Second Edition)
- 新手學(xué)Visual C# 2008程序設(shè)計(jì)
- Swift 3 New Features
- Modular Programming in Java 9
- 單片機(jī)應(yīng)用與調(diào)試項(xiàng)目教程(C語(yǔ)言版)
- Learning Concurrency in Kotlin
- Windows Embedded CE 6.0程序設(shè)計(jì)實(shí)戰(zhàn)
- Scratch趣味編程:陪孩子像搭積木一樣學(xué)編程
- Hands-On JavaScript for Python Developers
- JavaScript動(dòng)態(tài)網(wǎng)頁(yè)編程
- Arduino電子設(shè)計(jì)實(shí)戰(zhàn)指南:零基礎(chǔ)篇
- C# 7.0本質(zhì)論