#include <ESP8266WiFi.h> const char* ssid = "your_SSID"; // 你的WiFi网络名称const char* password = "your_PASSWORD"; // 你的WiFi密码const char* server = "example.com"; // 你要请求的服务器地址 void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("Connecting to WiFi.."); } Serial.println("Connected to the WiFi network");} void loop() { WiFiClient client; const int httpPort = 80; if (!client.connect(server, httpPort)) { Serial.println("Connection failed"); return; } // 构建HTTP GET请求 String url = "/"; // 你要请求的服务器路径 client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: close\r\n\r\n"); // 读取服务器响应 while(client.available()){ String line = client.readStringUntil('\r'); Serial.print(line); } Serial.println(); Serial.println("closing connection"); delay(5000); // 每隔5秒发送一次HTTP GET请求}
|