/
Phillips hue control - Favorite color!
107 lines (97 loc) · 3.23 KB
/
Phillips hue control - Favorite color!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//THIS CODE IS BUILT AFTER THE EXAMPLE BY TOM IGOE FOUND AT https://github.com/tigoe/hue-control/tree/master/client-example/public
var url = '128.122.151.172'; // the hub IP address
var username = 'RCeylvI5FQMsab4Fs8VFgqvg8r9NdDRTQdgBiOM-'; // fill in your Hub-given username var resultDiv;
var dimmer;
var lightNumber = 1;
var colorChanger;
var colores = [10000, 20000, 30000, 40000, 50000];
function setup() {
createCanvas(1000, 1000);
background(153);
text ("Enter your name and I will give you your new favorite color",200, 360);
console.log(text);
text ('Dont forget the Capital letter on your name!', 250, 385);
resultDiv = createDiv('Hub response'); // a div for the Hue hub's responses
resultDiv.position(10, 50); // position it
dimmer = createSlider(0, 254, 127) // a slider to dim one light
dimmer.position(300, 250); // position it
dimmer.mouseReleased(changeBrightness); // set a mouseReleased callback function
text ('Change brightness', 300, 280, 500);
colorChanger = createInput(' '); //connect to colores
colorChanger.position(300, 300, 500);
colorButton = createButton("Give me my color pleaaase!!! :)");
colorButton.position(275, 325, 500);
colorButton.mouseClicked(changeBrightness);
connect(); // connect to Hue hub; it will show all light states
}
/*
this function makes the HTTP GET call to get the light data:
HTTP GET http://your.hue.hub.address/api/username/lights/
*/
function connect() {
url = "http://" + url + '/api/' + username + '/lights/';
httpDo(url, 'GET', getLights);
}
/*
this function uses the response from the hub
to create a new div for the UI elements
*/
function getLights(result) {
resultDiv.html(result);
}
function changeBrightness() {
var huechange = 0;
var random = Math.floor(Math.random()*5);
var co = colores[random];
//console.log(co);
if(colorChanger.value() == "Arnab"){
huechange = 10000;
console.log("it's Arnab");
}
if(colorChanger.value() == "Nico"){
huechange = 20000;
console.log("it's Nico");
}
if (colorChanger.value() == "Tom"){
huechange = 30000;
console.log("it's Tom");
}
if (colorChanger.value() == "Tushar"){
huechange = 40000;
console.log("it's Tushar");
}
if (colorChanger.value() == "August"){
huechange = 50000;
console.log("it's August");
if (colorChanger.value() == "Anna") {
huechange = 60000;
console.log("it's Anna")
}
else {
huechange = 35000;
}
}
//random between o and 65535 ------> int(random(65535))
var brightness = this.value(); // get the value of this slider
var lightState = { // make a JSON object with it
bri: brightness,
on: true,
hue: huechange
}
// make the HTTP call with the JSON object:
setLight(lightNumber, lightState);
}
/*
this function makes an HTTP PUT call to change the properties of the lights:
HTTP PUT http://your.hue.hub.address/api/username/lights/lightNumber/state/
and the body has the light state:
{
on: true/false,
bri: brightness
}
*/
function setLight(whichLight, data) {
var path = url + whichLight + '/state/';
var content = JSON.stringify(data); // convert JSON obj to string
httpDo( path, 'PUT', content, 'text', getLights); //HTTP PUT the change
}