Skip to content

Instantly share code, notes, and snippets.

@marceloandriolli
Last active April 28, 2021 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marceloandriolli/01dddf0345155dabc7ecb0e0de023234 to your computer and use it in GitHub Desktop.
Save marceloandriolli/01dddf0345155dabc7ecb0e0de023234 to your computer and use it in GitHub Desktop.
POC - Sonoff mini DIY - API
<!DOCTYPE html>
<html>
<body>
<h1>Testiong Sonoff mini REST API</h1>
<button id="turnON" type="button">Turn ON</button>
</body>
</html>
<script>
(function () {
var IP = "127.0.0.1";
var port = "5000";
var baseUrl = "http://" + IP + ":" + port + "/"
document.getElementById("turnON").addEventListener('click', TurnOn);
function TurnOn() {
const httpRequest = new XMLHttpRequest();
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
const json = JSON.stringify({
deviceid: "100000140e",
data: {
switch: "on"
}
});
httpRequest.open("POST", baseUrl + "zeroconf/switch")
httpRequest.setRequestHeader('Content-Type', 'application/json');
httpRequest.send(json);
httpRequest.onload = (e) => {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
var response = JSON.parse(httpRequest.responseText);
if (response.error === 0) {
alert("ON");
}
}
} else {
alert("ERROR")
}
}
}
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment