Программисты Html
Anonymous
Тастота HTML Статус
Сообщение
Anonymous » 26 июл 2025, 03:17
Я хотел создать отдельный HTML -файл, который я могу открыть в своем браузере. Он должен показать состояние подключения питания Tasmota на HTML -странице, код ниже: < /p>
Код: Выделить всё
SP111
body {
font-family: sans-serif;
background-color: #f9f9f9;
text-align: center;
padding-top: 60px;
}
h1 {
font-size: 28px;
margin-bottom: 20px;
}
#status {
font-size: 24px;
margin: 20px;
color: #444;
}
button {
font-size: 18px;
padding: 10px 25px;
margin: 5px;
cursor: pointer;
}
Status SP111
Unknown
Refresh status
Turn ON
Turn OFF
const ip = "192.168.0.101"; // Tasmota ip address
async function getStatus() {
const url = `http://${ip}/cm?cmnd=Power`;
const statusDiv = document.getElementById("status");
statusDiv.textContent = "Connecting...";
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json();
if ("POWER" in data) {
statusDiv.textContent = `Status: ${data.POWER}`;
statusDiv.style.color = data.POWER === "ON" ? "green" : "red";
} else {
statusDiv.textContent = "Unknown answer format";
statusDiv.style.color = "gray";
}
} catch (error) {
console.error("Error:", error);
statusDiv.textContent = "Error: " + error.message;
statusDiv.style.color = "gray";
}
}
async function switchPower(state) {
const url = `http://${ip}/cm?cmnd=Power%20${state}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json();
console.log(`State after change`, data);
getStatus(); // refresh status after change
} catch (error) {
alert("Cannot change status: " + error.message);
}
}
// Optional: automatically get status after loading the page
window.addEventListener("load", getStatus);
Команда
http://192.168.0.101/cm?cmnd=power работает так же, как и ожидалось, я могу читать статус в браузере.
Но код выше не работает. Я предполагаю, что проблема с CORS? Или мне нужно что -то изменить в настройках?>
Подробнее здесь:
https://stackoverflow.com/questions/797 ... tml-status
1753489055
Anonymous
Я хотел создать отдельный HTML -файл, который я могу открыть в своем браузере. Он должен показать состояние подключения питания Tasmota на HTML -странице, код ниже: < /p> [code] SP111 body { font-family: sans-serif; background-color: #f9f9f9; text-align: center; padding-top: 60px; } h1 { font-size: 28px; margin-bottom: 20px; } #status { font-size: 24px; margin: 20px; color: #444; } button { font-size: 18px; padding: 10px 25px; margin: 5px; cursor: pointer; } Status SP111 Unknown Refresh status Turn ON Turn OFF const ip = "192.168.0.101"; // Tasmota ip address async function getStatus() { const url = `http://${ip}/cm?cmnd=Power`; const statusDiv = document.getElementById("status"); statusDiv.textContent = "Connecting..."; try { const response = await fetch(url); if (!response.ok) { throw new Error(`HTTP ${response.status}`); } const data = await response.json(); if ("POWER" in data) { statusDiv.textContent = `Status: ${data.POWER}`; statusDiv.style.color = data.POWER === "ON" ? "green" : "red"; } else { statusDiv.textContent = "Unknown answer format"; statusDiv.style.color = "gray"; } } catch (error) { console.error("Error:", error); statusDiv.textContent = "Error: " + error.message; statusDiv.style.color = "gray"; } } async function switchPower(state) { const url = `http://${ip}/cm?cmnd=Power%20${state}`; try { const response = await fetch(url); if (!response.ok) { throw new Error(`HTTP ${response.status}`); } const data = await response.json(); console.log(`State after change`, data); getStatus(); // refresh status after change } catch (error) { alert("Cannot change status: " + error.message); } } // Optional: automatically get status after loading the page window.addEventListener("load", getStatus); [/code] Команда http://192.168.0.101/cm?cmnd=power работает так же, как и ожидалось, я могу читать статус в браузере. Но код выше не работает. Я предполагаю, что проблема с CORS? Или мне нужно что -то изменить в настройках?> Подробнее здесь: [url]https://stackoverflow.com/questions/79715322/tasmota-html-status[/url]