widget to control a Relay

Lorenzo Scavarda il 22 Lug 2023
Ultime attività Replicato da Christopher Stapels il 24 Lug 2023

Good morning,
I am working on a smart garden project with an Arduino MKR WIFI 1010 and I am using Thingspeak as dashboard for monitoring some quantities (e.g. temperature, humidity, moisture, etc.). However, I would like to add a widget in order to change the state of my relay connected to the waterpump, for example if the variable "waterpump_state" is 0 turn off the pump, otherwise turning on. Do you think is it possible to implement it on Thingspeak? Among the predefined widget I have not found any useful in this sense.
Thanks in advance,
Lorenzo
Christopher Stapels
Christopher Stapels il 24 Lug 2023
Totally possible. You can build the button in a private plugin, using the plugins app.
Here is sample javascript.
function loadXMLDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "https://api.thingspeak.com/update?api_key=Y0A92ZT5UMPKK3N1&field1=36&field2=255&field3=250&field4=254&field5=0&field6=3600&field7=0&field8=9", true);
xhttp.send();
}
function loadXMLDocOff() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "https://api.thingspeak.com/update?api_key=Y0A92ZT5UMPKK3N1&field1=0&field2=0&field3=0&field4=20&field5=0&field6=3600&field7=0&field8=9", true);
xhttp.send();
}
</script>
with this very simple html section
<html>
<head>
<!--
NOTE: This plugin will not be visible on public views of a channel.
If you intend to make your channel public, consider using the
MATLAB Visualization App to create your visualizations.
-->
</head>
<body>
<div id="demo"></div>
<div id="button"><button type="button" onclick="loadXMLDoc()">Lights On</button>
</div>
<div id="offbutton"><button type="button" onclick="loadXMLDocOff()">Lights Off</button>
</div>
</body>
</html>