As part of this blog post we are going to setup a Key-less Door entry system using a Servo motor and a Raspberry Pi A+, basically we are going to use the servo motor to open and close the door lock bolt, when you are at the front door of your house and enter the password in the browser as shown in the video below.This is kind off two step authentication , where you will be able to open the door, if you mobile device is connected to your home WiFi network + you know the secret password which you need to enter in the browser.
We are going to setup a simple web app using flask which is a micro web development framework for Python, and then connect the servo to a PWM pin of the Raspberry Pi A+.
Here are the steps you will need to complete to setup flask, and the code to drive the servo.
#1 Installing Flask on the Pi
once you have ssh'ed into you Pi use the following commands
sudo apt-get update
sudo apt-get upgrade
install pip , a python package management system using
sudo apt-get install python-pip
install flask , which we are going to use to build a simple web app
sudo pip install flask
#2 Test the flask install
Setup a simple flask web app to check if the installation was successful, open the nano editor and copy paste the code below
sudo nano demoFlaskApp.py
And then to run the web app
sudo python demoFlaskApp.py
from flask import Flask app = Flask(__name__) @app.route('/') def doorlock(): return 'Hello python flask!' if __name__ == '__main__': app.run(host='0.0.0.0', port=80, debug=True)
To open the app in your browser type: http://ipaddressOfPi:80
#3 Connect the Servo to the Raspberry Pi A+
As part of the circuit
- Connect the servo to GPIO pin number 18 on the Pi, which is a PWM pin
- To power up the servo I am using 4 AA batteries (this is a temporary setup, but in the final setup we are going to cut a USB cable to power up the servo)
- Connect the GND pin of the Pi to GND pin of battery pack and the servo.
- I am also using a 1K ohms resistor to protect the GPIO pin from unexpected high current .
Note : In my case, I am using a Raspberry Pi A+ , but you can use any version of the Pi for this setup.
#4 Testing the Servo
Now lets write a small program to test the servo motor. Copy paste the code below into the nano editor
sudo nano servo.py
mport RPi.GPIO as GPIO import time import sys argVal = sys.argv[1] print argVal GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) pwm = GPIO.PWM(18, 50) pwm.start(7.5) try: #Reading the servo state value from the file to check if is Open/Closed fRead = open('servoState.txt') stateVal = fRead.read() print 'State value in file:'+ stateVal fRead.close() if argVal == 'open' and stateVal == 'close': #Open the door print 'opening the servo lock' pwm.ChangeDutyCycle(7.5) # turn towards 90 degree #Open fWrite = open('servoState.txt','w') fWrite.write('open') fWrite.close() time.sleep(1) elif argVal == 'close' and stateVal == 'open': #Close the door pwm.ChangeDutyCycle(2.5) # turn towards 0 degree time.sleep(1) # sleep 1 second print 'closing the servo lock' #pwm.ChangeDutyCycle(12.5) # turn towards 180 degree fWrite = open('servoState.txt','w') fWrite.write('close') fWrite.close() time.sleep(1) else: print 'do nothing' #except KeyboardInterrupt: pwm.stop() GPIO.cleanup() except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror)
you will also have to create a file, servoState.txt to store the state of the servo, basically to check the position of servo, that is if the door is open or closed
sudo nano servoState.txt
and type in open
Now to test type in, this will move the servo to one end
sudo python servo.py close
And to move the servo to the other end type in
sudo python servo.py open
#5 Once you have your servo's moving download zip which contains the flask web app and FTP it to your Pi
In the webApp folder run the following command to run the web-app
sudo python dooropen.py
Note: if you are using the same code in the zip file, the password is PiIoT to open the door ..