#include <WiFi.h>
#include <ArduinoJson.h>
#include <esp_camera.h>
const char* ssid = "火星种水稻";
const char* password = "bs12346578";
//wifi热点设置
const char* hssid = "my_esp32";
const char* hpassword = "1234567890";
const char* server = "ser.ok8.top";
const int serverPort = 80;
void connectWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("正在连接wifi...");
}
Serial.println("WiFi 连接成功!");
Serial.print("IP地址: ");
Serial.println(WiFi.localIP());
delay(1000);
}
void setup() {
Serial.begin(9600);
//Serial.setTimeout(5000); // 设置串口通信超时时间
connectWiFi();
}
void loop() {
WiFiClient client;
if (client.connect(server, serverPort)) {
Serial.println("连接到服务器成功!");
String httpRequest;
httpRequest.concat("GET /test/board/index HTTP/1.1\r\n");
httpRequest.concat("Host: ");
httpRequest.concat(server);
httpRequest.concat("\r\n");
httpRequest.concat("Connection: close\r\n");
httpRequest.concat("Content-Type: application/json\r\n");
httpRequest.concat("Accept: application/json\r\n");
httpRequest.concat("\r\n");
client.print(httpRequest);
while (client.connected()) {
if (client.available()) {
//String response = client.readString();
//Serial.println(response);
String response = client.readString();
// 只打印返回的 JSON
Serial.println(response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1));
}
}
client.stop();
Serial.println("请求完成");
} else {
Serial.println("无法连接到服务器");
}
delay(10000);
}