IOT

How to send ESP32 CAM Captured Image to Google Drive

In this blog, we will discuss how to send ESP32 CAM Captured Image to Google Drive in this project. We will utilize the ESP32-Cam Module to take images after a fixed period of time and then upload them to a folder. This project is unique in that we will upload photographs utilizing the Google Drive API. To access the Cloud folder, we will need a unique client ID and password.

The project is divided into four sections:

  • Writing code for ESP cam and uploading
  • Installing Python and all required libraries
  • Setting up the google drive API
  • Writing python code to upload the images

We learned about Face Detection systems, Color Detection systems, and Object Identification systems using Python and OpenCV in a previous ESP32 CAM-based project. For uploading the data to Google Drive, this project also necessitates the use of OpenCV.

Hardware Required:

  • ESP32-CAM Board-AI-Thinker ESP32 Camera Module
  • FTDI Module-USB-to-TTL Converter Module
  • USB Cable-5V Mini-USB Data Cable
  • Jumper Wires-Female to Female Connectors

ESP32 CAM Module

The ESP32 Based Camera Module was developed by AI-Thinker.  The controller contains a Wi-Fi + Bluetooth/BLE chip and is powered by a 32-bit CPU. It has a 520 KB internal SRAM and an external 4M PSRAM. UART, SPI, I2C, PWM, ADC, and DAC are all supported by its GPIO Pins.

The module is compatible with the OV2640 Camera Module, which has a camera resolution of 1600 x 1200 pixels. A 24-pin gold plated connector links the camera to the ESP32 CAM Board. A 4GB SD Card can be used on the board. The photographs captured are saved on the SD Card.

ESP32-CAM Features 

  • The smallest 802.11b/g/n Wi-Fi BT SoC module.
  • Low power 32-bit CPU, can also serve the application processor.
  • Up to 160MHz clock speed, summary computing power up to 600 DMIPS.
  • Built-in 520 KB SRAM, external 4MPSRAM.
  • Supports UART/SPI/I2C/PWM/ADC/DAC.
  • Support OV2640 and OV7670 cameras, built-in flash lamp.
  • Support image WiFI upload.
  • Supports TF card.
  • Supports multiple sleep modes.
  • Embedded Lwip and FreeRTOS.
  • Supports STA/AP/STA+AP operation mode.
  • Support Smart Config/AirKiss technology.
  • Support for serial port local and remote firmware upgrades (FOTA).

ESP32-CAM FTDI Connection

There is no programmer chip on the PCB. So, any form of USB-to-TTL Module can be used to program this board. FTDI Modules based on the CP2102 or CP2104 chip, or any other chip, are widely accessible.

  • Connect the FTDI Module to the ESP32 CAM Module as shown below.
ESP32 CAM FTDI Module Connection
ESP32-CAMFTDI Programmer
GNDGND
5VVCC
U0RTX
U0TRX
GPIO0GND

Connect the ESP32’s 5V and GND pins to the FTDI Module’s 5V and GND. Connect the Rx to UOT and the Tx to UOR Pin in the same way. The most crucial thing is that you must connect the IO0 and GND pins. The device will now be in programming mode. You can remove it once the programming is completed.

Project PCB Gerber File & PCB Ordering Online

If you don’t want to put the circuit together on a breadboard and instead prefer a PCB. EasyEDA s online Circuit Schematics & PCB Design tool was used to create the PCB Board for the ESP32 CAM Board. The PCB appears as seen below.

The Gerber File for the PCB is given below. You can simply download the Gerber File and order the PCB from https://www.nextpcb.com/

Download Gerber File: ESP32-CAM Multipurpose PCB

Now you can visit the NextPCB official website by clicking here: https://www.nextpcb.com/. So you will be directed to the NextPCB website

  • You can now upload the Gerber File to the Website and place an order. The PCB quality is excellent. That is why the majority of people entrust NextPCB with their PCB and PCBA needs.
  • The components can be assembled on the PCB Board.

Installing ESP32CAM Library

Another streaming process will be used instead of the general ESP webserver example. As a result, another ESPCAM library is required. On the ESP32 microcontroller, the esp32cam library provides an object-oriented API for using the OV2640 camera. It’s an esp32-camera library wrapper.

Download the zip library as shown in the image from the following Github Link

After downloading, unzip the library and place it in the Arduino Library folder. To do so, follow the instructions below:

Open Arduino -> Sketch -> Include Library -> Add .ZIP Library… -> Navigate to downloaded zip file -> add

Source Code/Program for ESP32 CAM Module

Here is a source code for sending ESP32 CAM Captured Image to Google Drive. Copy the code and paste it in the Arduino IDE.

#include <WiFi.h>
#include <esp32cam.h>
const char* WIFI_SSID = “ssid”;
const char* WIFI_PASS = “password”;
WebServer server(80);
static auto loRes = esp32cam::Resolution::find(320, 240);
static auto midRes = esp32cam::Resolution::find(350, 530);
static auto hiRes = esp32cam::Resolution::find(800, 600);
void serveJpg()
{
auto frame = esp32cam::capture();
if (frame == nullptr) {
Serial.println(“CAPTURE FAIL”);
server.send(503, “”, “”);
return;
}
Serial.printf(“CAPTURE OK %dx%d %db\n”, frame->getWidth(), frame->getHeight(),
static_cast<int>(frame->size()));
server.setContentLength(frame->size());
server.send(200, “image/jpeg”);
WiFiClient client = server.client();
frame->writeTo(client);
}
Void handleJpgLo()
{
if (!esp32cam::Camera.changeResolution(loRes)) {
Serial.println(“SET-LO-RES FAIL”);
}
serveJpg();
}
Void handleJpgHi()
{
if (!esp32cam::Camera.changeResolution(hiRes)) {
Serial.println(“SET-HI-RES FAIL”);
}
serveJpg();
}
Void handleJpgMid()
{
if (!esp32cam::Camera.changeResolution(midRes)) {
Serial.println(“SET-MID-RES FAIL”);
}
serveJpg();
}
Void setup(){
Serial.begin(115200);
Serial.println();
{
using namespace esp32cam;
Config cfg;
cfg.setPins(pins::AiThinker);
cfg.setResolution(hiRes);
cfg.setBufferCount(2);
cfg.setJpeg(80);
bool ok = Camera.begin(cfg);
Serial.println(ok ? “CAMERA OK” : “CAMERA FAIL”);
}
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.print(“http://”);
Serial.println(WiFi.localIP());
Serial.println(” /cam-lo.jpg”);
Serial.println(” /cam-hi.jpg”);
Serial.println(” /cam-mid.jpg”);
server.on(“/cam-lo.jpg”, handleJpgLo);
server.on(“/cam-hi.jpg”, handleJpgHi);
server.on(“/cam-mid.jpg”, handleJpgMid);
server.begin();
}
Void loop()
{
server.handleClient();
}

You must make a little adjustment to the code before uploading it. Change the SSID and password variables to match the WiFi network you’re using.

Compile the code and upload it to the ESP32 CAM Board. However, you must follow a few steps each time you post.

  • When you push the upload button, make sure the IO0 pin is shorted to the ground.
  • If you notice dots and dashes during uploading, immediately press the reset button.
  • Remove the I01 pin shorting with Ground and push the reset button one more after the code has been uploaded.
  • If the output is still not the Serial monitor, push the reset button once again.

Now you can see a similar output as in the image below.

Now, you’ve completed half of the task.

Python Library Installation

  • In order for the live video stream to appear on our computer, we must develop a Python script that allows us to retrieve the video frames. The first step is to get Python installed. Go to python.org and download Python.
  • Once downloaded, install Python.

Install NumPy and OpenCV libraries from the command prompt.

  • type: pip install numpy and press enter. After the installation is done.
  • type: pip install opencv-python and press enter.

Now open Idle or another Python code editor.

Setting up Google Drive API

Even though Google has made the Google Drive API public,  it has become difficult to grasp how things work. . You won’t have to worry about anything in this post because we’ll be using a library to help us out. PyDrive is the name of the library.

Before you begin using the Google Drive API or PyDrive, you must first create a project in the Google Developers Console, where Google will link your account to the API, provide you with authentication keys, and track your usage rate.

Steps to follow

1. Login with your Google account to the Google Developer Console. As shown in the figure below, a dashboard must be displayed.

2. Next, select a project (the option is shown on the blue bar) and then click New Project to start a new project.

3. Give the project a name and choose your organization ( or leave it as No Organization)

4. Once the project has been created, go to the Dashboard and select OAuth Consent Screen from the Sidebar. Then select Configure Consent Screen from the drop-down menu.

5. Now, click on External and CREATE, as seen in the figure below.

6. Fill in your email address and other information as shown in the images below, then click SAVE AND CONTINUE.

7. Select +CREATE CREDENTIALS from the Credentials menu.

8. Now pick Desktop App from the Application Type dropdown menu.

9. Once you’ve completed the above steps, click CREATE to be sent to the dashboard. The next step is to download a JSON file for credentials, which our Python code will use.

Rename this file client_sectrets.json after downloading it.

Now open Idle or any other Python code editor.

Python Code for Sending ESP32 CAM Captured Image to Google Drive

Please note that you must have already installed the previously specified Python libraries using PIP before running the following code. Let’s look at the code.

import cv2
import urllib.request
import numpy as np
import time
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
url=’http://192.168.162.162/cam-lo.jpg’
cv2.namedWindow(“live transmission”, cv2.WINDOW_AUTOSIZE)
count=0
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
folder =”1CCAlI_b5x7jjb4jlsk0NiDBpkkqpB9Nf”#please change this value according to your folder
while True:
img_resp=urllib.request.urlopen(url)
imgnp=np.array(bytearray(img_resp.read()),dtype=np.uint8)
frame=cv2.imdecode(imgnp,-1)

cv2.imshow(“live transmission”, frame)
key=cv2.waitKey(5)

if key==ord(‘k’):
count+=1
t=str(count)+’.png’

cv2.imwrite(t,frame)
print(“image saved as: “+t)
f= drive.CreateFile({‘parents’:[{‘id’:folder}],’title’:t})
f.SetContentFile(‘1.png’)
f.Upload()
print(“image uploaded as: “+t)

if key==ord(‘q’):
break
else:
continue
cv2.destroyAllWindows()

Testing & Results

You must make a few adjustments to the above code before running it.

  • To begin, update the IP address in the URL variable to the IP address seen in the Arduino Serial Monitor.
  • Save the json file in the same folder as the Python code.
  • To do so, go to the Google Drive folder where you wish to save the images and update the folder variable (make sure the google drive you are selecting is linked with the same email ID for which you did settings in Developer Console ). We must copy the folder id from the URL once we are inside the folder, as seen in the image below.
  • The code is then executed.

The code is then executed.

When it asks for authentication for the first time, you must approve everything. The image will now be recorded and uploaded every time you press K on your keyboard.

With the help of the ESP32 CAM, you have successfully uploaded the image to Google Drive.

Conclusion:I hope all of you understand how to send ESP32 CAM Captured Image to Google Drive. We will be back soon with more informative blogs soon

Leave a Reply

Your email address will not be published.