【问题标题】:Returning the value of a slider from a web page to a program on change of slider在更改滑块时将滑块的值从网页返回到程序
【发布时间】:2019-11-15 11:57:13
【问题描述】:

我正在尝试对 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("");
  }




}

【问题讨论】:

    标签: javascript html arduino slider esp32


    【解决方案1】:

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

    这是“实时”更新的唯一方式。

    我在您的代码中没有看到您的 updateBrightness 函数。你说,如果你松开滑块,你可以改变它。但是,如果没有代码可以更改呢?

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

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

    您要做的就是只向您的 ESP 发起 1 个请求并等待,直到 ESP 回答。如果在收到答案后,滑块值发生了变化,您可以发送下一个值,直到滑块值不再变化为止。

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

    【讨论】:

    • 感谢您的指导。到你上面的观点。 1.更新亮度部分不是函数,是/* Set the brightness of the LED based on the slider */下面的两行代码。 2.server.on("/slider/"....代码去哪了?我猜在server.begin 之后的设置中,但我不确定。 3. 根据您在最后 2 段中的描述,这听起来很像您在谈论在滑块值停止更改后获取它,但我正在寻找滑块值,因为它正在更改。还是我只是误读了?非常感谢您的帮助。
    • 您可以在开始之前添加“server.on”。由于您向服务器添加了回调函数(您也可以在 library 文件夹中找到很多示例)。在滑块回调中,每次更改都应该通知您,因此onchange 应该是正确的解决方案。它应该被称为onmousemove,这意味着很多次!
    • 嗯,试过了,但编译不了。还要查看 WiFiServer 类文档,没有 .on 方法,只有 Server(port)。
    • WebServer - 在 ESP32 上,您应该使用github.com/espressif/arduino-esp32/tree/master/libraries/… 创建服务器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    相关资源
    最近更新 更多