分离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

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>