This pir motion activated Raspberry Pi camera project will take and upload videos to the Gofile - Free file sharing and storage platform server via an HTTP address. The GoFile server will serve as an online database to save the video files and as a interface to browse through motion captured videos.
Features
- Senses motion
- Records video that is converted to a MP4 format
- Sends MP4 files to Gofile - Free file sharing and storage platform
- Code is logged to
app.log
file
-
Raspberry PI 4
-
microSD Card
-
Raspberry Pi Camera v2.1
-
Raspberry Pi T-cobbler
-
Solid core wires
-
Female header pins
-
Keyboard
-
Monitor
-
Personal Computer
-
microSD Card USB Adapter
-
Soldering iron
-
Helping Hands
Following Raspberry PI OS Install Instructions.
-
Download Raspberry Pi Imager
-
Install the Raspberry Pi Imager Software to your OS (Windows, Mac, or Linux)
-
Open the Raspberry Pi Imager Software Program
-
Plug in your SD Card
-
Click Choose OS
-
Click Raspberry Pi OS (32-bit)
-
Click Choose Storage
-
Select your microSD card
-
Hit the Write button
-
Wait for writing to complete
-
🥳 OS is ready!
-
Open the terminal (shortcut: ctrl + alt + T)
-
sudo raspi-config
-
Select Interfacing Options
-
Select Legacy Camera Enable to enable the camera
-
(Optional) Another thing I like to do is to enable ssh
-
Reboot the Raspberry Pi
We'll be creating the circuit as shown in the diagram below. to interact with the camera and the PIR motion sensor.
-
Connect the Camera Module to your Raspberry Pi (more details in Getting Started with Picamera)
-
Attach some female header pins to the T-cobbler and solder them to a Prototyping Board
-
Solder the connections from the T-cobbler prototyping board to the PIR motion sensor as in the diagram above
-
The T-cobbler will then be connected to the Raspberry Pi via the ribbon cable. That way you don't have to solder directly to the GPIO pins. Make sure that the ribbon cable is oriented the right way when connecting to the Raspberry Pi.
Gofile is a unlimited and free file sharing and storage platform that is secure and privacy focued. We'll be using this as our server to store our videos onto. Note, that these files will be hosted on the Gofile servers temporarily for about a week if unused. For my purposes, that was okay. There is also a paid tier via their patron to store the files.
-
Create a my_secrets.py file in the same folder as this file
-
Visit the page Gofile - Free file sharing and storage platform
-
Login to your account (yes login, Gofile makes the login system very simple)
-
Find your account token by clicking on My Profile
-
Locate your account token on the bottom of your profile page as shown below. Add
gofile_token = "<your_token>"
to the my_secrets.py file ( 🚨 Don't include <> in your string). -
Create a new folder by clicking Create folder
-
Name your folder. (I called it motion-camera)
-
By default the new folder is a private folder, but if you ever need to change it click on the 3 dots (kebab menu icon?)
-
In the same 3 dots menu, click on Info/Settings. Add
gofile_folder = "<your_folder_id>"
to the my_secrets.py file ( 🚨 Don't include <> in your string).
-
Install
gpac
on the terminal. the code will use this to convert a.h264
file to a.mp4
file. This is useful because the camera module library doesn't saves to a.h264
format.sudo apt-get install -y gpac
-
To run immediately:
python3 <code_path>/c
-
To run on startup refer to Appendix section D
graph TD
A(Start) --> B(Camera Setup)
B --> C{Waiting for Motion}
C --> |motion| D(Recording)
C --> |no motion| C
D --> E(Wait for no motion)
E --> F(Save to tmpfs)
F --> FF(Convert to mp4)
FF --> G(Upload to gofile)
G --> H(Delete from tmpfs)
H --> C
Instructions from Make Use Of - Set Static IP.
-
Find your NETWORK INTERFACE - your network connection type (eg. eth0, wlan0, etc.)
ip a
-
Find your current IP ADDRESS - your current ip address
ip a
-
Find your ROUTER_IP - the gateway IP address for your router on the local network. Use the first ip address listed.
ip r | grep default
-
Find your DNS_IP - the DNS IP address (tpically the same as your router's gateway address).
sudo cat /etc/resolv.conf
-
Append the following lines to the end
/etc/dhcpcd.conf
fileinterface <NETWORK INTERFACE> static ip_address=<IP ADDRESS>/24 static routers=<ROUTER_IP> static domain_name_servers=<DNS_IP>
⚠ Make sure you enable ssh
in your raspberry pi first.
These instructions will follow How to Enable Password-less Login on Raspberry Pi.
-
On the computer you would like to ssh from
ssh-keygen
-
Create authorized keys file
cat ~/.ssh/id_rsa.pub | ssh <pi_username>@<raspbery_pi_ip_address> 'mkdir .ssh/ && cat >> .ssh/authorized_keys'
-
Now when you should be able to ssh without being prompted with a password
ssh <pi_username>@<raspberry_pi_ip_address>
When using the Raspberry Pi Camera, we save the video files as a .h264
raw format. To make the video more accesible, we usually want to convert it to a .mp4
format.
sudo apt-get install -y gpac
MP4Box -add <YOUR_VIDEO_FILE_NAME>.h264 <YOUR_VIDEO_FILE_NAME>.mp4
-
sudo crontab -e
-
Add the following line to the end of your
crontab
file. Replace<code_path>
with the path to thepir_cam.py
file.@reboot sleep 30; sudo python3 <code_path>/pir_cam.py &For example:
For example:
@reboot sleep 30; sudo python3 /home/owen/github/tinyOkayProjects/pir_activated_cam_2/pir_cam.py &Save and exit
-
Now reboot the Pi and you should see your program start saving log files on startup.
(Raspberry Pi Camera)
- Raspberry Pi | Getting started with the Camera Module - really well-done beginner friendly tutorial on the Raspberry Pi Camera Module
(Raspberry Pi Autostart)
- Estefannie Explains | How to start a script at start-up on a Raspberry PI - great tutorial on how to run a progran on startup using
cron