-
Notifications
You must be signed in to change notification settings - Fork 26
/
ESP32-CAM-Video-Telegram_8.8x.ino
1150 lines (878 loc) · 36.9 KB
/
ESP32-CAM-Video-Telegram_8.8x.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-Telegram
This program records an mjpeg avi video in the psram of a ESP32-CAM, and sends a jpeg and a avi video to Telegram.
https://github.com/jameszah/ESP32-CAM-Video-Telegram is licensed under the
GNU General Public License v3.0
by James Zahary June 1, 2020
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 arduino-esp32 1.0.6 (latest release version) on Jun 10, 2021
Based on these two:
https://github.com/jameszah/ESP32-CAM-Video-Recorder-junior
https://github.com/jameszah/ESP32-CAM-Video-Recorder
and using a modified old version of:
https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
~~~~~~~~~~~~
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 WiFiClientSecure at version 1.0 in folder: C:\ArduinoPortable\arduino-1.8.13\portable\packages\esp32\hardware\esp32\1.0.6\libraries\WiFiClientSecure
Using library ArduinoJson at version 6.16.1 in folder: C:\ArduinoPortable\arduino-1.8.13\portable\sketchbook\libraries\ArduinoJson
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
"C:\\ArduinoPortable\\arduino-1.8.13\\portable\\packages\\esp32\\tools\\xtensa-esp32-elf-gcc\\1.22.0-97-gc752ad5-5.2.0/bin/xtensa-esp32-elf-size" -A "C:\\Users\\James\\AppData\\Local\\Temp\\arduino_build_985247/ESP32-CAM-Video-Telegram_8.8.ino.elf"
Sketch uses 1088618 bytes (34%) of program storage space. Maximum is 3145728 bytes.
Global variables use 64528 bytes (19%) of dynamic memory, leaving 263152 bytes for local variables. Maximum is 327680 bytes.
*******************************************************************/
/*******************************************************************
- original opening from Brian Lough telegram bot demo
A Telegram bot for taking a photo with an ESP32Cam
Parts used:
ESP32-CAM module* - http://s.click.aliexpress.com/e/bnXR1eYs
= Affiliate Links
Note:
- Make sure that you have either selected ESP32 Wrover Module,
or another board which has PSRAM enabled
- Choose "Huge App" partion scheme
Some of the camera code comes from Rui Santos:
https://randomnerdtutorials.com/esp32-cam-take-photo-save-microsd-card/
Written by Brian Lough
YouTube: https://www.youtube.com/brianlough
Tindie: https://www.tindie.com/stores/brianlough/
Twitter: https://twitter.com/witnessmenow
Aug 7, 2020 - jz
Mods to library and example to demonstrate
- bugfix with missing println statement
- method to send big and small jpegs
- sending a caption with a pictire
Mar 26, 2021 - jz
Mods for esp32 version 1.05
See line 250 of UniversalTelegramBot.cpp in the current github software
https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/issues/235#issue-842397567
See https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/65e6f826cbab242366d69f00cebc25cdd1e81305/src/UniversalTelegramBot.cpp#L250
*******************************************************************/
// ----------------------------
// Standard Libraries - Already Installed if you have ESP32 set up
// ----------------------------
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "esp_camera.h"
// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------
//#include <UniversalTelegramBot.h>
#include "UniversalTelegramBot.h" // use local library which is a modified copy of an old version
// Library for interacting with the Telegram API
// Search for "Telegegram" in the Library manager and install
// The universal Telegram library
// https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
#include <ArduinoJson.h>
// Library used for parsing Json from the API responses
// Search for "Arduino Json" in the Arduino Library manager
// https://github.com/bblanchon/ArduinoJson
static const char vernum[] = "pir-cam 8.8";
String devstr = "deskpir";
int max_frames = 150;
framesize_t configframesize = FRAMESIZE_VGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
int frame_interval = 0; // 0 = record at full speed, 100 = 100 ms delay between frames
float speed_up_factor = 0.5; // 1 = play at realtime, 0.5 = slow motion, 10 = speedup 10x
int framesize = FRAMESIZE_VGA; //FRAMESIZE_HD;
int quality = 10;
int qualityconfig = 5;
// Initialize Wifi connection to the router and Telegram BOT
char ssid[] = "yourssid"; // your network SSID (name)
char password[] = "yourssidpassword"; // your network key
// 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";
// you can enter your home chat_id, so the device can send you a reboot message, otherwise it responds to the chat_id talking to telegram
String chat_id = "1234567890";
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
// see here for information about getting free telegram credentials
// https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
// https://randomnerdtutorials.com/telegram-esp32-motion-detection-arduino/
bool reboot_request = false;
#define 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
#include "esp_system.h"
bool setupCamera() {
camera_config_t config;
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;
config.pixel_format = PIXFORMAT_JPEG;
//init with high specs to pre-allocate larger buffers
if (psramFound()) {
config.frame_size = configframesize;
config.jpeg_quality = qualityconfig;
config.fb_count = 4;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
}
//Serial.printf("Internal Total heap %d, internal Free Heap %d\n", ESP.getHeapSize(), ESP.getFreeHeap());
//Serial.printf("SPIRam Total heap %d, SPIRam Free Heap %d\n", ESP.getPsramSize(), ESP.getFreePsram());
static char * memtmp = (char *) malloc(32 * 1024);
static char * memtmp2 = (char *) malloc(32 * 1024); //32767
// camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return false;
}
free(memtmp2);
memtmp2 = NULL;
free(memtmp);
memtmp = NULL;
//Serial.printf("Internal Total heap %d, internal Free Heap %d\n", ESP.getHeapSize(), ESP.getFreeHeap());
//Serial.printf("SPIRam Total heap %d, SPIRam Free Heap %d\n", ESP.getPsramSize(), ESP.getFreePsram());
sensor_t * s = esp_camera_sensor_get();
// drop down frame size for higher initial frame rate
s->set_framesize(s, (framesize_t)framesize);
s->set_quality(s, quality);
delay(200);
return true;
}
#define FLASH_LED_PIN 4
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs = 5000; //mean time between scan messages
long Bot_lasttime; //last time messages' scan has been done
bool flashState = LOW;
camera_fb_t * fb = NULL;
camera_fb_t * vid_fb = NULL;
TaskHandle_t the_camera_loop_task;
void the_camera_loop (void* pvParameter) ;
static void IRAM_ATTR PIR_ISR(void* arg) ;
bool video_ready = false;
bool picture_ready = false;
bool active_interupt = false;
bool pir_enabled = false;
bool avi_enabled = false;
int avi_buf_size = 0;
int idx_buf_size = 0;
bool isMoreDataAvailable();
//////////////////////////////// send photo as 512 byte blocks or jzblocksize
int currentByte;
uint8_t* fb_buffer;
size_t fb_length;
bool isMoreDataAvailable() {
return (fb_length - currentByte);
}
uint8_t getNextByte() {
currentByte++;
return (fb_buffer[currentByte - 1]);
}
//////////////////////////////// send avi as 512 byte blocks or jzblocksize
int avi_ptr;
uint8_t* avi_buf;
size_t avi_len;
bool avi_more() {
return (avi_len - avi_ptr);
}
uint8_t avi_next() {
avi_ptr++;
return (avi_buf[avi_ptr - 1]);
}
bool dataAvailable = false;
///////////////////////////////
uint8_t * psram_avi_buf = NULL;
uint8_t * psram_idx_buf = NULL;
uint8_t * psram_avi_ptr = 0;
uint8_t * psram_idx_ptr = 0;
char strftime_buf[64];
void handleNewMessages(int numNewMessages) {
//Serial.println("handleNewMessages");
//Serial.println(String(numNewMessages));
for (int i = 0; i < numNewMessages; i++) {
chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
Serial.printf("\nGot a message %s\n", text);
String from_name = bot.messages[i].from_name;
if (from_name == "") from_name = "Guest";
String hi = "Got: ";
hi += text;
bot.sendMessage(chat_id, hi, "Markdown");
if (text == "/flash") {
flashState = !flashState;
digitalWrite(FLASH_LED_PIN, flashState);
}
if (text == "/status") {
String stat = "Device: " + devstr + "\nVer: " + String(vernum) + "\nRssi: " + String(WiFi.RSSI()) + "\nip: " + WiFi.localIP().toString() + "\nEnabled: " + pir_enabled + "\nAvi Enabled: " + avi_enabled;
if (frame_interval == 0) {
stat = stat + "\nFast 3 sec";
} else if (frame_interval == 125) {
stat = stat + "\nMed 10 sec";
} else {
stat = stat + "\nSlow 40 sec";
}
stat = stat + "\nQuality: " + quality;
bot.sendMessage(chat_id, stat, "");
}
if (text == "/reboot") {
reboot_request = true;
}
if (text == "/enable") {
pir_enabled = true;
}
if (text == "/disable") {
pir_enabled = false;
}
if (text == "/enavi") {
avi_enabled = true;
}
if (text == "/disavi") {
avi_enabled = false;
}
if (text == "/fast") {
max_frames = 150;
frame_interval = 0;
speed_up_factor = 0.5;
pir_enabled = true;
avi_enabled = true;
}
if (text == "/med") {
max_frames = 150;
frame_interval = 125;
speed_up_factor = 1;
pir_enabled = true;
avi_enabled = true;
}
if (text == "/slow") {
max_frames = 150;
frame_interval = 500;
speed_up_factor = 5;
pir_enabled = true;
avi_enabled = true;
}
if ( text == "/photo" || text == "/caption" ) {
fb = NULL;
// Take Picture with Camera
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
bot.sendMessage(chat_id, "Camera capture failed", "");
return;
}
currentByte = 0;
fb_length = fb->len;
fb_buffer = fb->buf;
if (text == "/caption") {
Serial.println("\n>>>>> Sending with a caption, bytes= " + String(fb_length));
String sent = bot.sendMultipartFormDataToTelegramWithCaption("sendPhoto", "photo", "img.jpg",
"image/jpeg", "Your photo", chat_id, fb_length,
isMoreDataAvailable, getNextByte, nullptr, nullptr);
Serial.println("done!");
} else {
Serial.println("\n>>>>> Sending, bytes= " + String(fb_length));
bot.sendPhotoByBinary(chat_id, "image/jpeg", fb_length,
isMoreDataAvailable, getNextByte,
nullptr, nullptr);
dataAvailable = true;
Serial.println("done!");
}
esp_camera_fb_return(fb);
}
if (text == "/vga" ) {
fb = NULL;
//sensor_t * s = esp_camera_sensor_get();
//s->set_framesize(s, FRAMESIZE_VGA);
Serial.println("\n\n\nSending VGA");
// Take Picture with Camera
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
bot.sendMessage(chat_id, "Camera capture failed", "");
return;
}
currentByte = 0;
fb_length = fb->len;
fb_buffer = fb->buf;
Serial.println("\n>>>>> Sending as 512 byte blocks, with jzdelay of 0, bytes= " + String(fb_length));
bot.sendPhotoByBinary(chat_id, "image/jpeg", fb_length,
isMoreDataAvailable, getNextByte,
nullptr, nullptr);
esp_camera_fb_return(fb);
}
if (text == "/clip") {
// record the video
bot.longPoll = 0;
xTaskCreatePinnedToCore( the_camera_loop, "the_camera_loop", 10000, NULL, 1, &the_camera_loop_task, 1);
//xTaskCreatePinnedToCore( the_camera_loop, "the_camera_loop", 10000, NULL, 1, &the_camera_loop_task, 0); //v8.5
if ( the_camera_loop_task == NULL ) {
//vTaskDelete( xHandle );
Serial.printf("do_the_steaming_task failed to start! %d\n", the_camera_loop_task);
}
}
if (text == "/start") {
String welcome = "ESP32Cam Telegram bot.\n\n";
welcome += "/photo: take a photo\n";
welcome += "/flash: toggle flash LED\n";
welcome += "/caption: photo with caption\n";
welcome += "/clip: short video clip\n";
welcome += "\n Configure the clip\n";
welcome += "/enable: enable pir\n";
welcome += "/disable: disable pir\n";
welcome += "/enavi: enable avi\n";
welcome += "/disavi: disable avi\n";
welcome += "\n/fast: 25 fps - 3 sec - play .5x speed\n";
welcome += "/med: 8 fps - 10 sec - play 1x speed\n";
welcome += "/slow: 2 fps - 40 sec - play 5x speed\n";
welcome += "\n/status: status\n";
welcome += "/reboot: reboot\n";
welcome += "/start: start\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// 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
char devname[30];
struct tm timeinfo;
time_t now;
camera_fb_t * fb_curr = NULL;
camera_fb_t * fb_next = NULL;
#define fbs 8 // how many kb of static ram for psram -> sram buffer for sd write - not really used because not dma for sd
char avi_file_name[100];
long avi_start_time = 0;
long avi_end_time = 0;
int start_record = 0;
long current_frame_time;
long last_frame_time;
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;
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 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
{{0x90, 0x01}, {0x28, 0x01}}, // FRAMESIZE_CIF, // 400x296
{{0xE0, 0x01}, {0x40, 0x01}}, // FRAMESIZE_HVGA, // 480x320
{{0x80, 0x02}, {0xE0, 0x01}}, // FRAMESIZE_VGA, // 640x480 8
{{0x20, 0x03}, {0x58, 0x02}}, // FRAMESIZE_SVGA, // 800x600 9
{{0x00, 0x04}, {0x00, 0x03}}, // FRAMESIZE_XGA, // 1024x768 10
{{0x00, 0x05}, {0xD0, 0x02}}, // FRAMESIZE_HD, // 1280x720 11
{{0x00, 0x05}, {0x00, 0x04}}, // FRAMESIZE_SXGA, // 1280x1024 12
{{0x40, 0x06}, {0xB0, 0x04}}, // FRAMESIZE_UXGA, // 1600x1200 13
// 3MP Sensors
{{0x80, 0x07}, {0x38, 0x04}}, // FRAMESIZE_FHD, // 1920x1080 14
{{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
// 5MP Sensors
{{0x00, 0x0A}, {0xA0, 0x05}}, // FRAMESIZE_QHD, // 2560x1440 18
{{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
};
#define AVIOFFSET 240 // AVI main header length
uint8_t buf[AVIOFFSET] = {
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, 0x38, 0x38, 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, uint8_t * fd) {
uint8_t y[4];
y[0] = i % 0x100;
y[1] = (i >> 8) % 0x100;
y[2] = (i >> 16) % 0x100;
y[3] = (i >> 24) % 0x100;
memcpy( fd, y, 4);
}
//
// Writes 2 uint32_t in Big Endian at current file position
//
static void inline print_2quartet(unsigned long i, unsigned long j, uint8_t * 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;
memcpy( fd, y, 8);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// 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;
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera Capture Failed");
failures++;
} else {
int get_fail = 0;
fblen = fb->len;
for (int j = 1; j <= 1025; j++) {
if (fb->buf[fblen - j] != 0xD9) {
} else {
if (fb->buf[fblen - j - 1] == 0xFF ) {
foundffd9 = 1;
break;
}
}
}
if (!foundffd9) {
Serial.printf("Bad jpeg, Frame %d, Len = %d \n", frame_cnt, fblen);
esp_camera_fb_return(fb);
failures++;
} else {
break;
}
}
} 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
if (failures == 10) {
Serial.printf("10 failures");
sensor_t * ss = esp_camera_sensor_get();
int qual = ss->status.quality ;
ss->set_quality(ss, qual + 3);
quality = qual + 3;
Serial.printf("\n\nDecreasing quality due to frame failures %d -> %d\n\n", qual, qual + 5);
delay(1000);
}
return fb;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// the_camera_loop()
void the_camera_loop (void* pvParameter) {
vid_fb = get_good_jpeg(); // esp_camera_fb_get();
if (!vid_fb) {
Serial.println("Camera capture failed");
//bot.sendMessage(chat_id, "Camera capture failed", "");
return;
}
picture_ready = true;
if (avi_enabled) {
frame_cnt = 0;
///////////////////////////// start a movie
avi_start_time = millis();
Serial.printf("\nStart the avi ... at %d\n", avi_start_time);
Serial.printf("Framesize %d, quality %d, length %d seconds\n\n", framesize, quality, max_frames * frame_interval / 1000);
fb_next = get_good_jpeg(); // should take zero time
last_frame_time = millis();
start_avi();
///////////////////////////// all the frames of movie
for (int j = 0; j < max_frames - 1 ; j++) { // max_frames
current_frame_time = millis();
if (current_frame_time - last_frame_time < frame_interval) {
if (frame_cnt < 5 || frame_cnt > (max_frames - 5) )Serial.printf("frame %d, delay %d\n", frame_cnt, (int) frame_interval - (current_frame_time - last_frame_time));
delay(frame_interval - (current_frame_time - last_frame_time)); // delay for timelapse
}
last_frame_time = millis();
frame_cnt++;
if (frame_cnt != 1) esp_camera_fb_return(fb_curr);
fb_curr = fb_next; // we will write a frame, and get the camera preparing a new one
another_save_avi(fb_curr );
fb_next = get_good_jpeg(); // should take near zero, unless the sd is faster than the camera, when we will have to wait for the camera
digitalWrite(33, frame_cnt % 2);
if (movi_size > avi_buf_size * .95) break;
}
///////////////////////////// stop a movie
Serial.println("End the Avi");
esp_camera_fb_return(fb_curr);
frame_cnt++;
fb_curr = fb_next;
fb_next = NULL;
another_save_avi(fb_curr );
digitalWrite(33, frame_cnt % 2);
esp_camera_fb_return(fb_curr);
fb_curr = NULL;
end_avi(); // end the movie
digitalWrite(33, HIGH); // light off
avi_end_time = millis();
float fps = 1.0 * frame_cnt / ((avi_end_time - avi_start_time) / 1000) ;
Serial.printf("End the avi at %d. It was %d frames, %d ms at %.2f fps...\n", millis(), frame_cnt, avi_end_time - avi_start_time, fps);
frame_cnt = 0; // start recording again on the next loop
video_ready = true;
}
Serial.println("Deleting the camera task");
delay(100);
vTaskDelete(the_camera_loop_task);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// start_avi - open the files and write in headers
//
static esp_err_t start_avi() {
Serial.println("Starting an avi ");
time(&now);
localtime_r(&now, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "DoorCam %F %H.%M.%S.avi", &timeinfo);
//memset(psram_avi_buf, 0, avi_buf_size); // save some time
//memset(psram_idx_buf, 0, idx_buf_size);
psram_avi_ptr = 0;
psram_idx_ptr = 0;
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);
psram_avi_ptr = psram_avi_buf;
psram_idx_ptr = psram_idx_buf;
memcpy( psram_avi_ptr, buf, AVIOFFSET);
psram_avi_ptr += AVIOFFSET;
startms = millis();
jpeg_size = 0;
movi_size = 0;
uVideoLen = 0;
idx_offset = 4;
} // 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 ) {
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();
memcpy(psram_avi_ptr, dc_buf, 4);
int jpeg_size_rem = jpeg_size + remnant;
print_quartet(jpeg_size_rem, psram_avi_ptr + 4);
fb_block_start = fb->buf;
if (fblen > fbs * 1024 - 8 ) { // fbs is the size of frame buffer static
fb_block_length = fbs * 1024;
fblen = fblen - (fbs * 1024 - 8);
memcpy( psram_avi_ptr + 8, fb_block_start, fb_block_length - 8);
fb_block_start = fb_block_start + fb_block_length - 8;
} else {
fb_block_length = fblen + 8 + remnant;
memcpy( psram_avi_ptr + 8, fb_block_start, fb_block_length - 8);
fblen = 0;
}
psram_avi_ptr += fb_block_length;
while (fblen > 0) {
if (fblen > fbs * 1024) {
fb_block_length = fbs * 1024;
fblen = fblen - fb_block_length;
} else {
fb_block_length = fblen + remnant;
fblen = 0;
}
memcpy( psram_avi_ptr, fb_block_start, fb_block_length);
psram_avi_ptr += fb_block_length;
fb_block_start = fb_block_start + fb_block_length;
}
movi_size += jpeg_size;
uVideoLen += jpeg_size;
print_2quartet(idx_offset, jpeg_size, psram_idx_ptr);
psram_idx_ptr += 8;
idx_offset = idx_offset + jpeg_size + remnant + 8;
movi_size = movi_size + remnant;
} // end of another_pic_avi
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// end_avi writes the index, and closes the files
//
static esp_err_t end_avi() {
Serial.println("End of avi - closing the files");
if (frame_cnt < 5 ) {
Serial.println("Recording screwed up, less than 5 frames, forget index\n");
} else {
elapsedms = millis() - startms;
float fRealFPS = (1000.0f * (float)frame_cnt) / ((float)elapsedms) * speed_up_factor;
float fmicroseconds_per_frame = 1000000.0f / fRealFPS;
uint8_t iAttainedFPS = round(fRealFPS) ;
uint32_t us_per_frame = round(fmicroseconds_per_frame);
//Modify the MJPEG header from the beginning of the file, overwriting various placeholders
print_quartet(movi_size + 240 + 16 * frame_cnt + 8 * frame_cnt, psram_avi_buf + 4);
print_quartet(us_per_frame, psram_avi_buf + 0x20);
unsigned long max_bytes_per_sec = (1.0f * movi_size * iAttainedFPS) / frame_cnt;
print_quartet(max_bytes_per_sec, psram_avi_buf + 0x24);
print_quartet(frame_cnt, psram_avi_buf + 0x30);
print_quartet(frame_cnt, psram_avi_buf + 0x8c);
print_quartet((int)iAttainedFPS, psram_avi_buf + 0x84);
print_quartet(movi_size + frame_cnt * 8 + 4, psram_avi_buf + 0xe8);
Serial.println(F("\n*** Video recorded and saved ***\n"));
Serial.printf("Recorded %5d frames in %5d seconds\n", frame_cnt, elapsedms / 1000);
Serial.printf("File size is %u bytes\n", movi_size + 12 * frame_cnt + 4);
Serial.printf("Adjusted FPS is %5.2f\n", fRealFPS);
Serial.printf("Max data rate is %lu bytes/s\n", max_bytes_per_sec);
Serial.printf("Frame duration is %d us\n", us_per_frame);
Serial.printf("Average frame length is %d bytes\n", uVideoLen / frame_cnt);
Serial.printf("Writng the index, %d frames\n", frame_cnt);
memcpy (psram_avi_ptr, idx1_buf, 4);
psram_avi_ptr += 4;
print_quartet(frame_cnt * 16, psram_avi_ptr);
psram_avi_ptr += 4;
psram_idx_ptr = psram_idx_buf;
for (int i = 0; i < frame_cnt; i++) {
memcpy (psram_avi_ptr, dc_buf, 4);
psram_avi_ptr += 4;
memcpy (psram_avi_ptr, zero_buf, 4);
psram_avi_ptr += 4;
memcpy (psram_avi_ptr, psram_idx_ptr, 8);
psram_avi_ptr += 8;
psram_idx_ptr += 8;
}
}
Serial.println("---");
digitalWrite(33, HIGH);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// setup some interupts during reboot
//
// int read13 = digitalRead(13); -- pir for video
int PIRpin = 13;
static void setupinterrupts() {
pinMode(PIRpin, INPUT_PULLDOWN) ; //INPUT_PULLDOWN);
Serial.print("Setup PIRpin = ");
for (int i = 0; i < 5; i++) {
Serial.print( digitalRead(PIRpin) ); Serial.print(", ");
}
Serial.println(" ");
esp_err_t err = gpio_isr_handler_add((gpio_num_t)PIRpin, &PIR_ISR, NULL);
if (err != ESP_OK) Serial.printf("gpio_isr_handler_add failed (%x)", err);
gpio_set_intr_type((gpio_num_t)PIRpin, GPIO_INTR_POSEDGE);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// PIR_ISR - interupt handler for PIR - starts or extends a video
//
static void IRAM_ATTR PIR_ISR(void* arg) {
int PIRstatus = digitalRead(PIRpin) + digitalRead(PIRpin) + digitalRead(PIRpin) ;
if (PIRstatus == 3) {
Serial.print("PIR Interupt>> "); Serial.println(PIRstatus);
if (!active_interupt && pir_enabled) {
active_interupt = true;
digitalWrite(33, HIGH);
Serial.print("PIR Interupt ... start recording ... ");
xTaskCreatePinnedToCore( the_camera_loop, "the_camera_loop", 10000, NULL, 1, &the_camera_loop_task, 1);
//xTaskCreatePinnedToCore( the_camera_loop, "the_camera_loop", 10000, NULL, 1, &the_camera_loop_task, 0); //v8.5
if ( the_camera_loop_task == NULL ) {
Serial.printf("do_the_steaming_task failed to start! %d\n", the_camera_loop_task);
}
}
}
}
#include "esp_wifi.h"
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include <ESPmDNS.h>
bool init_wifi() {
uint32_t brown_reg_temp = READ_PERI_REG(RTC_CNTL_BROWN_OUT_REG);
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
// Set WiFi to station mode and disconnect from an AP if it was Previously connected
devstr.toCharArray(devname, devstr.length() + 1); // name of your camera for mDNS, Router, and filenames
WiFi.mode(WIFI_STA);
WiFi.setHostname(devname);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
wifi_ps_type_t the_type;
//esp_err_t get_ps = esp_wifi_get_ps(&the_type);
esp_err_t set_ps = esp_wifi_set_ps(WIFI_PS_NONE);
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, brown_reg_temp);
configTime(0, 0, "pool.ntp.org");
char tzchar[80];
TIMEZONE.toCharArray(tzchar, TIMEZONE.length()); // name of your camera for mDNS, Router, and filenames
setenv("TZ", tzchar, 1); // mountain time zone from #define at top
tzset();
if (!MDNS.begin(devname)) {
Serial.println("Error setting up MDNS responder!");