Camera image motion detector
Raspberry Pi and camera module based motion detector that send notifications of movement to Telegram.
Table of Contents
Directory structure
Overall picture
root
├── assets
| ├── Diagram.drawio
| └── Diagram.jpg
├── scripts
| ├── test_bot_manual.py
| ├── test_camera_manual.py
| └── test_img_diff.py
├── .gitignore
├── README.md
├── config.py
├── main.py
└── requirements.txt
Detailed list
assets
Readme related web site assets.scripts
Smaller parts of the application used to test out things.config.py
Configurations for the application.main.py
The overall application can be started by running this file.secrets.py
Secret configurations left out of the version control like tokens etc.requirements.txt
The required Python packages neede to run this program.
Architecture
The program constantly captures images on certain intervals. Upon detecting motion between the images, a notification gets sent through Telegram API to a certain user to notify about the detection.
Requirements
Hardware requirements
- Raspberry Pi
- Raspberry Pi Camera module v2
The program has been tested to work with Raspberry Pi Camera module v2. Other support not guaranteed.
Software requirements
- Python
- Packages in:
requirements.txt
- Packages in:
- Telegram
- An account
- A bot user
How to run (development)
Set up the hardware, gather required software, create telegram bot, obtain the bot token and your Telegram chat id.
Step by step instructions
- Clone the repository.
- Install the requirements.
pip install -r requirements.txt
.
- Use Telegram chat to create a bot.
- Find user:
@BotFather
. - Run following commands and fill out your details on "<>" marked sections:
/start /newbot <BotName> <BotUsername_bot>
- Get the HTTP API access token.
- Example:
123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
.
- Example:
- Save the token to
secrets.py
astelegram_bot_token
.telegram_bot_token="XXXXXX:XX-XXXX-XXXXX-XXXXX"
- Message your bot.
- Find the bot by name.
- Send some message to it.
- Check bot's message history for your chat id.
- Go to see your bot's message history from the API.
- Fill in your token to the address.
- Example:
https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/getUpdates
.
- Obtain the chat id of your user as a secret.
- Save the ID to
secrets.py
astelegram_chat_id
. - Example:
telegram_chat_id="XXXXXXXXXX"
- Save the ID to
- Documentation (in case something fails):
Telegram bots
- Find user:
- Setup the camera module for the Raspberry Pi.
- Documentation:
Camera module setup
- Documentation:
- Run the program.
python main.py
Testing
test_bot_manual.py
- Telegram bot connection testing. Sends message to the given chat id.
test_camera_manual.py
- Camera module testing. Can be used to manually check that image capture and video recording works.
test_img_diff.py
- Image difference calculation logic test file.
How to run (production)
Once the development is working the program can be changed to run on startup.
Step by step instructions
- Create
MotionDetector.desktop
file to/home/pi/.config/autostart/
.cd /home/pi/.config/autostart/
touch MotionDetector.desktop
- Write the following content to that file:
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=MotionDetector
Comment=This runs the motion detector on startup.
Exec= python /home/pi/raspi-motion-detector/main.py
StartupNotify=false
Terminal=true
Hidden=false
- Reboot.