Skip to content

Commit 722eaa7

Browse files
authored
Merge pull request #180 from gerasdf/master
Accumulated changes from gerasdf's fork
2 parents b4ebcae + bae9efa commit 722eaa7

File tree

16 files changed

+1465
-477
lines changed

16 files changed

+1465
-477
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@
3030
.piolibdeps
3131
.clang_complete
3232
.gcc-flags.json
33+
.pio/
34+
.vscode/

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Here is a list of features that this library covers. (Note: The examples link to
5959
|*Location*|Your bot can receive location data, either from a single location data point or live location data. |Check the example.| [Location](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/tree/master/examples/ESP8266/Location/Location.ino)|
6060
|*Channel Post*|Reads posts from channels. |Check the example.| [ChannelPost](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/tree/master/examples/ESP8266/ChannelPost/ChannelPost.ino)|
6161
|*Long Poll*|Set how long the bot will wait checking for a new message before returning now messages. <br><br> This will decrease the amount of requests and data used by the bot, but it will tie up the arduino while it waits for messages |`bot.longPoll = 60;` <br><br> Where 60 is the amount of seconds it should wait | [LongPoll](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/tree/master/examples/ESP8266/LongPoll/LongPoll.ino)|
62+
|*Update Firmware and SPIFFS*|You can update firmware and spiffs area through send files as a normal file with a specific caption. |`update firmware` <br>or<br>`update spiffs`<br> These are captions for example. | [telegramOTA](https://github.com/solcer/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP32/telegramOTA/telegramOTA.ino)|```
63+
|*Set bot's commands*|You can set bot commands programmatically from your code. The commands will be shown in a special place in the text input area| ```bot.setMyCommands("[{\"command\":\"help\", \"description\":\"get help\"},{\"command\":\"start\",\"description\":\"start conversation\"}]");```. See examples| [SetMyCommands](examples/ESP8266/SetMyCommands/SetMyCommands.ino)|
6264

6365
The full Telegram Bot API documentation can be read [here](https://core.telegram.org/bots/api). If there is a feature you would like added to the library please either raise a Github issue or please feel free to raise a Pull Request.
6466

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
bool setupCamera() {
2+
camera_config_t config;
3+
config.ledc_channel = LEDC_CHANNEL_0;
4+
config.ledc_timer = LEDC_TIMER_0;
5+
config.pin_d0 = Y2_GPIO_NUM;
6+
config.pin_d1 = Y3_GPIO_NUM;
7+
config.pin_d2 = Y4_GPIO_NUM;
8+
config.pin_d3 = Y5_GPIO_NUM;
9+
config.pin_d4 = Y6_GPIO_NUM;
10+
config.pin_d5 = Y7_GPIO_NUM;
11+
config.pin_d6 = Y8_GPIO_NUM;
12+
config.pin_d7 = Y9_GPIO_NUM;
13+
config.pin_xclk = XCLK_GPIO_NUM;
14+
config.pin_pclk = PCLK_GPIO_NUM;
15+
config.pin_vsync = VSYNC_GPIO_NUM;
16+
config.pin_href = HREF_GPIO_NUM;
17+
config.pin_sscb_sda = SIOD_GPIO_NUM;
18+
config.pin_sscb_scl = SIOC_GPIO_NUM;
19+
config.pin_pwdn = PWDN_GPIO_NUM;
20+
config.pin_reset = RESET_GPIO_NUM;
21+
config.xclk_freq_hz = 20000000;
22+
config.pixel_format = PIXFORMAT_JPEG;
23+
//init with high specs to pre-allocate larger buffers
24+
if (psramFound()) {
25+
config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
26+
config.jpeg_quality = 10;
27+
config.fb_count = 2;
28+
} else {
29+
config.frame_size = FRAMESIZE_SVGA;
30+
config.jpeg_quality = 12;
31+
config.fb_count = 1;
32+
}
33+
34+
#if defined(CAMERA_MODEL_ESP_EYE)
35+
pinMode(13, INPUT_PULLUP);
36+
pinMode(14, INPUT_PULLUP);
37+
#endif
38+
39+
// camera init
40+
esp_err_t err = esp_camera_init(&config);
41+
if (err != ESP_OK) {
42+
Serial.printf("Camera init failed with error 0x%x", err);
43+
return false;
44+
}
45+
46+
sensor_t * s = esp_camera_sensor_get();
47+
//initial sensors are flipped vertically and colors are a bit saturated
48+
if (s->id.PID == OV3660_PID) {
49+
s->set_vflip(s, 1);//flip it back
50+
s->set_brightness(s, 1);//up the blightness just a bit
51+
s->set_saturation(s, -2);//lower the saturation
52+
}
53+
//drop down frame size for higher initial frame rate
54+
s->set_framesize(s, FRAMESIZE_QVGA);
55+
56+
#if defined(CAMERA_MODEL_M5STACK_WIDE)
57+
s->set_vflip(s, 1);
58+
s->set_hmirror(s, 1);
59+
#endif
60+
61+
return true;
62+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
2+
#if defined(CAMERA_MODEL_WROVER_KIT)
3+
#define PWDN_GPIO_NUM -1
4+
#define RESET_GPIO_NUM -1
5+
#define XCLK_GPIO_NUM 21
6+
#define SIOD_GPIO_NUM 26
7+
#define SIOC_GPIO_NUM 27
8+
9+
#define Y9_GPIO_NUM 35
10+
#define Y8_GPIO_NUM 34
11+
#define Y7_GPIO_NUM 39
12+
#define Y6_GPIO_NUM 36
13+
#define Y5_GPIO_NUM 19
14+
#define Y4_GPIO_NUM 18
15+
#define Y3_GPIO_NUM 5
16+
#define Y2_GPIO_NUM 4
17+
#define VSYNC_GPIO_NUM 25
18+
#define HREF_GPIO_NUM 23
19+
#define PCLK_GPIO_NUM 22
20+
21+
#elif defined(CAMERA_MODEL_ESP_EYE)
22+
#define PWDN_GPIO_NUM -1
23+
#define RESET_GPIO_NUM -1
24+
#define XCLK_GPIO_NUM 4
25+
#define SIOD_GPIO_NUM 18
26+
#define SIOC_GPIO_NUM 23
27+
28+
#define Y9_GPIO_NUM 36
29+
#define Y8_GPIO_NUM 37
30+
#define Y7_GPIO_NUM 38
31+
#define Y6_GPIO_NUM 39
32+
#define Y5_GPIO_NUM 35
33+
#define Y4_GPIO_NUM 14
34+
#define Y3_GPIO_NUM 13
35+
#define Y2_GPIO_NUM 34
36+
#define VSYNC_GPIO_NUM 5
37+
#define HREF_GPIO_NUM 27
38+
#define PCLK_GPIO_NUM 25
39+
40+
#elif defined(CAMERA_MODEL_M5STACK_PSRAM)
41+
#define PWDN_GPIO_NUM -1
42+
#define RESET_GPIO_NUM 15
43+
#define XCLK_GPIO_NUM 27
44+
#define SIOD_GPIO_NUM 25
45+
#define SIOC_GPIO_NUM 23
46+
47+
#define Y9_GPIO_NUM 19
48+
#define Y8_GPIO_NUM 36
49+
#define Y7_GPIO_NUM 18
50+
#define Y6_GPIO_NUM 39
51+
#define Y5_GPIO_NUM 5
52+
#define Y4_GPIO_NUM 34
53+
#define Y3_GPIO_NUM 35
54+
#define Y2_GPIO_NUM 32
55+
#define VSYNC_GPIO_NUM 22
56+
#define HREF_GPIO_NUM 26
57+
#define PCLK_GPIO_NUM 21
58+
59+
#elif defined(CAMERA_MODEL_M5STACK_WIDE)
60+
#define PWDN_GPIO_NUM -1
61+
#define RESET_GPIO_NUM 15
62+
#define XCLK_GPIO_NUM 27
63+
#define SIOD_GPIO_NUM 22
64+
#define SIOC_GPIO_NUM 23
65+
66+
#define Y9_GPIO_NUM 19
67+
#define Y8_GPIO_NUM 36
68+
#define Y7_GPIO_NUM 18
69+
#define Y6_GPIO_NUM 39
70+
#define Y5_GPIO_NUM 5
71+
#define Y4_GPIO_NUM 34
72+
#define Y3_GPIO_NUM 35
73+
#define Y2_GPIO_NUM 32
74+
#define VSYNC_GPIO_NUM 25
75+
#define HREF_GPIO_NUM 26
76+
#define PCLK_GPIO_NUM 21
77+
78+
#elif defined(CAMERA_MODEL_AI_THINKER)
79+
#define PWDN_GPIO_NUM 32
80+
#define RESET_GPIO_NUM -1
81+
#define XCLK_GPIO_NUM 0
82+
#define SIOD_GPIO_NUM 26
83+
#define SIOC_GPIO_NUM 27
84+
85+
#define Y9_GPIO_NUM 35
86+
#define Y8_GPIO_NUM 34
87+
#define Y7_GPIO_NUM 39
88+
#define Y6_GPIO_NUM 36
89+
#define Y5_GPIO_NUM 21
90+
#define Y4_GPIO_NUM 19
91+
#define Y3_GPIO_NUM 18
92+
#define Y2_GPIO_NUM 5
93+
#define VSYNC_GPIO_NUM 25
94+
#define HREF_GPIO_NUM 23
95+
#define PCLK_GPIO_NUM 22
96+
97+
#else
98+
#error "Camera model not selected"
99+
#endif

0 commit comments

Comments
 (0)