-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
78 lines (60 loc) · 1.87 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Arduino web interface boilerplate</title>
</head>
<body>
<h3>Toggle arduino led</h3>
<input type="button" value="On" onclick="ledOn();"/>
<input type="button" value="Off" onclick="ledOff();"/>
<script>
var ipAddress = "localhost";
var ipPort = "9000";
var inputMode = "0";
var outputMode = "1";
var analogMode = "2";
var pwmMode = "3";
var socket;
// send/receive arduino commands, see this link for reference
// https://github.com/MrYsLab/pymata-aio/wiki/JSON-API-for-pymata_iot
function ASend(method, params) {
socket.send(JSON.stringify({"method": method, "params": params}));
}
function ARecv(method, params) {
// interact with arduino responses here
console.log(method, params);
}
// setup arduino pins
function setupArduino() {
// Uncomment this to receive read data from Analog pin 0
// ASend("set_pin_mode", ['0', analogMode]);
// setting led pin to output mode
ASend("set_pin_mode", ["13", outputMode]);
}
function connectSocket() {
socket = new WebSocket('ws://' + ipAddress + ':' + ipPort);
socket.onopen = function (event) {
setupArduino();
console.log('Connected to Arduino');
};
socket.onclose = function (event) {
console.log('Connection closed - Reconnecting ...');
window.setTimeout(connectSocket, 2000);
};
socket.onmessage = function (message) {
var msg = JSON.parse(message.data);
ARecv(msg.method, msg.params);
};
}
connectSocket();
// turn the arduino led on or off
function ledOn() {
ASend("digital_write", ["13", "1"]);
}
function ledOff() {
ASend("digital_write", ["13", "0"]);
}
</script>
</body>
</html>