Automate your garage using a Raspberry Pi 3 B+
I got tired of leaving my garage opened by mistake overnight a few times. Not only that is not safe, but also it's an open invitation for someone driving by to come in and break into the house. To solve this problem, I used the new Raspberry Pi 3 B+, an IR Beam Break Sensor, a 2 Channel Relay Module, the Twilio API for SMS messaging and some Python codes. Now the garage communicates with me via SMS and I can reply back with a text to Open, Close or to check Status. If the garage stays open past 11:00 pm it will notify me via text, if I don't reply back within 5 minutes, it will automatically close it for me.
Here are the codes to make that happen. Enjoy it!
- Raspberry Pi 3 B, B+
- 2-Channel Relay (or Any compatible relay)
- IR Break Beam Sensor
- Project box plastic enclosure
- Garage remote (optional)
- Twilio Account for sending & receiving SMS messages.
Clone this repository in your home directory of the Raspberry Pi
git clone https://github.com/eddieespinal/GarageAutomation.git
You need to setup a virtual enviroment to install the dependancies that are required for this project.
Make sure you are inside the GarageAutomation directory
Install the virtualenv library
pip install virtualenv
Now create a virtual environment called
Now you need to activate this new virtual environment
There is a file called
requirements.txt inside the
GarageAutomation folder. Run the following command to install the dependancies listed there.
pip install -r requirements.txt
Go inside the
GarageAutomation directory and create a copy of the .env-sample and name it .env
Make a copy of
.env-sample and name it
mv .env-sample .env
Edit the .env file with your own settings from Twilio. Replace
YOUR_ACCOUNT_SID with your own. Also enter your own
TWILIO_NUMBER, this will be used as the From number when sending messages from the Raspberry Pi.
sudo nano .env
TWILIO_ACCOUNT_SID="YOUR_ACCOUNT_SID" TWILIO_AUTH_TOKEN="YOUR_AUTH_TOKEN" TWILIO_NUMBER="+18009997777" TO_NUMBER="+18006663333"
Save your changes by pressing
garageautomation.py file and configure the time you want the garage to automatically notify you and the duration before automatically closing itself.
sudo nano garageautomation.py
alarmTriggerTime = "11:00 PM" notificationDelayInSeconds = 300 # five minutes in seconds
Save your changes by pressing
This means that the garage will notify you at 11:00 PM if the door is opened via SMS. If you don't reply back with the
Close comand it will automatically close itself 5 minutes from the time you got notified.
You can change these values only if you don't want to use the same pins for the relay and the IR sensor. You can use the following image for reference.
IRSENSORPIN = 17 RELAYPIN = 18
Automatically run this script when the Raspberry Pi boot
In order to fully automate this project, we need to make sure that when the Raspberry start it runs the garageautomation.py script. Edit the following file to do that.
sudo nano /etc/rc.local
Copy and paste the following command at the end of this file but before
(sleep 10; . /home/pi/GarageAutomationRaspberryPi/venv/bin/activate; python /home/pi/GarageAutomationRaspberryPi/garageautomation.py)&