#include #include #include // ===== WiFi 配置 ===== const char* ssid = "HGN_MATE"; const char* password = "18626876203"; // ===== LED 配置 ===== #define LED_PIN D4 // XIAO ESP32C3 的 D4 引脚 (GPIO4) #define NUM_LEDS 8 // 关键修改:灯带上有8颗灯珠 Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800); WebServer server(80); int currentR = 0, currentG = 0, currentB = 0; // 设置整条灯带的颜色(所有灯珠同时变色) void setColor(int r, int g, int b) { for (int i = 0; i < NUM_LEDS; i++) { strip.setPixelColor(i, strip.Color(r, g, b)); } strip.show(); currentR = r; currentG = g; currentB = b; Serial.printf("All %d LEDs set to RGB(%d, %d, %d)\n", NUM_LEDS, r, g, b); } // ===== 网页内容(与之前相同,无需修改)===== const char index_html[] PROGMEM = R"rawliteral( ESP32 LED Control

ESP32 XIAO C3 Color Control

Color

R: G: B:
Send Color
)rawliteral"; // ===== HTTP 请求处理 ===== void handleColor() { if (server.hasArg("r") && server.hasArg("g") && server.hasArg("b")) { int r = server.arg("r").toInt(); int g = server.arg("g").toInt(); int b = server.arg("b").toInt(); r = constrain(r, 0, 255); g = constrain(g, 0, 255); b = constrain(b, 0, 255); setColor(r, g, b); server.send(200, "text/plain", "OK"); } else { server.send(400, "text/plain", "Missing parameters"); } } void handleRoot() { server.send(200, "text/html", index_html); } void handleNotFound() { server.send(404, "text/html", "

404 Not Found

"); } // ===== 初始化 ===== void setup() { Serial.begin(115200); delay(500); strip.begin(); strip.show(); // 初始全灭 setColor(0, 0, 0); // 连接 WiFi WiFi.begin(ssid, password); Serial.print("Connecting to WiFi"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi connected!"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); server.on("/", handleRoot); server.on("/color", handleColor); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); } void loop() { server.handleClient(); delay(10); }