Skip to main content

IOT based Garage door lock using raspberry pi

 


Hey,

In today's blog we are going to make a IOT based Garage door lock.


WHAT YOU NEED:

Raspberry pi

Relay module

Connecting wire


CIRCUIT DIAGRAM:


FLASK SETUP:

Here, we have created a web server using Flask, which provides a way to send the commands from webpage to Raspberry Pi to control the Robot over the network. Flask allows us to run our python scripts through a webpage and we can send & receive data from Raspberry Pi to web browser and vice versa. Flask is a microframework for Python. This tool is Unicode based having built-in development server and debugger, integrated unit testing support, support for secure cookies and its easy to use, these things make it useful for the hobbyist.

Run the following commands to install the 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

 

Flask Setup in Raspberry Pi for Controlling Garage Door

 

You can learn more about the programming using Flask here, also check our previous projects where we have used Flask to control robot through webserver,  send the message from Webpage to Raspberry Pi and send weight value to Raspberry Pi in Smart Container.

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


CREATING PYTHON SCRIPT:

This script will interact with our Raspberry Pi GPIOs and sets up the web server.  So, this is the core script for our application. Complete Python Script for door opener is given at the end, here we have explained few parts of it.

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

mkdir garage_door
cd garage_door
nano app.py

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

 

Start by including important libraries.

import RPi.GPIO as GPIO
from flask import Flask, render_template, request
app = Flask(__name__, static_url_path='/static')

 

Now, create a dictionary as pins to store the pin number, name, and pin state. You can use more than one pin according to your need.

pins = {
   14 : {'name' : 'Garage Door', 'state' : GPIO.LOW}
   }

 

Then, set the pin as output and make it low initially.

​for pin in pins:
   GPIO.setup(pin, GPIO.OUT)
   GPIO.output(pin, GPIO.LOW)

 

Now, make a main function to read pin state and store this state in a variable.

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

 

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

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

 

Now, make a function to handle the requests from the URL with the pin number and action in it.

@app.route("/<changePin>/<action>", methods=['GET', 'POST'])
def action(changePin, action):

 

Convert the pin from the URL into an integer.

   changePin = int(changePin)

 

   If the action part of the URL is "open," then do the following.

   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 done with the python script. Now, we have to make a HTML page to interact with the python script.


CREATING HTML PAGE :

In the same garage_door folder, create another folder named templates and inside this folder make an .html file using below commands.

mkdir templates
cd templates
nano main.html

 

Creating HTML Page for Raspberry Pi Webserver

 

In the nano text editor, write the html code. You can edit the <head> part of the page and style it according to your choice. I have just used the third party css scheme using link tag. The complete HTML code is given below:

<!DOCTYPE html>
<head>
   <title>Garage Door Web server</title>
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<center>
<body>
   <h1>Garage Door Web server</h1>
   {% for pin in pins %}
   <h2>{{ pins[pin].name }}
   {% if pins[pin].state == true %}
      is currently <strong>Open</strong></h2><div class="row"><div class="col-md-2">
      <a href="/{{pin}}/close" class="w3-button w3-blue" role="button">Close</a></div></div>
   {% else %}
      is currently <strong>Close</strong></h2><div class="row"><div class="col-md-2">
      <a href="/{{pin}}/open" class="w3-button w3-black" role="button">Open</a></div></div>
   {% endif %}
   {% endfor %}
</body>
</center>
</html>

 

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

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

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

sudo python app.py

 

Creating HTML Page for Raspberry Pi Webserver

 

Open the browser and enter your raspberry pi IP address and hit enter. To find your IP address you can run the below command in terminal.

hostname -I

 

You will see a page like this.

IoT Smart Garage Door Opener Webserver using Raspberry Pi

 

Make sure the relay module is connected to raspberry pi. Press Open button to switch on the Relay or to open the Garage Door. You can also see the state of the relay. As soon as you turned On the Relay, button text will be changed Close to turn off the relay. Now when you click the button again the relay will be turned off and the button text will be changed to Open again.

To stop the server press ctrl+c .

So just connect this relay to some Door Opener mechanism, which are readily available in market, and start controlling the garage door using Smartphone.


CODE :

import RPi.GPIO as GPIO
from flask import Flask, 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("/<changePin>/<action>", methods=['GET', 'POST'])
def action(changePin, action):
   changePin = int(changePin)
   deviceName = pins[changePin]['name']
   if action == "open":
      GPIO.output(changePin, GPIO.HIGH)
   if action == "close":
      GPIO.output(changePin, GPIO.LOW)
   for pin in pins:
      pins[pin]['state'] = GPIO.input(pin)
   templateData = {
      'pins' : pins
   }
   return render_template('main.html', **templateData)
if __name__ == "__main__":
   app.run(host='0.0.0.0', port=80, debug=True)
GPIO.cleanup()



Making electronics project is too pretty simple with Us.



Join and Make with Us.



Comments

Popular posts from this blog

How to use ADXL345 accelerometer with raspberry pi

 Hey,  Today's blog we are going to use ADXL345   Accelerometer with raspberry pi. WHAT YOU NEED : Raspberry pi 3 SD card (8gb+ recommend) Power supply ADXL345 accelerometer  Breadboard CIRCUIT :  PREPARING RASPBERRY PI : 1.  Before we can get our Raspberry Pi to retrieve data from our ADXL345 Accelerometer, there are a few changes we must make to the Pi’s configuration. Let’s first ensure that everything is up to date by running the following two commands. sudo apt-get update sudo apt-get upgrade 2.  Once the Raspberry Pi has finished updating, we will need to go ahead and launch the Raspberry configuration tool so that we can  enable I2C  on the Raspberry Pi. Run the following command to launch the  raspi configuration tool . sudo raspi-config 3.  On this screen, you need to head to the “ 5 Interfacing Options ” menu. You can navigate the  raspi-config  tools menus by using the  arrow keys . Use the  ENTER...

How To Make Pac- Man Game Using Arduino

Hello Guys , In This Post We Will Tell You How To Make A Arduino Based Pac- Man Game. For More Awesome Arduino Projects Subscribe Our Blog. #Microelectronics Components Required- 1-Arduino Uno / Nano  2-Display Module  3- Joystick Module  4- Jumper Wire   Circuit Diagram Is Given Below- Source Code- //Nokia 5110 LCD PacMan Game #include <LCD5110_Graph.h> #include <avr/pgmspace.h> #define RST 12    // RESET #define CE  13    // CS #define DC  11    // Data/Command #define DIN  10   // MOSI #define CLK  9    // SCK LCD5110 myGLCD(CLK, DIN, DC, RST, CE); // LCD5110(SCK, MOSI, DC, RST, CS); extern uint8_t SmallFont[]; const uint8_t pacman1[] PROGMEM={ 0x80, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7E, 0x3E, 0x1C,   // 0x0010 (16) pixels 0x0C, 0x00, 0x00, 0x00, 0x1F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, ...

Controlling A Servo Motor Using MPU 6050 Sensor .

Hello guys, In this post we are going to tell you that how you can control servo motor using MPU Sensor. Please subscribe to our blog, so that you can't miss any update from us.Join us @Microelectronics. Code and circuit diagram is given post. Material required- 1-MPU Sensor 2-An Arduino 3-Servo motor  4-Some jumper wires Circuit diagram- Source code- // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files // for both classes must be in the include path of your project #include "I2Cdev.h" #include "MPU6050_6Axis_MotionApps20.h" //#include "MPU6050.h" // not necessary if using MotionApps include file // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation // is used in I2Cdev.h #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE     #include "Wire.h" #endif #include <Servo.h> Servo myservo;  // create servo object to control a servo   // class default I2C address is 0x...