1
+ /* ******************************************************************
2
+
3
+ A Telegram bot for taking a photo with an ESP32Cam
4
+
5
+ Parts used:
6
+ ESP32-CAM module* - http://s.click.aliexpress.com/e/bnXR1eYs
7
+
8
+ = Affiliate Links
9
+
10
+ Note:
11
+ - Make sure that you have either selected ESP32 Wrover Module,
12
+ or another board which has PSRAM enabled
13
+ - Choose "Huge App" partion scheme
14
+
15
+ Some of the camera code comes from Rui Santos:
16
+ https://randomnerdtutorials.com/esp32-cam-take-photo-save-microsd-card/
17
+
18
+ Written by Brian Lough
19
+ YouTube: https://www.youtube.com/brianlough
20
+ Tindie: https://www.tindie.com/stores/brianlough/
21
+ Twitter: https://twitter.com/witnessmenow
22
+ *******************************************************************/
23
+
24
+ // ----------------------------
25
+ // Standard Libraries - Already Installed if you have ESP32 set up
26
+ // ----------------------------
27
+
28
+ #include < WiFi.h>
29
+ #include < WiFiClientSecure.h>
30
+ #include " esp_camera.h"
31
+
32
+ // ----------------------------
33
+ // Additional Libraries - each one of these will need to be installed.
34
+ // ----------------------------
35
+
36
+ #include < UniversalTelegramBot.h>
37
+ // Library for interacting with the Telegram API
38
+ // Search for "Telegegram" in the Library manager and install
39
+ // The universal Telegram library
40
+ // https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
41
+
42
+ #include < ArduinoJson.h>
43
+ // Library used for parsing Json from the API responses
44
+ // Search for "Arduino Json" in the Arduino Library manager
45
+ // https://github.com/bblanchon/ArduinoJson
46
+
47
+
48
+ // ------- Replace the following! ------
49
+
50
+ // #define CAMERA_MODEL_WROVER_KIT
51
+ // #define CAMERA_MODEL_ESP_EYE
52
+ // #define CAMERA_MODEL_M5STACK_PSRAM
53
+ // #define CAMERA_MODEL_M5STACK_WIDE
54
+ #define CAMERA_MODEL_AI_THINKER
55
+
56
+ // Initialize Wifi connection to the router
57
+ char ssid[] = " XXXXXX" ; // your network SSID (name)
58
+ char password[] = " YYYYYY" ; // your network key
59
+
60
+ // Initialize Telegram BOT
61
+ #define BOTtoken " XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
62
+
63
+ // ------- ------------------------ ------
64
+
65
+ #include " camera_pins.h"
66
+ #include " camera_code.h"
67
+
68
+ #define FLASH_LED_PIN 4
69
+
70
+ WiFiClientSecure client;
71
+ UniversalTelegramBot bot (BOTtoken, client);
72
+
73
+ int Bot_mtbs = 1000 ; // mean time between scan messages
74
+ long Bot_lasttime; // last time messages' scan has been done
75
+
76
+ bool flashState = LOW;
77
+
78
+ camera_fb_t * fb = NULL ;
79
+
80
+ bool isMoreDataAvailable ();
81
+ byte* getNextBuffer ();
82
+ int getNextBufferLen ();
83
+
84
+ bool dataAvailable = false ;
85
+
86
+ void handleNewMessages (int numNewMessages) {
87
+ Serial.println (" handleNewMessages" );
88
+ Serial.println (String (numNewMessages));
89
+
90
+ for (int i = 0 ; i < numNewMessages; i++) {
91
+ String chat_id = String (bot.messages [i].chat_id );
92
+ String text = bot.messages [i].text ;
93
+
94
+ String from_name = bot.messages [i].from_name ;
95
+ if (from_name == " " ) from_name = " Guest" ;
96
+
97
+ if (text == " /flash" ) {
98
+ flashState = !flashState;
99
+ digitalWrite (FLASH_LED_PIN, flashState);
100
+ }
101
+
102
+ if (text == " /photo" ) {
103
+
104
+ fb = NULL ;
105
+
106
+ // Take Picture with Camera
107
+ fb = esp_camera_fb_get ();
108
+ if (!fb) {
109
+ Serial.println (" Camera capture failed" );
110
+ bot.sendMessage (chat_id, " Camera capture failed" , " " );
111
+ return ;
112
+ }
113
+ dataAvailable = true ;
114
+ Serial.println (" Sending" );
115
+ bot.sendPhotoByBinary (chat_id, " image/jpeg" , fb->len ,
116
+ isMoreDataAvailable, nullptr ,
117
+ getNextBuffer, getNextBufferLen);
118
+
119
+ Serial.println (" done!" );
120
+
121
+ esp_camera_fb_return (fb);
122
+ }
123
+
124
+ if (text == " /start" ) {
125
+ String welcome = " Welcome to the ESP32Cam Telegram bot.\n\n " ;
126
+ welcome += " /photo : will take a photo\n " ;
127
+ welcome += " /flash : toggle flash LED (VERY BRIGHT!)\n " ;
128
+ bot.sendMessage (chat_id, welcome, " Markdown" );
129
+ }
130
+ }
131
+ }
132
+
133
+ bool isMoreDataAvailable () {
134
+ if (dataAvailable)
135
+ {
136
+ dataAvailable = false ;
137
+ return true ;
138
+ } else {
139
+ return false ;
140
+ }
141
+ }
142
+
143
+ byte* getNextBuffer (){
144
+ if (fb){
145
+ return fb->buf ;
146
+ } else {
147
+ return nullptr ;
148
+ }
149
+ }
150
+
151
+ int getNextBufferLen (){
152
+ if (fb){
153
+ return fb->len ;
154
+ } else {
155
+ return 0 ;
156
+ }
157
+ }
158
+
159
+ void setup () {
160
+ Serial.begin (115200 );
161
+
162
+ pinMode (FLASH_LED_PIN, OUTPUT);
163
+ digitalWrite (FLASH_LED_PIN, flashState); // defaults to low
164
+
165
+ if (!setupCamera ()) {
166
+ Serial.println (" Camera Setup Failed!" );
167
+ while (true ) {
168
+ delay (100 );
169
+ }
170
+ }
171
+
172
+ // Attempt to connect to Wifi network:
173
+ Serial.print (" Connecting Wifi: " );
174
+ Serial.println (ssid);
175
+
176
+ // Set WiFi to station mode and disconnect from an AP if it was Previously
177
+ // connected
178
+ WiFi.mode (WIFI_STA);
179
+ WiFi.begin (ssid, password);
180
+
181
+ while (WiFi.status () != WL_CONNECTED) {
182
+ Serial.print (" ." );
183
+ delay (500 );
184
+ }
185
+
186
+ Serial.println (" " );
187
+ Serial.println (" WiFi connected" );
188
+ Serial.print (" IP address: " );
189
+ Serial.println (WiFi.localIP ());
190
+
191
+ // Make the bot wait for a new message for up to 60seconds
192
+ bot.longPoll = 60 ;
193
+ }
194
+
195
+ void loop () {
196
+ if (millis () > Bot_lasttime + Bot_mtbs) {
197
+ int numNewMessages = bot.getUpdates (bot.last_message_received + 1 );
198
+
199
+ while (numNewMessages) {
200
+ Serial.println (" got response" );
201
+ handleNewMessages (numNewMessages);
202
+ numNewMessages = bot.getUpdates (bot.last_message_received + 1 );
203
+ }
204
+
205
+ Bot_lasttime = millis ();
206
+ }
207
+ }
0 commit comments