Skip to content

Instantly share code, notes, and snippets.

@jonathanhculver
Created June 24, 2013 03:34
Show Gist options
  • Select an option

  • Save jonathanhculver/5847600 to your computer and use it in GitHub Desktop.

Select an option

Save jonathanhculver/5847600 to your computer and use it in GitHub Desktop.
wifi lamp JS
window.onload = function() {
/* ip address of arduino */
var URL = "00.0.0.0";
var lightDiv = $('#lightbulbContainer');
/* find out current state of the light and adjust the UI accordingly */
$.ajax({
type: "GET",
url: URL+"/led=-1"
}).done(function(response) {
lightObj = $.parseJSON(''+response+'');
lightDiv.addClass('light'+lightObj.status);
});
/* click event to change light */
lightDiv.bind('click', changeLightDiv = function () {
var status = lightDiv.hasClass('lighton') ? 'on' : 'off';
if(status=='on') {
lightDiv[0].className='lightoff';
changeLightBulb('0');
} else {
lightDiv[0].className='lighton';
changeLightBulb('1');
}
});
/* tell the light to turn on or off */
function changeLightBulb(status) {
$.ajax({
type: "GET",
url: URL+"/led="+status
}).done(function(response) {
responseObj = $.parseJSON(''+response+'');
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment