1. Project Overview
• Description: This project automates photo capture on a Raspberry Pi, setting up a task to take photos twice a day and upload them to Google Drive. It utilizes crontab for scheduling, Python for capturing and processing the image, and Google Apps Script for storing photos in the cloud.
• Objective: Demonstrate how to use crontab on embedded systems to automate repetitive tasks and improve data accessibility.
• Applications: Monitoring, surveillance, IoT projects, environmental data logging, etc.
2. What is Crontab?
• Overview: crontab is a scheduling tool in Unix/Linux systems that enables automated execution of commands or scripts at specific intervals, making task automation easy and reliable.
• How It Works: crontab uses a configuration file where each line represents a scheduled task. The tasks run according to a specified syntax.
• Basic Syntax:
minute hour day_of_month month day_of_week command
• Examples:
• 0 3 * * * /home/pi/backup.sh: Runs a backup script every day at 3:00 AM.
• */15 * * * * /home/pi/check_status.sh: Runs a script every 15 minutes.
3. Materials and Tools
• Hardware:
• Raspberry Pi (with a compatible camera).
• Camera
• Software:
• Python: Script to capture and upload photos.
• Google Apps Script: Configured to receive and store photos in a Google Drive folder.
• crontab: Used to automate the Python script execution.
4. Project Steps
• Step 1: Set Up the Camera on Raspberry Pi
• Connect and enable the camera.
• Run an initial test to ensure the camera works correctly.
• Step 2: Create the Python Script for Capture and Upload
• Code to capture a photo, convert it to Base64, and send it to Google Drive using Google Apps Script.
• Example code snippet:
import requests
import base64
import json
def upload_photo_to_gdrive(photo_path):
url = "https://script.google.com/###" # Replace it with the URL of your script of Google Apps
# Read the file and convert it to Base64
with open(photo_path, "rb") as file:
encoded_file = base64.b64encode(file.read()).decode("utf-8")
# JSON format Data
data = {
'file': encoded_file,
'filename': 'photo.jpg',
'mimetype': 'image/jpeg'
}
# JSON format Data
headers = {'Content-Type': 'application/json'}
response = requests.post(url, data=json.dumps(data), headers=headers)
print(response.text) # Imprimir la respuesta de Google Apps Script
if __name__ == "__main__":
photo_path = "photo.jpg" # User the desired name
upload_photo_to_gdrive(photo_path)
• Step 3: Set Up Google Apps Script
• Create a new Google Apps Script in Drive to receive images.
• Configure the script to handle image uploads and save them to a specific folder.
function doPost(e) {
try {
// Check the folder by id
var folder = DriveApp.getFolderById("USE THE FOLDER ID");
// Decode on Base64
var data = JSON.parse(e.postData.contents);
var fileData = Utilities.base64Decode(data.file);
var blob = Utilities.newBlob(fileData, data.mimetype, data.filename);
// Create the folder on Google Drive
var file = folder.createFile(blob);
return ContentService.createTextOutput("File uploaded: " + file.getName());
} catch (error) {
return ContentService.createTextOutput("Error App Script: " + error.message);
}
}
• Step 4: Set Up Crontab to Run the Script Automatically
• Add the following command to crontab to execute the Python script at 5:30 AM and 5:30 PM daily:
30 5,17 * * * /usr/bin/python3 /home/pi/upload_photo.py >> /home/pi/photo_log.log 2>&1
• Explanation:
• 30 5, 17: Runs the script at 5:30 AM and 5:30 PM.
• Log Redirection: Directs output and errors to photo_log.log for troubleshooting.
5. Testing and Results
• Verifying Crontab Execution:
• Check the photo_log.log file to confirm that the task runs correctly.
• Verify that images appear in the specified Google Drive folder.
• Common Errors and Solutions:
• Permission issues: Ensure the script has the necessary permissions.
• Network errors: Make sure the Raspberry Pi is connected to the internet.
6. Conclusions
• Automation and Consistency: crontab allows consistent photo capture and upload without manual intervention.
• Scalability: This setup can be adapted to capture other sensor data, monitor environments, and more.
• Flexibility of Crontab: Highlights how crontab can schedule tasks at various intervals, supporting multiple automation applications.
7. Next Steps and Project Expansion
• Power Optimization: Set up the Raspberry Pi to turn off the camera and reduce CPU frequency during inactive hours.
• Notifications: Configure notifications to alert when new photos are captured and uploaded.
• Additional Applications: Add image processing or object recognition to the captured photos.
Comments
Please log in or sign up to comment.