-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | ||
} | ||
}; | ||
|
||
} |