rgb diode esp32
#include
#include
const char* ssid = "Your_SSID";
const char* password = "Your_Password";
const byte DNS_PORT = 53;
const int red_pin = 23;
const int green_pin = 22;
const int blue_pin = 21;
// Setting PWM frequency, channels and bit resolution
const int frequency = 5000;
const int redChannel = 0;
const int greenChannel = 1;
const int blueChannel = 2;
const int resolution = 8;
WebServer webServer(80);
String webpage = ""
"RGB control"
""
""
"";
void handleRoot() {
String red_pin = webServer.arg(0);
String green_pin = webServer.arg(1);
String blue_pin = webServer.arg(2);
if((red_pin != "") && (green_pin != "") && (blue_pin != ""))
{
ledcWrite(redChannel, 1023 - red_pin.toInt());
ledcWrite(greenChannel, 1023 - green_pin.toInt());
ledcWrite(blueChannel, 1023 - blue_pin.toInt());
}
Serial.print("Red: ");
Serial.println(red_pin.toInt());
Serial.print("Green: ");
Serial.println(green_pin.toInt());
Serial.print("Blue: ");
Serial.println(blue_pin.toInt());
Serial.println();
webServer.send(200, "text/html", webpage);
}
void setup() {
ledcSetup(redChannel, frequency, resolution);
ledcSetup(greenChannel, frequency, resolution);
ledcSetup(blueChannel, frequency, resolution);
ledcAttachPin(red_pin, redChannel);
ledcAttachPin(green_pin, greenChannel);
ledcAttachPin(blue_pin, blueChannel);
delay(1000);
Serial.begin(115200);
Serial.println();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
webServer.on("/", handleRoot);
webServer.begin();
}
void loop() {
webServer.handleClient();
}
|