web server - Webpage values wont update - Arduino Stack Exchange
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.
WebServer ser(80);
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>";
iso();
ser.send(200, "text/html", iso());
if (ser.hasArg("duty"))
s=ser.arg("duty").toInt();
ledcWrite(4, s);
ledcAttachPin(33, 4);
ledcSetup(4, 5000, 8);
WiFi.begin("ssid", "pass");
while (WiFi.status()!=WL_CONNECTED)
Serial.printf("\n.");
Serial.print("\nConnected");
ser.on("/", root);
ser.begin(80);
ser.handleClient();
Here's a GIF addressing the issue
upvote for visually demonstrating the issue – jsotola Jun 11 at 16:11
When the client sends you a request, you should answer only once. Your root() function attempts to answer twice whenever the request contains the argument “duty”. The client will not handle a duplicate response to a single request.
Here is your root() function:
ser.send(200, "text/html", iso());  // ← first response
ser.send(200, "text/html", iso());  // ← second response
You may notice that the first response was sent before updating the duty cycle.
The proper way to handle the request is to first handle the request parameters, if any, then send the response:
if (ser.hasArg("duty")) {
s = ser.arg("duty").toInt();
ser.send(200, "text/html", iso());  // ← only sent once
As a side note, there is absolutely no need to build the response five times (you have five calls to iso() within root()) when you only need it once!
I'm such an idiot ! ;P thanks a lot. – FOX Jun 11 at 8:29
Not the answer you're looking for? Browse other questions tagged web-server esp32 or ask your own question.
web server - Webpage values wont update - Arduino Stack Exchange
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.
WebServer ser(80);
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>";
iso();
ser.send(200, "text/html", iso());
if (ser.hasArg("duty"))
s=ser.arg("duty").toInt();
ledcWrite(4, s);
ledcAttachPin(33, 4);
ledcSetup(4, 5000, 8);
WiFi.begin("ssid", "pass");
while (WiFi.status()!=WL_CONNECTED)
Serial.printf("\n.");
Serial.print("\nConnected");
ser.on("/", root);
ser.begin(80);
ser.handleClient();
Here's a GIF addressing the issue
upvote for visually demonstrating the issue – jsotola Jun 11 at 1
6:11
When the client sends you a request, you should answer only once. Your root() function attempts to answer twice whenever the request contains the argument “duty”. The client will not handle a duplicate response to a single request.
Here is your root() function:
ser.send(200, "text/html", iso());  // ← first response
ser.send(200, "text/html", iso());  // ← second response
You may notice that the first response was sent before updating the duty cycle.
The proper way to handle the request is to first handle the request parameters, if any, then send the response:
if (ser.hasArg("duty")) {
s = ser.arg("duty").toInt();
ser.send(200, "text/html", iso());  // ← only sent once
As a side note, there is absolutely no need to build the response five times (you have five calls to iso() within root()) when you only need it once!
I'm such an idiot ! ;P thanks a lot. – FOX Jun 11 at 8:29
Not the answer you're looking for? Browse other questions tagged web-server esp32 or ask your own question.