Acoptex.Com

Learn ESP32, ESP8266, Arduino, and Raspberry Pi

How to take and send photos via email with ESP32-CAM

We will learn how to send captured photos from the ESP32-CAM to your email account using an SMTP Server. It will be an example when a picture is taken when the ESP32 boots and sent as an attachment in the email. This picture temporary saved in the ESP32-CAM SPIFFS. The ESP32-CAM needs to be connected trough the local network to a router with access to the internet to complete this project. We will use Simple Mail Transfer Protocol (SMTP) to send an email with an attachment (a picture taken with the ESP32-CAM). Simple Mail Transfer Protocol (SMTP) is an internet standard for email transmission. Each email provider has a different SMTP server. You need to know your SMTP server details to send emails. More ESP32-CAM projects you can find here.

Tag: Project 136d How to take and send photos via email using an SMTP Server with ESP32-CAM AI-Thinker

Project resources

  • Libraries used: ESP32 MailClient library. You can read more about it here.
  • Codesketch

1. ESP32-CAM Development Board with camera 1 set

ESP32-CAM

2. Arduino IDE ( you can download it from here  )

3. USB to TTL/Serial adaptor/converter 1 pc

USB to TTL/Serial adaptor/converter
USB to TTL/Serial adaptor/converter

4. Jumper cables F-F

JUMPER CABLES

5. Micro SD card and SD card adapter 1 pc

MICRO SD WITH ADAPTER

6. WiFi AntennaCompact Internal WiFi Antenna with U.FL connector and 5.9 inch cable 1 pc (optional)

WiFi Antenna, Compact Internal WiFi Antenna with U.FL connector and 5.9 inch cable

Understanding the USB to TTL converter

You can read more about them here.

Understanding the ESP32-CAM Development Board with camera

You can read more about it here.

You can find the ESP32-CAM schematic here, ESP32-CAM development board specification is here, camera specification is here.

Signals and connections of the ESP32-CAM Development Board with camera

Signals and connections of the ESP32-CAM Development Board with camera

ESP32-CAM pinout:

ESP32-CAM pinout
  • GND – ground pin. Connected to Arduino board GND pin.
  • 5V – external power supply pin 5VDC 2A
  • VCC – power supply pin. Connected to 5V pin.
  • 3V3 – power supply pin. Connected to 3V3 pin.
  • U0TXD, U0RXD – Serial pins. You need these pins to upload code to your board.
  • GPIO 0 – pin which determines whether the ESP32 is in flashing mode or not. When GPIO 0 connected to GND, the ESP32-CAM is in flashing mode.

The following pins are internally connected to the Micro SD card reader:

  • GPIO 14: CLK
  • GPIO 15: CMD
  • GPIO 2: Data 0
  • GPIO 4: Data 1 (also connected to the on-board LED)
  • GPIO 12: Data 2
  • GPIO 13: Data 3

Wiring

ESP32-CAM WIRING

ESP32-CAM module    FTDI programmer

U0TXD (GPIO1)         RXI                  

U0RXD (GPIO3)         TX0

GND                          GND

5V                             5V

GND and GPIO0 (for flashing only)                                 

Step by Step instruction

1.Preparations

We assume that you have the latest version the Arduino IDE and ESP32 add on installed in your Arduino IDE. You can read how to do it here – Basics: Project 137 Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, Linux). You can download the latest Arduino IDE version Windows Installer here. We are using PC with Windows 7 64 bit OS.

2. Creating a new email account

We recommend you to create a new email account for this project to send the emails to your main personal email address. Please do not use your main personal email to send emails via ESP32-CAM. If you will make too many requests, you can be banned by GOOGLE or have your account temporary disabled.

We will use a new gmail.com account to send the emails, but you can use a different email provider. The receiver email can be your personal email.

  1. Let’s create a new email account. Go to this address.

2. Click on Create an account button.

How to take and send photos via email using an SMTP Server with ESP32-CAM AI-Thinker

3. Fill in your details and click on Next button.

How to take and send photos via email using an SMTP Server with ESP32-CAM AI-Thinker

4. Fill in your details again and click on Next button.

How to take and send photos via email using an SMTP Server with ESP32-CAM AI-Thinker

5.Select Express personalisation (1step) and click on Next button.

How to take and send photos via email using an SMTP Server with ESP32-CAM AI-Thinker

6.Confirm personalisation settings and cookies. Click on Confirm button.

How to take and send photos via email using an SMTP Server with ESP32-CAM AI-Thinker

7. Agree with Privacy and Terms. Click on I agree button.

How to take and send photos via email using an SMTP Server with ESP32-CAM AI-Thinker

8. Select Turn off smart features and click on Next button.

How to take and send photos via email using an SMTP Server with ESP32-CAM AI-Thinker

9. Click on Turn off features button.

How to take and send photos via email using an SMTP Server with ESP32-CAM AI-Thinker

10. Click on Google Account icon. Then click on Manage you Google Account button.

How to take and send photos via email using an SMTP Server with ESP32-CAM AI-Thinker

11. Go to Security and find Less secure app access. Click on Turn on access (not recommended). You need to allow less secure apps to get access to this new Gmail account, so that you are able to send emails.

How to take and send photos via email using an SMTP Server with ESP32-CAM AI-Thinker
How to take and send photos via email using an SMTP Server with ESP32-CAM AI-Thinker

12. If you’re using a Gmail account, these are the SMTP Server details:

  • SMTP Server: smtp.gmail.com
  • SMTP username: your complete gmail email address
  • SMTP password: your gmail password
  • SMTP port (TLS): 587
  • SMTP port (SSL): 465
  • SMTP TLS/SSL required: yes

13. For Outlook accounts the SMTP Server settings are located below:

  • SMTP Server: smtp.office365.com
  • SMTP Username: complete outlook email address
  • SMTP Password: your outlook password
  • SMTP Port: 587
  • SMTP TLS/SSL Required: Yes

14. For Live or Hotmail accounts, use these SMTP Server settings:

  • SMTP Server: smtp.live.com
  • SMTP Username: complete Live/Hotmail email address
  • SMTP Password: your Windows Live/Hotmail password
  • SMTP Port: 587
  • SMTP TLS/SSL Required: Yes

If you are using another email provider then you need to search for its SMTP Server settings.

3.Uploading the code to ESP32-CAM

1.Do wiring. Very important: GPIO 0 (IO 0) must be connected to GND otherwise you will not be able to upload code.

2. Attach WiFi antenna (optional). It will help you to increase WiFi range.

3.Attach OV2640 camera.

4.Plug your FTDI programmer into your PC USB port.

5.Open Arduino IDE.

6.Go to Tools -> Board… -> ESP32 Arduino ->and select AI Thinker ESP32-CAM. Make sure you select the right camera module. In this project we use the AI-THINKER ESP32-CAM Development Board with camera. 

ESP32-CAM webserver

7.Go to Tools -> Port and select the COM port the ESP32-CAM Development Board connected to (we have COM3).

ESP32-CAM webserver

6. Open this sketch in your Arduino IDE, modify with your local network data (ssid and password) and sender and recipient SMTP server settings.

ESP32-CAM SMTP server

7.Click the upload button in Arduino IDE to upload the sketch. When you see Uploading… message press the ESP32-CAM Development Board on-board RST (RESET) button to restart your ESP32 in flashing mode.

ESP32-CAM SMTP server

9.When uploading completed disconnect GPIO 0 from GND.

10.Open the Serial Monitor of Arduino IDE (Tools -> Serial Monitor) at the baud rate of 115200. Press the ESP32-CAM Development board on-board RST (RESET) button. The picture will be taken and sent to provided email address.

ESP32-CAM SMTP server

11.Open your recipient email box. After a few seconds, you should have a new email from the ESP32-CAM in your inbox. As you can see in the image below, the sender’s email name is ESP32-CAM as we have defined in the code and the subject is ESP32-CAM Picture.

ESP32-CAM SMTP server

12.Open the email and you will see a picture captured by the ESP32-CAM. You can open or download the picture to see it in full size.

ESP32-CAM SMTP server
ESP32-CAM SMTP server
ESP32-CAM

Code

First we import all required libraries -the ESP32_MailClient.h is used to send emails, the FS.h and SPIFFS.h are used to access and save files to SPIFFS and the WiFi.h library is used to initialize WiFi and connect your ESP32-CAM to your local network.

We insert your local network credentials in the following variables:

const char* ssid = “yourSSIDhere”;
const char* password = “yourPASSWORDhere”;

and the email sender account in the emailSenderAccount variable with its password in the emailSenderPassword variable. Next we insert the recipient’s email which will receive the emails sent by the ESP32-CAM and your email provider SMTP settings – smtpServer and smtpServerPort.

Then we write the email subject on the emailSubject variable and create an STMPData object called smtpData that contains the data to send via email and all the other configurations.

The picture taken with the ESP32-CAM will be temporary saved in SPIFFS under the name picture.jpg.

We define the pins used by the AI-Thinker ESP32-CAM.

In the setup(), we initialize the serial communication at 115200 bps, connect the ESP32-CAM to the WiFi and initialize SPI Flash File System (SPIFFS) to save the last picture taken with the ESP32-CAM.

Next we print the ESP32-CAM IP address: Serial.println(WiFi.localIP());

The following lines configure the camera and set the camera settings:

  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;
  }

Then we initialize the camera.

esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }

After initializing the camera, we call the capturePictureSaveSpiffs() and the sendPicture() functions. They are defined at the end of the sketch.

The capturePictureSaveSpiffs() function captures a picture and saves it in the ESP32-CAM SPIFFS. In the following lines, you take a picture and save it in the framebuffer fb:

fb = esp_camera_fb_get();
    if (!fb) {
      Serial.println("Failed to take a picture!");
      return;
    }

Then we create a new file in SPIFFS where the picture will be saved.

 Serial.printf("Picture file name is %s\n", PICTURE_FILE);
    File file = SPIFFS.open(PICTURE_FILE, FILE_WRITE);

We check if the file was successfully created. If it is not, then we print an error message. If a new file was successfully created, we save the image from the buffer to that newly created file.

if (!file) {
      Serial.println("Failed to open file in writing mode");
    }
    else {
      file.write(fb->buf, fb->len); // payload (image), payload length
      Serial.print("The picture has been saved in ");
      Serial.print(PICTURE_FILE);
      Serial.print(" - Size: ");
      Serial.print(file.size());
      Serial.println(" bytes");
    }

We close the file and clear the buffer for future use.

 file.close();
    esp_camera_fb_return(fb);

We check whether the picture was successfully taken and saved. We can do that by checking the picture file size with the checkPicture() function.

ok = checkPicture(SPIFFS);

The checkPicture() function checks the file picture size. If the picture size is bigger than 100 bytes, it’s almost certain the photo was taken and saved successfully. So, the ok variable is equal to 1.

bool checkPicture( fs::FS &fs ) {
  File f_pic = fs.open( PICTURE_FILE );
  unsigned int pic_sz = f_pic.size();
  return ( pic_sz > 100 );
}

In case the picture size is less than 100 bytes, the ok variable will continue to be 0, and it will keep trying to take a new picture and save it.

After having the picture successfully saved in SPIFFS, we’ll send it via email by calling the sendPicture() function.

We set the SMTP Server Email host, port, account and password:

smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);

We set the sender name and email:

smtpData.setSender("ESP32-CAM", emailSenderAccount);

We set email priority or importance High, Normal, Low or 1 to 5 (1 is highest) and the subject of email:

smtpData.setPriority("High");
smtpData.setSubject(emailSubject);

The following line sets the message. It can be a HTML text or raw text. In our case, we are going to send a message with some HTML. When sending a message in HTML format, you should pass true as a second parameter to the setMessage() method. If you want to send raw text, set false.

smtpData.setMessage("<h2>Picture taken with ESP32-CAM attached in this email.</h2>", true);

smtpData.setMessage("Picture taken with ESP32-CAM attached in this email.", false);

Then we set the recipient email which will receive the messages from the ESP32-CAM.

smtpData.addRecipient(emailRecipient);

We need to call the addAtatachFile() on the smtpData object and pass as argument the file path to attach the file.

  smtpData.addAttachFile(PICTURE_FILE, "image/jpg");

Finally, we need to set where the files are saved (SPIFFS or SD card). We are using SPIFFS:

smtpData.setFileStorageType(MailClientStorageType::SPIFFS);

We set a callback function that will be called upon sending an email.

smtpData.setSendCallback(sendCallback);

The sendCallback() function returns whether the email was successfully sent or not. We have set all the details of the smtpData, so we are ready to send the email.

 if (!MailClient.sendMail(smtpData))
    Serial.println("Error sending Email, " + MailClient.smtpErrorReason());

After sending the email, we can clear all the data from the smtpData object.

smtpData.empty();

In this project, the email will be sent once as soon as the ESP32-CAM boots, that is why the loop() is empty.

Just reset your ESP32-CAM board (press the on-board RESET (RST) button) to send a new email, and it restarts and runs the code again.

Summary

We have learnt how to send captured photos from the ESP32-CAM to your email account using an SMTP Server.

Check for more DIY projects on Acoptex.lt and Acoptex.com!

If you are looking for high quality PCBs PCBWay is the best choice: