给我一个html页面中有一个滑条,拖动滑条能控制esp8266灯的亮度的代码
时间: 2024-03-11 09:47:02 浏览: 70
arduino+esp8266LED灯代码
好的,以下是一个简单的HTML页面,其中包含一个滑块和一个按钮,可以通过ESP8266控制灯的亮度:
```html
<!DOCTYPE html>
<html>
<head>
<title>ESP8266 LED Control</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.slidecontainer {
width: 100%;
text-align: center;
margin-top: 20px;
}
.slider {
-webkit-appearance: none;
width: 80%;
height: 10px;
margin-bottom: 10px;
background: #ddd;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
}
.slider:hover {
opacity: 1;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 20px;
height: 20px;
background: #4CAF50;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 20px;
height: 20px;
background: #4CAF50;
cursor: pointer;
}
</style>
</head>
<body>
<h1>ESP8266 LED Control</h1>
<div class="slidecontainer">
<input type="range" min="0" max="100" value="50" class="slider" id="myRange">
<p>LED Brightness: <span id="demo"></span>%</p>
<button onclick="sendData()">Submit</button>
</div>
<script>
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
// Update the current slider value (each time you drag the slider handle)
slider.oninput = function() {
output.innerHTML = this.value;
}
function sendData() {
var xhttp = new XMLHttpRequest();
var brightness = slider.value;
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert("Brightness set to " + brightness + "%");
}
};
xhttp.open("GET", "/setBrightness?brightness=" + brightness, true);
xhttp.send();
}
</script>
</body>
</html>
```
在这个页面中,我们使用了一个 `<input type="range">` 元素来创建一个滑块。通过JavaScript,我们可以监听滑块的输入事件并更新页面上的文本显示。点击“Submit”按钮时,我们将使用XMLHttpRequest对象向ESP8266发送一个GET请求,该请求将设置灯的亮度。ESP8266应该有一个Web服务器,它将响应此请求并控制LED的亮度。请注意,这只是一个示例代码,需要根据实际情况进行修改。
阅读全文