I am powering my esp cam thinker with a steady 5 volts. Every time I reboot I see 'dirty' data on the serial monitor.
This code takes 1 photo and sends it via email. But every time he tries to take pictures and he can't, he'll only get it the third or fourth time.
I wonder why ?
Another doubt: If he starts to take the picture right away, the connection with the smtp server will be the second longest delay.
If I were to take several photos in sequence, what would be the method to maintain the connection? that is, not disconnect until I want to through some command?
I have a row of products and I take pictures one by one every 5 seconds and send them by email. It has to be one by one. So staying connected to smtp would be fine, disconnecting only when done with all the photos.
#include "esp_camera.h"
#include "SPI.h"
#include "driver/rtc_io.h"
#include "soc/soc.h" // Disable brownour problems
#include "soc/rtc_cntl_reg.h" // Disable brownour problems
#include "ESP32_MailClient.h"
#include <FS.h>
#include <SPIFFS.h>
#include <WiFi.h>
const char* ssid = "SINAL_2Ghz";
const char* password = "sinal123";
#define sender_email "xxxxxxxxxxxxx@yahoo.com"
#define sender_email_password "xxxxxxxxxxxxx"
#define SMTP_Server "smtp.mail.yahoo.com"
#define SMTP_Server_Port 587
#define email_subject "xxxxxxxxxxxxxxx"
#define email_recipient "xxxxxxxxxxxxx"
#define LED 4
#define CAMERA_MODEL_AI_THINKER
#if defined(CAMERA_MODEL_AI_THINKER)
#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
#else
#error "Camera model not selected"
#endif
SMTPData smtpData;
#define IMAGE_PATH "/image.jpg"
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
Serial.begin(9600);
Serial1.begin(9600, SERIAL_8N1, 14, 15);
Serial.println();
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
ESP.restart();
}
else {
delay(500);
Serial.println("SPIFFS mounted successfully");
}
Serial.print("IP Address: http://");
Serial.println(WiFi.localIP());
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;
if(psramFound()){
config.frame_size = FRAMESIZE_UXGA;
config.jpeg_quality = 10;
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
}
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
captureSave_image();
sendImage();
}
void loop() {
comunicacao();
//listDir(SPIFFS, "/", 0);
//delay(2000);
//deleteFile(SPIFFS, "/image.jpg");
//delay(2000);
//deleteFile(SPIFFS, "/photo.jpg");
//delay(2000);
}
// Check if photo capture was successful
bool check_photo( fs::FS &fs ) {
File f_pic = fs.open( IMAGE_PATH );
unsigned int pic_sz = f_pic.size();
return ( pic_sz > 100 );
}
// Capture Photo and Save it to SPIFFS
void captureSave_image( void ) {
camera_fb_t * fb = NULL;
bool ok = 0;
do {
Serial.println("ESP32-CAM capturing photo...");
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Failed");
return;
}
Serial.printf("Picture file name: %s\n", IMAGE_PATH);
File file = SPIFFS.open(IMAGE_PATH, FILE_WRITE);
if (!file) {
Serial.println("Failed to open file in writing mode");
}
else {
file.write(fb->buf, fb->len);
Serial.print("The picture has been saved in ");
Serial.print(IMAGE_PATH);
Serial.print(" - Size: ");
Serial.print(file.size());
Serial.println(" bytes");
}
file.close();
esp_camera_fb_return(fb);
ok = check_photo(SPIFFS);
} while ( !ok );
}
//LISTAR DIRETÓRIOS
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("Failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
//DELETAR ARQUIVOS
void deleteFile(fs::FS &fs, const char * path){
Serial.printf("Deleting file: %s\n", path);
if(fs.remove(path)){
Serial.println("File deleted");
} else {
Serial.println("Delete failed");
}
}
void sendImage( void ) {
smtpData.setLogin(SMTP_Server, SMTP_Server_Port, sender_email, sender_email_password);
smtpData.setSender("ESP32-CAM", sender_email);
smtpData.setPriority("High");
smtpData.setSubject(email_subject);
smtpData.setMessage("PFA ESP32-CAM Captured Image.", false);
smtpData.addRecipient(email_recipient);
smtpData.addAttachFile(IMAGE_PATH, "image/jpg");
smtpData.setFileStorageType(MailClientStorageType::SPIFFS);
smtpData.setSendCallback(sendCallback);
if (!MailClient.sendMail(smtpData))
Serial.println("Error sending Email, " + MailClient.smtpErrorReason());
smtpData.empty();
}
void sendCallback(SendStatus msg) {
Serial.println(msg.info());
}
void comunicacao() {
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
//Serial.write(inByte);
Serial1.write(inByte);
}
}