Overview: Sending ESP32 CAM Captured Image to Google Drive
In this project, we will learn how to send ESP32 CAM Captured Image to Google Drive. We can use ESP32-Cam Module to click pictures after any specific duration of time and then upload it to a specific folder. This project is totally different as we will be using Google Drive API to upload images. We will require a specific client ID and password to access the folder in the Cloud.
The project is completed in four major steps:
- 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
In an earlier ESP32 CAM Based project we learned about Face Detection System or Color Detection System & also Object Identification System using Python & OpenCV. This project also requires the use of OpenCV for uploading the data to Google Drive.
Bill of Materials
The following is the list of Bill of Materials for building an ESP32 CAM Based Google Drive Image Upload System. TheESP32 CAM when combined with other hardware & firmware Take a Picture & Send to Google Drive.You can purchase all these components from Amazon.
| S.N. | Components | Quantity | Purchase Links |
|---|---|---|---|
| 1 | ESP32-CAM Board AI-Thinker | 1 | Amazon | AliExpress |
| 2 | FTDI Module | 1 | Amazon | AliExpress |
| 3 | Micro-USB Cable | 1 | Amazon | AliExpress |
| 4 | Jumper Wires | 10 | Amazon | AliExpress |
ESP32 CAM Module
The ESP32 Based Camera Module developed by AI-Thinker. The controller is based on a 32-bit CPU & has a combined Wi-Fi + Bluetooth/BLE Chip. It has a built-in 520 KB SRAM with an external 4M PSRAM. Its GPIO Pins have support like UART, SPI, I2C, PWM, ADC, and DAC.
The module combines with the OV2640 Camera Module which has the highest Camera Resolution up to 1600 × 1200. The camera connects to the ESP32 CAM Board using a 24 pins gold plated connector. The board supports an SD Card of up to 4GB. The SD Card stores capture images.
To learn in detail about the ESP32 Camera Module you can refer to our previous Getting Started Tutorial.
ESP32-CAM FTDI Connection
The board doesn’t have a programmer chip. So In order to program this board, you can use any type of USB-to-TTL Module. There are so many FTDI Module available based on CP2102 or CP2104 Chip or any other chip.
Make a following connection between FTDI Module and ESP32 CAM module.
| ESP32-CAM | FTDI Programmer |
| GND | GND |
| 5V | VCC |
| U0R | TX |
| U0T | RX |
| GPIO0 | GND |
Connect the 5V & GND Pin of ESP32 to 5V & GND of FTDI Module. Similarly, connect the Rx to UOT and Tx to UOR Pin. And the most important thing, you need to short the IO0 and GND Pin together. This is to put the device in programming mode. Once programming is done you can remove it.
Project PCB Gerber File & PCB Ordering Online
If you don’t want to assemble the circuit on a breadboard and you want PCB for the project, then here is the PCB for you. The PCB Board for ESP32 CAM Board is designed using EasyEDA online Circuit Schematics & PCB designing tool. The PCB looks something like below.
The Gerber File for the PCB is given below. You can simply download the Gerber File and order the PCB from ALLPCB at 1$ only.
You can use this Gerber file to order high quality PCB for this project. To do that visit the ALLPCB official website by clicking here: https://www.allpcb.com/.
You can now upload the Gerber File by choosing the Quote Now option. From these options, you can choose the Material Type, Dimensions, Quantity, Thickness, Solder Mask Color and other required parameters.
After filling all details, select your country and shipping method. Finally you can place the order.
You can assemble the components on the PCB Board.
Installing ESP32CAM Library
Here we will not use the general ESP webserver example rather another streaming process. Therefore we need to add another ESPCAM library. The esp32cam library provides an object oriented API to use OV2640 camera on ESP32 microcontroller. It is a wrapper of esp32-camera library.
Go to the following Github Link and download the zip library as in the image
Once downloaded add this zip library to Arduino Libray Folder. To do so follow the following steps:
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
#include <WebServer.h> #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(); } |
Before Uploading the code you have to make a small change to the code. Change the SSID and password variable and in accordance with your WiFi network.
Now compile and upload it to the ESP32 CAM Board. But during uploading, you have to follow few steps every time.
- Make sure the IO0 pin is shorted with the ground when you have pressed the upload button.
- If you see the dots and dashes while uploading press the reset button immediately
- Once the code is uploaded, remove the I01 pin shorting with Ground and press the reset button once again.
- If the output is the Serial monitor is still not there then press the reset button again.
Now you can see a similar output as in the image below.
Congratulations, half of the work is done now.
Python Library Installation
For the live stream of video to be visible on our computer we need to write a Python script that will enable us to retrieve the frames of the video. The first step is to install Python. Go to python.org and download Python.
Once download, install Python.
Then Go to the command prompt and install NumPy and OpenCV libraries.
- type: pip install numpy and press enter. After the installation is done.
- type: pip install opencv-python and press enter, close the command prompt.
Now open Idle code editor or any other python code editor.
Setting up Google Drive API
Google has made the google drive API public but still, it has become difficult to grasp how things work. With this article, you don’t need to worry, as we are going to use a library that makes our work smoother. The library is known as PyDrive.
Before starting to use Google Drive API or PyDrive, you need to create a project in Google Developers Console, this is where Google will link your account with the API, provide you with keys to authenticate, and track your usage rate.
Steps to follow
1. Go to the Google Developer Console and log in with your Google account. A dashboard must be visible as in the image below.
2. Now create a new project, by clicking on select a project (The option is visible on the blue bar ) and then click on New Project.
3. Now add a name to the project and select your organization( or leave it as No Organization)
4. Once the project is created you will be directed to the Dashboard, now at the Sidebar click on OAuth Consent Screen. After that click on Configure Consent Screen.
5. Now follow as in the image below; click on External and CREATE
6. Now add your email ID and fill your details below as in the images below and then click on SAVE AND CONTINUE
7. Now go to Credentials and Click on +CREATE CREDENTIALS
8. Now click on Application Type dropdown options and select Desktop App.
9. Now once the above step is done click on CREATE and you’ll be directed to the dashboard, Now comes an essential step that is to download a JSON file for credentials which our python code will be using.
Download and save this file by renaming it client_sectrets.json
Now open Idle code editor or any python code editor
Python Code for Sending ESP32 CAM Captured Image to Google Drive
Please note that before implementing the following code, you have already installed the previously mentioned python libraries using PIP. Let’s see the code.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
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
Now before you run the code you have to do few changes to the above code.
- First, change the IP in URL variable to IP that is shown in the Arduino Serial monitor
- Now, Save the json file in the same directory of the python code
- Now, we also have to change the folder variable, to do so, go to the folder of google drive where you want to save pictures (make sure the google drive you are selecting is linked with the same email ID for which you did settings in Developer Console ). Once you are inside the folder, we have to copy the folder id from the link as in the image below
Once done you run the code.
For the first time, it will ask for Authentication you have to allow it all. Now every time you press K from your keyboard the image will be captured and uploaded.
Congratulations you have successfully uploaded the image to google drive with the help of ESP32 CAM.


























5 Comments
how do you capture the image? My IDLE shell gets to a point where it says authentication successful and I link it to the google drive but I don’t get the next “image saved as” line
“client_sectrets.json” should be “client_secrets.json” – that caused some pain until I realised the typo in the instructions.
error compiling.. any reason why? exit status 1
Compilation error: ‘Void’ does not name a type
Yes noticed the typo. !
pip3 install pydrive
To be included in the requirements.