Код: Выделить всё
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);
Но код выше не работает. Я предполагаю, что проблема с CORS? Или мне нужно что -то изменить в настройках?>
Подробнее здесь: https://stackoverflow.com/questions/797 ... tml-status