Creating a Remote Monitor with Raspberry Pi and Webcam

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:

Display after entering lsusb

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 <your_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

A Beginner’s Guide to Raspberry Pi File System
A Beginner’s Guide to Raspberry Pi File System

The article provides a detailed overview of essential files in the Raspberry Pi file system, covering their functions and significance in system operation. It explores critical files like LICENCE.broadcom, cmdline.txt, config.txt, and overlays, elucidating their roles in system configuration and hardware management.

Raspberry Pi Controls Precision Landing for Drone
Raspberry Pi Controls Precision Landing for Drone

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.

auto connect wifi raspberry pi
Tutorial on Setting Raspberry Pi to Auto Connect to Wi-Fi on Startup

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.

Interface SSD1306 OLED Display with Raspberry Pi via SPI
SPI Interface between Raspberry Pi and SSD1306 OLED Display Tutorial

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 and SSD1306 wiring, library installations, and Python programming for graphics and text display. The tutorial enables users to create engaging visuals on the OLED screen, highlighting the SSD1306 display’s versatility with Raspberry Pi’s SPI interface.

raspberry pi autostart
How to Auto Start Programs on 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 be as reliable. A more robust approach involves creating a service script in “/etc/init.d/”, providing better control over autostart processes.

Scroll to Top