-
Notifications
You must be signed in to change notification settings - Fork 56
/
ESP32-CAM-Video-Recorder-junior-50x-lpmod.ino
2082 lines (1606 loc) · 71.2 KB
/
ESP32-CAM-Video-Recorder-junior-50x-lpmod.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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
ESP32-CAM-Video-Recorder-junior
This program records an mjpeg avi video to the sd card of an ESP32-CAM.
It is the junior version of https://github.com/jameszah/ESP32-CAM-Video-Recorder
which has 100 other features of wifi, streaming video, http control, telegram updates, pir control,
touch control, ftp downloads, .... and other things that make it very big and complex.
Just set a few parameters, compile and download, and it will record on power-on, until sd is full, or power-off.
Then pull out the sd and move it to your computer, and you will see all but the last file avi which died during the unplug.
The files will have the name such as:
desklens10.003.avi
"desklens" is your devname
10 - is a number stored in eprom that will increase everytime your device boots
3 - is the 3rd file created during the current boot
Small red led on the back blinks with every frame.
by James Zahary Sep 12, 2020
- v50 apr 13, 2021 - tidy
- v50lpmod apr 28, 2021 - shut off low power modem
https://github.com/jameszah/ESP32-CAM-Video-Recorder-junior
jameszah/ESP32-CAM-Video-Recorder-junior is licensed under the
GNU General Public License v3.0
The is Arduino code, with standard setup for ESP32-CAM
- Board ESP32 Wrover Module
- Partition Scheme Huge APP (3MB No OTA)
- or with AI Thinker ESP32-CAM
Compiled with Arduino 1.8.13, and esp32-arduino core version 1.06, which is the current version as of Apr 13, 2021.
... and then it will compile the Arduuino esp32-core with ov5640 support, and use the following other libraries.
Using library FS at version 1.0 in folder: C:\ArduinoPortable\arduino-1.8.13\portable\packages\esp32\hardware\esp32\1.0.6\libraries\FS
Using library SD_MMC at version 1.0 in folder: C:\ArduinoPortable\arduino-1.8.13\portable\packages\esp32\hardware\esp32\1.0.6\libraries\SD_MMC
Using library EEPROM at version 1.0.3 in folder: C:\ArduinoPortable\arduino-1.8.13\portable\packages\esp32\hardware\esp32\1.0.6\libraries\EEPROM
Using library WiFi at version 1.0 in folder: C:\ArduinoPortable\arduino-1.8.13\portable\packages\esp32\hardware\esp32\1.0.6\libraries\WiFi
Using library WiFiManager at version 2.0.3-alpha in folder: C:\ArduinoPortable\arduino-1.8.13\portable\sketchbook\libraries\WiFiManager
Using library WebServer at version 1.0 in folder: C:\ArduinoPortable\arduino-1.8.13\portable\packages\esp32\hardware\esp32\1.0.6\libraries\WebServer
Using library DNSServer at version 1.1.0 in folder: C:\ArduinoPortable\arduino-1.8.13\portable\packages\esp32\hardware\esp32\1.0.6\libraries\DNSServer
Using library ESPmDNS at version 1.0 in folder: C:\ArduinoPortable\arduino-1.8.13\portable\packages\esp32\hardware\esp32\1.0.6\libraries\ESPmDNS
Using library HTTPClient at version 1.2 in folder: C:\ArduinoPortable\arduino-1.8.13\portable\packages\esp32\hardware\esp32\1.0.6\libraries\HTTPClient
Using library WiFiClientSecure at version 1.0 in folder: C:\ArduinoPortable\arduino-1.8.13\portable\packages\esp32\hardware\esp32\1.0.6\libraries\WiFiClientSecure
Sketch uses 1107274 bytes (35%) of program storage space. Maximum is 3145728 bytes.
Global variables use 59860 bytes (18%) of dynamic memory, leaving 267820 bytes for local variables. Maximum is 327680 bytes.
*/
//#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
#include "esp_log.h"
#include "esp_http_server.h"
#include "esp_camera.h"
#include "sensor.h"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// user edits here:
static const char vernum[] = "v50lpmod";
char devname[30];
String devstr = "desklens";
int IncludeInternet = 0; // 0 for no internet, 1 for time only, 2 streaming with WiFiMan, 3 ssid in file, 4 default internet on and file
const char* ssid = "jzjzjz";
const char* password = "jzjzjz";
// https://sites.google.com/a/usapiens.com/opnode/time-zones -- find your timezone here
String TIMEZONE = "GMT0BST,M3.5.0/01,M10.5.0/02";
#define Lots_of_Stats 1
int framesize = FRAMESIZE_HD;
int quality = 12;
int framesizeconfig = FRAMESIZE_UXGA;
int qualityconfig = 5;
int buffersconfig = 3;
int avi_length = 1800; // how long a movie in seconds -- 1800 sec = 30 min
int frame_interval = 0; // record at full speed
int speed_up_factor = 1; // play at realtime
int stream_delay = 500; // minimum of 500 ms delay between frames
int MagicNumber = 12; // change this number to reset the eprom in your esp32 for file numbers
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool configfile = false;
bool InternetOff = true;
bool reboot_now = false;
String cssid;
String cpass;
String czone;
TaskHandle_t the_camera_loop_task;
TaskHandle_t the_sd_loop_task;
TaskHandle_t the_streaming_loop_task;
SemaphoreHandle_t wait_for_sd;
SemaphoreHandle_t sd_go;
long current_frame_time;
long last_frame_time;
// https://github.com/espressif/esp32-camera/issues/182
#define fbs 8 // was 64 -- how many kb of static ram for psram -> sram buffer for sd write
uint8_t framebuffer_static[fbs * 1024 + 20];
// 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
camera_fb_t * fb_curr = NULL;
camera_fb_t * fb_next = NULL;
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "soc/soc.h"
#include "soc/cpu.h"
#include "soc/rtc_cntl_reg.h"
static esp_err_t cam_err;
float most_recent_fps = 0;
int most_recent_avg_framesize = 0;
uint8_t* framebuffer;
int framebuffer_len;
int first = 1;
long frame_start = 0;
long frame_end = 0;
long frame_total = 0;
long frame_average = 0;
long loop_average = 0;
long loop_total = 0;
long total_frame_data = 0;
long last_frame_length = 0;
int done = 0;
long avi_start_time = 0;
long avi_end_time = 0;
int start_record = 0;
int start_record_2nd_opinion = -2;
int start_record_1st_opinion = -1;
int we_are_already_stopped = 0;
long total_delay = 0;
long bytes_before_last_100_frames = 0;
long time_before_last_100_frames = 0;
long time_in_loop = 0;
long time_in_camera = 0;
long time_in_sd = 0;
long time_in_good = 0;
long time_total = 0;
long time_in_web1 = 0;
long time_in_web2 = 0;
long delay_wait_for_sd = 0;
long wait_for_cam = 0;
int do_it_now = 0;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Avi Writer Stuff here
// MicroSD
#include "driver/sdmmc_host.h"
#include "driver/sdmmc_defs.h"
#include "sdmmc_cmd.h"
#include "esp_vfs_fat.h"
#include "FS.h"
#include <SD_MMC.h>
File logfile;
File avifile;
File idxfile;
char avi_file_name[100];
static int i = 0;
uint16_t frame_cnt = 0;
uint16_t remnant = 0;
uint32_t length = 0;
uint32_t startms;
uint32_t elapsedms;
uint32_t uVideoLen = 0;
int bad_jpg = 0;
int extend_jpg = 0;
int normal_jpg = 0;
int file_number = 0;
int file_group = 0;
long boot_time = 0;
long totalp;
long totalw;
#define BUFFSIZE 512
uint8_t buf[BUFFSIZE];
#define AVIOFFSET 240 // AVI main header length
unsigned long movi_size = 0;
unsigned long jpeg_size = 0;
unsigned long idx_offset = 0;
uint8_t zero_buf[4] = {0x00, 0x00, 0x00, 0x00};
uint8_t dc_buf[4] = {0x30, 0x30, 0x64, 0x63}; // "00dc"
uint8_t dc_and_zero_buf[8] = {0x30, 0x30, 0x64, 0x63, 0x00, 0x00, 0x00, 0x00};
uint8_t avi1_buf[4] = {0x41, 0x56, 0x49, 0x31}; // "AVI1"
uint8_t idx1_buf[4] = {0x69, 0x64, 0x78, 0x31}; // "idx1"
struct frameSizeStruct {
uint8_t frameWidth[2];
uint8_t frameHeight[2];
};
// data structure from here https://github.com/s60sc/ESP32-CAM_MJPEG2SD/blob/master/avi.cpp, extended for ov5640
static const frameSizeStruct frameSizeData[] = {
{{0x60, 0x00}, {0x60, 0x00}}, // FRAMESIZE_96X96, // 96x96
{{0xA0, 0x00}, {0x78, 0x00}}, // FRAMESIZE_QQVGA, // 160x120
{{0xB0, 0x00}, {0x90, 0x00}}, // FRAMESIZE_QCIF, // 176x144
{{0xF0, 0x00}, {0xB0, 0x00}}, // FRAMESIZE_HQVGA, // 240x176
{{0xF0, 0x00}, {0xF0, 0x00}}, // FRAMESIZE_240X240, // 240x240
{{0x40, 0x01}, {0xF0, 0x00}}, // FRAMESIZE_QVGA, // 320x240 framessize
{{0x90, 0x01}, {0x28, 0x01}}, // FRAMESIZE_CIF, // 400x296 bytes per buffer required in psram - quality must be higher number (lower quality) than config quality
{{0xE0, 0x01}, {0x40, 0x01}}, // FRAMESIZE_HVGA, // 480x320 low qual med qual high quality
{{0x80, 0x02}, {0xE0, 0x01}}, // FRAMESIZE_VGA, // 640x480 8 11+ ## 6-10 ## 0-5 indoor(56,COUNT=3) (56,COUNT=2) (56,count=1)
// 38,400 61,440 153,600
{{0x20, 0x03}, {0x58, 0x02}}, // FRAMESIZE_SVGA, // 800x600 9
{{0x00, 0x04}, {0x00, 0x03}}, // FRAMESIZE_XGA, // 1024x768 10
{{0x00, 0x05}, {0xD0, 0x02}}, // FRAMESIZE_HD, // 1280x720 11 115,200 184,320 460,800 (11)50.000 25.4fps (11)50.000 12fps (11)50,000 12.7fps
{{0x00, 0x05}, {0x00, 0x04}}, // FRAMESIZE_SXGA, // 1280x1024 12
{{0x40, 0x06}, {0xB0, 0x04}}, // FRAMESIZE_UXGA, // 1600x1200 13 240,000 384,000 960,000
// 3MP Sensors
{{0x80, 0x07}, {0x38, 0x04}}, // FRAMESIZE_FHD, // 1920x1080 14 259,200 414,720 1,036,800 (11)210,000 5.91fps
{{0xD0, 0x02}, {0x00, 0x05}}, // FRAMESIZE_P_HD, // 720x1280 15
{{0x60, 0x03}, {0x00, 0x06}}, // FRAMESIZE_P_3MP, // 864x1536 16
{{0x00, 0x08}, {0x00, 0x06}}, // FRAMESIZE_QXGA, // 2048x1536 17 393,216 629,146 1,572,864
// 5MP Sensors
{{0x00, 0x0A}, {0xA0, 0x05}}, // FRAMESIZE_QHD, // 2560x1440 18 460,800 737,280 1,843,200 (11)400,000 3.5fps (11)330,000 1.95fps
{{0x00, 0x0A}, {0x40, 0x06}}, // FRAMESIZE_WQXGA, // 2560x1600 19
{{0x38, 0x04}, {0x80, 0x07}}, // FRAMESIZE_P_FHD, // 1080x1920 20
{{0x00, 0x0A}, {0x80, 0x07}} // FRAMESIZE_QSXGA, // 2560x1920 21 614,400 983,040 2,457,600 (15)425,000 3.25fps (15)382,000 1.7fps (15)385,000 1.7fps
};
const int avi_header[AVIOFFSET] PROGMEM = {
0x52, 0x49, 0x46, 0x46, 0xD8, 0x01, 0x0E, 0x00, 0x41, 0x56, 0x49, 0x20, 0x4C, 0x49, 0x53, 0x54,
0xD0, 0x00, 0x00, 0x00, 0x68, 0x64, 0x72, 0x6C, 0x61, 0x76, 0x69, 0x68, 0x38, 0x00, 0x00, 0x00,
0xA0, 0x86, 0x01, 0x00, 0x80, 0x66, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x02, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4C, 0x49, 0x53, 0x54, 0x84, 0x00, 0x00, 0x00,
0x73, 0x74, 0x72, 0x6C, 0x73, 0x74, 0x72, 0x68, 0x30, 0x00, 0x00, 0x00, 0x76, 0x69, 0x64, 0x73,
0x4D, 0x4A, 0x50, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x74, 0x72, 0x66,
0x28, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x00,
0x01, 0x00, 0x18, 0x00, 0x4D, 0x4A, 0x50, 0x47, 0x00, 0x84, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x4E, 0x46, 0x4F,
0x10, 0x00, 0x00, 0x00, 0x6A, 0x61, 0x6D, 0x65, 0x73, 0x7A, 0x61, 0x68, 0x61, 0x72, 0x79, 0x20,
0x76, 0x35, 0x30, 0x20, 0x4C, 0x49, 0x53, 0x54, 0x00, 0x01, 0x0E, 0x00, 0x6D, 0x6F, 0x76, 0x69,
};
//
// Writes an uint32_t in Big Endian at current file position
//
static void inline print_quartet(unsigned long i, File fd) {
uint8_t y[4];
y[0] = i % 0x100;
y[1] = (i >> 8) % 0x100;
y[2] = (i >> 16) % 0x100;
y[3] = (i >> 24) % 0x100;
size_t i1_err = fd.write(y , 4);
}
//
// Writes 2 uint32_t in Big Endian at current file position
//
static void inline print_2quartet(unsigned long i, unsigned long j, File fd) {
uint8_t y[8];
y[0] = i % 0x100;
y[1] = (i >> 8) % 0x100;
y[2] = (i >> 16) % 0x100;
y[3] = (i >> 24) % 0x100;
y[4] = j % 0x100;
y[5] = (j >> 8) % 0x100;
y[6] = (j >> 16) % 0x100;
y[7] = (j >> 24) % 0x100;
size_t i1_err = fd.write(y , 8);
}
//
// if we have no camera, or sd card, then flash rear led on and off to warn the human SOS - SOS
//
void major_fail() {
Serial.println(" ");
logfile.close();
for (int i = 0; i < 10; i++) { // 10 loops or about 100 seconds then reboot
for (int j = 0; j < 3; j++) {
digitalWrite(33, LOW); delay(150);
digitalWrite(33, HIGH); delay(150);
}
delay(1000);
for (int j = 0; j < 3; j++) {
digitalWrite(33, LOW); delay(500);
digitalWrite(33, HIGH); delay(500);
}
delay(1000);
Serial.print("Major Fail "); Serial.print(i); Serial.print(" / "); Serial.println(10);
}
ESP.restart();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
static esp_err_t config_camera() {
camera_config_t config;
//Serial.println("config camera");
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000; // 10000000 or 20000000 -- 100 is faster with v1.04 // 200 is faster with v1.06 // 16500000 is an option
config.pixel_format = PIXFORMAT_JPEG;
Serial.printf("Frame config %d, quality config %d, buffers config %d\n", framesizeconfig, qualityconfig, buffersconfig);
config.frame_size = (framesize_t)framesizeconfig;
config.jpeg_quality = qualityconfig;
config.fb_count = buffersconfig;
if (Lots_of_Stats) {
Serial.printf("Before camera config ...");
Serial.printf("Internal Total heap %d, internal Free Heap %d, ", ESP.getHeapSize(), ESP.getFreeHeap());
Serial.printf("SPIRam Total heap %d, SPIRam Free Heap %d\n", ESP.getPsramSize(), ESP.getFreePsram());
}
esp_err_t cam_err = ESP_FAIL;
int attempt = 5;
while (attempt && cam_err != ESP_OK) {
cam_err = esp_camera_init(&config);
if (cam_err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", cam_err);
digitalWrite(PWDN_GPIO_NUM, 1);
delay(500);
digitalWrite(PWDN_GPIO_NUM, 0); // power cycle the camera (OV2640)
attempt--;
}
}
if (Lots_of_Stats) {
Serial.printf("After camera config ...");
Serial.printf("Internal Total heap %d, internal Free Heap %d, ", ESP.getHeapSize(), ESP.getFreeHeap());
Serial.printf("SPIRam Total heap %d, SPIRam Free Heap %d\n", ESP.getPsramSize(), ESP.getFreePsram());
}
if (cam_err != ESP_OK) {
major_fail();
}
sensor_t * ss = esp_camera_sensor_get();
///ss->set_hmirror(ss, 1); // 0 = disable , 1 = enable
///ss->set_vflip(ss, 1); // 0 = disable , 1 = enable
Serial.printf("\nCamera started correctly, Type is %x (hex) of 9650, 7725, 2640, 3660, 5640\n\n", ss->id.PID);
if (ss->id.PID == OV5640_PID ) {
//Serial.println("56 - going mirror");
ss->set_hmirror(ss, 1); // 0 = disable , 1 = enable
} else {
ss->set_hmirror(ss, 0); // 0 = disable , 1 = enable
}
ss->set_quality(ss, quality);
ss->set_framesize(ss, (framesize_t)framesize);
ss->set_brightness(ss, 1); //up the blightness just a bit
ss->set_saturation(ss, -2); //lower the saturation
delay(800);
for (int j = 0; j < 4; j++) {
camera_fb_t * fb = esp_camera_fb_get(); // get_good_jpeg();
if (!fb) {
Serial.println("Camera Capture Failed");
} else {
Serial.print("Pic, len="); Serial.print(fb->len);
Serial.printf(", new fb %X\n", (long)fb->buf);
esp_camera_fb_return(fb);
delay(50);
}
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
static esp_err_t init_sdcard()
{
int succ = SD_MMC.begin("/sdcard", true);
if (succ) {
Serial.printf("SD_MMC Begin: %d\n", succ);
uint8_t cardType = SD_MMC.cardType();
Serial.print("SD_MMC Card Type: ");
if (cardType == CARD_MMC) {
Serial.println("MMC");
} else if (cardType == CARD_SD) {
Serial.println("SDSC");
} else if (cardType == CARD_SDHC) {
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024);
Serial.printf("SD_MMC Card Size: %lluMB\n", cardSize);
} else {
Serial.printf("Failed to mount SD card VFAT filesystem. \n");
Serial.println("Do you have an SD Card installed?");
Serial.println("Check pin 12 and 13, not grounded, or grounded with 10k resistors!\n\n");
major_fail();
}
return ESP_OK;
}
void read_config_file() {
// put a file "config.txt" onto SD card, to set parameters different from your hardcoded parameters
// it should look like this - one paramter per line, in the correct order, followed by 2 spaces, and any comments you choose
/*
desklens // camera name for files, mdns, etc
11 // framesize 9=svga, 10=xga, 11=hd, 12=sxga, 13=uxga, 14=fhd, 17=qxga, 18=qhd, 21=qsxga
8 // quality 0-63, lower the better, 10 good start, must be higher than "quality config"
11 // framesize config - must be equal or higher than framesize
5 / quality config - high q 0..5, med q 6..10, low q 11+
3 // buffers - 1 is half speed of 3, but you might run out od memory with 3 and framesize > uxga
900 // length of video in seconds
0 // interval - ms between frames - 0 for fastest, or 500 for 2fps, 10000 for 10 sec/frame
1 // speedup - multiply framerate - 1 for realtime, 24 for record at 1fps, play at 24fps or24x
0 // streamdelay - ms between streaming frames - 0 for fast as possible, 500 for 2fps
4 // 0 no internet, 1 get time then shutoff, 2 streaming using wifiman, 3 for use ssid names below default off, 4 names below default on
MST7MDT,M3.2.0/2:00:00,M11.1.0/2:00:00 // timezone - this is mountain time, find timezone here https://sites.google.com/a/usapiens.com/opnode/time-zones
ssid1234 // ssid
mrpeanut // ssid password
Lines above are rigid - do not delete lines, must have 2 spaces after the number or string
*/
File config_file = SD_MMC.open("/config.txt", "r");
if (!config_file) {
Serial.println("Failed to open config_file for reading");
} else {
String junk;
Serial.println("Reading config.txt");
String cname = config_file.readStringUntil(' ');
junk = config_file.readStringUntil('\n');
int cframesize = config_file.parseInt();
junk = config_file.readStringUntil('\n');
int cquality = config_file.parseInt();
junk = config_file.readStringUntil('\n');
int cframesizeconfig = config_file.parseInt();
junk = config_file.readStringUntil('\n');
int cqualityconfig = config_file.parseInt();
junk = config_file.readStringUntil('\n');
int cbuffersconfig = config_file.parseInt();
junk = config_file.readStringUntil('\n');
int clength = config_file.parseInt();
junk = config_file.readStringUntil('\n');
int cinterval = config_file.parseInt();
junk = config_file.readStringUntil('\n');
int cspeedup = config_file.parseInt();
junk = config_file.readStringUntil('\n');
int cstreamdelay = config_file.parseInt();
junk = config_file.readStringUntil('\n');
int cinternet = config_file.parseInt();
junk = config_file.readStringUntil('\n');
String czone = config_file.readStringUntil(' ');
junk = config_file.readStringUntil('\n');
cssid = config_file.readStringUntil(' ');
junk = config_file.readStringUntil('\n');
cpass = config_file.readStringUntil(' ');
junk = config_file.readStringUntil('\n');
config_file.close();
Serial.printf("========= Data fram config.txt =========\n");
Serial.printf("Name %s\n", cname); logfile.printf("Name %s\n", cname);
Serial.printf("Framesize %d\n", cframesize); logfile.printf("Framesize %d\n", cframesize);
Serial.printf("Quality %d\n", cquality); logfile.printf("Quality %d\n", cquality);
Serial.printf("Framesize config %d\n", cframesizeconfig); logfile.printf("Framesize config%d\n", cframesizeconfig);
Serial.printf("Quality config %d\n", cqualityconfig); logfile.printf("Quality config%d\n", cqualityconfig);
Serial.printf("Buffers config %d\n", cbuffersconfig); logfile.printf("Buffers config %d\n", cbuffersconfig);
Serial.printf("Length %d\n", clength); logfile.printf("Length %d\n", clength);
Serial.printf("Interval %d\n", cinterval); logfile.printf("Interval %d\n", cinterval);
Serial.printf("Speedup %d\n", cspeedup); logfile.printf("Speedup %d\n", cspeedup);
Serial.printf("Streamdelay %d\n", cstreamdelay); logfile.printf("Streamdelay %d\n", cstreamdelay);
Serial.printf("Internet %d\n", cinternet); logfile.printf("Internet %d\n", cinternet);
//Serial.printf("Zone len %d, %s\n", czone.length(), czone); //logfile.printf("Zone len %d, %s\n", czone.length(), czone);
Serial.printf("Zone len %d\n", czone.length()); logfile.printf("Zone len %d\n", czone.length());
Serial.printf("ssid %s\n", cssid); logfile.printf("ssid %s\n", cssid);
Serial.printf("pass %s\n", cpass); logfile.printf("pass %s\n", cpass);
framesize = cframesize;
quality = cquality;
framesizeconfig = cframesizeconfig;
qualityconfig = cqualityconfig;
buffersconfig = cbuffersconfig;
avi_length = clength;
frame_interval = cinterval;
speed_up_factor = cspeedup;
stream_delay = cstreamdelay;
IncludeInternet = cinternet;
configfile = true;
TIMEZONE = czone;
cname.toCharArray(devname, cname.length() + 1);
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// delete_old_stuff() - delete oldest files to free diskspace
//
void listDir( const char * dirname, uint8_t levels) {
Serial.printf("Listing directory: %s\n", "/");
File root = SD_MMC.open("/");
if (!root) {
Serial.println("Failed to open directory");
return;
}
if (!root.isDirectory()) {
Serial.println("Not a directory");
return;
}
File filex = root.openNextFile();
while (filex) {
if (filex.isDirectory()) {
Serial.print(" DIR : ");
Serial.println(filex.name());
if (levels) {
listDir( filex.name(), levels - 1);
}
} else {
Serial.print(" FILE: ");
Serial.print(filex.name());
Serial.print(" SIZE: ");
Serial.println(filex.size());
}
filex = root.openNextFile();
}
}
void delete_old_stuff() {
Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024));
Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024));
//listDir( "/", 0);
float full = 1.0 * SD_MMC.usedBytes() / SD_MMC.totalBytes();;
if (full < 0.8) {
Serial.printf("Nothing deleted, %.1f%% disk full\n", 100.0 * full);
} else {
Serial.printf("Disk is %.1f%% full ... deleting oldest file\n", 100.0 * full);
while (full > 0.8) {
double del_number = 999999999;
char del_numbername[50];
File f = SD_MMC.open("/");
File file = f.openNextFile();
while (file) {
//Serial.println(file.name());
if (!file.isDirectory()) {
char foldname[50];
strcpy(foldname, file.name());
for ( int x = 0; x < 50; x++) {
if ( (foldname[x] >= 0x30 && foldname[x] <= 0x39) || foldname[x] == 0x2E) {
} else {
if (foldname[x] != 0) foldname[x] = 0x20;
}
}
double i = atof(foldname);
if ( i > 0 && i < del_number) {
strcpy (del_numbername, file.name());
del_number = i;
}
//Serial.printf("Name is %s, number is %f\n", foldname, i);
}
file = f.openNextFile();
}
Serial.printf("lowest is Name is %s, number is %f\n", del_numbername, del_number);
if (del_number < 999999999) {
deleteFolderOrFile(del_numbername);
}
full = 1.0 * SD_MMC.usedBytes() / SD_MMC.totalBytes();
Serial.printf("Disk is %.1f%% full ... \n", 100.0 * full);
f.close();
}
}
}
void deleteFolderOrFile(const char * val) {
// Function provided by user @gemi254
Serial.printf("Deleting : %s\n", val);
File f = SD_MMC.open(val);
if (!f) {
Serial.printf("Failed to open %s\n", val);
return;
}
if (f.isDirectory()) {
File file = f.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR : ");
Serial.println(file.name());
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.print(file.size());
if (SD_MMC.remove(file.name())) {
Serial.println(" deleted.");
} else {
Serial.println(" FAILED.");
}
}
file = f.openNextFile();
}
f.close();
//Remove the dir
if (SD_MMC.rmdir(val)) {
Serial.printf("Dir %s removed\n", val);
} else {
Serial.println("Remove dir failed");
}
} else {
//Remove the file
if (SD_MMC.remove(val)) {
Serial.printf("File %s deleted\n", val);
} else {
Serial.println("Delete failed");
}
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// get_good_jpeg() - take a picture and make sure it has a good jpeg
//
camera_fb_t * get_good_jpeg() {
camera_fb_t * fb;
long start;
int failures = 0;
do {
int fblen = 0;
int foundffd9 = 0;
long bp = millis();
long mstart = micros();
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera Capture Failed");
failures++;
} else {
long mdelay = micros() - mstart;
int get_fail = 0;
totalp = totalp + millis() - bp;
time_in_camera = totalp;
fblen = fb->len;
for (int j = 1; j <= 1025; j++) {
if (fb->buf[fblen - j] != 0xD9) {
// no d9, try next for
} else { //Serial.println("Found a D9");
if (fb->buf[fblen - j - 1] == 0xFF ) { //Serial.print("Found the FFD9, junk is "); Serial.println(j);
if (j == 1) {
normal_jpg++;
} else {
extend_jpg++;
}
foundffd9 = 1;
if (Lots_of_Stats) {
if (j > 900) { // rarely happens - sometimes on 2640
Serial.print("Frame "); Serial.print(frame_cnt); logfile.print("Frame "); logfile.print(frame_cnt);
Serial.print(", Len = "); Serial.print(fblen); logfile.print(", Len = "); logfile.print(fblen);
//Serial.print(", Correct Len = "); Serial.print(fblen - j + 1);
Serial.print(", Extra Bytes = "); Serial.println( j - 1); logfile.print(", Extra Bytes = "); logfile.println( j - 1);
logfile.flush();
}
if ( frame_cnt % 100 == 50) {
Serial.printf("Frame %6d, len %6d, extra %4d, cam time %7d ", frame_cnt, fblen, j - 1, mdelay / 1000);
logfile.printf("Frame %6d, len %6d, extra %4d, cam time %7d ", frame_cnt, fblen, j - 1, mdelay / 1000);
do_it_now = 1;
}
}
break;
}
}
}
if (!foundffd9) {
bad_jpg++;
Serial.printf("Bad jpeg, Frame %d, Len = %d \n", frame_cnt, fblen);
logfile.printf("Bad jpeg, Frame %d, Len = %d\n", frame_cnt, fblen);
esp_camera_fb_return(fb);
failures++;
} else {
break;
// count up the useless bytes
}
}
} while (failures < 10); // normally leave the loop with a break()
// if we get 10 bad frames in a row, then quality parameters are too high - set them lower (+5), and start new movie
if (failures == 10) {
Serial.printf("10 failures");
logfile.printf("10 failures");
logfile.flush();
sensor_t * ss = esp_camera_sensor_get();
int qual = ss->status.quality ;
ss->set_quality(ss, qual + 5);
quality = qual + 5;
Serial.printf("\n\nDecreasing quality due to frame failures %d -> %d\n\n", qual, qual + 5);
logfile.printf("\n\nDecreasing quality due to frame failures %d -> %d\n\n", qual, qual + 5);
delay(1000);
start_record = 0;
//reboot_now = true;
}
return fb;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// eprom functions - increment the file_group, so files are always unique
//
#include <EEPROM.h>
struct eprom_data {
int eprom_good;
int file_group;
};
void do_eprom_read() {
eprom_data ed;
EEPROM.begin(200);
EEPROM.get(0, ed);
if (ed.eprom_good == MagicNumber) {
Serial.println("Good settings in the EPROM ");
file_group = ed.file_group;
file_group++;
Serial.print("New File Group "); Serial.println(file_group );
} else {
Serial.println("No settings in EPROM - Starting with File Group 1 ");
file_group = 1;
}
do_eprom_write();
file_number = 1;
}
void do_eprom_write() {
eprom_data ed;
ed.eprom_good = MagicNumber;
ed.file_group = file_group;
Serial.println("Writing to EPROM ...");
EEPROM.begin(200);
EEPROM.put(0, ed);
EEPROM.commit();
EEPROM.end();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Make the avi functions
//
// start_avi() - open the file and write headers
// another_pic_avi() - write one more frame of movie
// end_avi() - write the final parameters and close the file
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// start_avi - open the files and write in headers
//
static esp_err_t start_avi() {
long start = millis();
Serial.println("Starting an avi ");
sprintf(avi_file_name, "/%s%d.%03d.avi", devname, file_group, file_number);
file_number++;
avifile = SD_MMC.open(avi_file_name, "w");
idxfile = SD_MMC.open("/idx.tmp", "w");
if (avifile) {
Serial.printf("File open: %s\n", avi_file_name);
logfile.printf("File open: %s\n", avi_file_name);
} else {
Serial.println("Could not open file");
major_fail();
}
if (idxfile) {
//Serial.printf("File open: %s\n", "//idx.tmp");
} else {
Serial.println("Could not open file /idx.tmp");
major_fail();
}
for ( i = 0; i < AVIOFFSET; i++){
char ch = pgm_read_byte(&avi_header[i]);
buf[i] = ch;
}
memcpy(buf + 0x40, frameSizeData[framesize].frameWidth, 2);
memcpy(buf + 0xA8, frameSizeData[framesize].frameWidth, 2);
memcpy(buf + 0x44, frameSizeData[framesize].frameHeight, 2);
memcpy(buf + 0xAC, frameSizeData[framesize].frameHeight, 2);
size_t err = avifile.write(buf, AVIOFFSET);
avifile.seek( AVIOFFSET, SeekSet);
Serial.print(F("\nRecording "));
Serial.print(avi_length);
Serial.println(" seconds.");
startms = millis();
totalp = 0;
totalw = 0;
jpeg_size = 0;
movi_size = 0;
uVideoLen = 0;
idx_offset = 4;
bad_jpg = 0;
extend_jpg = 0;
normal_jpg = 0;
time_in_loop = 0;
time_in_camera = 0;
time_in_sd = 0;
time_in_good = 0;
time_total = 0;
time_in_web1 = 0;
time_in_web2 = 0;
delay_wait_for_sd = 0;
wait_for_cam = 0;
time_in_sd += (millis() - start);
logfile.flush();
avifile.flush();
} // end of start avi
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// another_save_avi saves another frame to the avi file, uodates index
// -- pass in a fb pointer to the frame to add
//
static esp_err_t another_save_avi(camera_fb_t * fb ) {
long start = millis();
int fblen;
fblen = fb->len;
int fb_block_length;
uint8_t* fb_block_start;
jpeg_size = fblen;
remnant = (4 - (jpeg_size & 0x00000003)) & 0x00000003;
long bw = millis();
long frame_write_start = millis();
framebuffer_static[0] = 0x30; // "00dc"
framebuffer_static[1] = 0x30;
framebuffer_static[2] = 0x64;
framebuffer_static[3] = 0x63;
int jpeg_size_rem = jpeg_size + remnant;
framebuffer_static[4] = jpeg_size_rem % 0x100;
framebuffer_static[5] = (jpeg_size_rem >> 8) % 0x100;
framebuffer_static[6] = (jpeg_size_rem >> 16) % 0x100;
framebuffer_static[7] = (jpeg_size_rem >> 24) % 0x100;