This project is a motion detection system that uses a Raspberry Pi (or similar Linux system) with a camera to monitor for motion. When motion is detected, it captures a photo and sends a notification with the photo to a Telegram chat. It also provides Telegram bot commands to control motion detection, take snapshots, and retrieve the latest media.
-
Motion Detection: Uses the
motiondaemon for efficient motion detection. -
Telegram Notifications: Sends instant notifications to your Telegram chat when motion is detected, including a photo of the event.
-
Continuous Recording:
motiondaemon can be configured to continuously record videos. -
Telegram Bot Commands:
/start: Starts motion detection and the Telegram bot./stop: Stops motion detection./check: Checks if motion detection is currently running./lastphoto: Sends the latest captured photo./lastvideo: Sends the latest captured video./help: Displays a list of available commands.
-
Rate Limiting and Error Handling: Implements safe message sending with retry mechanisms to handle Telegram API rate limits and network errors.
-
Configuration: Easily configurable via
motion.conffile and environment variables. -
Logging: Comprehensive logging for debugging and monitoring.
- Hardware:
- A Linux-based system (e.g., Raspberry Pi, Laptop running Linux).
- A camera connected to the system (e.g., USB webcam, Raspberry Pi camera module).
- Software:
- Python 3.x
motiondaemon (installation instructions below)fswebcam(if using laptop webcam for snapshots, install if needed:sudo apt-get install fswebcam)- Python libraries (install using pip):
pip install python-telegram-bot python-telegram-bot[ext] requests psutil
- Telegram Bot:
- Create a Telegram bot using BotFather and obtain a
BOT_TOKEN. - Get your Telegram chat ID (
TELEGRAM_ID). You can use a bot like@userinfobotto find your chat ID.
- Create a Telegram bot using BotFather and obtain a
- Environment Variables:
- Set the following environment variables in your system:
BOT_TOKEN: Your Telegram bot token.TELEGRAM_ID: Your Telegram chat ID.
- Set the following environment variables in your system:
-
Install
motiondaemon:sudo apt-get update sudo apt-get install motion
-
Clone the repository:
git clone [https://github.com/GokulrajKS/Motion-detection.git](https://github.com/GokulrajKS/Motion-detection.git) cd motion # Or the project directory name if different
-
Configure
motiondaemon:- Copy the provided
motion.confto the desired configuration directory. A common location is within your project directory or/etc/motion/motion.conf. - Important: Edit
motion.conf:- Adjust
videodeviceto your camera device (e.g.,/dev/video0,/dev/video1). - Modify
width,height,framerateas needed. - Verify
target_diris set to the path where you want to store pictures and videos. This should align with thepics_pathvariable inengine.pyandmotion.py. - Tune motion detection parameters (
threshold,minimum_motion_frames, etc.) as needed.
- Adjust
- Copy the provided
-
Set Environment Variables:
-
You need to set
BOT_TOKENandTELEGRAM_IDenvironment variables. -
You can set them in your
.bashrc,.zshrc, or directly before running the scripts:Example (in terminal before running):
export BOT_TOKEN="YOUR_BOT_TOKEN" export TELEGRAM_ID="YOUR_TELEGRAM_ID" python engine.py
For persistent environment variables (add to
.bashrcor.zshrcand source the file):echo 'export BOT_TOKEN="YOUR_BOT_TOKEN"' >> ~/.bashrc echo 'export TELEGRAM_ID="YOUR_TELEGRAM_ID"' >> ~/.bashrc source ~/.bashrc
-
-
Run the Telegram Bot Engine:
python engine.py
The bot should start polling for commands. Use
/startcommand in Telegram to start motion detection.
engine.py: The main Python script that runs the Telegram bot. It handles bot commands, interacts with themotiondaemon, and sends Telegram messages.motion.conf: Configuration file for themotiondaemon. Defines camera settings, recording options, motion detection parameters, and specifiesmotion.pyto be executed on motion detection. Important: Ensure the paths within this file, especiallytarget_dir,log_file,process_id_file, andon_motion_detectedscript path, are correctly configured for your system.motion.py: Python script executed bymotionwhen motion is detected. It sends Telegram notifications with photos. Important: Verify the paths forpics_path,last_notification_file, andlast_photo_sent_filewithin this script match your intended file locations.pics/: Directory where captured photos and videos are stored. This path is defined in bothengine.pyandmotion.confand should be consistent.motion_last_notification.txt: File used for cooldown mechanism to prevent excessive notifications. Ensure the path inmotion.pyis correct.motion_last_photo_sent.txt: File to track the last photo sent to avoid duplicate notifications. Ensure the path inmotion.pyis correct./tmp/motion/motion.pid: Process ID file for themotiondaemon. This is a common temporary file path on Linux systems.[project_directory]/motion.log: Log file for themotiondaemon. This path is configured inmotion.confand is relative to your project directory in the provided example, but can be configured to another location./tmp/motion_notification.log: Log file for themotion.pyscript. This is a common temporary file path on Linux systems, configured withinmotion.py.
After starting engine.py and setting up the environment variables, you can use the following commands in your Telegram chat with the bot:
/start- Starts motion detection and the bot./stop- Stops motion detection./check- Checks if motion detection is running./lastphoto- Requests and sends the latest captured photo./lastvideo- Requests and sends the latest captured video./help- Displays this help message with available commands.
- Check Logs: Examine the log files. The
motiondaemon log is typically found at the path configured inmotion.conf(e.g.,[project_directory]/motion.log), and themotion.pyscript log is at/tmp/motion_notification.log. Check these files for errors. - Permissions: Ensure the scripts have execute permissions (
chmod +x motion.pyandchmod +x engine.pyif needed). Check directory permissions for thepicsfolder and other directories where the scripts need to write files. - Environment Variables: Verify that
BOT_TOKENandTELEGRAM_IDare correctly set in your environment. - Camera Device: Double-check the
videodevicepath inmotion.confand ensure it corresponds to your camera device. - Dependencies: Ensure all Python libraries (listed in Prerequisites) and the
motiondaemon are installed. - Telegram Bot: Verify the bot token is correct and the bot is not blocked. Test sending a message to your bot directly through the Telegram Bot API if needed.
- Network: Check internet connectivity for sending Telegram messages.