-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32-cam-autonom.ino
792 lines (637 loc) · 23.6 KB
/
esp32-cam-autonom.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#include <driver/adc.h> // sleep adc
#include <esp_bt.h> // sleep bt
#include <esp_wifi.h> // sleep wifi
#include <esp_sleep.h> // sleep
#include <esp_camera.h> // camera
#include <SD_MMC.h> // SD
#include <soc/rtc_cntl_reg.h> // sleep RTC
#include <driver/rtc_io.h> // RTC
#include <soc/rtc_wdt.h> // Watchdog
#include <LittleFS.h> // Internal storage
#include <EEPROM.h> // Storage for config
#include <ESP32Time.h> // Used to set time in RTC
#include <ESPmDNS.h> // WiFi web server with DNS
#include <WiFi.h> // WiFi web server
#include <AsyncTCP.h> // Async TCP connection
#include <ESPAsyncWebServer.h> // Async web server
#define US_TO_S_FACTOR 1000000
#define TIMEZONE 3600 * 2
#define null nullptr
// Pin definition for CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
//#define RESET_CONFIG // uncomment to put default Config struct and CURRENT_EPOCH in EEPROM
#define CURRENT_EPOCH 1630228216 + 10 + TIMEZONE
#define WIFI_SSID "HiHiHi"
#define WIFI_PASSWORD "U5z^26Gi9B&un4J7"
typedef struct
{
unsigned int timeSleepDay;
unsigned int timeSleepNight;
unsigned long epoch;
byte hourStartNight;
byte hourStopNight;
byte hourModuloWifi;
byte hourStartWifi;
byte hourStopWifi;
byte minuteModuloWifi;
byte minuteStartWifi;
byte minuteStopWifi;
bool forceWifi;
} Config;
String outputHtml;
unsigned long lastPhotoTakenTime = 0;
unsigned long lastPhotoSize = 0;
String lastPhotoPath, tempPath;
bool isDoingSomething = false;
ESP32Time rtc;
AsyncWebServer server(80);
Config config = {
.timeSleepDay = 50,
.timeSleepNight = 3590,
.epoch = CURRENT_EPOCH,
.hourStartNight = 21,
.hourStopNight = 6,
.hourModuloWifi = 24,
.hourStartWifi = 8,
.hourStopWifi = 10,
.minuteModuloWifi = 30,
.minuteStartWifi = 0,
.minuteStopWifi = 10,
.forceWifi = true
};
/*
Watchdog functions
I don't know if it works properly
https://stackoverflow.com/a/58116274/7088874
https://www.esp32.com/viewtopic.php?p=56880#p56880
*/
void disableWatchdog() {
rtc_wdt_protect_off(); //Disable RTC WDT write protection
rtc_wdt_disable();
}
void enableWatchdog() {
rtc_wdt_enable(); //Start the RTC WDT timer
rtc_wdt_protect_on(); //Enable RTC WDT write protection
}
void printConfig() {
Serial.println(F("Config :"));
Serial.print(F("\ttimeSleepDay : ")); Serial.println(config.timeSleepDay);
Serial.print(F("\ttimeSleepNight : ")); Serial.println(config.timeSleepNight);
Serial.print(F("\tepoch : ")); Serial.println(config.epoch);
Serial.print(F("\thourStartNight : ")); Serial.println(config.hourStartNight);
Serial.print(F("\thourStopNight : ")); Serial.println(config.hourStopNight);
Serial.print(F("\thourModuloWifi : ")); Serial.println(config.hourModuloWifi);
Serial.print(F("\thourStartWifi : ")); Serial.println(config.hourStartWifi);
Serial.print(F("\thourStopWifi : ")); Serial.println(config.hourStopWifi);
Serial.print(F("\tminuteModuloWifi : ")); Serial.println(config.minuteModuloWifi);
Serial.print(F("\tminuteStartWifi : ")); Serial.println(config.minuteStartWifi);
Serial.print(F("\tminuteStopWifi : ")); Serial.println(config.minuteStopWifi);
Serial.print(F("\tforceWifi : ")); Serial.println(config.forceWifi);
}
void getConfig() {
if (!EEPROM.begin(sizeof(Config))) {
Serial.println(F("Failed to initialise EEPROM"));
}
#ifdef RESET_CONFIG
config.forceWifi = true;
saveConfig();
#else
Config savedConfig;
EEPROM.get(0, savedConfig);
if (savedConfig.epoch >= CURRENT_EPOCH) {
config = savedConfig;
}
#endif
printConfig();
}
void saveConfig() {
EEPROM.put(0, config);
EEPROM.commit();
}
void goToSleep(unsigned int timeToSleep) {
Serial.println(rtc.getTimeDate());
Serial.print(F("Going to sleep now for : ")); Serial.println(timeToSleep);
// Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
rtc_gpio_hold_en(GPIO_NUM_4);
SD_MMC.end();
LittleFS.end();
adc_power_release();
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
btStop();
esp_bt_controller_disable();
esp_sleep_enable_timer_wakeup(timeToSleep * US_TO_S_FACTOR);
delay(1000);
Serial.flush();
config.epoch = rtc.getEpoch() + timeToSleep + 1; // next startup
saveConfig();
esp_deep_sleep_start();
}
void cleanPhotosLittleFS(unsigned long sizeNeeded) {
File root = LittleFS.open("/");
if (!root) {
Serial.println(F("Failed to open LittleFS root directory"));
return;
}
unsigned long spaceAvailable = LittleFS.totalBytes() - LittleFS.usedBytes();
bool shouldRemove = spaceAvailable <= sizeNeeded;
while (shouldRemove) {
Serial.print(F("LittleFS space available : ")); Serial.println(spaceAvailable);
Serial.print(F("Should remove for size ")); Serial.println(sizeNeeded);
File file = root.openNextFile();
if (!file) {
break;
} else {
while (file) {
if (!file.isDirectory()) {
tempPath.remove(0);
tempPath += String("/") + file.name();
Serial.print(F("Delete file from LittleFS : ")); Serial.print(tempPath); Serial.print(F(" for size ")); Serial.println(file.size());
file.close();
if (!LittleFS.remove(tempPath)) {
Serial.println(F("Error during deletion"));
} else {
break;
}
}
file = root.openNextFile();
yield();
}
}
spaceAvailable = LittleFS.totalBytes() - LittleFS.usedBytes();
shouldRemove = spaceAvailable <= sizeNeeded;
}
root.close();
}
unsigned long takePhoto() {
Serial.println(rtc.getTimeDate());
Serial.println(F("Taking photo"));
if (isDoingSomething) {
Serial.println(F("Something already running"));
return 0;
}
isDoingSomething = true;
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) {
Serial.println(F("Camera capture failed"));
goToSleep(config.timeSleepDay);
}
Serial.print(F("File size : ")); Serial.println(fb->len);
lastPhotoPath.remove(0);
lastPhotoPath += "/" + rtc.getTime("%Y-%m-%d_%H-%M-%S") + ".jpg";
bool hasError = true;
Serial.print(F("Picture file name: ")); Serial.println(lastPhotoPath.c_str());
File file = SD_MMC.open(lastPhotoPath, FILE_WRITE);
if (!file) {
Serial.println(F("Failed to open SD file in writing mode"));
} else {
if (file.write(fb->buf, fb->len) != fb->len) {
Serial.println(F("Failed to write on SD Card"));
} else {
Serial.println(F("Saved on SD"));
hasError = false;
}
file.close();
}
yield();
cleanPhotosLittleFS(fb->len);
file = LittleFS.open(lastPhotoPath, FILE_WRITE);
if (!file) {
Serial.println(F("Failed to open LittleFS file in writing mode"));
} else {
if (file.write(fb->buf, fb->len) != fb->len) {
Serial.println(F("Failed to write on LittleFS"));
} else {
Serial.println(F("Saved on LittleFS"));
hasError = false;
}
file.close();
}
yield();
if (!hasError) {
lastPhotoTakenTime = rtc.getEpoch();
lastPhotoSize = fb->len;
}
esp_camera_fb_return(fb);
isDoingSomething = false;
return lastPhotoSize;
}
void handleFileList(AsyncWebServerRequest *request) {
Serial.print(F("GET ")); Serial.println(request->url());
if (isDoingSomething) {
Serial.println(F("Something already running"));
request->send(503);
return;
}
isDoingSomething = true;
outputHtml.remove(0);
outputHtml += "<html><head><meta charset=\"utf-8\">";
outputHtml += "<script type=\"text/javascript\">function addtimestamp() { const link = document.getElementById('timeLink'); const now = Math.round(new Date().getTime() / 1000); link.setAttribute(\"href\", \"/time?epoch=\" + now); }</script>";
outputHtml += "<title>HiHiHi Camera</title>";
outputHtml += "</head><body><h1>HiHiHi Camera</h1>";
outputHtml += "<h2>Actions</h2><a target=\"_blank\" href=\"/take\">Prendre une photo</a> <a target=\"\" href=\"/wifi\">Basculer forcage WiFi</a> <a onclick=\"addtimestamp()\" href=\"#\" id=\"timeLink\">Régler l'heure</a> <a target=\"\" href=\"/format\">Formatter LittleFS + SD</a><br /><br />";
outputHtml += "<h2>Photos SD</h2>";
outputHtml += "<a target=\"_blank\" href=\"/wget\">Liste photos</a><br />";
unsigned int nbPhotos = 0;
File root = SD_MMC.open("/");
if (root) {
File file = root.openNextFile();
while (file) {
if (!file.isDirectory() && file.size() > 0) {
//outputHtml += String("<a target=\"_blank\" href=\"/photos_sd/") + file.name() + String("\">") + file.name() + String(" - ") + String(file.size()) + String(" - SD</a><br />");
nbPhotos++;
}
file.close();
file = root.openNextFile();
yield();
}
root.close();
outputHtml += "<br />Nombre de photos SD : " + String(nbPhotos);
} else {
Serial.println(F("Failed to open SD root directory"));
outputHtml += "<br />Impossible d'ouvrir le stockage";
}
outputHtml += "<h2>Photos internes</h2>";
nbPhotos = 0;
root = LittleFS.open("/");
if (root) {
File file = root.openNextFile();
while (file) {
if (!file.isDirectory() && file.size() > 0) {
outputHtml += String("<a target=\"_blank\" href=\"/photos_internal/") + file.name() + String("\">") + file.name() + String(" - ") + String(file.size()) + String(" - Interne</a><br />");
nbPhotos++;
}
file.close();
file = root.openNextFile();
yield();
}
root.close();
outputHtml += "<br />Nombre de photos internes : " + String(nbPhotos);
} else {
Serial.println(F("Failed to open LittleFS root directory"));
outputHtml += "<br />Impossible d'ouvrir le stockage";
}
outputHtml += "<div>Prochaine photo dans <strong>" + String(config.timeSleepDay - (rtc.getEpoch() - lastPhotoTakenTime)) + " secondes</strong></div>";
outputHtml += "<h2>Configuration</h2>";
outputHtml += "<form method=\"POST\" action=\"/set\">";
outputHtml += "<div><label for=\"timeSleepDay\">Pause journée (s)</label> <input type=\"number\" value=\"" + String(config.timeSleepDay) + "\" name=\"timeSleepDay\" id=\"timeSleepDay\" /></div><br />";
outputHtml += "<div><label for=\"timeSleepNight\">Pause nuit (s)</label> <input type=\"number\" value=\"" + String(config.timeSleepNight) + "\" name=\"timeSleepNight\" id=\"timeSleepNight\" /></div><br />";
outputHtml += "<div><label for=\"hourStartNight\">Heure début nuit (soir)</label> <input type=\"number\" value=\"" + String(config.hourStartNight) + "\" name=\"hourStartNight\" id=\"hourStartNight\" /></div><br />";
outputHtml += "<div><label for=\"hourStopNight\">Heure fin nuit (matin)</label> <input type=\"number\" value=\"" + String(config.hourStopNight) + "\" name=\"hourStopNight\" id=\"hourStopNight\" /></div><br />";
outputHtml += "<div><label for=\"hourModuloWifi\">Heure modulo WiFi</label> <input type=\"number\" value=\"" + String(config.hourModuloWifi) + "\" name=\"hourModuloWifi\" id=\"hourModuloWifi\" /></div><br />";
outputHtml += "<div><label for=\"hourStartWifi\">Heure début WiFi</label> <input type=\"number\" value=\"" + String(config.hourStartWifi) + "\" name=\"hourStartWifi\" id=\"hourStartWifi\" /></div><br />";
outputHtml += "<div><label for=\"hourStopWifi\">Heure fin WiFi</label> <input type=\"number\" value=\"" + String(config.hourStopWifi) + "\" name=\"hourStopWifi\" id=\"hourStopWifi\" /></div><br />";
outputHtml += "<div><label for=\"minuteModuloWifi\">Minute modulo WiFi</label> <input type=\"number\" value=\"" + String(config.minuteModuloWifi) + "\" name=\"minuteModuloWifi\" id=\"minuteModuloWifi\" /></div><br />";
outputHtml += "<div><label for=\"minuteStartWifi\">Minute début; WiFi</label> <input type=\"number\" value=\"" + String(config.minuteStartWifi) + "\" name=\"minuteStartWifi\" id=\"minuteStartWifi\" /></div><br />";
outputHtml += "<div><label for=\"minuteStopWifi\">Minute fin WiFi</label> <input type=\"number\" value=\"" + String(config.minuteStopWifi) + "\" name=\"minuteStopWifi\" id=\"minuteStopWifi\" /></div><br />";
outputHtml += "<div><label for=\"forceWifi\">Forcer le WiFi</label> <input type=\"number\" value=\"" + String(config.forceWifi) + "\" name=\"forceWifi\" id=\"forceWifi\" /></div><br />";
outputHtml += "<div>Date et heure : <strong>" + rtc.getDateTime() + "</strong></div>";
outputHtml += "<button type=\"submit\">Mettre à jour</button></form>";
outputHtml += "</body></html>";
request->send(200, PSTR("text/html"), outputHtml);
isDoingSomething = false;
}
// can trigger watchdog if too much files !
void handleFileWget(AsyncWebServerRequest *request) {
disableWatchdog();
Serial.print(F("GET ")); Serial.println(request->url());
if (isDoingSomething) {
Serial.println(F("Something already running"));
request->send(503);
return;
}
isDoingSomething = true;
unsigned int nbPhotos = 0;
outputHtml.remove(0);
outputHtml += "<html><head><meta charset=\"utf-8\">";
outputHtml += "<title>HiHiHi Camera - Photos</title>";
outputHtml += "</head><body><h1>Listes photos</h1>";
outputHtml += "<h2>Photos SD</h2>";
File root = SD_MMC.open("/");
if (root) {
File file = root.openNextFile();
while (file) {
if (!file.isDirectory() && file.size() > 0) {
outputHtml += String("<a target=\"_blank\" href=\"/photos_sd/") + file.name() + String("\">") + file.name() + String(" - ") + String(file.size()) + String("- SD</a><br />");
nbPhotos++;
}
file.close();
file = root.openNextFile();
yield();
}
root.close();
outputHtml += "<br />Nombre de photos SD : " + String(nbPhotos);
} else {
Serial.println(F("Failed to open SD root directory"));
outputHtml += "<br />Impossible d'ouvrir le stockage";
}
outputHtml += "<h2>Photos internes</h2>";
root = LittleFS.open("/");
if (root) {
File file = root.openNextFile();
while (file) {
if (!file.isDirectory() && file.size() > 0) {
outputHtml += String("<a target=\"_blank\" href=\"/photos_internal/") + file.name() + String("\">") + file.name() + String(" - ") + String(file.size()) + String(" - Interne</a><br />");
nbPhotos++;
}
file.close();
file = root.openNextFile();
yield();
}
root.close();
outputHtml += "<br />Nombre de photos internes : " + String(nbPhotos);
} else {
Serial.println(F("Failed to open LittleFS root directory"));
outputHtml += "<br />Impossible d'ouvrir le stockage";
}
outputHtml += "</body></html>";
request->send(200, PSTR("text/html"), outputHtml);
isDoingSomething = false;
enableWatchdog();
}
void handleTakePhoto(AsyncWebServerRequest *request) {
Serial.print(F("GET ")); Serial.println(request->url());
if (takePhoto() > 0) {
request->redirect(String("/photos_sd") + lastPhotoPath);
} else {
request->send_P(500, PSTR("text/plain"), PSTR("Failed to capture"));
}
}
void handleSetTime(AsyncWebServerRequest *request) {
Serial.print(F("GET ")); Serial.println(request->url());
if (isDoingSomething) {
Serial.println(F("Something already running"));
request->send(503);
return;
}
isDoingSomething = true;
unsigned long newEpoch = request->hasArg(F("epoch")) ? request->arg(F("epoch")).toInt() : 0;
if (newEpoch <= 0) {
Serial.println(F("No epoch received"));
request->send_P(400, PSTR("text/plain"), PSTR("No epoch received"));
}
newEpoch += TIMEZONE;
Serial.print(F("Set EEPROM epoch to ")); Serial.println(newEpoch);
config.epoch = newEpoch;
saveConfig();
initTime();
lastPhotoTakenTime = rtc.getEpoch(); // in order to not take immediatly a photo
request->redirect(PSTR("/"));
isDoingSomething = false;
}
void handleSetConfig(AsyncWebServerRequest *request) {
Serial.print(F("POST ")); Serial.println(request->url());
if (isDoingSomething) {
Serial.println(F("Something already running"));
request->send(503);
return;
}
isDoingSomething = true;
byte params = request->params();
for (byte i = 0; i < params; i++) {
AsyncWebParameter* p = request->getParam(i);
Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
config.timeSleepDay = request->arg(F("timeSleepDay")).toInt();
config.timeSleepNight = request->arg(F("timeSleepNight")).toInt();
config.hourStartNight = request->arg(F("hourStartNight")).toInt();
config.hourStopNight = request->arg(F("hourStopNight")).toInt();
config.hourModuloWifi = request->arg(F("hourModuloWifi")).toInt();
config.hourStartWifi = request->arg(F("hourStartWifi")).toInt();
config.hourStopWifi = request->arg(F("hourStopWifi")).toInt();
config.minuteModuloWifi = request->arg(F("minuteModuloWifi")).toInt();
config.minuteStartWifi = request->arg(F("minuteStartWifi")).toInt();
config.minuteStopWifi = request->arg(F("minuteStopWifi")).toInt();
config.forceWifi = request->arg(F("forceWifi")) == "1";
printConfig();
saveConfig();
initTime();
lastPhotoTakenTime = rtc.getEpoch(); // in order to not take immediatly a photo
request->redirect(PSTR("/"));
isDoingSomething = false;
}
void handleToggleWifi(AsyncWebServerRequest *request) {
Serial.print(F("GET ")); Serial.println(request->url());
if (isDoingSomething) {
Serial.println(F("Something already running"));
request->send(503);
return;
}
isDoingSomething = true;
config.forceWifi = !config.forceWifi;
saveConfig();
request->redirect(PSTR("/"));
isDoingSomething = false;
}
void handleFormat(AsyncWebServerRequest *request) {
Serial.print(F("GET ")); Serial.println(request->url());
if (isDoingSomething) {
Serial.println(F("Something already running"));
request->send(503);
return;
}
isDoingSomething = true;
removeAllLittleFS();
removeAllSD();
request->redirect(PSTR("/"));
isDoingSomething = false;
}
void removeAllLittleFS() {
File root = LittleFS.open("/");
if (!root) {
Serial.println(F("Failed to open LittleFS root directory"));
}
File file = root.openNextFile();
while (file) {
if (!file.isDirectory()) {
tempPath.remove(0);
tempPath += String("/") + file.name();
Serial.print(F("Delete file from LittleFS : ")); Serial.println(tempPath);
file.close();
if (!LittleFS.remove(tempPath)) {
Serial.println(F("Error during deletion"));
}
}
file = root.openNextFile();
yield();
}
root.close();
}
// can trigger watchdog if too much files !
void removeAllSD() {
disableWatchdog();
File root = SD_MMC.open("/");
if (!root) {
Serial.println(F("Failed to open SD root directory"));
}
File file = root.openNextFile();
while (file) {
if (!file.isDirectory()) {
tempPath.remove(0);
tempPath += String("/") + file.name();
Serial.print(F("Delete file from SD : ")); Serial.println(tempPath);
file.close();
if (!SD_MMC.remove(tempPath)) {
Serial.println(F("Error during deletion"));
}
}
file = root.openNextFile();
yield();
}
root.close();
enableWatchdog();
}
bool isOkForWifi() {
return config.forceWifi || (
// hours
rtc.getHour(true) % config.hourModuloWifi >= config.hourStartWifi
&& rtc.getHour(true) % config.hourModuloWifi <= config.hourStopWifi
// minutes
&& rtc.getMinute() % config.minuteModuloWifi >= config.minuteStartWifi
&& rtc.getMinute() % config.minuteModuloWifi <= config.minuteStopWifi
);
}
bool isNight() {
return rtc.getHour(true) >= config.hourStartNight || rtc.getHour(true) <= config.hourStopNight;
}
bool needToTakePhoto() {
return rtc.getEpoch() - lastPhotoTakenTime >= config.timeSleepDay;
}
void initCam() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
camera_config_t configCamera;
configCamera.ledc_channel = LEDC_CHANNEL_0;
configCamera.ledc_timer = LEDC_TIMER_0;
configCamera.pin_d0 = Y2_GPIO_NUM;
configCamera.pin_d1 = Y3_GPIO_NUM;
configCamera.pin_d2 = Y4_GPIO_NUM;
configCamera.pin_d3 = Y5_GPIO_NUM;
configCamera.pin_d4 = Y6_GPIO_NUM;
configCamera.pin_d5 = Y7_GPIO_NUM;
configCamera.pin_d6 = Y8_GPIO_NUM;
configCamera.pin_d7 = Y9_GPIO_NUM;
configCamera.pin_xclk = XCLK_GPIO_NUM;
configCamera.pin_pclk = PCLK_GPIO_NUM;
configCamera.pin_vsync = VSYNC_GPIO_NUM;
configCamera.pin_href = HREF_GPIO_NUM;
configCamera.pin_sscb_sda = SIOD_GPIO_NUM;
configCamera.pin_sscb_scl = SIOC_GPIO_NUM;
configCamera.pin_pwdn = PWDN_GPIO_NUM;
configCamera.pin_reset = RESET_GPIO_NUM;
configCamera.xclk_freq_hz = 20000000;
configCamera.pixel_format = PIXFORMAT_JPEG;
if (psramFound()) {
configCamera.frame_size = FRAMESIZE_UXGA;
configCamera.jpeg_quality = 8;
configCamera.fb_count = 2;
} else {
configCamera.frame_size = FRAMESIZE_SVGA;
configCamera.jpeg_quality = 12;
configCamera.fb_count = 1;
}
pinMode(4, INPUT);
digitalWrite(4, LOW);
rtc_gpio_hold_dis(GPIO_NUM_4);
// Init Camera
esp_err_t err = esp_camera_init(&configCamera);
if (err != ESP_OK) {
Serial.print(F("Camera init failed with error 0x")); Serial.println(err, HEX);
goToSleep(config.timeSleepDay);
}
sensor_t * s = esp_camera_sensor_get();
s->set_whitebal(s, 1);
s->set_awb_gain(s, 1);
s->set_wb_mode(s, 0);
delay(5000);
}
void initTime() {
long eepromTime = config.epoch;
Serial.print(F("RTC epoch : ")); Serial.println(rtc.getEpoch());
Serial.print(F("EEPROM epoch : ")); Serial.println(eepromTime);
Serial.print(F("CURRENT_EPOCH : ")); Serial.println(CURRENT_EPOCH);
if (eepromTime > CURRENT_EPOCH) {
Serial.print(F("Set time from EEPROM to ")); Serial.println(eepromTime);
rtc.setTime(eepromTime);
} else if (rtc.getEpoch() < CURRENT_EPOCH) {
Serial.print(F("Set time from build to ")); Serial.println(CURRENT_EPOCH);
rtc.setTime(CURRENT_EPOCH);
}
Serial.println(rtc.getTimeDate());
}
void initWifi() {
Serial.println(F("Start WiFi SSID : HiHiHi"));
adc_power_acquire();
WiFi.disconnect(false);
WiFi.softAP(WIFI_SSID, WIFI_PASSWORD);
Serial.print(F("AP IP address: ")); Serial.println(WiFi.softAPIP());
if (!MDNS.begin("hihihi")) {
Serial.println(F("Error setting up MDNS responder!"));
}
Serial.println(F("mDNS started hihihi.local"));
server.on("/take", HTTP_GET, handleTakePhoto);
server.on("/time", HTTP_GET, handleSetTime);
server.on("/wifi", HTTP_GET, handleToggleWifi);
server.on("/set", HTTP_POST, handleSetConfig);
server.on("/wget", HTTP_GET, handleFileWget);
server.on("/format", HTTP_GET, handleFormat);
server.serveStatic("/photos_sd", SD_MMC, "/");
server.serveStatic("/photos_internal", LittleFS, "/");
server.onNotFound(handleFileList);
server.begin();
}
void initStorage() {
if (!SD_MMC.begin()) {
Serial.println(F("SD Card Mount Failed"));
} else {
if (SD_MMC.cardType() == CARD_NONE) {
Serial.println(F("No SD Card attached"));
}
}
if (!LittleFS.begin(true)) {
Serial.println(F("Error during LittleFS init"));
}
}
void setup() {
lastPhotoPath.reserve(64);
tempPath.reserve(64);
outputHtml.reserve(4096);
Serial.begin(115200);
Serial.println(F("Starting"));
getConfig();
initTime();
initCam();
initStorage();
if (isOkForWifi()) {
initWifi();
}
}
void loop() {
if (needToTakePhoto()) {
takePhoto();
}
if (!isOkForWifi()) {
if (isNight()) {
Serial.println(F("It's night !"));
goToSleep(config.timeSleepNight);
} else {
goToSleep(config.timeSleepDay);
}
}
}