【发布时间】: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