Making a DashCam Using the Pi Zero (pt.1)

Published by frenoy on

This is the start of a new series where we learn how to build a dashcam using the Raspberry Pi Zero. This project is actually a continuous video recorder and can be used for other applications including wildlife monitoring.

In part 1, we tackle the recording section, which involves capturing video from the Pi camera and storing it to the microSD card. We also take care of file numbering, detecting remaining storage, stopping the script if it is low and we also add the FTP functionality to access the files remotely.

The video takes a closer look at how everything comes together and we also create some test scripts which help understand the final script. I would strongly recommend watching the video first to get a better understanding of the project.

Step 1: Preparing the Raspberry Pi

Dashcam Project
Video Module

The first thing we need to do is download the Raspbian operating system for the Raspberry Pi. I am using the Pi Zero for this project as it is a compact board. I will also not be using the desktop and that’s the reason why I downloaded the lite version. You then need to flash this image to a microSD card but before we can boot the Pi with it, we need to copy the wpa_supplicant.conf and ssh files to the boot drive. These files are needed to allow us to control the Raspberry Pi remotely. Another option is using a wired connection and connecting a keyboard/mouse/display to control the Pi. The choice is yours and here’s a link to a previous post which explains this in more detail along with remote access and FTP:

https://www.hackster.io/BnBe_Club/remotely-accessing-the-raspberry-pi-ssh-dekstop-ftp-b07442

Here’s a summary on how to get this configured:

The wpa_supplicant.conf file provides the network details which allows the Pi to connect to your WiFi network – this is needed to install software and also control it for now. A WiFi connection is only needed during the initial setup and we do not need it to record video while in the car. You can use the link below to download a template file for this. Simply update your country code, network name and password and then copy it across to the microSD card. You can use a text editor like notepad++, Sublime Text or Atom to update the file.

WPA template file (please rename file to .conf): https://www.bitsnblobs.com/wp-content/uploads/2020/05/wpa_supplicant.txt

Use the following link for a list of country codes: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

Components

We then need to create an empty file with the name ssh using your preferred text editor. Do not add any extension to the file and simply copy it over to the board. Once done, connect the camera, insert the microSD card and then power on the board. The board will take about a minute to connect to your network for the first time.

Before we can proceed, we need to obtain the IP address for the board. One way to do this is by using software called AngryIP scanner. Install and run that and you will then be able to obtain the board’s IP address.

We then need to log into the board by using a terminal (or command prompt in Windows). Simply type in “ssh [email protected]” and hit enter. Be sure to use your board’s IP address. Type in the default password which is raspberry and you will then have access to the board.

Enabling The Camera

We now need to do a few things before we can start with the script. The first thing is enabling the camera which can be done by running the “sudo raspi-config” command. You will have to navigate to “Interfacing options”, then “Camera” and enable it. Use the tab key to select the “Finish” option and then reboot the board. Give the board a minute and then log back into it.

We then need to update the Raspbian operating system and this can be done by running the “sudo apt update && sudo apt full-upgrade -y” command. Then, install proftpd by running the “sudo apt install proftpd” command. This installs the FTP software that allows us to access the files on the microSD card. You can then open up FileZilla, and connect to the board which will allow you to access the files.

Step 2: Create the Script

Python Scripts

We have previously created script files that had the .sh extension, these were bash scripts. For this project, we will be creating a python script which will have a .py extension. Using python gives us access to a lot of modules, libraries and scripts which makes it easier for us to create projects.

Final Script

We first need to install the picamera module so that we can use the camera in our scripts. This can be done by running the “sudo apt install python-picamera” command. Once that is completed, simply create a python script by running “sudo nano dashcam.py” and then copy/paste the following contents.

import picamera
import os
import psutil

MAX_FILES = 999
DURATION = 1
SPACE_LIMIT = 80

file_root = "/home/pi/videos/"

if(psutil.disk_usage(".").percent > SPACE_LIMIT):
	print('WARNING: Low space!')
	exit()

with picamera.PiCamera() as camera:
	camera.resolution = (1920,1080)
	camera.framerate = 30

	print('Searching files...')
	for i in range(1, MAX_FILES):
		file_number = i
		file_name = file_root + "video" + str(i).zfill(3) + ".h264"
		exists = os.path.isfile(file_name)
		if not exists:
			print "Search complete"
			break

	for file_name in camera.record_sequence(file_root + "video%03d.h264" % i for i in range(file_number, MAX_FILES)):
		print('Recording to %s' % file_name)
		camera.wait_recording(DURATION*60)
		if(psutil.disk_usage(".").percent > SPACE_LIMIT):
			print('WARNING: Low space!')
			break;

Dashcam Script – Part 1

You can save the file by typing in “CTRL+X”, then Y, then ENTER. Please watch the video for a full explanation of how it all works together. We also created two additional scripts before this to get a better understanding.

Step 3: Test the Script

Before we can run the script, we need to install the psutil module as we use that to obtain the disk usage. In order to install psutil, we first need to install something called pip which is a package installer for python. This can be done by running the “sudo apt install python-pip” command, followed by the “pip install psutil” command which will install psutil.

Testing The Script

Once done, create a new folder by running the “mkdir videos” command and we can then simply run the “python dashcam.py” command which will run the script. You should be able to view the files being created as shown in the image. You can also obtain them using FileZilla and play them back using VLC. You can stop the script by typing “CTRL+C” and it is recommended to then reboot the board.

The video for part 2 will go live next week, along with a written post to accompany it. Please do consider subscribing to our YouTube channel as it helps support this kind of work.

Here are the additional scritpts that were mentioned in the video:

import picamera

with picamera.PiCamera() as camera:
	camera.resolution = (1920, 1080)
	camera.framerate = 30

	camera.start_recording('test_video.h264')
	camera.wait_recording(5)
	camera.stop_recording()

Camera Recording Test

import picamera

with picamera.PiCamera() as camera:
	camera.resolution = (1920, 1080)
	camera.framerate = 30
for file_name in camera.record_sequence('video%03d.h264' % i for i in range(3)):
       print('Recording to %s' % file_name)
       camera.wait_recording(5)

Loop Video Recording

Thank you for reading!