<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Smart Bulb Controller</title>
</head>
<body>
<h1>Smart Bulb Controller</h1>
<button id="turnOnButton">Turn On Bulb</button>
<button id="turnOffButton">Turn Off Bulb</button>
<script>
// javaScript code ..
const url = 'https://px1.tuyaus.com/homeassistant/skill';
const bulbId = 'd7e1da4388d0f00dcexnm7'; // Bulb's ID
const accessToken = 'INv051h5f7c3bc0in1697945905900oeqpA2XFpXXXXX'; // Access token
const turnOnData = {
payload: {
devId: bulbId,
accessToken: accessToken,
value: '1',
},
header: {
payloadVersion: 1,
name: 'turnOnOff',
namespace: 'control',
},
};
const turnOffData = {
payload: {
devId: bulbId,
accessToken: accessToken,
value: '0',
},
header: {
payloadVersion: 1,
name: 'turnOnOff',
namespace: 'control',
},
};
// Function to send request to turn the bulb on or off
async function controlBulb(data) {
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
const text = await response.text();
console.log(text);
} catch (error) {
console.error('Error:', error);
}
}
// Event listener for the button press to turn the bulb on
document.getElementById('turnOnButton').addEventListener('click', () => {
controlBulb(turnOnData);
});
// Event listener for the button press to turn the bulb off
document.getElementById('turnOffButton').addEventListener('click', () => {
controlBulb(turnOffData);
});
</script>
</body>
</html>