-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWALLE.ino
More file actions
282 lines (245 loc) · 8.33 KB
/
WALLE.ino
File metadata and controls
282 lines (245 loc) · 8.33 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
#include <Servo.h>
#include <WiFiS3.h>
#include <WebSocketServer.h>
#include "index.h"
#include "DFRobotDFPlayerMini.h"
using namespace net;
DFRobotDFPlayerMini myDFPlayer;
#define SERVO_PIN 9 // Arduino pin 9 connected to servo motor
#define ENA 5 // Enable A (PWM) for Motor 1
#define IN1 6 // Input 1 for Motor 1
#define IN2 7 // Input 2 for Motor 1
#define ENB 10 // Enable B (PWM) for Motor 2
#define IN3 11 // Input 3 for Motor 2
#define IN4 12 // Input 4 for Motor 2
#define LEDHEAD 2
#define LEDCHARGE 8
#define LEDRED 4
Servo servo;
const char *ssid = "Hack4u";
const char *password = "ngljjrt54";
WebSocketServer webSocket(81);
WiFiServer server(80);
int status = WL_IDLE_STATUS;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("Iniciando DFPlayer...");
if (!myDFPlayer.begin(Serial1)) {
Serial.println("No se pudo iniciar DFPlayer.");
while (true)
;
}
Serial.println("DFPlayer listo.");
myDFPlayer.volume(25);
servo.attach(SERVO_PIN);
// L298N pins
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// Set motors to stop initially
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
// LEDS
pinMode(LEDHEAD, OUTPUT);
pinMode(LEDCHARGE, OUTPUT);
pinMode(LEDRED, OUTPUT);
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION)
Serial.println("Please upgrade the firmware");
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, password);
// wait 4 seconds for connection:
delay(4000);
}
// print your board's IP address:
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
server.begin();
webSocket.onConnection([](WebSocket &ws) {
const auto protocol = ws.getProtocol();
if (protocol) {
Serial.print(F("Client protocol: "));
Serial.println(protocol);
}
ws.onMessage([](WebSocket &ws, const WebSocket::DataType dataType,
const char *message, uint16_t length) {
if (dataType == WebSocket::DataType::TEXT) {
String msg = String((char *)message);
Serial.print("Received WebSocket message: ");
Serial.println(msg);
// Check if message is for servo (numeric angle)
if (msg.toInt() > 0 || msg == "0") {
int angle_value = msg.toInt();
servo.write(angle_value);
Serial.print("Rotate Servo Motor to ");
Serial.println(angle_value);
}
// Check if message is for Motor 1
else if (msg.startsWith("M1")) {
if (msg.startsWith("M1F")) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 125);
delay(250);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0);
Serial.println("Motor 1 Forward");
} else if (msg.startsWith("M1R")) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, 38);
delay(200);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, 115);
delay(15);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0);
Serial.println("Motor 1 Reverse");
} else if (msg.startsWith("M1S")) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0);
Serial.println("Motor 1 Stop");
}
}
// Check if message is for Motor 2
else if (msg.startsWith("M2")) {
if (msg.startsWith("M2F")) {
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 120);
delay(200);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(ENB, 0);
Serial.println("Motor 2 Forward");
} else if (msg.startsWith("M2R")) {
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENB, 50);
delay(200);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, 115);
delay(15);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(ENB, 0);
Serial.println("Motor 2 Reverse");
} else if (msg.startsWith("M2S")) {
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
analogWrite(ENB, 0);
Serial.println("Motor 2 Stop");
}
} else if (msg.startsWith("LED")) {
if (msg.startsWith("LED:CABEZA-ON")) {
digitalWrite(LEDHEAD, HIGH);
} else if (msg.startsWith("LED:CABEZA-OFF")) {
digitalWrite(LEDHEAD, LOW);
} else if (msg.startsWith("LED:PECHO-ON")) {
digitalWrite(LEDRED, HIGH);
} else if (msg.startsWith("LED:PECHO-OFF")) {
digitalWrite(LEDRED, LOW);
} else if (msg.startsWith("LED:CHARGE-OFF")) {
digitalWrite(LEDCHARGE, LOW);
} else if (msg.startsWith("LED:CHARGE-ON")) {
digitalWrite(LEDCHARGE, HIGH);
}
}
else if (msg.startsWith("AUDIO")) {
if (msg.startsWith("AUDIO:WALLE")) {
myDFPlayer.play(5);
} else if (msg.startsWith("AUDIO:EVACONFUNDIDO")) {
myDFPlayer.play(1);
} else if (msg.startsWith("AUDIO:ITTAKES")) {
myDFPlayer.play(2);
} else if (msg.startsWith("AUDIO:PUTONRYOURSUNDAY")) {
myDFPlayer.play(3);
} else if (msg.startsWith("AUDIO:LAKERS")) {
myDFPlayer.play(4);
} else if (msg.startsWith("AUDIO:TADA")) {
myDFPlayer.play(7);
} else if (msg.startsWith("AUDIO:LENTES")) {
servo.write(90);
myDFPlayer.play(6);
delay(1000);
digitalWrite(LEDHEAD, LOW);
for (int i = 90; i < 150; i++) {
servo.write(i);
delay(25);
}
delay(400);
digitalWrite(LEDHEAD, HIGH);
for (int i = 150; i > 50; i--) {
servo.write(i);
delay(30);
}
delay(200);
digitalWrite(LEDHEAD, LOW);
for (int i = 50; i < 91; i++) {
servo.write(i);
delay(20);
}
digitalWrite(LEDHEAD, HIGH);
} else if (msg.startsWith("AUDIO:PARAR")) {
myDFPlayer.stop();
}
}
} else if (dataType == WebSocket::DataType::BINARY) {
Serial.println("Received binary data");
}
});
ws.onClose([](WebSocket &, const WebSocket::CloseCode, const char *,
uint16_t) {
Serial.println(F("Disconnected"));
});
Serial.print(F("New WebSocket Connnection from client: "));
Serial.println(ws.getRemoteIP());
});
webSocket.begin();
}
void loop() {
webSocket.listen();
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
// read the HTTP request header line by line
while (client.connected()) {
if (client.available()) {
String HTTP_header = client.readStringUntil('\n'); // read the header line of HTTP request
if (HTTP_header.equals("\r")) // the end of HTTP request
break;
Serial.print("<< ");
Serial.println(HTTP_header); // print HTTP request to Serial Monitor
}
}
// send the HTTP response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println(); // the separator between HTTP header and body
String html = String(HTML_CONTENT);
client.println(html);
client.flush();
// give the web browser time to receive the data
delay(50);
// close the connection:
client.stop();
}
}