写一个有继电器开关选择,蜂鸣器开关选择,彩灯亮度控制,温湿度和光敏采集的页面代码
时间: 2024-02-27 19:53:01 浏览: 19
好的,下面是一个简单的 HTML/CSS/JavaScript 页面代码,可以实现你想要的功能。请注意,这个代码只是一个示例,具体的实现方式可能因为硬件和软件平台的不同而有所不同。你需要根据具体的情况来修改代码和调整参数。
HTML 代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Smart Home Control Panel</title>
<style type="text/css">
body {
font-family: Arial, sans-serif;
background-color: #eee;
}
.container {
width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.3);
}
.row {
margin-bottom: 10px;
}
.col-6 {
display: inline-block;
width: 50%;
vertical-align: top;
}
.switch {
display: inline-block;
position: relative;
width: 60px;
height: 34px;
margin-right: 10px;
}
.switch input {
display: none;
}
.switch label {
display: block;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
cursor: pointer;
background-color: #ddd;
border-radius: 34px;
transition: background-color 0.3s;
}
.switch label:before {
display: block;
position: absolute;
content: "";
width: 30px;
height: 30px;
top: 2px;
left: 2px;
background-color: #fff;
border-radius: 50%;
box-shadow: 0 0 5px rgba(0,0,0,0.3);
transition: left 0.3s;
}
.switch input:checked + label {
background-color: #4caf50;
}
.switch input:checked + label:before {
left: 28px;
}
.slider {
-webkit-appearance: none;
appearance: none;
width: 100%;
height: 10px;
border-radius: 5px;
background-color: #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;
border-radius: 50%;
background-color: #4caf50;
cursor: pointer;
-webkit-transition: .2s;
transition: .2s;
}
.slider::-moz-range-thumb {
width: 20px;
height: 20px;
border-radius: 50%;
background-color: #4caf50;
cursor: pointer;
transition: .2s;
}
.value {
display: inline-block;
margin-left: 10px;
font-weight: bold;
}
.sensor {
display: inline-block;
margin-left: 10px;
font-weight: bold;
color: #888;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-6">
<h3>Relay Control</h3>
<div class="switch">
<input type="checkbox" id="relay-switch">
<label for="relay-switch"></label>
</div>
</div>
<div class="col-6">
<h3>Buzzer Control</h3>
<div class="switch">
<input type="checkbox" id="buzzer-switch">
<label for="buzzer-switch"></label>
</div>
</div>
</div>
<div class="row">
<div class="col-6">
<h3>LED Brightness Control</h3>
<input type="range" min="0" max="100" value="50" class="slider" id="led-slider">
<span class="value" id="led-value">50</span>
</div>
<div class="col-6">
<h3>Sensors Data</h3>
<p>Temperature: <span class="sensor" id="temp-sensor">N/A</span></p>
<p>Humidity: <span class="sensor" id="humidity-sensor">N/A</span></p>
<p>Light: <span class="sensor" id="light-sensor">N/A</span></p>
</div>
</div>
</div>
<script type="text/javascript">
// Relay switch control
var relaySwitch = document.getElementById("relay-switch");
relaySwitch.addEventListener("change", function() {
if (relaySwitch.checked) {
// Send command to turn on relay
console.log("Relay turned on");
} else {
// Send command to turn off relay
console.log("Relay turned off");
}
});
// Buzzer switch control
var buzzerSwitch = document.getElementById("buzzer-switch");
buzzerSwitch.addEventListener("change", function() {
if (buzzerSwitch.checked) {
// Send command to turn on buzzer
console.log("Buzzer turned on");
} else {
// Send command to turn off buzzer
console.log("Buzzer turned off");
}
});
// LED brightness control
var ledSlider = document.getElementById("led-slider");
var ledValue = document.getElementById("led-value");
ledSlider.addEventListener("input", function() {
// Send command to set LED brightness
var brightness = ledSlider.value;
console.log("LED brightness set to " + brightness);
ledValue.textContent = brightness;
});
// Sensor data display
var tempSensor = document.getElementById("temp-sensor");
var humiditySensor = document.getElementById("humidity-sensor");
var lightSensor = document.getElementById("light-sensor");
// Function to update sensor data
function updateSensors() {
// Send command to read sensor data
var temp = 25;
var humidity = 50;
var light = 500;
console.log("Sensor data updated");
// Update sensor data display
tempSensor.textContent = temp + " ℃";
humiditySensor.textContent = humidity + " %";
lightSensor.textContent = light + " lx";
}
// Call updateSensors() every 5 seconds
setInterval(updateSensors, 5000);
</script>
</body>
</html>
```
在这个代码中,我们使用了 HTML、CSS 和 JavaScript 来实现一个带有继电器开关选择、蜂鸣器开关选择、彩灯亮度控制和温湿度光敏采集的页面。其中:
- `switch` 类表示一个开关控件,使用了 CSS3 的 `transition` 和 `transform` 属性来实现平滑的过渡效果。
- `slider` 类表示一个滑块控件,使用了 HTML5 的 `input` 元素和 CSS3 的 `appearance` 属性来实现自定义样式。
- JavaScript 代码中使用了一些 DOM 操作和事件监听,来响应用户的操作和更新传感器数据。在实际应用中,你需要根据具体的情况来修改这些代码,并且与硬件和软件进行适当的交互。