-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwifi_wheeler_pins_and_while_loop.ino
More file actions
366 lines (327 loc) · 10 KB
/
wifi_wheeler_pins_and_while_loop.ino
File metadata and controls
366 lines (327 loc) · 10 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* ESP8266 WeMos code for WIFI Wheeler
*
* REVISION HISTORY
* v101 - Date: 20240911
* - cleaned up code and added comments
* v102 - Date: 20240911
* - added #, * commands
* - updated pin assignments (d0-tx)
**/
# include <Arduino.h>
# include <ESP8266WiFi.h> //import for wifi functionality
# include <WebSocketsServer.h> //import for websocket
//- pin assignments
static const uint8_t d0 = 16;
static const uint8_t d1 = 5;
static const uint8_t d2 = 4;
static const uint8_t d3 = 0;
static const uint8_t d4 = 2;
static const uint8_t d5 = 14;
static const uint8_t d6 = 12;
static const uint8_t d7 = 13;
static const uint8_t d8 = 15;
static const uint8_t rX = 3;
static const uint8_t tX = 1;
# define MOTOR_A1A d0
# define MOTOR_A1B d1
# define MOTOR_B1A d2
# define MOTOR_B1B d3
# define LEDPIN d4
# define ECHO d6
# define TRIGGER d7
# define MAX_SPEED 255 //- set max speed of car. Value between 0 and 255
# define EDWARD 1
/** WIFI SSID and Password that will be broadcast from your ESP8266 board.
* If you change these values, make sure to change your WIFI connection settings on your phone/PC
*/
const char *ssid = "My Barnabas Bot"; //- WIFI SSID (Network Name)
const char *pass = "12345678"; //- WIFI password
WebSocketsServer webSocket = WebSocketsServer(81); //- Initialize WebSocket Server using Port 81
/** Mandatory setup function.
* Set up pins and WIFI stuff
*/
void setup() {
pinMode(LEDPIN, OUTPUT);
pinMode(MOTOR_A1A, OUTPUT);
pinMode(MOTOR_A1B, OUTPUT);
pinMode(MOTOR_B1A, OUTPUT);
pinMode(MOTOR_B1B, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(TRIGGER, OUTPUT);
stopAllMotors(); //- make sure that all WIFI Wheeler motors are off by default
Serial.begin(9600); //- Turn on serial monitor for debugging. Set baud to 9600 bps
Serial.println("Connecting to wifi");
IPAddress apIP(192, 168, 0, 1); //- static IP for your ESP8266 board.
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(ssid, pass); //- turn on WIFI
webSocket.begin(); //- Initialize WebSocket Handling
webSocket.onEvent(webSocketEvent); //- Allow WebSocket to detect events
Serial.println("Websocket is started");
}
/** Mandatory loop function. Runs over and over. Keeps the WebSocket running
*
*/
void loop() {
webSocket.loop(); //- keep this line on loop method
}
//- Handles all events on the WebSocket
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
String cmd = "";
switch(type) {
case WStype_DISCONNECTED:
Serial.println("Websocket is disconnected");
stopAllMotors(); //-stop the motors if disconnected
break;
case WStype_CONNECTED:{
Serial.println("Websocket is connected");
Serial.println(webSocket.remoteIP(num).toString());
webSocket.sendTXT(num, "connected");}
break;
case WStype_TEXT:
cmd = "";
//merging payload to single string
for(int i = 0; i < length; i++) {
cmd = cmd + (char) payload[i];
}
handleText(cmd, num);
break;
case WStype_FRAGMENT_TEXT_START:
break;
case WStype_FRAGMENT_BIN_START:
break;
case WStype_BIN:
hexdump(payload, length);
break;
default:
break;
}
}
/** DC Motor Driver Helper Function
*
*/
//- stop both motor A and motor B
void stopAllMotors() {
analogWrite(MOTOR_A1A,0);
analogWrite(MOTOR_A1B,0);
analogWrite(MOTOR_B1A,0);
analogWrite(MOTOR_B1B,0);
}
//- move WIFI Wheeler forward at half speed
void moveForward() {
analogWrite(MOTOR_A1A,0);
analogWrite(MOTOR_A1B,MAX_SPEED*.5);
analogWrite(MOTOR_B1A,MAX_SPEED*.5);
analogWrite(MOTOR_B1B,0);
}
//- move WIFI Wheeler backwards at half speed
void moveBackward() {
analogWrite(MOTOR_A1A,MAX_SPEED*.5);
analogWrite(MOTOR_A1B,0);
analogWrite(MOTOR_B1A,0);
analogWrite(MOTOR_B1B,MAX_SPEED*.5);
}
//- spin WIFI Wheeler left at half speed
void spinLeft() {
analogWrite(MOTOR_A1A,0);
analogWrite(MOTOR_A1B,MAX_SPEED*.5);
analogWrite(MOTOR_B1A,0);
analogWrite(MOTOR_B1B,MAX_SPEED*.5);
}
//- spin WIFI Wheeler right at half speed
void spinRight() {
analogWrite(MOTOR_A1A,MAX_SPEED*.5);
analogWrite(MOTOR_A1B,0);
analogWrite(MOTOR_B1A,MAX_SPEED*.5);
analogWrite(MOTOR_B1B,0);
}
//- have WIFI wheeler move forward and stops when it sees an object less than 5 cm away
void moveToObject() {
int distance;
distance = ultrasonic();
while (distance > 5) {
moveForward();
distance = ultrasonic();
delay(100);
}
stopAllMotors();
}
//- use ultrasonic sensor to determine the distance of an object. Returns distance in centimeters.
int ultrasonic() {
long time;
float distance;
//-trigger a sound
// send out trigger signal
digitalWrite(TRIGGER, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(20);
digitalWrite(TRIGGER, LOW);
//- a sound has gone out!!
//- wait for a sound to come back
time = pulseIn(ECHO, HIGH);
//- calculate the distance in centimeters
distance = 0.01715 * time;
return distance;
}
//- handle commands from your phone/computer
void handleText(String cmd, uint8_t num){
Serial.println(cmd); //- Print command on serial monitor for debugging
bool cmdFound = true; //- cmdFound default is true. Set to false later if command is not found
//- Onboard LED on-off
if (cmd == "poweron") {
digitalWrite(LEDPIN, LOW);
}
else if (cmd == "poweroff") {
digitalWrite(LEDPIN, HIGH);
}
//- Motor A Commands
else if (cmd == "Servo1_0"){
analogWrite(MOTOR_A1A,0);
analogWrite(MOTOR_A1B,0);
}
else if (cmd == "Servo1_.25"){
analogWrite(MOTOR_A1A,MAX_SPEED*.25);
analogWrite(MOTOR_A1B,0);
}
else if (cmd == "Servo1_.5"){
analogWrite(MOTOR_A1A,MAX_SPEED*.5);
analogWrite(MOTOR_A1B,0);
}
else if (cmd == "Servo1_.75"){
analogWrite(MOTOR_A1A,MAX_SPEED*.75);
analogWrite(MOTOR_A1B,0);
}
else if (cmd == "Servo1_1"){
analogWrite(MOTOR_A1A,MAX_SPEED*1.0);
analogWrite(MOTOR_A1B,0);
}
else if (cmd == "Servo1_-.25"){
analogWrite(MOTOR_A1A,0);
analogWrite(MOTOR_A1B,MAX_SPEED*.25);
}
else if (cmd == "Servo1_-.5"){
analogWrite(MOTOR_A1A,0);
analogWrite(MOTOR_A1B,MAX_SPEED*.5);
}
else if (cmd == "Servo1_-.75"){
analogWrite(MOTOR_A1A,0);
analogWrite(MOTOR_A1B,MAX_SPEED*.75);
}
else if (cmd == "Servo1_-1"){
analogWrite(MOTOR_A1A,0);
analogWrite(MOTOR_A1B,MAX_SPEED*1);
}
//- Motor B Commands
else if (cmd == "Servo2_0"){
analogWrite(MOTOR_B1A,0);
analogWrite(MOTOR_B1B,0);
}
else if (cmd == "Servo2_.25"){
analogWrite(MOTOR_B1A,0);
analogWrite(MOTOR_B1B,MAX_SPEED*.25);
}
else if (cmd == "Servo2_.5"){
analogWrite(MOTOR_B1A,0);
analogWrite(MOTOR_B1B,MAX_SPEED*.5);
}
else if (cmd == "Servo2_.75"){
analogWrite(MOTOR_B1A,0);
analogWrite(MOTOR_B1B,MAX_SPEED*.75);
}
else if (cmd == "Servo2_1"){
analogWrite(MOTOR_B1A,0);
analogWrite(MOTOR_B1B,MAX_SPEED*1);
}
else if (cmd == "Servo2_-.25"){
analogWrite(MOTOR_B1A,MAX_SPEED*.25);
analogWrite(MOTOR_B1B,0);
}
else if (cmd == "Servo2_-.5"){
analogWrite(MOTOR_B1A,MAX_SPEED*.5);
analogWrite(MOTOR_B1B,0);
}
else if (cmd == "Servo2_-.75"){
analogWrite(MOTOR_B1A,MAX_SPEED*.75);
analogWrite(MOTOR_B1B,0);
}
else if (cmd == "Servo2_-1"){
analogWrite(MOTOR_B1A,MAX_SPEED*1);
analogWrite(MOTOR_B1B,0);
}
//- Reserved Control Panel Commands
else if (cmd == "0") {
//-code here
stopAllMotors();
}
else if (cmd == "1") {
//-code here
moveBackward();
}
else if (cmd == "2") {
//-code here
moveForward();
}
else if (cmd == "3") {
//-code here
spinLeft();
}
else if (cmd == "4") {
//-code here
spinRight();
}
else if (cmd == "5") {
//-code here
moveToObject();
}
else if (cmd == "6") {
//-code here
//-sense distace of nearest object
cmd = cmd + " = " + ultrasonic() + " cm";
}
else if (cmd == "7") {
//-code here
moveForward();
delay(1000);
stopAllMotors();
moveBackward();
delay(1000);
stopAllMotors();
}
else if (cmd == "8") {
//-code here
digitalWrite(LEDPIN,LOW); //on
}
else if (cmd == "9") {
//-code here
digitalWrite(LEDPIN,HIGH); //off
}
else if (cmd == "*") {
int counter;
counter = 0;
while ( counter < 5 ) {
digitalWrite(LEDPIN,LOW); //on
delay(500);
digitalWrite(LEDPIN,HIGH); //off
delay(500);
counter = counter + 1;
}
//-code here
}
else if (cmd == "#") {
//-code here
}
else {
//- this is an unknown command
cmdFound = false;
}
//- build response. Append ':success' if commands is found. Append ':command not found' if it is an unknown command
if( cmdFound ){
cmd = cmd + ":success";
}
else {
cmd = cmd + ":Command not found";
}
//- send response via webSocket
webSocket.sendTXT(num, cmd);
}