#include <ESP8266WiFi.h>
const char* host = "www.example.com"; / 网络服务器地址
const int httpPort = 80; / http端口80
void setup() {
/设置ESP8266工作模式为无线终端模式
WiFi.mode(WIFI_STA);
/开始连接wifi
WiFi.begin(ssid, password);
wifiClientRequest();
}
void wifiClientRequest(){
/ 建立WiFi客户端对象,对象名称client
WiFiClient client;
/ 建立字符串,用于HTTP请求
String httpRequest = String("GET /") + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"\r\n";
------一、连接网络服务器-------
client.connect(host, httpPort);
client.print(httpRequest); / 向服务器发送HTTP请求
------二、通过串口输出响应信息-------
Serial.println("Web Server Response:");
while (client.connected() || client.available()){
if (client.available()){
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
client.stop(); / 断开与服务器的连接
}