-
Notifications
You must be signed in to change notification settings - Fork 0
/
light.html
118 lines (94 loc) · 3.78 KB
/
light.html
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
108
109
110
111
112
113
114
115
116
117
118
<!DOCTYPE html>
<html>
<head>
<title>Let's Dance</title>
</head>
<body>
<h2>Dancing Lights</h2>
<button type="button" onclick="loadLight(true)">Change!</button>
<button type="button" onclick="loadLight(false)">Stop!</button>
<button type="button" onclick="loadListen(true)">Listen!</button>
<button type="button" onclick="loadMagic()">MAGIC!</button>
<button type="button" onclick="loadDark()">Off!</button>
<script>
function loadListen(s) {
var currentVolume =0;
//Audio code from https://codepen.io/travisholliday/pen/gyaJk?editors=1111
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({
audio: true
},
function(stream) {
audioContext = new AudioContext();
analyser = audioContext.createAnalyser();
microphone = audioContext.createMediaStreamSource(stream);
javascriptNode = audioContext.createScriptProcessor(2048, 1, 1);
analyser.smoothingTimeConstant = 0.8;
analyser.fftSize = 1024;
microphone.connect(analyser);
analyser.connect(javascriptNode);
javascriptNode.connect(audioContext.destination);
javascriptNode.onaudioprocess = function() {
var array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(array);
var values = 0;
var length = array.length;
for (var i = 0; i < length; i++) {
values += (array[i]);
}
var average = values / length;
// console.log(Math.round(average - 40));
currentVolume = Math.round(Math.min(254, Math.max(1, average/80*253))); //use 80 for max brightness, making sure it's not out of bounds for bri (1-254) per hue API
} // end fn stream
},
function(err) {
console.log("The following error occured: " + err.name)
});
} else {
console.log("getUserMedia not supported");
}
//send colume to light
listenInt = setInterval(() => {
console.log('changing to '+ currentVolume);
var xhr = new XMLHttpRequest();
xhr.open('PUT', "http://192.168.1.153/api/zORywYzOzLvpRvzIocACSIXt29dTls2Viqaxnc9R/lights/1/state", true);
xhr.send(JSON.stringify({"bri":currentVolume, "transitiontime": 1}));
}, 200);
}
function loadLight(s) {
console.log("so it begins");
var xhr = new XMLHttpRequest();
//xhr.open('GET', "http://192.168.1.153/api/zORywYzOzLvpRvzIocACSIXt29dTls2Viqaxnc9R/lights/1", true);
//xhr.send();
//console.log(xhr.responseText);
xhr.open('PUT', "http://192.168.1.153/api/zORywYzOzLvpRvzIocACSIXt29dTls2Viqaxnc9R/lights/1/state", true); //userID for hue bridge, hardcoded :(
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
var val = Math.floor((Math.random() * 65535) + 1);
xhr.send(JSON.stringify({"bri":255, "transitiontime": 0, "hue": val, "on": true}));
console.log(xhr.responseText);
//xhr.addEventListener("loadend", loadEnd);
if (s==true) this.timeoutID = setTimeout(() => loadLight(true), 50);
else window.clearTimeout(this.timeoutID);
function loadEnd(e) {
//console.log("The transfer finished (although we don't know if it succeeded or not).");
console.log(xhr.responseText);
}
}
function loadDark() {
console.log("so it blacks");
var xhr = new XMLHttpRequest();
xhr.open('PUT', "http://192.168.1.153/api/zORywYzOzLvpRvzIocACSIXt29dTls2Viqaxnc9R/lights/1/state", true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify({"on": false}));
}
function loadMagic() {
var xhr = new XMLHttpRequest();
xhr.open('PUT', "http://192.168.1.153/api/zORywYzOzLvpRvzIocACSIXt29dTls2Viqaxnc9R/lights/1/state", true);
xhr.send(JSON.stringify({"bri":255, "transitiontime": 0, "hue": 26365, "on": true}));
}
</script>
</body>
</html>