分离html并优化

This commit is contained in:
2025-07-09 17:29:39 +08:00
parent 62060e254e
commit 1f790a3ec3
17 changed files with 1020 additions and 1021 deletions

94
data/config.html Normal file
View File

@ -0,0 +1,94 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>ESP32 WiFi 设置</title>
<style>
body {
font-family: 'Segoe UI', sans-serif;
background-color: #f0f2f5;
display: flex;
flex-direction: column;
align-items: center;
padding: 40px;
}
h1 {
color: #333;
margin-bottom: 30px;
}
form {
background-color: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
width: 100%;
max-width: 400px;
}
label {
font-weight: 500;
display: block;
margin: 10px 0 5px;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
}
input[type="submit"] {
width: 100%;
background-color: #1890ff;
color: white;
border: none;
padding: 12px;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s;
}
input[type="submit"]:hover {
background-color: #40a9ff;
}
</style>
<script>
async function saveWiFiSettings() {
const ssid = document.getElementById("ssid").value;
const password = document.getElementById("password").value;
try {
const response = await fetch("/saveWiFi", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: "ssid=" + encodeURIComponent(ssid) + "&password=" + encodeURIComponent(password)
});
const result = await response.json();
alert(result.message || "WiFi 设置保存成功!");
} catch (error) {
alert("保存失败,请重试。");
}
}
</script>
</head>
<body>
<h1>WiFi 设置</h1>
<form onsubmit="event.preventDefault(); saveWiFiSettings();">
<label for="ssid">SSID</label>
<input type="text" id="ssid" name="ssid" required />
<label for="password">密码</label>
<input type="password" id="password" name="password" required />
<input type="submit" value="保存设置" />
</form>
</body>
</html>

View File

@ -0,0 +1,50 @@
<!DOCTYPE html>
<html>
<head>
<title>ESP32 WiFi Setup</title>
<meta charset="UTF-8">
<script>
async function saveWiFiSettings() {
const ssid = document.getElementById("ssid").value;
const password = document.getElementById("password").value;
try {
const response = await fetch("/saveWiFi", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: "ssid=" + encodeURIComponent(ssid) + "&password=" + encodeURIComponent(password)
});
if (response.ok) {
// 解析JSON响应
const result = await response.json();
alert(result.message || "WiFi 设置保存成功!");
} else {
// 尝试解析错误消息
try {
const errorResult = await response.json();
alert(errorResult.message || "保存失败,请重试。");
} catch (e) {
alert("保存失败,请重试。");
}
}
} catch (error) {
console.error("Error:", error);
alert("ok");
}
}
</script>
</head>
<body>
<h1>WiFi 设置</h1>
<form onsubmit="event.preventDefault(); saveWiFiSettings();">
<label for="ssid">SSID:</label><br/>
<input type="text" id="ssid" name="ssid"><br/><br/>
<label for="password">密码:</label><br/>
<input type="password" id="password" name="password"><br/><br/>
<input type="submit" value="保存">
</form>
</body>
</html>

View File

@ -0,0 +1,146 @@
<!DOCTYPE html>
<html>
<head>
<title>ESP32 Data Display</title>
<meta charset="UTF-8">
<script>
async function fetchData() {
const response = await fetch('/data');
if (!response.ok) {
alert('数据获取失败');
return;
}
const data = await response.json();
document.getElementById('activeDuration').innerText = data.duration;
document.getElementById('temperature').innerText = data.temperature;
document.getElementById('humidity').innerText = data.humidity;
document.getElementById('lightA').innerText = data.lightA;
document.getElementById('lightB').innerText = data.lightB;
document.getElementById('wiper').innerText = data.wiper;
document.getElementById('busVoltage').innerText = data.busVoltage;
document.getElementById('shuntVoltage').innerText = data.shuntVoltage;
document.getElementById('current').innerText = data.current;
document.getElementById('power').innerText = data.power;
// 获取灯光状态
const statusResponse = await fetch('/lightStatus');
const statusText = await statusResponse.text();
document.getElementById('lightStatus').innerText = statusText === 'on' ? '开启' : '关闭';
// 获取自动调节状态
const autoAdjustStatusResponse = await fetch('/autoAdjustStatus');
const autoAdjustStatusText = await autoAdjustStatusResponse.text();
document.getElementById('autoAdjustStatus').innerText = autoAdjustStatusText === 'enabled' ? '开启' : '关闭';
}
async function setWiper() {
const wiperValue = document.getElementById('wiperValue').value;
const response = await fetch('/setWiper', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'value=' + encodeURIComponent(wiperValue)
});
if (response.ok) {
alert('Wiper value set successfully!');
} else {
alert('Failed to set Wiper value!');
}
}
async function resetDuration() {
const response = await fetch('/resetDuration', { method: 'POST' });
if (response.ok) {
alert('累计使用时长已清空');
} else {
alert('清空失败');
}
}
async function turnOnLight() {
const response = await fetch('/LightOpen', { method: 'POST' });
if (response.ok) {
updateLightStatus("开启");
} else {
alert('打开失败');
}
}
async function turnOffLight() {
const response = await fetch('/LightClose', { method: 'POST' });
if (response.ok) {
updateLightStatus("关闭");
} else {
alert('关闭失败');
}
}
function updateLightStatus(status) {
document.getElementById('lightStatus').innerText = status;
}
async function enableAutoAdjust() {
const targetLux = document.getElementById('targetLux').value;
if (!targetLux || isNaN(targetLux) || parseInt(targetLux) <= 0) {
alert('请输入有效的正整数作为目标照度');
return;
}
const response = await fetch('/startAutoAdjust', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'target=' + encodeURIComponent(targetLux)
});
if (response.ok) {
alert('已开启自动调节');
} else {
alert('开启失败');
}
}
async function disableAutoAdjust() {
const response = await fetch('/stopAutoAdjust', { method: 'POST' });
if (response.ok) {
alert('已关闭自动调节');
} else {
alert('关闭失败');
}
}
setInterval(fetchData, 1000);
</script>
</head>
<body onload="fetchData()">
<h1>实时数据显示</h1>
<p>累计使用时长: <span id="activeDuration">加载中...</span></p>
<button onclick="resetDuration()">清空累计时长</button>
<p>温度: <span id="temperature">加载中...</span> °C</p>
<p>湿度: <span id="humidity">加载中...</span> %RH</p>
<p>光照强度A: <span id="lightA">加载中...</span> lx</p>
<p>光照强度B: <span id="lightB">加载中...</span> lx</p>
<p>电位器Wiper值: <span id="wiper">加载中...</span></p>
<p>总线电压: <span id="busVoltage">加载中...</span> V</p>
<p>分流电压: <span id="shuntVoltage">加载中...</span> mV</p>
<p>电流: <span id="current">加载中...</span> A</p>
<p>功率: <span id="power">加载中...</span> W</p>
<h2>设置Wiper值</h2>
<input type="range" id="wiperValue" min="0" max="127" step="1" value="2" />
<button onclick="setWiper()">设置</button>
<h2>灯光控制</h2>
<p>灯光状态: <span id="lightStatus">加载中...</span></p>
<button onclick="turnOnLight()">开灯</button>
<button onclick="turnOffLight()">关灯</button>
<h2>自动调节</h2>
<p>状态: <span id="autoAdjustStatus">加载中...</span></p>
<p>目标照度: <input type="number" id="targetLux" min="0" max="100000" step="1" value="2000" /> lux</p>
<button onclick="enableAutoAdjust()">开启</button>
<button onclick="disableAutoAdjust()">关闭</button>
</body>
</html>

264
data/index.html Normal file
View File

@ -0,0 +1,264 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>ESP32 实时监控面板</title>
<style>
body {
font-family: 'Segoe UI', sans-serif;
background-color: #f5f7fa;
color: #333;
padding: 30px;
max-width: 1000px;
margin: auto;
}
h1 {
text-align: center;
color: #1890ff;
margin-bottom: 30px;
}
.row {
display: flex;
flex-wrap: wrap;
gap: 20px;
margin-bottom: 20px;
}
.data-box {
flex: 1;
min-width: 140px;
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 6px rgba(0,0,0,0.05);
padding: 12px;
text-align: center;
}
.data-label {
font-size: 14px;
color: #666;
}
.data-value {
font-size: 20px;
font-weight: bold;
color: #1890ff;
margin-top: 4px;
}
.control-section {
margin-top: 30px;
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 6px rgba(0,0,0,0.05);
}
.control-group {
margin-bottom: 20px;
}
.control-group label {
display: block;
margin-bottom: 8px;
color: #555;
font-weight: 500;
}
.control-group input[type="range"] {
width: 100%;
}
.control-group input[type="number"] {
width: 100px;
padding: 6px;
margin-right: 10px;
}
.wiper-value {
display: inline-block;
font-size: 16px;
color: #1890ff;
font-weight: bold;
margin-left: 10px;
}
button {
padding: 8px 16px;
margin: 5px 5px 0 0;
background-color: #1890ff;
color: white;
border: none;
border-radius: 6px;
font-size: 14px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #40a9ff;
}
.status-inline {
font-weight: bold;
color: #1890ff;
}
</style>
<script>
async function fetchData() {
const res = await fetch('/data');
if (!res.ok) return alert('获取数据失败');
const data = await res.json();
document.getElementById('duration').innerText = data.duration;
document.getElementById('temperature').innerText = data.temperature + ' °C';
document.getElementById('humidity').innerText = data.humidity + ' %RH';
document.getElementById('lightA').innerText = data.lightA + ' lx';
document.getElementById('lightB').innerText = data.lightB + ' lx';
document.getElementById('busVoltage').innerText = data.busVoltage + ' V';
document.getElementById('current').innerText = data.current + ' A';
document.getElementById('power').innerText = data.power + ' W';
document.getElementById('wiperNow').innerText = data.wiper;
const lightStatus = await (await fetch('/lightStatus')).text();
document.getElementById('lightStatus').innerText = lightStatus === 'on' ? '开启' : '关闭';
const autoStatus = await (await fetch('/autoAdjustStatus')).text();
document.getElementById('autoAdjustStatus').innerText = autoStatus === 'enabled' ? '开启' : '关闭';
}
function updateWiperDisplay() {
const val = document.getElementById('wiperValue').value;
document.getElementById('wiperDisplay').innerText = val;
}
async function setWiper() {
const val = document.getElementById('wiperValue').value;
const res = await fetch('/setWiper', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'value=' + encodeURIComponent(val)
});
alert(res.ok ? 'Wiper 设置成功!' : '设置失败');
}
async function resetDuration() {
const res = await fetch('/resetDuration', { method: 'POST' });
alert(res.ok ? '已清空累计时长' : '操作失败');
}
async function turnOnLight() {
const res = await fetch('/LightOpen', { method: 'POST' });
if (res.ok) document.getElementById('lightStatus').innerText = '开启';
else alert('打开失败');
}
async function turnOffLight() {
const res = await fetch('/LightClose', { method: 'POST' });
if (res.ok) document.getElementById('lightStatus').innerText = '关闭';
else alert('关闭失败');
}
async function enableAutoAdjust() {
const val = document.getElementById('targetLux').value;
if (!val || val <= 0 || val > 65535) {
alert('请输入合理的照度1~65535');
return;
}
const res = await fetch('/startAutoAdjust', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'target=' + encodeURIComponent(val)
});
alert(res.ok ? '已开启自动调节' : '开启失败');
}
async function disableAutoAdjust() {
const res = await fetch('/stopAutoAdjust', { method: 'POST' });
alert(res.ok ? '已关闭自动调节' : '关闭失败');
}
setInterval(fetchData, 1000);
</script>
</head>
<body onload="fetchData()">
<h1>📟 ESP32 实时监控面板</h1>
<div class="row">
<div class="data-box">
<div class="data-label">累计使用时长</div>
<div class="data-value" id="duration">--</div>
</div>
<div class="data-box">
<button onclick="resetDuration()">清空累计时长</button>
</div>
</div>
<!-- 温湿度 -->
<div class="row">
<div class="data-box">
<div class="data-label">温度</div>
<div class="data-value" id="temperature">--</div>
</div>
<div class="data-box">
<div class="data-label">湿度</div>
<div class="data-value" id="humidity">--</div>
</div>
</div>
<!-- 光照 -->
<div class="row">
<div class="data-box">
<div class="data-label">光照 A</div>
<div class="data-value" id="lightA">--</div>
</div>
<div class="data-box">
<div class="data-label">光照 B</div>
<div class="data-value" id="lightB">--</div>
</div>
</div>
<!-- 电源信息(已去除分流电压) -->
<div class="row">
<div class="data-box">
<div class="data-label">总线电压</div>
<div class="data-value" id="busVoltage">--</div>
</div>
<div class="data-box">
<div class="data-label">电流</div>
<div class="data-value" id="current">--</div>
</div>
<div class="data-box">
<div class="data-label">功率</div>
<div class="data-value" id="power">--</div>
</div>
</div>
<!-- 控制面板 -->
<div class="control-section">
<div class="control-group">
<label>Wiper 当前值:<span class="status-inline" id="wiperNow">--</span></label>
<input type="range" id="wiperValue" min="0" max="100" value="0" oninput="updateWiperDisplay()" />
<span class="wiper-value" id="wiperDisplay">0</span>
<button onclick="setWiper()">设置</button>
</div>
<div class="control-group">
<label>灯光状态:<span class="status-inline" id="lightStatus">--</span></label>
<button onclick="turnOnLight()">开灯</button>
<button onclick="turnOffLight()">关灯</button>
</div>
<div class="control-group">
<label>自动调节状态:<span class="status-inline" id="autoAdjustStatus">--</span></label>
<input type="number" id="targetLux" min="1" max="65535" value="2000" />
<button onclick="enableAutoAdjust()">开启</button>
<button onclick="disableAutoAdjust()">关闭</button>
</div>
</div>
</body>
</html>