#include <WiFi.h>
#include <FastLED.h>
// Wi-Fi credentials
const char* ssid = "iot07"; // Replace with your Wi-Fi SSID
const char* password = "rpw1@iot"; // Replace with your Wi-Fi password
// LED setup
#define LED_PIN 5 // Data pin connected to the LED
#define NUM_LEDS 30 // Number of LEDs in the strip
CRGB leds[NUM_LEDS];
// Web server setup
WiFiServer server(80);
String header;
// Variables to store RGB values
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
void setup() {
// Serial Monitor for debugging
Serial.begin(115200);
// Initialize FastLED
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.clear();
FastLED.show();
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Start the server
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("New Client.");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
header += c;
if (c == '\n') {
if (currentLine.length() == 0) {
// HTTP response
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// HTML content
client.println("<!DOCTYPE html><html>");
client.println("<head><title>ESP32 RGB LED Control</title></head>");
client.println("<body>");
client.println("<h1>ESP32 RGB LED Control</h1>");
client.println("<p>Use the sliders to set the color of the LED:</p>");
// RGB sliders
client.println("<form action=\"/\">");
client.println("Red: <input type=\"range\" name=\"r\" min=\"0\" max=\"255\" onchange=\"this.form.submit()\"><br>");
client.println("Green: <input type=\"range\" name=\"g\" min=\"0\" max=\"255\" onchange=\"this.form.submit()\"><br>");
client.println("Blue: <input type=\"range\" name=\"b\" min=\"0\" max=\"255\" onchange=\"this.form.submit()\"><br>");
client.println("</form>");
client.println("</body></html>");
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
// Parse the GET request to extract RGB values
if (header.indexOf("GET /?r=") >= 0) {
int rStart = header.indexOf("r=") + 2;
int gStart = header.indexOf("g=") + 2;
int bStart = header.indexOf("b=") + 2;
int andIndex1 = header.indexOf("&", rStart);
int andIndex2 = header.indexOf("&", gStart);
redValue = header.substring(rStart, andIndex1).toInt();
greenValue = header.substring(gStart, andIndex2).toInt();
blueValue = header.substring(bStart).toInt();
Serial.print("Red: "); Serial.println(redValue);
Serial.print("Green: "); Serial.println(greenValue);
Serial.print("Blue: "); Serial.println(blueValue);
// Update LED colors
fill_solid(leds, NUM_LEDS, CRGB(redValue, greenValue, blueValue));
FastLED.show();
}
// Clear the header variable
header = "";
client.stop();
Serial.println("Client disconnected.");
}
}