/* Set PWM Properties */ // Use first channel of 16 channels (started from zero) #define PWM_channel 0 // Frequency of the PWM in Hz #define PWM_freq 25000 // Resolution of the LEDC timer (in bits) #define PWM_resolution 10 // The duty cycle is the ratio of time a load or circuit is ON compared to the time the load or circuit is OFF. // The max duty cycle value based on PWM resolution (will be 255 if resolution is 8 bits) const int MAX_DUTY_CYCLE = (int)(pow(2, PWM_resolution) - 1); // GPIO Pin of the ESP32 board that connects to the LED strip #define LED_pin 13 float brightness = 0; float value = 0; /* Set WiFi Options */ #include #include #include const char* ssid = "SET HERE"; const char* password = "SET HERE"; // The name of the parameter used in the web interface const char* PARAM_INPUT = "value"; // Set web server port number to 80 AsyncWebServer server(80); // HTML web page to handle the range slider const char index_html[] PROGMEM = R"rawliteral( ESP32 LED Lamp - Adjust brightness

ESP32 LED Lamp - Adjust brightness

%SLIDERVALUE%

)rawliteral"; // Replaces placeholder with button section in your web page String processor(const String& var){ if (var == "SLIDERVALUE") { return String(value); } return String(); }//String processor /////////////////// SETUP ////////////////// void setup() { Serial.begin(9600); delay(10); //////////////////// LED //////////////////////// // Setup a channel with a PWM waveform frequency and duty cycle ledcSetup(PWM_channel, PWM_freq, PWM_resolution); // Attach the LED PWM channel to the GPIO Pin ledcAttachPin(LED_pin, PWM_channel); //////////////////// WIFI ////////////////////// WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); if (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("WiFi Failed!"); return; } Serial.println(""); Serial.println("IP address: "); Serial.println(WiFi.localIP()); // Send web page with input fields to client server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/html", index_html, processor); }); // Send a GET request to /brightness?value= server.on("/brightness", HTTP_GET, [] (AsyncWebServerRequest *request) { String inputMessage; // GET brightness value on /brightness?value= if (request->hasParam(PARAM_INPUT)) { inputMessage = request->getParam(PARAM_INPUT)->value(); value = inputMessage.toFloat(); Serial.print("Value entered: "); Serial.println(value); // Go from 0 - 1 to 0 - 100 brightness = value * 100; if(brightness >= 0 && brightness <= 100) { // Map from 0%-100% value to 0-MAX_DUTY_CYCLE value brightness = map(brightness, 0, 100, 0, MAX_DUTY_CYCLE); } else if (brightness > 100) { brightness = MAX_DUTY_CYCLE; } else { brightness = 0; }//else ledcWrite(PWM_channel, brightness); } else { inputMessage = "unknown"; }//else Serial.print("Brightness: "); Serial.println(brightness); //This below needs to stay on, otherwise the ESP will keep looping over all old values entered request->send(200, "text/html", "HTTP GET request sent to your ESP on input value: " + inputMessage + "
Return to Home Page"); });//server.on // Start server server.begin(); }//void setup void loop() { }//void loop