I've been trying to run the following esp32 LED PWM code to change the LED's brightness, and show the current PWM duty on the webpage, but the PWM values on the webpage won't change until I hit the submit button twice.
#include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h>
WebServer ser(80);
int s=0;
String k;
String iso()
{
k="<p style=\"text-align: center;\"><strong><font size=\"9\">LED PWM</font></strong><font size=\"3\"></font><FORM action=\"/\" method=\"post\"><input type=\"text\" name=\"duty\"><br> <input type=\"submit\" value=\"Submit\">Cureent Duty=";
k=k+s;
k=k+"</p>";
return k;
}
void root()
{
iso();
ser.send(200, "text/html", iso());
if (ser.hasArg("duty"))
{
iso();
s=ser.arg("duty").toInt();
ledcWrite(4, s);
iso();
ser.send(200, "text/html", iso());
}
}
void setup() {
Serial.begin(9600);
ledcAttachPin(33, 4);
ledcSetup(4, 5000, 8);
WiFi.begin("ssid", "pass");
Serial.print("\nConnecting To WiFi");
while (WiFi.status()!=WL_CONNECTED)
{
Serial.printf("\n.");
}
Serial.print("\nConnected");
ser.on("/", root);
ser.begin(80);
}
void loop() {
ser.handleClient();
}
Here's a GIF addressing the issue
