1
+ /* *
2
+ * Camera motion detection + JPEG capture + Telegram Bot demo
3
+ */
4
+
5
+ // replace with your own values
6
+ #define TELEGRAM_TOKEN " xxxxxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
7
+ #define CHAT_ID " 0123456789"
8
+ #define WIFI_SSID " SSID"
9
+ #define WIFI_PASSWORD " PASSWORD"
10
+
11
+ #include < WiFi.h>
12
+ #include < eloquent.h>
13
+ #include < eloquent/vision/image/gray/custom.h>
14
+ #include < eloquent/vision/motion/naive.h>
15
+ #include < eloquent/fs/spiffs.h>
16
+ #include < eloquent/networking/wifi.h>
17
+ #include < eloquent/apps/telegram/bot/wifi.h>
18
+
19
+
20
+ // uncomment based on your camera and resolution
21
+
22
+ // #include "eloquent/vision/camera/ov767x/gray/vga.h"
23
+ // #include "eloquent/vision/camera/ov767x/gray/qvga.h"
24
+ // #include "eloquent/vision/camera/ov767x/gray/qqvga.h"
25
+ // #include "eloquent/vision/camera/esp32/aithinker/gray/vga.h"
26
+ // #include "eloquent/vision/camera/esp32/aithinker/gray/qvga.h"
27
+ // #include "eloquent/vision/camera/esp32/aithinker/gray/qqvga.h"
28
+ // #include "eloquent/vision/camera/esp32/wrover/gray/vga.h"
29
+ // #include "eloquent/vision/camera/esp32/wrover/gray/qvga.h"
30
+ // #include "eloquent/vision/camera/esp32/wrover/gray/qqvga.h"
31
+ // #include "eloquent/vision/camera/esp32/eye/gray/vga.h"
32
+ // #include "eloquent/vision/camera/esp32/eye/gray/qvga.h"
33
+ // #include "eloquent/vision/camera/esp32/eye/gray/qqvga.h"
34
+ // #include "eloquent/vision/camera/esp32/m5/gray/vga.h"
35
+ // #include "eloquent/vision/camera/esp32/m5/gray/qvga.h"
36
+ // #include "eloquent/vision/camera/esp32/m5/gray/qqvga.h"
37
+ // #include "eloquent/vision/camera/esp32/m5wide/gray/vga.h"
38
+ // #include "eloquent/vision/camera/esp32/m5wide/gray/qvga.h"
39
+ // #include "eloquent/vision/camera/esp32/m5wide/gray/qqvga.h"
40
+
41
+
42
+ #define THUMB_WIDTH 32
43
+ #define THUMB_HEIGHT 24
44
+
45
+ // allocate memory to store the thumbnail into a new image
46
+ Eloquent::Vision::Image::Gray::CustomWithBuffer<THUMB_WIDTH, THUMB_HEIGHT> thumbnail;
47
+ Eloquent::Vision::Motion::Naive<THUMB_WIDTH, THUMB_HEIGHT> detector;
48
+
49
+
50
+ void setup () {
51
+ delay (4000 );
52
+ Serial.begin (115200 );
53
+ filesystem.begin (true );
54
+
55
+ // turn on high freq for fast streaming speed
56
+ camera.setHighFreq ();
57
+
58
+ if (!camera.begin ())
59
+ eloquent::abort (Serial, " Camera init error" );
60
+
61
+ if (!wifi.connectTo (WIFI_SSID, WIFI_PASSWORD))
62
+ eloquent::abort (Serial, " Cannot connect to WiFi" );
63
+
64
+ if (!telegramBot.connect ()) {
65
+ eloquent::abort (Serial, " Cannot connect to Telegram API" );
66
+ }
67
+
68
+ Serial.println (" Camera init OK" );
69
+ Serial.println (" WiFi connected OK" );
70
+ Serial.println (" Telegram API connected OK" );
71
+
72
+ detector.throttle (10 );
73
+ detector.setIntensityChangeThreshold (15 );
74
+ detector.setPixelChangesThreshold (0.1 );
75
+ }
76
+
77
+
78
+ void loop () {
79
+ if (!camera.capture ()) {
80
+ Serial.println (camera.getErrorMessage ());
81
+ delay (1000 );
82
+ return ;
83
+ }
84
+
85
+ // perform motion detection on resized image for fast detection
86
+ // but keep original image for capture at full resolution
87
+ camera.image .resizeTo <THUMB_WIDTH, THUMB_HEIGHT>(thumbnail);
88
+ detector.update (thumbnail);
89
+
90
+ // if motion is detected, save original image as jpeg
91
+ if (detector.isMotionDetected ()) {
92
+ Serial.println (" Motion detected" );
93
+
94
+ auto file = filesystem.writeBinary (" /capture_best.jpg" );
95
+ auto jpeg = camera.image .jpeg ().bestQuality ();
96
+
97
+ if (jpeg.writeTo (file)) {
98
+ Serial.println (" Jpeg written" );
99
+
100
+ // send to Telegram
101
+ file.reopenAs (" rb" );
102
+
103
+ bool messageOk = telegramBot.sendMessageTo (CHAT_ID, " Motion detected" );
104
+ Serial.println (messageOk ? " Message sent OK" : " Message send error" );
105
+
106
+ bool jpegOk = telegramBot.sendJpegTo (CHAT_ID, file);
107
+ Serial.println (jpegOk ? " Jpeg sent OK" : " Jpeg send error" );
108
+
109
+ if (!jpegOk) {
110
+ Serial.print (" Telegram error: " );
111
+ Serial.println (telegramBot.getErrorMessage ());
112
+ }
113
+ }
114
+ else {
115
+ Serial.print (" Jpeg encoding error: " );
116
+ Serial.println (jpeg.getErrorMessage ());
117
+ }
118
+ }
119
+
120
+ // release memory
121
+ camera.free ();
122
+ }
0 commit comments