Skip to content

Instantly share code, notes, and snippets.

@solon
Created July 19, 2010 05:08
Show Gist options
  • Save solon/481035 to your computer and use it in GitHub Desktop.
Save solon/481035 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Changing webpage contents in response to a light sensor</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<style type="text/css">
body {
margin:0;
padding:50px;
text-align:center;
background-color:#ccc;
color:#999;
font-family: Georgia, Times, serif;
}
</style>
</head>
<body onload="getOutputFromPachube();">
<script type="text/javascript">
var api_key = 'YOUR_PACHUBE_API_KEY_HERE';
var feed_id = 8909; // change this to your own feed
var my_url = "http://www.pachube.com/api/feeds/"+feed_id+".json?key="+api_key;
var light_threshold = 500; // this might need to be adjusted depending on your light sensor and the resistor value
function getOutputFromPachube() {
$.ajax({
url: my_url,
dataType: 'jsonp',
success: function(obj) {
successCallback(obj, "success");
},
error: function(obj) {
errorCallback(obj, "error");
},
});
}
// This function will be called if our request is successful.
function successCallback(data, strStatus) {
var lightlevel = data.datastreams[0].values[0].value;
if (lightlevel > 500) {
$('body').css({'background-color':'#fff', 'color':'#000'});
document.getElementById("message").innerHTML = "lights on!";
} else {
$('body').css({'background-color':'#000', 'color':'#fff'});
document.getElementById("message").innerHTML = "lights off!";
}
}
function errorCallback(XMLHttpRequest, strStatus, errorThrown) {
// no error handling implemented
}
setInterval(getOutputFromPachube, 10000); // reload every 15 seconds
</script>
<h1 id="message">waiting for data...</h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment