-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-web-socket.html
157 lines (155 loc) · 4.15 KB
/
test-web-socket.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE HTML><html>
<head>
<title>ESP Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<style>
html {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
}
h1 {
font-size: 1.8rem;
color: white;
}
h2{
font-size: 1.5rem;
font-weight: bold;
color: #143642;
}
.topnav {
overflow: hidden;
background-color: #143642;
}
body {
margin: 0;
}
.content {
padding: 30px;
max-width: 600px;
margin: 0 auto;
}
.card {
background-color: #F8F7F9;;
box-shadow: 2px 2px 12px 1px rgba(140,140,140,.5);
padding-top:10px;
padding-bottom:20px;
}
.button {
padding: 15px 50px;
font-size: 24px;
text-align: center;
outline: none;
color: #fff;
background-color: #0f8b8d;
border: none;
border-radius: 5px;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
/*.button:hover {background-color: #0f8b8d}*/
.button:active {
background-color: #0f8b8d;
box-shadow: 2 2px #CDCDCD;
transform: translateY(2px);
}
.state {
font-size: 1.5rem;
color:#8c8c8c;
font-weight: bold;
}
</style>
<title>ESP Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
</head>
<body>
<div class="topnav">
<h1>ESP WebSocket Server</h1>
</div>
<!--<img src="http://192.168.0.19:81/stream"/>-->
<div class="content">
<div class="card">
<h2>Output - GPIO 2</h2>
<p class="state">state: <span id="state">%STATE%</span></p>
<p><button id="servo_left" class="button">Servo Left</button></p>
<p><button id="servo_right" class="button">Servo Right</button></p>
<p><button id="servo_off" class="button">Servo Off</button></p>
<p><button id="engine_plus" class="button">Engine Up</button></p>
<p><button id="engine_minus" class="button">Engine Down</button></p>
<p><button id="engine_off" class="button">Engine Off</button></p>
</div>
</div>
<script>
var gateway = `ws://192.168.0.19:82/ws`;
var websocket;
let servoPosition = 90;
window.addEventListener('load', onLoad);
function initWebSocket() {
console.log('Trying to open a WebSocket connection...');
websocket = new WebSocket(gateway);
websocket.onopen = onOpen;
websocket.onclose = onClose;
websocket.onmessage = onMessage; // <-- add this line
}
function onOpen(event) {
console.log('Connection opened');
}
function onClose(event) {
console.log('Connection closed');
setTimeout(initWebSocket, 2000);
}
function onMessage(event) {
var state;
if (event.data == "1"){
state = "ON";
}
else{
state = "OFF";
}
document.getElementById('state').innerHTML = state;
}
function onLoad(event) {
initWebSocket();
initButton();
}
function initButton() {
document.getElementById('servo_right').addEventListener('click', servoRight);
document.getElementById('servo_left').addEventListener('click', servoLeft);
document.getElementById('servo_off').addEventListener('click', servoOff);
document.getElementById('engine_plus').addEventListener('click', enginePlus);
document.getElementById('engine_minus').addEventListener('click', engineMinus);
document.getElementById('engine_off').addEventListener('click', engineOff);
}
function servoRight(){
servoPosition += 25;
console.log(servoPosition);
websocket.send(`servo_rotate ${servoPosition}`);
}
function servoLeft(){
servoPosition -= 35;
console.log(servoPosition);
websocket.send(`servo_rotate ${servoPosition}`);
}
function servoOff(){
servoPosition = 90;
console.log(servoPosition);
websocket.send(`servo_rotate ${servoPosition}`);
}
function enginePlus(){
websocket.send('engine_move 1');
}
function engineMinus(){
websocket.send('engine_move 0');
}
function engineOff(){
websocket.send('engine_move 0.5');
}
</script>
</body>
</html>