Living in a bustling city often means facing the risk of having your valuable car or motorbike damaged, tipped over, or stolen when parked on the street. Finding a nearby lockup space is a challenge, which is why I've attempted to gather a few components to create a system that can alert me and prompt a response when there's a potential threat to my vehicle.
It's important to note that my motorbike already has a tracker installed, so I'm not aiming to replicate its functionality. Instead, I want to receive additional information about the surroundings.
Here's a checklist of the requirements for the system:
- 4G connectivity since WiFi is not available.
- Sufficient battery life to operate for an extended period of time (recommended reading required).
- Motion detection capability.
- Captures an image when motion is detected.
- Sends me a warning notification via email.
- Plastic Tupperware container
- Glass Tupperware container
- Drill bits for creating holes in the Tupperware
- Raspberry Pi Zero 2 (Consider checking Facebook Marketplace due to the current chip shortage)
- 4G hat for mounting on top of the Pi using pins
- Data-enabled SIM card
- 12V rechargeable lead battery
- Crimp connectors for connecting the battery
- Buck power module with USB output to convert 12V battery power to 5V for the Pi
- Raspberry Pi camera
- Holder for the Pi camera
- Motion sensor for the Pi
- GPIO leads for connecting the motion sensor to the Pi
- USB mouse, keyboard, and an HDMI to mini-HDMI adapter
- PCB spacers for mounting components
- Rubber grommets
- Electrical wire
- Breakaway pins for soldering to the Pi
- High-quality soldering iron with a pointed and hot tip for soldering the small Pi pins.
Step 1: Prep Your Containers
To modify the plastic Tupperware, utilize the drill bit and grommets to create holes for the following components:
- 4G antenna output
- Micro USB power input
- GPIO leads for motion detector output
- Camera output
It's advisable to test the fit of the components before proceeding with drilling holes or soldering spacer screws. Ensure that the chosen mounting location allows for convenient connection of the USB power cable and accessibility to the GPIO pins.
For the glass Tupperware, simply cut openings on the top for the power leads. Use grommets as needed. This container will accommodate the buck module separately to prevent heat interference.
Step 2: Prep Your Pi
Detach two sets of breakaway pins and solder them onto pins 1&2 and 13&14.
The purpose of this will become clear as we proceed with the coding process.
Step 3: Prep Alert Management – AWS
Having some familiarity with Amazon Web Services (AWS) can be helpful for this step, but here's a general overview:
AWS is a cloud computing platform that offers various tools to set up a notification system for our Raspberry Pi. Through AWS, we can establish a communication channel between the Pi and a notification service, which can then relay messages, such as email notifications, as needed. Setting up an AWS account is free, but there may be charges based on your usage.
The basic concept is as follows:
- The Pi sends a message to a notification service topic.
- The topic is configured with a subscription that triggers an action to send an email.
Step 4: Prep Alert Management – Create Topic
To create topics within AWS, we'll be using the SNS service.
SNS enables us to send messages to a specific topic. Once a message is received by the topic, we can set up a subscription to define the action to be taken. In our case, we want to receive email notifications.
Let's proceed by creating a topic in SNS using the AWS Console (refer to the provided screenshot). You only need to provide the name for the topic, such as ‘garage-motion.'
Important: Remember to select the correct region for your setup. It's crucial to keep the region consistent.
After creating the topic, make a note of its ARN (Amazon Resource Name). We'll need it later in the process.
Step 5: Prep Alert Management – Create Subscription
Once again, within the AWS SNS console, proceed to create a Subscription.
From the provided dropdown menu, select your Topic ARN. Choose 'email' as the protocol, and input your email address as the endpoint.
Click on ‘create subscription' located at the bottom of the page.
Now we're prepared to have the Raspberry Pi send messages to AWS, which will then forward them to our specified email address. (Note: You can also choose to send SMS messages and explore other options, but they can be costly.)
Step 6: Pi Zero OS
Use the Raspberry Pi Imager to flash your micro SD card.
It is recommended to choose a Pi OS version that includes a desktop. This is because if you were to connect to the Pi via SSH from another machine to configure networking options for the 4G HAT, the connection would drop and you would be unable to perform any further terminal operations due to the change in IP address and other related factors.
To enable SSH on startup and provide your Wi-Fi credentials, utilize the advanced options cog icon located at the bottom right of the imager. From there, you can configure your wireless LAN and SSH settings.
Once the SD card is installed in the Pi, SSH into it and execute the following command to prepare the GPIO pins:
sudo apt-get install rpi.gpio
Navigate to the Interface Options menu, then select I2C. Enable I2C by choosing the option for ‘Yes'.
Restart the system.
Once the system has rebooted, repeat the same steps for SPI in the Interface Options.
Restart the system again.
Step 7: Install the AWS CLI
pip3 install awscli export PATH=/home/pi/.local/bin:$PATH aws --version aws configure
You'll be asked to input some details you'll be able to grab from your AWS account
Step 8: Install Some Code Dependencies
mkdir projects && cd &_ && mkdir garage-motion && cd &_ && python3 -m venv venv
Create requirements file:
Add requirements to file:
boto3==1.26.45 botocore==1.29.45 colorzero==2.0 gpiozero==1.6.2 jmespath==1.0.1 python-dateutil==2.8.2 RPi.GPIO==0.7.1 s3transfer==0.6.0 six==1.16.0 urllib3==1.26.13
Activate the venv:
Install the requirements:
pip install -r requirements.txt
Step 9: Motion Sensor Connections
Ensure that the sensor is oriented with the sensor facing upwards, and connect the GPIO pins as follows:
- Positive 5V: Connect to the 5V pin (pin 2).
- Output: Connect to GPIO27 (pin 13).
- Ground: Connect to the ground pin (pin 14).
Note: Pin configurations may vary, so refer to the PCB or documentation for accurate pin assignments.
Step 10: Motion Sensor and Sending Message
Open up the file and add the code, being careful of indentation:
import time import boto3 import RPi.GPIO as GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(13, GPIO.IN) # Middle pin defined in Step 9 client = boto3.client('sns') while True: i=GPIO.input(13) if i==0: # When output from motion sensor is LOW print("No intruders",i) time.sleep(0.1) elif i==1: # When output from motion sensor is HIGH print("Intruder detected",i) response = client.publish( TargetArn='yourSNSTopicARN', Subject='Garage update', Message="Yo, you got movement", MessageStructure='string' ) print(response['MessageId']) time.sleep(0.1)
Remember to replace the topic ARN from Step 4 here.
Next step is to run the script:
Stand in front of the motion sensor to test its functionality and trigger an email notification. It is recommended to adjust the potentiometers on the motion sensor and rerun the script.
The right potentiometer controls the sensitivity, while the left potentiometer adjusts the timeout duration. Turning them fully anti-clockwise sets the sensitivity and timeout to their lowest levels.
With the timeout turned fully anti-clockwise, the sensor will produce a signal for approximately 2.5 seconds upon detecting motion. When the potentiometer is turned fully clockwise, the signal will last for around 250 seconds. It is advisable to fine-tune the sensitivity while keeping the timeout duration as low as possible.
Step 11: Time for Local Pi Development!
Next, we will proceed to install the 4G hat on the Raspberry Pi, but please note that once it's configured, the SSH connection will be lost as the traffic won't route through the wireless interface anymore.
Here are the steps to follow:
- Insert your SIM card into the slot on the 4G hat (e.g., using a Things Mobile SIM).
- Securely attach the hat to the Raspberry Pi, ensuring that the pins align properly with the GPIO board.
- Connect your USB mouse and USB keyboard to the ports on the hat.
- Connect the HDMI mini output to the Pi for display.
- Establish a connection between the GPIO pins of the sensor and the Pi.
- Power up the Pi by connecting it to a USB power source, and treat it like a computer by using the mouse, keyboard, and display.
- Open a terminal window to proceed with further instructions.