更改滑块时,将滑块的值从网页返回到程序



我正在尝试使用滑块对ESP32进行编程以控制LED亮度,并且一直在使用从https://randomnerdtutorials.com/esp32-servo-motor-web-server-arduino-ide/和https://randomnerdtutorials.com/esp32-pwm-arduino-ide/

到目前为止,我可以让ESP32连接到我的网络,显示滑块及其值,当滑块更改时,设置PWM以设置LED亮度。

缺点是,尽管我可以在更改滑块值时在浏览器中显示滑块值,但只有当我释放鼠标按钮时,才能运行滑块值。因此,我得到的不是一个实时控制LED的滑块,而是一个以跳跃方式设置PWM值的滑块。

我想做的是让LED亮度代表滑块上的值,因为滑块发生了变化(在释放鼠标按钮之前(。

我试过oninput和onchange,但没有成功。这并不是因为我在HTML方面有些欠缺。

任何人能给我的建议都将不胜感激。

我的代码如下:-

#include <WiFi.h>
/* Web server initialisation stuff */
/* Set the network credentials to connect to */
const char* ssid     = "MySID";
const char* password = "MySIDPassword";
/* Create a server instance on port 80 (http) */
WiFiServer server(80);
/* Create a variable to store the HTTP request */
String header;
/* Decode HTTP GET value */
String valueString = String(5);
int pos1 = 0;
int pos2 = 0;
/* LED initialisation stuff */
const byte led_pin = 2;
int intBbrightness = 0;
void setup() {
/* Set the serial speed */
Serial.begin(115200);
/* Set up the LED Pin */
ledcAttachPin (led_pin, 0);
/* Set the initial PWM value, the frequency and the nummer of bits for resolution */
ledcSetup(0,5000,8);
/* Connect to Wi-Fi network with SSID and password */
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
/* Print local IP address and start web server */
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();   // Listen for incoming clients
if (client) {                             // If a new client connects,
Serial.println("New Client.");          // print a message out in the serial port
String currentLine = "";                // make a String to hold incoming data from the client
while (client.connected()) {            // loop while the client's connected
if (client.available()) {             // if there's bytes to read from the client,
char c = client.read();             // read a byte, then
Serial.write(c);                    // print it out the serial monitor
header += c;
if (c == 'n') {                    // if the byte is a newline character
/* if the current line is blank, you got two newline characters in a row.
that's the end of the client HTTP request, so send a response: */
if (currentLine.length() == 0) {
/* HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
and a content-type so the client knows what's coming, then a blank line: */
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
/* Display the HTML web page */
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name="viewport" content="width=device-width, initial-scale=1">");
client.println("<link rel="icon" href="data:,">");
/* CSS to style the slider */
client.println("<style>body { text-align: center; font-family: "Trebuchet MS", Arial; margin-left:auto; margin-right:auto;}");
client.println(".slider { width: 300px; }</style>");
client.println("<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>");
/* Web Page */
/* Give the page a header here */
client.println("</head><body><h1>ESP32 with Servo</h1>");
/* Show the slider here */
client.println("<p>Position: <span id="servoPos"></span></p>");          
client.println("<input type="range" min="0" max="256" class="slider" id="servoSlider" onchange="servo(this.value)" value=""+valueString+""/>");
/* Javascript for the slider */
client.println("<script>var slider = document.getElementById("servoSlider");");
client.println("var servoP = document.getElementById("servoPos"); servoP.innerHTML = slider.value;");
client.println("slider.oninput = function() { slider.value = this.value; servoP.innerHTML = this.value; }");
client.println("$.ajaxSetup({timeout:1000}); function servo(pos) { ");
client.println("$.get("/?value=" + pos + "&"); {Connection: close};}</script>");
client.println("</body></html>");     
/*GET /?value=180& HTTP/1.1 */
if(header.indexOf("GET /?value=")>=0) {
pos1 = header.indexOf('=');
pos2 = header.indexOf('&');
valueString = header.substring(pos1+1, pos2);
/* Set the brightness of the LED based on the slider */
ledcWrite(0,valueString.toInt());
Serial.println(valueString); 
}         
/* The HTTP response ends with another blank line */
client.println();
/* Break out of the while loop */
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != 'r') {  // if you got anything else but a carriage return character,
currentLine += c;      // add it to the end of the currentLine
}
}
}
/* Clear the header variable */
header = "";
/* Close the connection */
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}


}

如果你想从网页更新代码的亮度,你必须使用onchange

这是"实时"更新的唯一方法。

我在您的代码中看不到您的updateBrightness函数。你说,如果你释放滑块,你可以改变它。但是,如果没有代码来更改它,该怎么办呢?

如果更改滑块,则需要使用新页面调用ESP。要在这个页面上收听,您必须将其添加到您的设置代码中:例如server.on("/slider", handleSliderChange);

为什么即使使用onchange(),它也不起作用
它不起作用,因为HTML每秒将生成1000个带有新值的请求。您的ESP将永远无法处理这么多请求。

你要做的是只向你的ESP启动一个请求,然后等待,直到ESP回复。如果在收到答案后,滑块值发生了更改,则可以发送下一个值,直到滑块值不再更改为止。

如果在传输新值的过程中,滑块调用onchange,则只需保存新滑块值,而无需向ESP发送请求。

最新更新