Home > Control Circuits >Other Control Circuitss > Build a Smart Garage Door Opener Using a Raspberry Pi

Build a Smart Garage Door Opener Using a Raspberry Pi

Source: InternetPublisher:兰博 Keywords: Internet of Things Smart Garage Updated: 2024/12/27

In this tutorial, we will build a smart garage door opener using Raspberry Pi. Here we will create a Raspberry Pi web server to open and close the garage door using your smartphone.

Required Components

Raspberry Pi board with Raspbian installed

Relay Module

Connect the wires

It is assumed that your Raspberry Pi has been flashed with an operating system and is able to connect to the Internet. If not, please follow the Raspberry Pi Getting Started tutorial before continuing. Here we use Rasbian Jessie to install the Raspberry Pi 3.

Here, an external monitor using an HDMI cable is used as a monitor connected with the Raspberry Pi. If you don't have a monitor, you can connect to the Raspberry Pi using a laptop or computer using an SSH client (Putty) or a VNC server. Learn more about setting up a Raspberry Pi headlessly here .

Circuit Schematic

pYYBAGMYTfiAN0QyAAD9ws9nxmk903.png

Connect the garage door opener mechanism to the output of the relay. Here, for demonstration purpose, we have just connected an LED with the relay.

pYYBAGMYTfOAWMGLAASHt1Fi9Fo986.png

Flask setup for controlling garage door on Raspberry Pi

Here, we have created a web server using Flask, which provides a way to send commands from a web page to the Raspberry Pi to control the robot over the network. Flask allows us to run our python scripts through a web page, and we can send and receive data from the Raspberry Pi to the web browser and vice versa. Flask is a microframework for Python. This tool is based on Unicode and has a built-in development server and debugger, integrated unit testing support, support for secure cookies, and its ease of use, which makes it useful for hobbyists.

Run the following command to install Flask in your Raspberry Pi:

sudo apt-get update

sudo apt-get install python-pip python-flask

Now, run the pip command to install Flask and its dependencies:

sudo pip install flask

poYBAGMYTfCAEWoAAAG2CxYxbDo157.png

You can learn more about programming with Flask here , and you can also check out our previous projects where we used Flask to control a robot through a webserver, send messages from a webpage to a Raspberry Pi, and send weight values ​​to a Raspberry Pi in a Smart Controller.

Now, we will write a Python script for our garage door web server.

Creating a Python Script for a Smart Garage Door Opener

This script will interact with our Raspberry Pi GPIO and set up the web server. So, this is the core script of our application. Finally, given is the complete door opener Python script, here we explain its several parts.

First, make a folder. All other required folders should be in this folder only. Run the following command to create a folder and create a python file named app.py inside this folder.

mkdirgarage_do

This will open the Nano editor where we have to write our script.

First include the important libraries.

from flask import RPi.GPIO as GPIO
Import Flask, render_

Now, create a dictionary as pins to store the pin number, name, and pin status. You can use as many pins as you want.

pin = { 
   14 : {'name' : 'Garage door', 'state' : GPIO.LOW} 
   }

Then, set the pin to output and initially set it low.

For a pin in a pin:
   GPIO.setup(pin, GPIO.OUT)
   GPIO.OUTPUT(pin, GPIO.LOW)

Now, create a main function to read the pin status and store this status in a variable.

@app.route("/") 
def main(): 
   for pin in pin: 
      pins[pin]['state'] = GPIO.input(pin) 
..

We have to pass this data to our html page so that we can control the state of the input button.

   return render_template('main.html', **templateData)

Now, create a function to handle the request from the URL, which contains the pin number and action.

@app.route("//", methods=['GET', 'POST']) 
def

Converts a pin from a URL to an integer.

   changePin = int(changePin)

If the action portion of the URL is "Open", the following actions are performed.

   if action == "open": 
      GPIO.output(changePin, GPIO.HIGH) 
   if action == "close": 
      GPIO.output(changePin, GPIO.LOW)

You can copy the complete script from the end of this tutorial and save it using ctrl+x and then press enter. We have completed the python script. Now, we have to make an HTML page to interact with the python script.

Creating HTML Pages for the Raspberry Pi Webserver

In the same garage_door folder, create another folder named templates and in that folder create a .html file using the following command.

pYYBAGMYTemAY8sSAAAmGqAS9WA150.png

In the nano text editor, write html code. You can edit the page

section and style it according to your choice. I have just used a third party css solution using link tag. The complete HTML code is as follows:
poYBAGMYUCSAFDSRAACNFBWxHMM937.png

The important part here is to create a button to open and close the door and assign a state to the open and close button. This button will send and get the GPIO state from the python script.

You can use the above given HTML code in your editor and save it. Now the web server is ready to start.

Open terminal and navigate to garage_door folder and run the following command

sudo python app.py

poYBAGMYTeOAHXbcAAFP5FZ-epg436.png

Open your browser and type in your Raspberry Pi IP address and press enter. To find your IP address, you can run the following command in the terminal.

Hostname-I

You will see a page like this.

poYBAGMYTeCAOj0wAABi9Exhqd8256.png

Make sure the relay module is connected to the Raspberry Pi. Press the On button to turn on the relay or open the garage door. You can also view the status of the relay. Once you turn on the relay, the button text will change to Off to turn off the relay. Now, when you click the button again, the relay will turn off and the button text will change to On again.

To stop the server, press ctrl+c.

So, just connect this relay to some door opener mechanism readily available in the market and start controlling your garage door using your smartphone. from flask import RPi.GPIO import Flask
as GPIO , render_template, request app = Flask(__name__, static_url_path='/static') GPIO.setmode(GPIO.BCM) pins = { 14 : { 'name' : 'Garage Door', 'state' : GPIO.LOW} } for pin in pins: GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, GPIO.LOW) @app.route("/") def main(): for pin in pins: pins[pin]['state'] = GPIO.input(pin) templateData = { 'pins' : pins } return render_template('main. html', **templateData) @app.route("//", methods=['GET', 'POST']) def action(changePin, action): changePin = int(changePin) deviceName = pin[changePin]['name'] if action == "open": GPIO.output(changePin, GPIO.HIGH) if action == "close": GPIO.output(changePin, GPIO.LOW) for pin in pins : pin[pin]['state'] = GPIO.input(pin) templateData = { "pin": pin } return render_template('main.html', **templateData) if __name__ == "__main__": app.run(host='0.0.0.0', port=80, debug=True) GPIO.cleanup()




































































EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号