forked from lionello/nodemcu-httpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.html
92 lines (77 loc) · 2.32 KB
/
setup.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
<html>
<head>
<script type="text/javascript">
var startTime = new Date();
function get_ip(mac, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://soundswarm.azurewebsites.net/ip?mac='+encodeURIComponent(mac)+"&rnd="+Math.random(), true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var json = xhr.responseText;
if (json) {
callback(null, json);
}
else {
callback("Error "+xhr.statusText);
}
}
};
xhr.send();
}
function poll_for_ip(ssid, mac) {
get_ip(mac, function(err, json) {
document.getElementById('error').innerText = err?err+". Please connect to the "+ssid+" Wi-Fi.":'Waiting for SoundThing to connect...';
if (json) {
var obj = JSON.parse(json);
// if (obj.now && !startTime) {
// startTime = Date.parse(obj.now);
// }
// Check the time, redirect if newer
if (obj.ip && Date.parse(obj.utc) > startTime) {
// Redirect to final site
window.location = "http://"+obj.ip;
}
}
});
}
function submit(e) {
e.target.disabled = true;
var ssid = document.getElementById('ssid').value;
var pw = document.getElementById('pw').value;
var xhr = new XMLHttpRequest();
xhr.open('POST', '/setup.lua?ssid='+ssid+"&pw="+pw, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var mac = xhr.responseText;
if (mac) {
e.target.value = "Reconnecting...";
setInterval(function() {
poll_for_ip(ssid, mac);
}, 2000);
}
}
};
xhr.send('')
}
function init() {
document.getElementById('submit').addEventListener('click', submit);
document.getElementById('ssid').focus();
}
window.addEventListener('load', init);
</script>
</head>
<body>
<h1>Welcome to your SoundThing!</h1>
<p>
<input id='ssid' type='text' placeholder="SSID"/>
</p>
<p>
<input id='pw' type='text' placeholder="Password" autocomplete="off"/>
</p>
<p>
<input id='submit' type='button' value="Reconnect" />
</p>
<div id='error' style="color:red;"></div>
<a href="/index.html">Just wanna drum!</a>
</body>
</html>