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:
lsusb
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:
motion -s
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:
start_motion_daemon=no
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
Now we’ll set up a simple alert mechanism for the project using msmtp and mutt, which will allow us to send alerts via email.
Msmtp Settings
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.
Install msmtp:
sudo apt-get install msmtp
Create msmtp configuration file ~/.msmtprc:
account default
host smtp.gmail.com
port 587
from eGuidezhan@gmail.com
auth login
user eGuidezhan@gmail.com
password
logfile ~/.msmtp.log
Test config:
msmtp -t eGuidezhan@gmail.com
Enter the email content, press Ctrl+d to send, and check for successful delivery.
Possible errors:
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 # Edit and add configuration information
sudo nano /root/.msmtprc # Edit and add configuration information
sudo chown pi:pi /etc/msmtprc /root/.msmtprc
Mutt Settings:
Mutt is a command-line email client, and its primary advantage is the ability to send emails with attachments.
Installation Mutt:
sudo apt-get install mutt
Create mutt configuration file ~/.muttrc:
set sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="Your Real Name"
set from=eGuidezhan@gmail.com
set envelope_from=yes
Test config:
echo "test" | mutt -s "mutt_test" eGuidezhan@gmail.com
Writing Bash Scripts
#!/bin/bash
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
done
fi
fi
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.
You Might Be Interested


This Raspberry Pi project successfully integrates a servo pan-tilt module for remote control, allowing precise movement and positioning. The servo motor’s principles, Python control logic, and UDP-based remote control communication enhance the understanding and implementation of this pan-tilt system. The modularized code structure facilitates easy customization, making it a versatile and practical solution for various applications.


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 efficient and controlled connectivity. The guide also outlines simple steps to create a systemd service, enabling seamless auto-connection on startup, enhancing the Raspberry Pi’s wireless management.


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, the Raspberry Pi handles complex tasks like facial recognition, while the STM32 manages control functions for door operations and user permissions.


In this Raspberry Pi DHT22 Temperature & Humidity Sensor Project, a cost-effective and reliable DHT22 sensor is used to measure real-time temperature and humidity readings. The simple Python code and easy wiring make it accessible for beginners, while its accuracy and versatility make it a valuable addition to weather station projects.


When you need to connect to your Raspberry Pi, you don’t necessarily have to rely on a computer; you can also achieve this using a


This guide provides a comprehensive overview of how to use SSH (Secure Shell) for remote management of a Raspberry Pi. It covers enabling SSH via ‘Raspi Config,’ including steps for Mac and Windows users. The guide emphasizes finding the Raspberry Pi’s unique IP address and provides troubleshooting tips for common connection issues.


This article provides a comprehensive guide on mining Bitcoin using a Raspberry Pi, offering an accessible entry point for cryptocurrency enthusiasts. It covers essential steps, from configuring the Raspberry Pi to creating blockchain nodes and connecting external drives. The article emphasizes that this is a hobbyist project, not a get-rich-quick scheme, and highlights key commands for managing the Bitcoin mining process on the Raspberry Pi.


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 Python script. Algorithmic control, using MAVROS, is illustrated for drone maneuvering. Overall, it provides a holistic solution for precise drone localization.