WIFI CONTROL RGB LED

#include <WiFi.h>

// Configuration
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const uint16_t port = 80;
const int redPin = 13;   // GPIO13 for red
const int grnPin = 12;   // GPIO12 for green
const int bluPin = 14;   // GPIO14 for blue

WiFiServer server(port);

void setup() {
  Serial.begin(115200);
  
  // Set RGB pins as PWM outputs
  ledcSetup(0, 5000, 8);  // 5kHz PWM, 8-bit resolution
  ledcSetup(1, 5000, 8);
  ledcSetup(2, 5000, 8);
  ledcAttachPin(redPin, 0);
  ledcAttachPin(grnPin, 1);
  ledcAttachPin(bluPin, 2);

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected! IP: " + WiFi.localIP().toString());
  server.begin();
}

void loop() {
  WiFiClient client = server.available();
  if (client) {
    String req = client.readStringUntil('\r');
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println();
    client.println("<h1>RGB Control Active</h1>");
    client.stop();

    // Parse color commands (e.g., "R255G100B50")
    if (req.indexOf("R") != -1) {
      int r = req.substring(req.indexOf("R")+1, req.indexOf("G")).toInt();
      int g = req.substring(req.indexOf("G")+1, req.indexOf("B")).toInt();
      int b = req.substring(req.indexOf("B")+1).toInt();
      ledcWrite(0, r);
      ledcWrite(1, g);
      ledcWrite(2, b);
    }
  }
}
滚动至顶部