Raspberry Pi is a microcomputer that’s perfect for creating exciting projects, one of which is building a simple surveillance system. While there’s an abundance of books and online tutorials about Raspberry Pi, beginners may encounter some peculiar challenges when setting up such projects.
In this article, we will document TechSparks’ journey as a beginner in creating a Raspberry Pi remote surveillance system. Though the process is full of interesting discoveries, it also involves tackling some tricky issues. However, it’s precisely this learning journey that makes the project all the more engaging. Let’s begin recording this fun and challenging learning adventure.
What Do You Need
- Raspberry Pi
- Logitech C270i, an affordable webcam, approximately $30
- Bash scripting knowledge
Webcam Connected to Raspberry Pi
After connecting the Raspberry Pi and Logitech C270, enter the following command to verify if it’s correctly recognized:
This is a Linux command used to list information about USB devices connected to the computer. If the connection is correct, you will see “Webcam C270” as shown in the image below:
Now, you need a program to operate the webcam. Here, we are using Motion. Execute the following command to install it:
sudo apt-get install motion
After the installation is complete, execute the following command to start the webcam’s capture mode:
If the program is running correctly, when an object moves in front of the webcam, the command line will display: “event_newfile: File of type 1 saved to: ./xxx.jpg,” indicating that the captured image is saved in JPG format. If you need to terminate the capture mode, press Ctrl + C. You can use the “ls” command to check the webcam’s output, which may include several .jpg files and one .avi file.
Once you’ve confirmed that the Raspberry Pi webcam is working correctly, you need to further configure Motion, which includes two main configuration files: /etc/default/motion and /etc/motion/motion.conf.
In the /etc/default/motion configuration file, there is a line:
Change “no” to “yes,” and then execute the following command:
sudo /etc/init.d/motion start
Motion will start running.
The /etc/motion/motion.conf configuration file provides more configuration options, including:
- Daemon mode: Controls whether Motion runs in the background or foreground. Simply change “yes” or “no” as needed.
- Threshold: Defines the threshold for capturing image pixel values. While clarity is important, setting it too high can lead to delays.
- Target_dir: Sets the storage path for captured images. Choose as needed. If you wish to automatically clean image files, TechSparks recommends setting the target_dir to the /tmp directory, as data will be automatically cleared when the system restarts.
The Raspberry Pi webcam may have additional configurations like webcam_localhost, which might be related to the webcam model you are using. If you find more or fewer configurations, don’t worry.
If you encounter issues with the target_dir configuration, it may be due to permission problems. Simply add “sudo” before the command.
Establishing an Email Alert System
现在我们将使用msmtp 和 mutt为项目创建一个简易的报警机制，完成后可以通过邮件发送警报。
Now, we will use msmtp and mutt to create a simple alert mechanism for the Raspberry Pi network camera monitor. When an event is detected in the system, it will trigger an email alert.
sudo apt-get install msmtp
Create msmtp configuration file ~/.msmtprc:
msmtp -t eGuidezhan@gmail.com
Enter the email content, press Ctrl+d to send, and check for successful delivery.
If you encounter an “account default not found” error, it may be because the /etc/msmtprc and /root/.msmtprc files do not exist. You can resolve this by following these steps:
sudo touch /etc/msmtprc
sudo touch /root/.msmtprc
sudo nano /etc/msmtprc # 编辑并添加配置信息
sudo nano /root/.msmtprc # 编辑并添加配置信息
sudo chown pi:pi /etc/msmtprc /root/.msmtprc
Mutt is a command-line email client, and its primary advantage is the ability to send emails with attachments.
sudo apt-get install mutt
Create mutt configuration file ~/.muttrc:
set realname="Your Real Name"
echo "test" | mutt -s "mutt_test" eGuidezhan@gmail.com
Writing Bash Scripts
MOTIONDIR="/tmp/motion_save" # Path to store images and video files
if [ -d "$MOTIONDIR" ]; then # Check if the path exists
LISTFILES=$(sudo find "$MOTIONDIR" -maxdepth 1 -type f -name "*.avi") # Retrieve a list of avi files
NUMFILES=$(echo "$LISTFILES" | wc -l) # Get the number of avi files
if [ "$NUMFILES" -gt 0 ]; then
for AVIFILE in $LISTFILES; do
echo "warning" | mutt -s "Intruder Detected" \
-a "$AVIFILE" -- eGuidezhan@gmail.com # Send avi file as an attachment to the email
sudo rm "$AVIFILE" # Delete the file
This script first checks if the specified path exists. Then, it uses the find command to look for .avi files in the directory. If files are found, it sends an email with the subject “Intruder Detected” to eGuidezhan@gmail.com, attaching the .avi files. Afterward, it deletes the sent files to avoid consuming too much disk space.
Before running the script, make sure mutt is installed, and the Motion camera application is correctly configured to generate image files stored in the specified path.
Setting Up Automatic Execution
Finally, we need to configure the cron service to automatically run the script above, ensuring the Raspberry Pi monitoring system stays online continuously. Cron allows you to automatically execute commands according to your schedule. The configuration file for Cron is called crontab, and Cron checks the crontab file every minute to see if there are any pending commands.
First, you can use the command crontab -l to view the current user’s crontab content. Typically, this file is empty. Next, you can use the command crontab -e to edit the crontab file, then add the following entry:
* * * * * /home/pi/monitor_test
The five asterisks * represent minutes, hours, day of the month, month, and day of the week, respectively. In this example, they are all set to *, meaning the script will run every minute. You can change these values as needed to meet your specific requirements.
After editing, you can use the command sudo /etc/init.d/cron restart to restart the cron service, ensuring that the new crontab settings take effect.
Now, your script will automatically run at the specified intervals, and meanwhile, your monitoring system will always be operational. If needed, you can also set Motion to start automatically and run in the background for added convenience.
More content you may be interested in
This tutorial guides precision drone landing with Raspberry Pi, covering ROS Kinetic, MAVROS, and PX4 Autopilot setup. It explains Gazebo config, showcasing landmark recognition via
This collaborative project seamlessly integrates the Raspberry Pi 4B and STM32 to create a sophisticated Intelligent Access Control System. Leveraging the strengths of each component,
This tutorial streamlines Raspberry Pi’s auto-connect feature by editing the wpa_supplicant.conf file, allowing users to specify preferred Wi-Fi networks. By customizing network configurations, it ensures
This tutorial provides a concise guide to establishing SPI communication between a Raspberry Pi and an SSD1306 OLED display. It covers SPI principles, Raspberry Pi
Automating program startup on the Raspberry Pi can be achieved through various methods. Editing the “/etc/rc.local” file or using desktop applications, while simpler, may not
Raspberry Pi, a revolutionary single-board computer introduced by the Raspberry Pi Foundation, has become a global sensation, initially designed for educational purposes. With its integrated