forked from embeddedlab786/Wifi_Robot_AP_Web_Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wifi_Robot_AP_Web_Server.ino
303 lines (267 loc) · 9.31 KB
/
Wifi_Robot_AP_Web_Server.ino
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* https://arduino.esp8266.com/stable/package_esp8266com_index.json
* ESP8266 as Web Server using WiFi Access Point (AP) mode
* Connect to AP "Robot Wifi", password = "87654321"
* Open browser, visit 192.168.4.1
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <Hash.h>
//SSID and Password to your ESP Access Point
const char* ssid = "Robot Wifi";
const char* password = "87654321";
#define ENA 4 // Enable/speed motors Right GPIO4(D2)
#define IN_1 0 // L298N in1 motors Right GPIO0(D3)
#define IN_2 2 // L298N in2 motors Right GPIO2(D4)
#define IN_3 12 // L298N in3 motors Left GPIO12(D6)
#define IN_4 13 // L298N in4 motors Left GPIO13(D7)
#define ENB 15 // Enable/speed motors Left GPIO15(D8)
#define Light 16 // Light GPIO16(D0)
int speedCar = 150; // 0 to 255
char Data;
static const char PROGMEM INDEX_HTML[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0">
<title>ESP8266 Robot</title>
<style>
"body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }"
#JD {
text-align: center;
}
#JD {
text-align: center;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
font-size: 24px;
}
.foot {
text-align: center;
font-family: "Comic Sans MS", cursive;
font-size: 30px;
color: #F00;
}
.button {
border: none;
color: white;
padding: 17px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 12px;
width: 100%;
}
.red {background-color: #F00;}
.green {background-color: #090;}
.yellow {background-color:#F90;}
.blue {background-color:#03C;}
</style>
<script>
var websock;
function start() {
websock = new WebSocket('ws://' + window.location.hostname + ':81/');
websock.onopen = function(evt) { console.log('websock open'); };
websock.onclose = function(evt) { console.log('websock close'); };
websock.onerror = function(evt) { console.log(evt); };
websock.onmessage = function(evt) {
console.log(evt);
var e = document.getElementById('ledstatus');
if (evt.data === 'ledon') {
e.style.color = 'red';
}
else if (evt.data === 'ledoff') {
e.style.color = 'black';
}
else {
console.log('unknown event');
}
};
}
function buttonclick(e) {
websock.send(e.id);
}
</script>
</head>
<body onload="javascript:start();">
<table width="100%" border="1">
<tr>
<td bgcolor="#FFFF33" id="JD">Robot Controller</td>
</tr>
</table>
<table width="100" height="249" border="0" align="center">
<tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr>
<tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr>
<td> </td>
<td align="center" valign="middle"><form name="form1" method="post" action="">
<label>
<button id="F" type="button" onclick="buttonclick(this);" class="button green">Forward</button>
</label>
</form></td>
<td> </td>
</tr>
<tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr>
<td align="center" valign="middle"><form name="form1" method="post" action="">
<label>
<button id="L" type="button" onclick="buttonclick(this);" class="button green">Turn_Left</button>
</label>
</form></td>
<td align="center" valign="middle"><form name="form1" method="post" action="">
<label>
<button id="S" type="button" onclick="buttonclick(this);" class="button red">Stop</button>
</label>
</form></td>
<td align="center" valign="middle"><form name="form1" method="post" action="">
<label>
<button id="R" type="button" onclick="buttonclick(this);" class="button green">Turn_Right</button>
</label>
</form></td>
</tr>
<tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr>
<td> </td>
<td align="center" valign="middle"><form name="form1" method="post" action="">
<label>
<button id="B" type="button" onclick="buttonclick(this);" class="button green">Backward</button>
</label>
</form></td>
<td> </td>
</tr>
<tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr>
<td align="center" valign="middle"><form name="form1" method="post" action="">
<label>
<button id="a" type="button" onclick="buttonclick(this);" class="button blue">Speed Low</button>
</label>
</form></td>
<td align="center" valign="middle"><form name="form1" method="post" action="">
<label>
<button id="b" type="button" onclick="buttonclick(this);" class="button blue">Speed Medium</button>
</label>
</form></td>
<td align="center" valign="middle"><form name="form1" method="post" action="">
<label>
<button id="c" type="button" onclick="buttonclick(this);" class="button blue">Speed High</button>
</label>
</form></td>
</tr>
<tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr> <tr>
<td align="center" valign="middle"><form name="form1" method="post" action="">
<label>
<button id="o" type="button" onclick="buttonclick(this);" class="button yellow">Light On</button>
</label>
</form></td>
<td align="center" valign="middle"><form name="form1" method="post" action="">
</form></td>
<td align="center" valign="middle"><form name="form1" method="post" action="">
<label>
<button id="f" type="button" onclick="buttonclick(this);" class="button yellow">Light Off</button>
</label>
</form></td>
</tr>
</table>
<p class="foot">Muhammad Ansar</p>
</body>
</html>
)rawliteral";
WebSocketsServer webSocket = WebSocketsServer(81);
ESP8266WebServer server(80);
void setup() {
Serial.begin(115200);
pinMode(ENA, OUTPUT);
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
pinMode(IN_3, OUTPUT);
pinMode(IN_4, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(Light, OUTPUT);
delay(1000);
Serial.println (">> Setup");
WiFi.mode(WIFI_AP); //Only Access point
WiFi.softAP(ssid, password); //Start HOTspot removing password will disable security
IPAddress myIP = WiFi.softAPIP(); //Get IP address
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(myIP);
server.on("/", [](){
server.send(200, "text/html", INDEX_HTML);
});
server.begin();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void loop() {
if(Data=='a')speedCar = 70;
else if(Data=='b')speedCar = 150;
else if(Data=='c')speedCar = 255;
else if(Data=='o')digitalWrite(Light, HIGH); // light is on
else if(Data=='f')digitalWrite(Light, LOW); // light is off
else if(Data=='F')forword();
else if(Data=='B')backword();
else if(Data=='R')turnRight();
else if(Data=='L')turnLeft();
else if(Data=='S')Stop();
analogWrite(ENA, speedCar);
analogWrite(ENB, speedCar);
webSocket.loop();
server.handleClient();
}
void webSocketEvent(uint8_t num, WStype_t type,uint8_t * Payload, size_t length){
switch(type) {
case WStype_DISCONNECTED:
//Serial.printf("[%u] Disconnected!\r\n", num);
break;
case WStype_CONNECTED:
{IPAddress ip = webSocket.remoteIP(num);}
break;
case WStype_TEXT:
//Serial.printf("%s\r\n", Payload);
Data = Payload[0];
Serial.println(Data);
// send data to all connected clients
webSocket.broadcastTXT(Payload, length);
break;
case WStype_BIN:
hexdump(Payload, length);
// echo data back to browser
webSocket.sendBIN(num, Payload, length);
break;
default:
break;
}
}
void forword(){ //forword
digitalWrite(IN_1, HIGH); //Right Motor forword Pin
digitalWrite(IN_2, LOW); //Right Motor backword Pin
digitalWrite(IN_3, LOW); //Left Motor backword Pin
digitalWrite(IN_4, HIGH); //Left Motor forword Pin
}
void backword(){ //backword
digitalWrite(IN_1, LOW); //Right Motor forword Pin
digitalWrite(IN_2, HIGH); //Right Motor backword Pin
digitalWrite(IN_3, HIGH); //Left Motor backword Pin
digitalWrite(IN_4, LOW); //Left Motor forword Pin
}
void turnRight(){ //turnRight
digitalWrite(IN_1, LOW); //Right Motor forword Pin
digitalWrite(IN_2, HIGH); //Right Motor backword Pin
digitalWrite(IN_3, LOW); //Left Motor backword Pin
digitalWrite(IN_4, HIGH); //Left Motor forword Pin
}
void turnLeft(){ //turnLeft
digitalWrite(IN_1, HIGH); //Right Motor forword Pin
digitalWrite(IN_2, LOW); //Right Motor backword Pin
digitalWrite(IN_3, HIGH); //Left Motor backword Pin
digitalWrite(IN_4, LOW); //Left Motor forword Pin
}
void Stop(){ //stop
digitalWrite(IN_1, LOW); //Right Motor forword Pin
digitalWrite(IN_2, LOW); //Right Motor backword Pin
digitalWrite(IN_3, LOW); //Left Motor backword Pin
digitalWrite(IN_4, LOW); //Left Motor forword Pin
}