-
Notifications
You must be signed in to change notification settings - Fork 0
/
CapsTrainer.ino
539 lines (457 loc) · 14.5 KB
/
CapsTrainer.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#include <Arduino.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
#include <Preferences.h>
#include "SPIFFS.h"
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
// create servo object to control a servo
Servo servoBase;
Servo servoArm1;
Servo servoArm2;
Servo servoHead;
//ppreferences added to "save" positions between launch
Preferences preferences;
// Search for parameter in HTTP POST request
const char* PARAM_WIFI_INPUT_1 = "ssid";
const char* PARAM_WIFI_INPUT_2 = "pass";
const char* PARAM_WIFI_INPUT_3 = "ip";
const char* PARAM_WIFI_INPUT_4 = "gateway";
//Variables to save values from HTML form
String ssid;
String pass;
String ip;
String gateway;
// File paths to save input values permanently
const char* ssidPath = "/ssid.txt";
const char* passPath = "/pass.txt";
const char* ipPath = "/ip.txt";
const char* gatewayPath = "/gateway.txt";
IPAddress localIP;
//IPAddress localIP(192, 168, 1, 200); // hardcoded
// Set your Gateway IP address
IPAddress localGateway;
//IPAddress localGateway(192, 168, 1, 1); //hardcoded
IPAddress subnet(255, 255, 0, 0);
// Parameters for servo movement
const char* PARAM_VALUE = "value";
const char* PARAM_SERVO = "servo";
// Parameter for number of used electromagnet
const char* PARAM_NB_EM = "nbEM";
// Timer variables
unsigned long previousMillis = 0;
const long interval = 10000; // interval to wait for Wi-Fi connection (milliseconds)
int posServo = 0;
int posServoBase = 90;
int posServoArm1 = 90;
int posServoArm2 = 90;
int posServoHead = 90;
int elecAimDev = 25;
int elecAimAD = 26;
int elecAimAG = 27;
int nbEM = 3; // 3 par défaut, vu que c'est le jeu 'normal'
String inputValue;
String inputServo;
bool MOVE_SERVO = false;
bool SAVE_SLEEP_POSITIONS = false;
bool SAVE_LOAD_POSITIONS = false;
bool LOAD_SLEEP_POSITIONS = false;
bool LOAD_LOAD_POSITIONS = false;
bool RELOAD_CAPS = false;
bool SERVO_BASE = false;
bool SERVO_ARM1 = false;
bool SERVO_ARM2 = false;
bool SERVO_HEAD = false;
// Initialize SPIFFS
void initSPIFFS() {
if (!SPIFFS.begin(true)) {
Serial.println("An error has occurred while mounting SPIFFS");
}
Serial.println("SPIFFS mounted successfully");
}
// Read File from SPIFFS
String readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if(!file || file.isDirectory()){
Serial.println("- failed to open file for reading");
return String();
}
String fileContent;
while(file.available()){
fileContent = file.readStringUntil('\n');
break;
}
return fileContent;
}
// Write file to SPIFFS
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\r\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("- failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("- file written");
} else {
Serial.println("- write failed");
}
}
// Initialize WiFi
bool initWiFi() {
if(ssid=="" || ip==""){
Serial.println("Undefined SSID or IP address.");
return false;
}
WiFi.mode(WIFI_STA);
localIP.fromString(ip.c_str());
localGateway.fromString(gateway.c_str());
if (!WiFi.config(localIP, localGateway, subnet)){
Serial.println("STA Failed to configure");
return false;
}
WiFi.begin(ssid.c_str(), pass.c_str());
Serial.println("Connecting to WiFi...");
unsigned long currentMillis = millis();
previousMillis = currentMillis;
while(WiFi.status() != WL_CONNECTED) {
currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
Serial.println("Failed to connect.");
return false;
}
}
Serial.println(WiFi.localIP());
return true;
}
// Replaces placeholder with servo values
String processor(const String& var) {
if(var == "POSITION_SERVO_BASE") {
return String(posServoBase);
}
if(var == "POSITION_SERVO_ARM1") {
return String(posServoArm1);
}
if(var == "POSITION_SERVO_ARM2") {
return String(posServoArm2);
}
if(var == "POSITION_SERVO_HEAD") {
return String(posServoHead);
}
return String();
}
void initServo() {
servoBase.attach(18); // attaches the servo on the servoPin to the servo object
servoArm1.attach(19);
servoBase.write(90);
servoArm2.attach(21);
servoHead.attach(5);
}
void initAimant() {
pinMode(elecAimDev, OUTPUT);
digitalWrite(elecAimDev, HIGH);
pinMode(elecAimAD, OUTPUT);
digitalWrite(elecAimAD, HIGH);
pinMode(elecAimAG, OUTPUT);
digitalWrite(elecAimAG, HIGH);
}
void saveServoLoadPositions() {
preferences.putInt("servoBaseLoad", posServoBase);
preferences.putInt("servoArm1Load", posServoArm1);
Serial.println("Saving servo 2 value : " + String(posServoArm2));
preferences.putInt("servoArm2Load", posServoArm2);
preferences.putInt("servoHeadLoad", posServoHead);
}
void loadServoLoadPositions() {
moveServo(servoBase, posServoBase, preferences.getInt("servoBaseLoad", 0));
posServoBase = preferences.getInt("servoBaseLoad", 0);
moveServo(servoArm1, posServoArm1, preferences.getInt("servoArm1Load", 0));
posServoArm1 = preferences.getInt("servoArm1Load", 0);
moveServo(servoArm2, posServoArm2, preferences.getInt("servoArm2Load", 0));
posServoArm2 = preferences.getInt("servoArm2Load", 0);
moveServo(servoHead, posServoHead, preferences.getInt("servoHeadLoad", 0));
posServoHead = preferences.getInt("servoHeadLoad", 0);
}
void saveServoSleepPositions() {
preferences.putInt("servoBaseSleep", posServoBase);
preferences.putInt("servoArm1Sleep", posServoArm1);
preferences.putInt("servoArm2Sleep", posServoArm2);
preferences.putInt("servoHeadSleep", posServoHead);
}
void loadServoSleepPositions() {
moveServo(servoArm2, posServoArm2, preferences.getInt("servoArm2Sleep", 0));
posServoArm2 = preferences.getInt("servoArm2Sleep", 0);
moveServo(servoBase, posServoBase, preferences.getInt("servoBaseSleep", 0));
posServoBase = preferences.getInt("servoBaseSleep", 0);
moveServo(servoArm1, posServoArm1, preferences.getInt("servoArm1Sleep", 0));
posServoArm1 = preferences.getInt("servoArm1Sleep", 0);
moveServo(servoHead, posServoHead, preferences.getInt("servoHeadSleep", 0));
posServoHead = preferences.getInt("servoHeadSleep", 0);
}
void reloadCaps() {
loadServoLoadPositions();
delay(2000);
elecAimOff();
delay(1000);
loadServoSleepPositions();
delay(500);
elecAimOn();
}
void elecAimOff() {
if (nbEM == 1) {
Serial.println("Only one EM going off");
digitalWrite(elecAimDev, LOW);
}
if (nbEM == 2) {
Serial.println("Two EM going off");
digitalWrite(elecAimAD, LOW);
digitalWrite(elecAimAG, LOW);
}
if (nbEM == 3) {
Serial.println("Three EM going off");
digitalWrite(elecAimDev, LOW);
digitalWrite(elecAimAD, LOW);
digitalWrite(elecAimAG, LOW);
}
}
void elecAimOn() {
if (nbEM == 1) {
digitalWrite(elecAimDev, HIGH);
}
if (nbEM == 2) {
digitalWrite(elecAimAD, HIGH);
digitalWrite(elecAimAG, HIGH);
}
if (nbEM == 3) {
digitalWrite(elecAimDev, HIGH);
digitalWrite(elecAimAD, HIGH);
digitalWrite(elecAimAG, HIGH);
}
}
void engageServo(String servo) {
if (servo == "servoBase") {
posServo = posServoBase;
SERVO_BASE = true;
}
else if (servo == "servoArm1") {
posServo = posServoArm1;
SERVO_ARM1 = true;
}
else if (servo == "servoArm2") {
posServo = posServoArm2;
SERVO_ARM2 = true;
}
else if (servo == "servoHead") {
posServo = posServoHead;
SERVO_HEAD = true;
}
}
void moveServo(Servo &servo, int basePos, int newPos) {
if (basePos <= newPos) {
for(int i = basePos; i <= newPos; i += 1) {
servo.write(i);
delay(15);
}
}
else{
for(int i = basePos; i >= newPos; i -= 1) {
servo.write(i);
delay(15);
}
}
}
void writeNewPos(String servo, int value) {
if (servo == "servoBase") {
posServoBase = value;
}
else if (servo == "servoArm1") {
posServoArm1 = value;
}
else if (servo == "servoArm2") {
posServoArm2 = value;
}
else if (servo == "servoHead") {
posServoHead = value;
}
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
preferences.begin("capsTrainer", false);
initSPIFFS();
initServo();
initAimant();
// Set GPIO 2 as an OUTPUT
// Load values saved in SPIFFS
ssid = readFile(SPIFFS, ssidPath);
pass = readFile(SPIFFS, passPath);
ip = readFile(SPIFFS, ipPath);
gateway = readFile (SPIFFS, gatewayPath);
Serial.println(ssid);
Serial.println(ip);
Serial.println(gateway);
if(initWiFi()) {
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SPIFFS, "/index.html", "text/html", false, processor);
});
server.serveStatic("/", SPIFFS, "/");
// Route to save all servo load positions
server.on("/saveLoadPositions", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SPIFFS, "/index.html", "text/html", false, processor);
SAVE_LOAD_POSITIONS = true;
});
// Route to save all servo sleep positions
server.on("/saveSleepPositions", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SPIFFS, "/index.html", "text/html", false, processor);
SAVE_SLEEP_POSITIONS = true;
});
// Route to load all servo load positions
server.on("/loadLoadPositions", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SPIFFS, "/index.html", "text/html", false, processor);
LOAD_LOAD_POSITIONS = true;
});
// Route to load all servo sleep positions
server.on("/loadSleepPositions", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SPIFFS, "/index.html", "text/html", false, processor);
LOAD_SLEEP_POSITIONS = true;
});
server.on("/reload", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(SPIFFS, "/index.html", "text/html", false, processor);
RELOAD_CAPS = true;
});
// Route to handle a moving servo
server.on("/move", HTTP_GET, [](AsyncWebServerRequest *request) {
if (request->hasParam(PARAM_VALUE) && request->hasParam(PARAM_SERVO)) {
inputValue = request->getParam(PARAM_VALUE)->value();
inputServo = request->getParam(PARAM_SERVO)->value();
MOVE_SERVO = true;
engageServo(inputServo);
}
});
server.on("/nbCaps", HTTP_GET, [](AsyncWebServerRequest *request) {
if (request->hasParam(PARAM_NB_EM)) {
nbEM = request->getParam(PARAM_NB_EM)->value().toInt();
}
});
server.begin();
}
else {
// Connect to Wi-Fi network with SSID and password
Serial.println("Setting AP (Access Point)");
// NULL sets an open Access Point
WiFi.softAP("ESP-WIFI-MANAGER", NULL);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
// Web Server Root URL
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/wifimanager.html", "text/html");
});
server.serveStatic("/", SPIFFS, "/");
server.on("/", HTTP_POST, [](AsyncWebServerRequest *request) {
int params = request->params();
for(int i=0;i<params;i++){
AsyncWebParameter* p = request->getParam(i);
if(p->isPost()){
// HTTP POST ssid value
if (p->name() == PARAM_WIFI_INPUT_1) {
ssid = p->value().c_str();
Serial.print("SSID set to: ");
Serial.println(ssid);
// Write file to save value
writeFile(SPIFFS, ssidPath, ssid.c_str());
}
// HTTP POST pass value
if (p->name() == PARAM_WIFI_INPUT_2) {
pass = p->value().c_str();
Serial.print("Password set to: ");
Serial.println(pass);
// Write file to save value
writeFile(SPIFFS, passPath, pass.c_str());
}
// HTTP POST ip value
if (p->name() == PARAM_WIFI_INPUT_3) {
ip = p->value().c_str();
Serial.print("IP Address set to: ");
Serial.println(ip);
// Write file to save value
writeFile(SPIFFS, ipPath, ip.c_str());
}
// HTTP POST gateway value
if (p->name() == PARAM_WIFI_INPUT_4) {
gateway = p->value().c_str();
Serial.print("Gateway set to: ");
Serial.println(gateway);
// Write file to save value
writeFile(SPIFFS, gatewayPath, gateway.c_str());
}
//Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
request->send(200, "text/plain", "Done. ESP will restart, connect to your router and go to IP address: " + ip);
delay(3000);
ESP.restart();
});
server.begin();
}
}
//Je suppose que ça fonctionne pas car il y a la boucle qui reset à chaque fois --> pas ça
void loop() {
if (MOVE_SERVO) {
Serial.println("Moving servo");
if (SERVO_BASE) {
Serial.println("Servo base is moving");
moveServo(servoBase, posServo, inputValue.toInt());
writeNewPos("servoBase", inputValue.toInt());
SERVO_BASE = false;
}
else if (SERVO_ARM1) {
Serial.println("Servo arm 1 is moving");
moveServo(servoArm1, posServo, inputValue.toInt());
writeNewPos("servoArm1", inputValue.toInt());
SERVO_ARM1 = false;
}
else if (SERVO_ARM2) {
Serial.println("Servo arm 2 is moving");
moveServo(servoArm2, posServo, inputValue.toInt());
writeNewPos("servoArm2", inputValue.toInt());
SERVO_ARM2 = false;
}
else if (SERVO_HEAD) {
Serial.println("Servo head is moving");
moveServo(servoHead, posServo, inputValue.toInt());
writeNewPos("servoHead", inputValue.toInt());
SERVO_HEAD = false;
}
MOVE_SERVO = false;
}
if (SAVE_LOAD_POSITIONS) {
Serial.println("Saving load positions");
saveServoLoadPositions();
SAVE_LOAD_POSITIONS = false;
}
if (SAVE_SLEEP_POSITIONS) {
Serial.println("Saving sleep positions");
saveServoSleepPositions();
SAVE_SLEEP_POSITIONS = false;
}
if (LOAD_LOAD_POSITIONS) {
Serial.println("Loading load positions");
loadServoLoadPositions();
LOAD_LOAD_POSITIONS = false;
}
if (LOAD_SLEEP_POSITIONS) {
Serial.println("Loading sleep positions");
loadServoSleepPositions();
LOAD_SLEEP_POSITIONS = false;
}
if (RELOAD_CAPS) {
Serial.println("Reloading Caps");
reloadCaps();
RELOAD_CAPS = false;
}
}