Raspberry Pi Garage Controller with HD Video (piCam) – [Cloned #102]

Raspberry Pi Garage Controller with HD Video (piCam) – [Cloned #102]

raspberry-pi-relay-parts

This only works with Rpi with GPIO headers- so please don’t ask if it will work with other devices.

This is a complete rewrite of my old tutorial, with much better software choices thanks to time.

In my original example, I used a raspberry pi 1 as the additional software (webiopi) at the time was not compatible with anything other than the pi1.

Link to Chris Driscoll’s Initial Instructions- Driscocity

1. **Setup Raspberry Pi:** – Download the latest Raspberry Pi OS (formerly Raspbian) from the official website and follow the installation instructions to flash it onto an SD card. – After booting up your Raspberry Pi, open a terminal and run the following commands to update the system: “`bash sudo apt update -y && sudo apt upgrade -y “` 2. **Install Required Software:** – Install Flask, a Python web framework, Picamera for PiCamera module, and RPi.GPIO for GPIO control: “`bash sudo apt install python3-flask python3-picamera python3-rpi.gpio “` 3. **Create Flask Web Application:** – Create a Python script for your Flask application. Let’s call it `app.py`. – Here’s the complete code for `app.py`: “`python from flask import Flask, render_template, Response from picamera import PiCamera import RPi.GPIO as GPIO from time import sleep import io import atexit app = Flask(__name__) camera = PiCamera() # Set up GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT, initial=GPIO.LOW) # Define a route to toggle GPIO 7 with debounce @app.route(‘/toggle_gpio’, methods=[‘POST’]) def toggle_gpio(): # Implement debounce mechanism sleep(0.5) # Adjust debounce time as needed GPIO.output(7, not GPIO.input(7)) # Toggle GPIO state return ‘GPIO 7 toggled successfully’ # Route to stream PiCamera @app.route(‘/stream’) def stream(): return Response(gen_frames(), mimetype=’multipart/x-mixed-replace; boundary=frame’) def gen_frames(): stream = io.BytesIO() for _ in camera.capture_continuous(stream, format=’jpeg’, use_video_port=True): stream.seek(0) yield (b’–framern’ b’Content-Type: image/jpegrnrn’ + stream.read() + b’rn’) # Clean up resources on application exit def cleanup(): camera.close() GPIO.cleanup() atexit.register(cleanup) # Error handling @app.errorhandler(404) def not_found(error): return ‘Page Not Found’, 404 @app.errorhandler(Exception) def server_error(error): app.logger.exception(error) return ‘Internal Server Error’, 500 if __name__ == ‘__main__’: app.run(host=’0.0.0.0′, port=80, debug=True) “` 4. **Create HTML Template:** – Inside the `templates` directory (create one if it doesn’t exist), create or update the `index.html` file with the following content: “`html

 

GPIO Control

Live Stream

Live Stream

“` 5. **Run the Flask Application:** – Save the `app.py` file and the `index.html` file inside the `templates` directory. – Run the Flask application using the following command: “`bash python3 app.py “` With these instructions, you’ll have a Flask web application running on your Raspberry Pi that provides GPIO control and a live stream from the PiCamera. You can access the live stream by navigating to `http:///stream` in a web browser. Make sure to replace “ with the actual IP address of your Raspberry Pi.

Here’s a simple Python script to open and close the garage door using the relay:

**6. Run the Script:**
– Save the script to your Raspberry Pi and run it.
– When you press Enter, the script should activate the relay, simulating a button press on the garage door opener remote.

**7. Test the Garage Door Opener:**
– Make sure the garage door responds as expected. You may need to adjust the delay in the script to match your garage door opener’s requirements.

**8. Secure the Components:**
– Securely mount the Raspberry Pi and relay module, ensuring they are safely positioned and do not interfere with the garage door’s operation.

**9. Finalize and Test:**
– Test the system thoroughly to ensure it reliably opens and closes the garage door.

Remember to exercise caution and prioritize safety when working on this project. It’s essential to ensure that the garage door opener functions reliably and does not pose any safety risks.