SMTP Service based Raspberry Pi surveillance system

Fourth Part in my Raspberry Pi Project Series

Finally the series is approaching towards my main project. The previous posts can be seen as build up for the canon story. Anyways let’s get started.

In this post I am using the previous setup of SMTP service to send video files from Raspberry Pi camera to my mail.

I am using Pi NoIR Camera for my setup. It’s with me for quite some time now, I thought let’s put it to good use.

Connecting Pi Camera —
Let’s see how to interface Pi camera module with our Pi and do basic things like capturing image.

The module have a ribbon connector, one side of which have the exposed tin plated connects. You need to connect the ribbon connector as explained in the images below —

Pi camera connect steps

Interfacing —
To interface camera using Python, readthedocs have great explanation.

A basic video recording code looks like this —

import picamera
camera = picamera.PiCamera()
camera.resolution = (640, 480)
camera.wait_recording(60) # time in seconds

h264 is the supported file format for the camera. I tried creating mp4 file but it gave this error —
camera.exc.PiCameraValueError: Unsupported format mp4

Format Conversion —
Now you cannot directly play this video, so to convert h264 format to mp4 we have to use a packager called MP4Box. To install —
sudo apt install gpac
Check — which MP4Box and it should return the path — /usr/bin/MP4Box
A basic command is like — MP4Box -add video.h264 video.mp4
But as this is a shell command, you cannot use this directly. You have to use subprocess library in python —

import subprocess
X= "MP4Box -add video.h264 video.mp4", shell=True)

File Size —
Now that we have our mp4 file, we can easily play it and mail it using SMTP service. But Gmail only allows 25MB file size to be attached. So we have to test multiple files wrt resolution and length of the video, to understand which file can be under our size constraints.

I tested 1280*720, 640*480, 320*240 resolutions, and 2–5 mins of video lengths. And I concluded that 320*240 resolution and 2.5 mins of video is 5–20 MB long, depending on the foreground and background.

Naming Convention —
As the camera is being used for surveillance, the file name should contain date-time. Using time library in python does the job —

import time
dt = time.strftime("%Y%m%d-%H%M%S")

So when a video starts recording, that video is saved as the date-time it started at. And then the next video will start at that later date-time, in this case 2.5 mins later. So it’s easy to keep a track of video timestamps.

But then converting this file to mp4 should be like this —

X= "MP4Box -add " + dt+".h264 " + dt+".mp4", shell=True)

Triggering Surveillance —
There is no point is recording video when there is no one present in the video frame. Or if you are present in the frame. In my later videos I will try to implement Motion detection using OpenCV to trigger surveillance. But for now to test a basic trigger, I have attached a switch with Pi’s GPIO pins to start the video recording.

state = GPIO.input(switch)
if(state == 1):

Attaching the video —
After recording, conversion, and taking care of naming, the video is ready to be send using attachment service uuencode, that we discussed about in last post.

X = "uuencode " + dt+".mp4 " + dt+".mp4 " + "| sendmail -v"
process =, shell=True)

Logging —
A good coding practice is to create logs. In case there are errors, logs make it easy to troubleshoot. A basic logging code can be implemented as given,

# Python Logging - Link
import logging
from logging.handlers import RotatingFileHandler
debug_logger = logging.getLogger("Debug Rotating Log")
# add a rotating handler
debug_handler = RotatingFileHandler("debug.log", maxBytes=419430400,backupCount=1)
debug_logger.debug("verbose email: ")

Final Results —
So after integrating all the above parts of code and running it, I was receiving videos at regular interval on my mail. I was easily able to download and play it. I observed that the recorded video was always 3 mins long, even though I put 2.5*60 in the function.

Attached recorded video

Conclusion —
There are a few drawbacks in this setup.

  1. File size Constraints because of using Gmail attachments. I can try integrating and testing Dropbox to store the files.
  2. Right now I have to press the button to trigger the recording. But later on I will implement motion detection using OpenCV.
  3. If the camera is still then it only have a fixed frame for recording. To cover a much larger area, we would require multiple camera system and that might not be feasible. There is a concept regarding this, namely, Object Tracking.

I am going to discuss the 3rd point in my next post.

Thanks for reading and Stay Tuned!




Hacking the Physical World | Senior Embedded Systems Engineer @ PiRhoAlpha Research (ActiveBuildings) | I write posts about AVR and Raspberry Pi.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Low Code: Using Custom Connectors in Power Apps to pull data from existing APIs

How to edit website from spreadsheet

Let’s set up a WordPress development environment with Docker on Windows

What a spicy and juicy piece that I ALSO deeply

Open Source Inflection Points

2D Game Dev Journey — Day 14: More Improvements

What is Java Instrumentation? Why is it needed?

AndroidThings: Hello Rainbow HAT

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akshul Goyal

Akshul Goyal

Hacking the Physical World | Senior Embedded Systems Engineer @ PiRhoAlpha Research (ActiveBuildings) | I write posts about AVR and Raspberry Pi.

More from Medium

Turn Raspberry Pi into a backup server

Utilizing and Installing WSL 2 as a Virtualization Medium

Install Linux Kernel 5.15 on Ubuntu 20.04 LTS

Ubuntu on the command line

Building an Automated Xylophone — Part I