/MotionPy

A motion detection system for a Raspberry Pi 3 along with an Android app to control it. Now i know which roommate eats my food.

Primary LanguageKotlin

MotionPy

A motion detection system for a Raspberry Pi 3 along with an Android app to control it.

How does it work?

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.

Required parts

  1. Raspberry Pi 3
  2. 5v power adapter with micro USB cable
  3. HC-SR04 Distance sensor
  4. 5.5×8.2×0.85cm Mini breadboard
  5. 3 1k ohm resistors
  6. Camera module (Note: you can use any camera but this project was build aroundthis specific camera)
  7. 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)
  8. 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)

Setting it up

  1. 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.
  2. Set up a quick Heroku site and take note of the url.
  3. 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:

  1. If everything is set up correctly, running raspberrypi/check_distance.py should give you a valid distance.
  2. 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'
  1. 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 is serviceAccountKey.json altough I've renamed it to firebase-admin.json. This can be found under Settings > Service accounts > Generate new private key. Place this file in the raspberrypi/ directory.

The end result

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: