Introduction: Making a DashCam Using the Raspberry Pi Zero (pt.1)
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
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.instructables.com/id/Remotely-Accessing-the-Raspberry-Pi-SSH-Dekstop-FT/
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: 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
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@example.com" 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.
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 && 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
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.
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 type in the contents from the following file:
You can also copy/paste the contents which is one benefit of using SSH. 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.
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.
Thank you for reading!