I’m hoping someone can help me with this, as I’m pretty new to html etc.
I’m trying to create two buttons that can turn a remote light ON
and OFF
, but also reflect their status.
- To either turn on or off, the light has two specific http API calls..
Turn On = http://192.168.102.22:3480/data_request?id=action&output_format=xml&DeviceNum=110&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1
Turn Off = http://192.168.102.22:3480/data_request?id=action&output_format=xml&DeviceNum=110&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0
I tried the following, but was unsuccessful..
<button type="button"><iframe src="192.168.102.22:3480/data_request?id=action&output_format=xml&DeviceNum=110&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1" style="visibility:hidden;display:none"></iframe>On</button>
<button type="button"><iframe src="192.168.102.22:3480/data_request?id=action&output_format=xml&DeviceNum=110&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0" style="visibility:hidden;display:none"></iframe>Off</button>
- And then to confirm the status of the light, (if ON
1
is returned, if OFF0
is returned) and the API call for that is.
Status = http://192.168.102.22:3480/data_request?id=variableget&DeviceNum=110&serviceId=urn:upnp-org:serviceId:SwitchPower1&Variable=Status
The challenge is I don’t want any of the api urls to be called and a new web page opened, all of this should really occurs behind the scenes.
In addition to that, I’m looking to generate these buttons dynamically via a Lua script, so I need to be able to write the on/off button code via a loop into a cell of a table, incrementing the DeviceNum=110
value as I go. (I think I can do the Lua part, but not the html aspects)
All help/advice is appreciated..
UPDATE:
Here’s my progress so far, just keep in mind that this code will be created via a Lua script , so where possible things need to be consistent so I can create much of it via a loop call against a table. If there’s an easier route someone can think of, please let me know..
<html>
<head>
<title>Home Energy Usage</title>
</head>
<script>
function loadDoc11a() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = xhttp.responseText;
console.log("ok"+response);
}
};
xhttp.open("GET", "http://192.168.102.22:3480/data_request?id=action&output_format=xml&DeviceNum=11&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1", true);
xhttp.send();
}
function loadDoc11b() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("POST", "http://192.168.102.22:3480/data_request?id=action&output_format=xml&DeviceNum=11&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0", true);
xhttp.send();
}
function loadDoc113a() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = xhttp.responseText;
console.log("ok"+response);
}
};
xhttp.open("GET", "http://192.168.102.22:3480/data_request?id=action&output_format=xml&DeviceNum=113&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1", true);
xhttp.send();
}
function loadDoc113b() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("POST", "http://192.168.102.22:3480/data_request?id=action&output_format=xml&DeviceNum=113&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0", true);
xhttp.send();
}
function loadDoc231a() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = xhttp.responseText;
console.log("ok"+response);
}
};
xhttp.open("GET", "http://192.168.102.22:3480/data_request?id=action&output_format=xml&DeviceNum=113&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1", true);
xhttp.send();
}
function loadDoc231b() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("POST", "http://192.168.102.22:3480/data_request?id=action&output_format=xml&DeviceNum=110&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0", true);
xhttp.send();
}
</script>
<body>
<table class=custom>
<tr class=custom>
<th class=custom>Energy Sensor</th>
<th class=custom>Wattage</th>
<th class=custom>Control</th>
<th class=custom>Status</th>
</tr>
<br/>
<tr class=custom>
<td class=custom>Living Room Media</td>
<td class=custom>54.1</td>
<td class=custom>
<button type="button" onclick="loadDoc11a()">On</button>
<button type="button" onclick="loadDoc11b()">Off</button></td>
<td class=custom>0</td>
</tr>
<tr class=custom>
<td class=custom>Kitchen Energy</td>
<td class=custom>31.4</td>
<td class=custom>
<button type="button" onclick="loadDoc113a()">On</button>
<button type="button" onclick="loadDoc113b()">Off</button></td>
<td class=custom>1</td>
</tr>
<tr class=custom>
<td class=custom>Office Energy</td>
<td class=custom>11.1</td>
<td class=custom>
<button type="button" onclick="loadDoc231a()">On</button>
<button type="button" onclick="loadDoc231b()">Off</button></td>
<td class=custom>1</td>
</tr>
</body>