robotzero1 / esp32cam-telegram

Use an ESP32-CAM as a Telegram bot.
23 stars 7 forks source link

take picture and send picture serarately #2

Closed mugginsjm closed 2 years ago

mugginsjm commented 4 years ago

Hi can any one help please. I want to have 2 functions, one to take photo and each time to overwrite the previous one, and another to send it. My efforts do not work, it does not overwrite and esp32 crashes every so often with: esp_camera_fb_get(): Failed to get the frame on time!

void takePhoto(){ 
  fb_length = NULL;
  fb_buffer = NULL;
camera_fb_t * fb = NULL;
  fb = esp_camera_fb_get();
  currentByte = 0;
  fb_length = fb->len;
  fb_buffer = fb->buf;
}
void sendPhoto(String chat_id){
 bot->sendPhotoByBinary(chat_id, "image/jpeg", fb_length, isMoreDataAvailable, photoNextByte, nullptr, nullptr);

  esp_camera_fb_return(fb);
  fb_length = NULL;
  fb_buffer = NULL;
}
robotzero1 commented 4 years ago

HI, You want to take a photo, save it over a previous photo and then send it? Where are the saving the photos?

mugginsjm commented 4 years ago

Hi

I have an external trigger which captures a photo to a buffer. Each time it triggers I want to discard the old photo and overwrite with the new one. Only when another condition is met will I send to Telegram.

So I want to replace void take_send_photo() (which works perfectly) with 2 separate functions.

thanks

From: robotzero1 [mailto:notifications@github.com] Sent: 20 June 2020 18:10 To: robotzero1/esp32cam-telegram esp32cam-telegram@noreply.github.com Cc: mugginsjm mugginsjm@gmail.com; Author author@noreply.github.com Subject: Re: [robotzero1/esp32cam-telegram] take picture and send picture serarately (#2)

HI, You want to take a photo, save it over a previous photo and then send it? Where are the saving the photos?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/robotzero1/esp32cam-telegram/issues/2#issuecomment-647022073 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJGBCNSUE2J53QN3S3RQWLRXTUPPANCNFSM4ODKLBZA . https://github.com/notifications/beacon/ABJGBCPKER7EH2GQLIARS6TRXTUPPA5CNFSM4ODKLBZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOE2IML6I.gif

mugginsjm commented 4 years ago

I'm not using an SD card, just using buffer

robotzero1 commented 4 years ago

Does this work?

void trigger_photo()
{
  fb_length = NULL;
  fb_buffer = NULL;
  camera_fb_t * fb = NULL;
  fb = esp_camera_fb_get();
  fb_length = fb->len;
  fb_buffer = fb->buf;
  esp_camera_fb_return(fb);
}

void send_photo(String chat_id)
{
  currentByte = 0;
  bot.sendPhotoByBinary(chat_id, "image/jpeg", fb_length, isMoreDataAvailable, photoNextByte, nullptr, nullptr);
}
mugginsjm commented 4 years ago

Hi

Much appreciate the response as I have been fighting with this for some time.

I had to change fb->len to fb_length in the send photo function.

Unfortunately there is still a problem as the images are now corrupted and “blocky”

John

From: robotzero1 [mailto:notifications@github.com] Sent: 22 June 2020 10:32 To: robotzero1/esp32cam-telegram esp32cam-telegram@noreply.github.com Cc: mugginsjm mugginsjm@gmail.com; Author author@noreply.github.com Subject: Re: [robotzero1/esp32cam-telegram] take picture and send picture serarately (#2)

Does this work?

void trigger_photo() { fb_length = NULL; fb_buffer = NULL; camera_fb_t * fb = NULL; fb = esp_camera_fb_get(); fb_length = fb->len; fb_buffer = fb->buf; esp_camera_fb_return(fb); }

void send_photo(String chat_id) { currentByte = 0; bot.sendPhotoByBinary(chat_id, "image/jpeg", fb->len, isMoreDataAvailable, photoNextByte, nullptr, nullptr); }

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/robotzero1/esp32cam-telegram/issues/2#issuecomment-647401350 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJGBCKV46YDJIAQ65IIL33RX4QK3ANCNFSM4ODKLBZA . https://github.com/notifications/beacon/ABJGBCNSDQ5NFZW3XAESQCLRX4QK3A5CNFSM4ODKLBZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOE2LI7BQ.gif

robotzero1 commented 4 years ago

Maybe change the value here: s->set_framesize(s, FRAMESIZE_XGA); to a smaller one to see what happens?

I would strip back the code as much as possible and just have the Telegram part but with the two functions above rather than just the take_send_photo(chat_id); one

mugginsjm commented 4 years ago

Thanks, I’ll try that but with my current settings I get perfect photos every time using take_send_photo(chat_id).

I’ve also experimented with saving to spiffs but don’t know how to send it to telegram.

From: robotzero1 [mailto:notifications@github.com] Sent: 22 June 2020 13:18 To: robotzero1/esp32cam-telegram esp32cam-telegram@noreply.github.com Cc: mugginsjm mugginsjm@gmail.com; Author author@noreply.github.com Subject: Re: [robotzero1/esp32cam-telegram] take picture and send picture serarately (#2)

Maybe change the value here: s->set_framesize(s, FRAMESIZE_XGA); to a smaller one to see what happens?

I would strip back the code as much as possible and just have the Telegram part but with the two functions above rather than just the take_send_photo(chat_id); one

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/robotzero1/esp32cam-telegram/issues/2#issuecomment-647480368 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJGBCNBC57IIQ5IDT3WQF3RX5DWFANCNFSM4ODKLBZA . https://github.com/notifications/beacon/ABJGBCN4UZJIXYJB4XC7CCTRX5DWFA5CNFSM4ODKLBZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOE2L4IMA.gif

robotzero1 commented 4 years ago

I briefly tested like this and it seems OK to me... (it keeps messing up the formatting but you can hopefully see the two functions and them in the loop.)

/*** An example of bot that echos back any messages received

  • * written by Giacarlo Bacchio (Gianbacchio on Github) adapted by Brian Lough ***/

    include

    include

    include "esp_camera.h"

    include "UniversalTelegramBotRZO.h"

// Initialize Wifi connection to the router char ssid[] = "NSA"; // your network SSID (name) char password[] = "on"; // your network key String chat_id;

// Initialize Telegram BOT

define BOTtoken "10-------------------2hu0" // your Bot Token (Get from Botfather)

String token = BOTtoken;

WiFiClientSecure client; UniversalTelegramBot bot(BOTtoken, client);

int Bot_mtbs = 3000; //mean time between scan messages long Bot_lasttime; //last time messages' scan has been done

camera_fb_t fb; uint8_t fb_buffer; size_t fb_length; int currentByte;

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

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

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 = FRAMESIZE_UXGA; config.jpeg_quality = 10; config.fb_count = 1; // Trying to reduce memory use } else { config.frame_size = FRAMESIZE_SVGA; config.jpeg_quality = 12; config.fb_count = 1; } //pinMode(13, INPUT_PULLUP); //pinMode(14, INPUT_PULLUP); // 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; }

sensor_t * s = esp_camera_sensor_get();

s->set_framesize(s, FRAMESIZE_XGA);

// 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 WiFi.mode(WIFI_STA); WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); }

Serial.println(""); Serial.println("WiFi connected"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); }

bool isMoreDataAvailable() { return (fb_length - currentByte); }

uint8_t photoNextByte() { currentByte++; return (fb_buffer[currentByte - 1]); }

void take_send_photo(String chat_id) { camera_fb_t * fb = NULL; fb = esp_camera_fb_get(); currentByte = 0; fb_length = fb->len; fb_buffer = fb->buf; bot.sendPhotoByBinary(chat_id, "image/jpeg", fb->len, isMoreDataAvailable, photoNextByte, nullptr, nullptr); esp_camera_fb_return(fb); fb_length = NULL; fb_buffer = NULL; }

void trigger_photo() { fb_length = NULL; fb_buffer = NULL; camera_fb_t * fb = NULL; fb = esp_camera_fb_get(); fb_length = fb->len; fb_buffer = fb->buf; esp_camera_fb_return(fb); }

void send_photo(String chat_id) { currentByte = 0; bot.sendPhotoByBinary(chat_id, "image/jpeg", fb_length, isMoreDataAvailable, photoNextByte, nullptr, nullptr); }

void loop() { if (millis() > Bot_lasttime + Bot_mtbs) { int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

while (numNewMessages) {
  Serial.println("got response");
  chat_id = bot.messages[0].chat_id;

  for (int i = 0; i < numNewMessages; i++) {
    //bot.sendMessage(bot.messages[i].chat_id, bot.messages[i].text, "");
    trigger_photo();
    send_photo(chat_id);
  }
  numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}

Bot_lasttime = millis();

} }

mugginsjm commented 4 years ago

Thank you so much for taking the time to look at this. Your code works perfectly and now I just need to go through my own code which uses mqtt triggers and also ftp uploads and compare line by line.

Thanks again.

John

From: robotzero1 [mailto:notifications@github.com] Sent: 22 June 2020 16:57 To: robotzero1/esp32cam-telegram esp32cam-telegram@noreply.github.com Cc: mugginsjm mugginsjm@gmail.com; Author author@noreply.github.com Subject: Re: [robotzero1/esp32cam-telegram] take picture and send picture serarately (#2)

I briefly tested like this and it seems OK to me...

/*** An example of bot that echos back any messages received

// Initialize Wifi connection to the router char ssid[] = "NSA"; // your network SSID (name) char password[] = "on"; // your network key String chat_id;

// Initialize Telegram BOT

define BOTtoken "10-------------------2hu0" // your Bot Token (Get from Botfather)

String token = BOTtoken;

WiFiClientSecure client; UniversalTelegramBot bot(BOTtoken, client);

int Bot_mtbs = 3000; //mean time between scan messages long Bot_lasttime; //last time messages' scan has been done

camera_fb_t fb; uint8_t fb_buffer; size_t fb_length; int currentByte;

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

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

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 = FRAMESIZE_UXGA; config.jpeg_quality = 10; config.fb_count = 1; // Trying to reduce memory use } else { config.frame_size = FRAMESIZE_SVGA; config.jpeg_quality = 12; config.fb_count = 1; } //pinMode(13, INPUT_PULLUP); //pinMode(14, INPUT_PULLUP); // 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; }

sensor_t * s = esp_camera_sensor_get();

s->set_framesize(s, FRAMESIZE_XGA);

// 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 WiFi.mode(WIFI_STA); WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); }

Serial.println(""); Serial.println("WiFi connected"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); }

bool isMoreDataAvailable() { return (fb_length - currentByte); }

uint8_t photoNextByte() { currentByte++; return (fb_buffer[currentByte - 1]); }

void take_send_photo(String chat_id) { camera_fb_t * fb = NULL; fb = esp_camera_fb_get(); currentByte = 0; fb_length = fb->len; fb_buffer = fb->buf; bot.sendPhotoByBinary(chat_id, "image/jpeg", fb->len, isMoreDataAvailable, photoNextByte, nullptr, nullptr); esp_camera_fb_return(fb); fb_length = NULL; fb_buffer = NULL; }

void trigger_photo() { fb_length = NULL; fb_buffer = NULL; camera_fb_t * fb = NULL; fb = esp_camera_fb_get(); fb_length = fb->len; fb_buffer = fb->buf; esp_camera_fb_return(fb); }

void send_photo(String chat_id) { currentByte = 0; bot.sendPhotoByBinary(chat_id, "image/jpeg", fb_length, isMoreDataAvailable, photoNextByte, nullptr, nullptr); }

void loop() { if (millis() > Bot_lasttime + Bot_mtbs) { int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

while (numNewMessages) { Serial.println("got response"); chat_id = bot.messages[0].chat_id;

for (int i = 0; i < numNewMessages; i++) { //bot.sendMessage(bot.messages[i].chat_id, bot.messages[i].text, ""); trigger_photo(); send_photo(chat_id); } numNewMessages = bot.getUpdates(bot.last_message_received + 1); }

Bot_lasttime = millis();

} }

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/robotzero1/esp32cam-telegram/issues/2#issuecomment-647613188 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJGBCNF2VQMF4CCUQZV2ADRX55NDANCNFSM4ODKLBZA . https://github.com/notifications/beacon/ABJGBCKCBVO5T47LF7P6ERDRX55NDA5CNFSM4ODKLBZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOE2M4WBA.gif

robotzero1 commented 4 years ago

It might be a memory issue. The HTTPs takes up memory when it runs. I don't fully understand what happens but I remember when working on this that the memory was being fragmented during the SSL connection and it was leading to problems. I don't remember what I did to fix it!

Are you using the large app partition size?

Great article on memory here: https://learn.adafruit.com/memories-of-an-arduino/optimizing-sram

mugginsjm commented 4 years ago

Yes .. large partition. What I cannot understand is why using “take send photo” works perfectly every time.

Also strangely during testing I no longer receive messages (to trigger photo) I wonder has it been blocked. Maybe I’ll generate a new token…..

From: robotzero1 [mailto:notifications@github.com] Sent: 23 June 2020 11:43 To: robotzero1/esp32cam-telegram esp32cam-telegram@noreply.github.com Cc: mugginsjm mugginsjm@gmail.com; Author author@noreply.github.com Subject: Re: [robotzero1/esp32cam-telegram] take picture and send picture serarately (#2)

It might be a memory issue. The HTTPs takes up memory when it runs. I don't fully understand what happens but I remember when working on this that the memory was being fragmented during the SSL connection and it was leading to problems. I don't remember what I did to fix it!

Are you using the large app partition size?

Great article on memory here: https://learn.adafruit.com/memories-of-an-arduino/optimizing-sram

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/robotzero1/esp32cam-telegram/issues/2#issuecomment-648062288 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJGBCORJ6G2C2SMLNGY36TRYCBL3ANCNFSM4ODKLBZA . https://github.com/notifications/beacon/ABJGBCJSD7BIAQQI66JDDOLRYCBL3A5CNFSM4ODKLBZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOE2QKKUA.gif

robotzero1 commented 4 years ago

Another thing.. if you set debug to verbose you see a lot of information about the connection with Telegram.

mugginsjm commented 4 years ago

Thanks yes I did that and polling said connection was good but nothing was coming in. Will investigate further.

On Tue, 23 Jun 2020, 12:41 robotzero1, notifications@github.com wrote:

Another thing.. if you set debug to verbose you see a lot of information about the connection with Telegram.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/robotzero1/esp32cam-telegram/issues/2#issuecomment-648092042, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJGBCMXAZUBQ2IF7OKX2NDRYCIGDANCNFSM4ODKLBZA .