Closed feiticeir0 closed 7 months ago
Regarding using deepsleep, if you want to save power, I suggest you keep the camera Off until there is movement detected by the PIR. By using one N-Mosfet (AO3400) and one P-Mosfet(AO3401), the camera stays ON while the PIR output stays HIGH, during enough time to connect to the internet and send the photo to Telegram.
(everything is working at 3.3V in the project below)
Replace:
camera_fb_t * fb = NULL;
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
delay(1000);
ESP.restart();
return "Camera capture failed";
}
With:
camera_fb_t * fb = NULL;
for (int j = 0; j < 7; j++) {
camera_fb_t * newfb = esp_camera_fb_get();
if (!newfb) {
Serial.println("Camera Capture Failed");
} else {
esp_camera_fb_return(newfb);
delay(10); //delay(10);
}
}
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
bot.sendMessage(chat_id, "Camera capture failed", "");
return;
}
If you want to try a different sketch/library, this works for me. It sends a photo when powered ON.
#include <WiFi.h>
#include <AsyncTelegram2.h> //https://github.com/cotestatnt/AsyncTelegram2/tree/2.3.0
#include "esp_camera.h"
#include "soc/soc.h" // Brownout error fix
#include "soc/rtc_cntl_reg.h" // Brownout error fix
#include <WiFiClientSecure.h>
WiFiClientSecure secure_client;
const char* ssid = "your-ssid"; // SSID WiFi network
const char* pass = "your-password"; // Password WiFi network
const char* token = "your-token"; // (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 = 0000000; //replace with your userid
int wifi_start_counter = 0;
// Timezone definition to get properly time from NTP server
#define MYTZ "WET0WEST,M3.5.0/1,M10.5.0" // Portugal
AsyncTelegram2 myBot(secure_client);
#define LED_PIN 33 // Pin 33 on ESP32CAM
#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
static camera_config_t camera_config = {
.pin_pwdn = PWDN_GPIO_NUM,
.pin_reset = RESET_GPIO_NUM,
.pin_xclk = XCLK_GPIO_NUM,
.pin_sscb_sda = SIOD_GPIO_NUM,
.pin_sscb_scl = SIOC_GPIO_NUM,
.pin_d7 = Y9_GPIO_NUM,
.pin_d6 = Y8_GPIO_NUM,
.pin_d5 = Y7_GPIO_NUM,
.pin_d4 = Y6_GPIO_NUM,
.pin_d3 = Y5_GPIO_NUM,
.pin_d2 = Y4_GPIO_NUM,
.pin_d1 = Y3_GPIO_NUM,
.pin_d0 = Y2_GPIO_NUM,
.pin_vsync = VSYNC_GPIO_NUM,
.pin_href = HREF_GPIO_NUM,
.pin_pclk = PCLK_GPIO_NUM,
.xclk_freq_hz = 20000000,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,
.pixel_format = PIXFORMAT_JPEG, /* PIXFORMAT_RGB565, // for face detection/recognition */
.frame_size = FRAMESIZE_VGA,
.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 */
};
camera_fb_t * fb = NULL;
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);
if (err != ESP_OK) {
delay(100); // need a delay here or the next serial o/p gets missed
Serial.printf("\n\nCRITICAL FAILURE: Camera sensor failed to initialise.\n\n");
Serial.printf("A full (hard, power off/on) reboot will probably be needed to recover from this.\n");
return err;
} else {
Serial.println("succeeded");
// Get a reference to the sensor
sensor_t* s = esp_camera_sensor_get();
// Dump camera module, warn for unsupported modules.
switch (s->id.PID) {
case OV9650_PID: Serial.println("WARNING: OV9650 camera module is not properly supported, will fallback to OV2640 operation"); break;
case OV7725_PID: Serial.println("WARNING: OV7725 camera module is not properly supported, will fallback to OV2640 operation"); break;
case OV2640_PID: Serial.println("OV2640 camera module detected"); break;
case OV3660_PID: Serial.println("OV3660 camera module detected"); break;
default: Serial.println("WARNING: Camera module is unknown and not properly supported, will fallback to OV2640 operation");
}
// Adjust sensor settings
s->set_whitebal(s, 1); // 0 = disable , 1 = enable
s->set_awb_gain(s, 1); // 0 = disable , 1 = enable
s->set_wb_mode(s, 2); // 0 to 4 - if awb_gain enabled (0 - Auto, 1 - Sunny, 2 - Cloudy, 3 - Office, 4 - Home)
s->set_hmirror(s, 1); // 0 = disable , 1 = enable
s->set_vflip(s, 1); // 0 = disable , 1 = enable
}
return ESP_OK;
}
size_t sendPicture(int64_t userid) {
// Take Picture with Camera;
Serial.println("Camera capture requested");
for (int j = 0; j < 4; j++) {
camera_fb_t * newfb = esp_camera_fb_get();
if (!newfb) {
Serial.println("Camera Capture Failed");
} else {
//Serial.print("Pic, len="); Serial.print(newfb->len);
//Serial.printf(", new fb %X\n", (long)newfb->buf);
esp_camera_fb_return(newfb);
delay(100);
}
}
// Take Picture with Camera
camera_fb_t* fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return 0;
}
size_t len = fb->len;
myBot.sendPhoto(userid, fb->buf, fb->len);
// Clear buffer
esp_camera_fb_return(fb);
return len;
}
void init_wifi() {
// Init WiFi connections
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
wifi_start_counter++;
if (wifi_start_counter >= 60) {
break; //
}
}
Serial.print("\nWiFi connected: ");
Serial.println(WiFi.localIP());
}
void blinkLED(int numBlinks, int blinkInterval) {
for (int i = 0; i < numBlinks; i++) {
digitalWrite(LED_PIN, LOW); // Turn on the LED
delay(blinkInterval);
digitalWrite(LED_PIN, HIGH); // Turn off the LED
delay(blinkInterval);
}
}
void sendToTelegram() {
// Init the camera module (accordind the camera_config_t defined)
init_camera();
// Sync time with NTP
configTzTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
secure_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);
// Check if all things are ok
Serial.print("\nTest Telegram connection... ");
myBot.begin() ? Serial.println("OK") : Serial.println("NOK");
Serial.println("\nSending Photo from CAM");
if (sendPicture(userid))
Serial.println("Picture sent successfull");
else
myBot.sendTo(userid, "Error, picture not sent.");
}
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // disable brownout detector
Serial.begin(115200);
Serial.println();
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
blinkLED(2, 100); // blink red led two times on power on
init_wifi();
sendToTelegram();
blinkLED(3, 100); // blink red led three times when picture has been sent
}
void loop() {
}
Hi ! Thank you @FBMinis ! Will definitely try your method. Will keep you posted. But, still don't know why ESP32-Cam keeps turning the images white.
Regarding using deepsleep, if you want to save power, I suggest you keep the camera Off until there is movement detected by the PIR. By using one N-Mosfet (AO3400) and one P-Mosfet(AO3401), the camera stays ON while the PIR output stays HIGH, during enough time to connect to the internet and send the photo to Telegram.
(everything is working at 3.3V in the project below)
Regarding your first post, I'm using a PIR module connected to PIN 2 (if not mistaken) - and the ESP32 is in deep sleep. Only when theres a HIGH value on the PIN, it wakes up, initializes the camera, takes a picture and goes back to sleep.
I would love to try your approach. can you send me a schematics ?
This issue appears to be stale. Please close it if its no longer valid.
I'm experiencing weird problems with the ESP32-CAM photos when coming from deep sleep.
The first two to three photos came well, but after that, they start getting white until I can't see anything. This only happens outside and in daylight and only with deep sleep.
The system is configure with a PIR sensor that, if detects activity, it wakes up from deep sleep, takes a picture, sends it to telegram and enters the deep sleep state again. After a while, they just keep getting whiter... Here's the first one while I was setting the ESP32-Cam:
After 3 more photos:
The thing is, if I try the webserver stream example and go outside, the image is always good. I access the ESP32-CAM and see the stream and nothing like this happens.
I've even tried in the code to configure the camera with the same default parameters that are defined in the webserver stream and it starts to happen the same thing.
Here's my camera sensor settings:
I did try to reduce some values to the mininum, like brightness and contrast, etc, but still the same.
With or without this settings, the end result is always the same - after a while, full white images. I've already tried all the ESP32-Cam that I have (5 of them) and they all do the same.
Here's the full code.
Thank you