A motion detection system for a Raspberry Pi 3 along with an Android app to control it.
There is a distance sensor attatched to the Raspberry Pi that reads a fixed distance at a set interval, for example, 120 cm every 0.02 seconds. When that distance gets lower than a certain threshold (40 cm in this project), the Pi captures an image, uploads it to Firebase Storage, and then uses Firebase Cloud Messaging to send a notification containing a timestamp and image url to your phone. In order for the phone to send messages to the Raspberry Pi, I set up a Heroku server running Flask to listen for web requests. The route that listens for requests is protcted by a server key that you'll need to generate (to prevent random access). When it gets a command, that command is sent to the Raspberry Pi.
- Raspberry Pi 3
- 5v power adapter with micro USB cable
- HC-SR04 Distance sensor
- 5.5×8.2×0.85cm Mini breadboard
- 3 1k ohm resistors
- Camera module (Note: you can use any camera but this project was build aroundthis specific camera)
- M/F Jumper wires (Optional: The sensor comes with wires but extra jumper wires means you don'thave to attach the sensor to the breadboard directly)
- TP-Link 150Mbps High Gain Wireless USB Adapter (Optional: Allows you to use VNC to view the Pi's screen remotely while it's also connected to WiFi, see here for setup)
- Before setting up the Raspberry Pi, head over to Firebase and create a new app. In order to be able to send Firebase messages, you'll need a device registration token.
- Set up a quick Heroku site and take note of the url.
- This part might depend on how you want to set up the sensor. Take the 4 jumper wires and connect them to the following GPIO pins on the Pi: 5v, Ground, GPIO 4, GPIO 18. (See this video for a visual). Again the setup depends but the end result should look something like this:
- If everything is set up correctly, running raspberrypi/check_distance.py should give you a valid distance.
- The last step is to create a few config files I've excluded from this repo. The first one should be in /app/.../motionpy/Config.kt:
// Config.kt
object Config {
/* *
* This is generated with the Python uuid lib. In this project,
* I just used str(uuid4()), but any random key will do.
*/
const val SERVER_KEY = "Your server key here"
/* *
* Here, the "/control" route is what receives for commands.
* See: https://github.com/ctcuff/MotionPy/blob/master/server/app.py#L43
*/
const val URL = "[Your Heroku URL here]/control"
}
The next one should be in raspberrypi/config.py
# Find this in Firebase project settings under the cloud messaging tab.
API_KEY = 'Firebase Legacy Server key'
# This is the name of your Firebase App
PROJECT_ID = 'Your project if here'
# This can be found with step 1 of the setup.
REGISTRATION_ID = 'Device regidtration id'
# This is the same url thats in Config.kt (minus the /control route)
SERVER_URL = 'Your Heroku server URL'
Lastly, this config should bein server/config.py
# You can generate this with uuid4() from the uuid lib. For example:
# >>> from uuid import uuid4
# >>> print(uuid4())
# c0958cb9-7b24-448b-a555-5f89c0c312c4
# Note that this should be the same key in Config.kt
SERVER_KEY = 'Key here'
- Now that the config is set up, the last step is to place a 2 Firebase json files in the project. You'll
google-services.json
after creating a new Firebase app or you can find a link to download it under the project settings tab. Place this file under app/app. The next json file isserviceAccountKey.json
altough I've renamed it tofirebase-admin.json
. This can be found under Settings > Service accounts > Generate new private key. Place this file in the raspberrypi/ directory.
This took quite a bit of time to get up and running but here's the end result:
Here's what the output looks like when the sensor is running on the Raspberry Pi:
Here's what happens when movement is detected: