Skip to content

Commit

Permalink
- fix forwardMessage(), sendAnimationByUrl(), sendPhotoByUrl(), sendT…
Browse files Browse the repository at this point in the history
…oChannel()

- ESP32-CAM examples update
  • Loading branch information
cotestatnt committed Aug 11, 2023
1 parent 002ff88 commit 5260273
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 106 deletions.
32 changes: 11 additions & 21 deletions examples/ESP32/ESP32-CAM-PIR/ESP32-CAM-PIR.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ WiFiClientSecure client;

#define PIR_PIN GPIO_NUM_13

const char* ssid = "xxxxxxxxxxxx"; // SSID WiFi network
const char* pass = "xxxxxxxxxxxx"; // Password WiFi network
const char* token = "xxxxxxxx:xxxxxxxxxxx-xxxxxxxxxxxxxxxxxxx";
const char* ssid = "xxxxxxxxx"; // SSID WiFi network
const char* pass = "xxxxxxxxx"; // Password WiFi network
const char* token = "xxxxxxxxxx:xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxx"; // Telegram token
// Check the userid with the help of bot @JsonDumpBot or @getidsbot (work also with groups)
// https://t.me/JsonDumpBot or https://t.me/getidsbot
int64_t userid = 123456789;


#define PIR_PIN GPIO_NUM_13
#define NUM_PHOTO 3 // Total number of photos to send on motion detection
#define DELAY_PHOTO 2000 // Waiting time between one photo and the next

int currentPict = 3;

// Check the userid with the help of bot @JsonDumpBot or @getidsbot (work also with groups)
// https://t.me/JsonDumpBot or https://t.me/getidsbot
int64_t userid = 1234567890;

AsyncTelegram2 myBot(client);

// Timezone definition to get properly time from NTP server
Expand All @@ -54,6 +52,9 @@ size_t sendPicture(bool);

/////////////////////////////////// SETUP ///////////////////////////////////////
void setup() {
Serial.begin(115200);
Serial.println();

WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // disable brownout detect

// PIR Motion Sensor setup
Expand All @@ -65,9 +66,6 @@ void setup() {
setLamp(0); // set default value
ledcAttachPin(LAMP_PIN, lampChannel); // attach the GPIO pin to the channel

Serial.begin(115200);
Serial.println();

// Start WiFi connection
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
Expand Down Expand Up @@ -245,7 +243,6 @@ size_t sendPicture(bool saveImg = false) {

// If is necessary keep the image file, save and send as stream object
if (saveImg) {

// Keep files on SD memory, filename is time based (YYYYMMDD_HHMMSS.jpg)
#if USE_MMC
char filename[30];
Expand All @@ -265,19 +262,12 @@ size_t sendPicture(bool saveImg = false) {
file.write(fb->buf, fb->len);
file.close();
Serial.printf("Saved file to path: %s - %zu bytes\n", filename, fb->len);

if (!myBot.sendPhoto(userid, filename, FILESYSTEM)) {
len = 0;
myBot.sendTo(userid, "Error, picture not sent.");
}
myBot.sendPhoto(userid, filename, FILESYSTEM);
}

// If is NOT necessary keep the image file, send picture directly from ram buffer
else {
if (!myBot.sendPhoto(userid, fb->buf, fb->len)){
len = 0;
myBot.sendTo(userid, "Error, picture not sent.");
}
myBot.sendPhoto(userid, fb->buf, fb->len);
}

// Clear buffer
Expand Down
25 changes: 19 additions & 6 deletions examples/ESP32/ESP32-CAM-PIR/camera_pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
#error "Camera model not selected, did you forget to uncomment it in myconfig?"
#endif


static camera_config_t camera_config = {
.pin_pwdn = PWDN_GPIO_NUM,
.pin_reset = RESET_GPIO_NUM,
Expand All @@ -238,14 +239,20 @@ static camera_config_t camera_config = {
.pin_d0 = Y2_GPIO_NUM,
.pin_vsync = VSYNC_GPIO_NUM,
.pin_href = HREF_GPIO_NUM,
.pin_pclk = PCLK_GPIO_NUM,
.xclk_freq_hz = 10000000, //XCLK 20MHz or 10MHz
.pin_pclk = PCLK_GPIO_NUM,
.xclk_freq_hz = 20000000,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,
.pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
.frame_size = FRAMESIZE_UXGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
.jpeg_quality = 13, //0-63 lower number means higher quality
.fb_count = 1 //if more than one, i2s runs in continuous mode. Use only with JPEG
.pixel_format = PIXFORMAT_JPEG, /* PIXFORMAT_RGB565, // for face detection/recognition */
.frame_size = FRAMESIZE_UXGA,
.jpeg_quality = 12,
.fb_count = 1,
.fb_location = CAMERA_FB_IN_PSRAM,
.grab_mode = CAMERA_GRAB_WHEN_EMPTY,
#if CONFIG_CAMERA_CONVERTER_ENABLED
.conv_mode = CONV_DISABLE, /*!< RGB<->YUV Conversion mode */
#endif
.sccb_i2c_port = 1 /*!< If pin_sccb_sda is -1, use the already configured I2C bus by number */
};


Expand All @@ -256,6 +263,12 @@ const int pwmMax = pow(2,pwmresolution)-1;


static esp_err_t init_camera() {
// Best picture quality, but first frame requestes get lost sometimes (comment/uncomment to try)
if (psramFound()) {
Serial.println("PSRAM found");
camera_config.fb_count = 2;
camera_config.grab_mode = CAMERA_GRAB_LATEST;
}
//initialize the camera
Serial.print("Camera init... ");
esp_err_t err = esp_camera_init(&camera_config);
Expand Down
93 changes: 54 additions & 39 deletions examples/ESP32/ESP32-CAM/ESP32-CAM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
#include <WiFiClientSecure.h>
WiFiClientSecure client;

const char* ssid = "xxxxxxxxxxxx"; // SSID WiFi network
const char* pass = "xxxxxxxxxxxx"; // Password WiFi network
const char* token = "xxxxxxxx:xxxxxxxxxxx-xxxxxxxxxxxxxxxxxxx";

const char* ssid = "xxxxxxxxx"; // SSID WiFi network
const char* pass = "xxxxxxxxx"; // Password WiFi network
const char* token = "xxxxxxxxxx:xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxx"; // Telegram token
// Check the userid with the help of bot @JsonDumpBot or @getidsbot (work also with groups)
// https://t.me/JsonDumpBot or https://t.me/getidsbot
int64_t userid = 1234567890;
int64_t userid = 123456789;

// Timezone definition to get properly time from NTP server
#define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
Expand Down Expand Up @@ -56,38 +55,52 @@ static camera_config_t camera_config = {
.pin_d0 = Y2_GPIO_NUM,
.pin_vsync = VSYNC_GPIO_NUM,
.pin_href = HREF_GPIO_NUM,
.pin_pclk = PCLK_GPIO_NUM,
.xclk_freq_hz = 10000000, //XCLK 20MHz or 10MHz
.pin_pclk = PCLK_GPIO_NUM,
.xclk_freq_hz = 20000000,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,
.pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
.frame_size = FRAMESIZE_UXGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
.jpeg_quality = 12, //0-63 lower number means higher quality
.fb_count = 1 //if more than one, i2s runs in continuous mode. Use only with JPEG
.pixel_format = PIXFORMAT_JPEG, /* PIXFORMAT_RGB565, // for face detection/recognition */
.frame_size = FRAMESIZE_UXGA,
.jpeg_quality = 12,
.fb_count = 1,
.fb_location = CAMERA_FB_IN_PSRAM,
.grab_mode = CAMERA_GRAB_WHEN_EMPTY,
#if CONFIG_CAMERA_CONVERTER_ENABLED
.conv_mode = CONV_DISABLE, /*!< RGB<->YUV Conversion mode */
#endif
.sccb_i2c_port = 1 /*!< If pin_sccb_sda is -1, use the already configured I2C bus by number */
};


int lampChannel = 7; // a free PWM channel (some channels used by camera)
const int pwmfreq = 50000; // 50K pwm frequency
const int pwmresolution = 9; // duty cycle bit range
const int pwmMax = pow(2,pwmresolution)-1;
int lampChannel = 7; // a free PWM channel (some channels used by camera)
const int pwmfreq = 50000; // 50K pwm frequency
const int pwmresolution = 9; // duty cycle bit range
const int pwmMax = pow(2, pwmresolution) - 1;

// Lamp Control
void setLamp(int newVal) {
if (newVal != -1) {
// Apply a logarithmic function to the scale.
int brightness = round((pow(2,(1+(newVal*0.02)))-2)/6*pwmMax);
ledcWrite(lampChannel, brightness);
Serial.print("Lamp: ");
Serial.print(newVal);
Serial.print("%, pwm = ");
Serial.println(brightness);
}
if (newVal != -1) {
// Apply a logarithmic function to the scale.
int brightness = round((pow(2, (1 + (newVal * 0.02))) - 2) / 6 * pwmMax);
ledcWrite(lampChannel, brightness);
Serial.print("Lamp: ");
Serial.print(newVal);
Serial.print("%, pwm = ");
Serial.println(brightness);
}
}

static esp_err_t init_camera() {

// Best picture quality, but first frame requestes get lost sometimes (comment/uncomment to try)
if (psramFound()) {
Serial.println("PSRAM found");
camera_config.fb_count = 2;
camera_config.grab_mode = CAMERA_GRAB_LATEST;
}

//initialize the camera
Serial.print("Camera init... ");
Serial.println("Camera init... ");
esp_err_t err = esp_camera_init(&camera_config);

if (err != ESP_OK) {
Expand Down Expand Up @@ -126,26 +139,23 @@ size_t sendPicture(TBMessage& msg) {
return 0;
}
size_t len = fb->len;
if (!myBot.sendPhoto(msg, fb->buf, fb->len)){
len = 0;
myBot.sendMessage(msg, "Error! Picture not sent.");
}
myBot.sendPhoto(msg, fb->buf, fb->len);

// Clear buffer
esp_camera_fb_return(fb);
return len;
}

void setup() {
void setup() {
Serial.begin(115200);
Serial.println();

WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // disable brownout detector
pinMode(LAMP_PIN, OUTPUT); // set the lamp pin as output
pinMode(LAMP_PIN, OUTPUT); // set the lamp pin as output
ledcSetup(lampChannel, pwmfreq, pwmresolution); // configure LED PWM channel
setLamp(0); // set default value
setLamp(0); // set default value
ledcAttachPin(LAMP_PIN, lampChannel); // attach the GPIO pin to the channel

Serial.begin(115200);
Serial.println();

// Start WiFi connection
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
Expand All @@ -160,6 +170,12 @@ void setup() {
configTzTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
client.setCACert(telegram_cert);

myBot.addSentCallback([](bool sent){
const char* res = sent ? "Picture delivered!" : "Error! Picture NOT delivered";
if (!sent)
myBot.sendTo(userid, res);
}, 3000);

// Set the Telegram bot properies
myBot.setUpdateTime(1000);
myBot.setTelegramToken(token);
Expand Down Expand Up @@ -191,11 +207,10 @@ void loop() {
if (msg.text.equalsIgnoreCase("/takePhoto")) {
Serial.println("\nSending Photo from CAM");
if (sendPicture(msg))
Serial.println("Picture sent successfull");
Serial.println("Picture sent successfull");
else
myBot.sendMessage(msg, "Error, picture not sent.");
}
else {
myBot.sendMessage(msg, "Error, picture not sent.");
} else {
Serial.print("\nText message received: ");
Serial.println(msg.text);
String replyStr = "Message received:\n";
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=AsyncTelegram2
version=2.2.1
version=2.2.2
author=Tolentino Cotesta <[email protected]>
maintainer=Tolentino Cotesta <[email protected]>
sentence=Powerful, flexible and secure Arduino Telegram BOT library. Hardware independent, it can be used with any MCU capable of handling an SSL connection.
Expand Down
Loading

0 comments on commit 5260273

Please sign in to comment.