En este proyecto se tiene como objetivo la de controlar un led mediante los pines RGB, donde se tiene la intensidad de los led Rojo, Verde y Azul integrados en un solo led.
/*
Creado: Luis Alvarez (edualv1723@gmail.com)
https://alvelectronics.com
*/
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "TuSSID";
const char* password = "TuPassword";
WebServer server(80);
// Pines RGB
const int redPin = 14;
const int greenPin = 12;
const int bluePin = 27;
void setColor(int r, int g, int b) {
// Para LED Ánodo Común → invertir valor PWM
ledcWrite(0, 255 - r);
ledcWrite(1, 255 - g);
ledcWrite(2, 255 - b);
}
void handleRoot() {
String html = "Control RGB ";
html += "Control de LED RGB - ESP32
";
html += "";
html += "";
server.send(200, "text/html", html);
}
void handleSetColor() {
if (server.hasArg("hex")) {
String hex = server.arg("hex");
long number = strtol(&hex[1], NULL, 16);
int r = number >> 16;
int g = (number >> 8) & 0xFF;
int b = number & 0xFF;
setColor(r, g, b);
Serial.printf("Color: R=%d, G=%d, B=%d\n", r, g, b);
}
server.send(200, "text/plain", "Color cambiado");
}
void setup() {
Serial.begin(115200);
// Configurar PWM en 3 canales
ledcSetup(0, 5000, 8);
ledcSetup(1, 5000, 8);
ledcSetup(2, 5000, 8);
ledcAttachPin(redPin, 0);
ledcAttachPin(greenPin, 1);
ledcAttachPin(bluePin, 2);
setColor(0, 0, 0); // Apagado inicial
WiFi.begin(ssid, password);
Serial.print("Conectando a WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Conectado, IP: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/setColor", handleSetColor);
server.begin();
Serial.println("Servidor HTTP iniciado");
}
void loop() {
server.handleClient();
}