Skip to content

Commit

Permalink
Adding cheerlights.js file
Browse files Browse the repository at this point in the history
  • Loading branch information
nothans authored Dec 9, 2021
1 parent 1caa0d1 commit a9a5f8e
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions cheerlights.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// CheerLights client library for connecting to thingspeak.com

// namespace for cheerlights
var CheerLights = {
// base domain of thingspeak
domainThingSpeak: 'https://api.thingspeak.com/',
// thingspeak channel for cheerlights
channelThingSpeak: 1417
};

// get the latest color value
CheerLights.getColor = function(callback) {

// contruct URL for thingspeak
var url = CheerLights.domainThingSpeak + 'channels/' + CheerLights.channelThingSpeak + '/feeds/last.json';

// send color request to thingspeak
CheerLights.ajaxColorFromThingSpeak(url, callback);

}

// get channel data from thingspeak
CheerLights.ajaxColorFromThingSpeak = function(url, callback) {

// set up new request
const request = new XMLHttpRequest();
request.open('GET', url, true);
request.send();

// callback when the response is received
request.onload = function() {
// successful response
if (request.status >= 200 && request.status < 400) {
// get response text
response = request.responseText;

// parse response
response = JSON.parse(response);

// set color values
const color = {
htmlName: response.field1,
hexValue: response.field2
};

// execute the callback if it is a function
if (typeof callback === 'function') { callback(color); }
}
};

}

0 comments on commit a9a5f8e

Please sign in to comment.