Simple Python Relay Switch For Door Unlock

View previous topic View next topic Go down

Simple Python Relay Switch For Door Unlock

Post by jamied_uk on 27th August 2016, 12:44



Inside Electronic Lock is 12 Volt from Amazon (12 V DC Power adapter required)!
RFID From Amazon (if I remember correctly) (12 Volt same adapter runs both rfid and lock)!
Normal Door Lock (Works with the other lock) from Ebay (comes with 3 keys)
RPI 3 Amazon

Code:
import RPi.GPIO as GPIO
import time
import os
# https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/buttons_and_switches
#
# http://raspi.tv/2013/rpi-gpio-basics-6-using-inputs-and-outputs-together-with-rpi-gpio-pull-ups-and-pull-downs
#adjust for where your switch is connected
# Custom Pins
buttonPin=11 # 17
doorPin=7 # 17
SLEEP=4
SLEEP2=1
HIGH=True #1
LOW=False #0
# pin setups
GPIO.setmode(GPIO.BOARD) # or BOARD or BCM
GPIO.setup(buttonPin,GPIO.IN)
GPIO.setup(doorPin,GPIO.OUT)

try:
 while True:
  #assuming the script to call is long enough we can ignore bouncing
  if GPIO.input(buttonPin)==0:
#  if GPIO.input(buttonPin)==0:
    #this is the script that will be called (as root)
    # unlock door here
       print "Door Locked!"
       time.sleep(SLEEP2)
  else:
       GPIO.output(doorPin, LOW)
       print "Door Unlocked!"
       time.sleep(SLEEP)
       GPIO.output(doorPin, HIGH)
    #os.system("python /home/pi/start.py")
finally:
 GPIO.cleanup()

Open door via Web Page


Code:
import RPi.GPIO as GPIO
import time
import os
# https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/buttons_and_switches
# http://jnet.forumn.biz/t1381-simple-python-relay-switch-for-door-unlock#1959
# http://raspi.tv/2013/rpi-gpio-basics-6-using-inputs-and-outputs-together-with-rpi-gpio-pull-ups-and-pull-downs
#adjust for where your switch is connected
# Custom Pins
buttonPin=11 # 17
doorPin=7 # 17
SLEEP=4
SLEEP2=1
HIGH=True #1
LOW=False #0
# pin setups
GPIO.setmode(GPIO.BOARD) # or BOARD or BCM
GPIO.setup(buttonPin,GPIO.IN)
GPIO.setup(doorPin,GPIO.OUT)

GPIO.output(doorPin, LOW)
print "Door Unlocked!"
time.sleep(SLEEP)
GPIO.output(doorPin, HIGH)
#os.system("python /home/pi/start.py")
#finally:
GPIO.cleanup()


Web PHP Code




Code:
<?php
require('sas.php');
// may need to comment out this require line to make this work
// but if it works leave it alone!
// http://www.openhomeautomation.net/control-a-relay-from-anywhere-using-the-raspberry-pi
// doorPin=7
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>J~Net Remote Access Control & Automation System</title>
  <script type="text/javascript" src="script.js"></script>
</head>
<body>
<center>
<STYLE TYPE="text/css">
BODY {
    background-color:#0099ff;
    background-image: url('/images/bg.png');
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: center;
}
h1 {
color:#1aff1a;
}
</STYLE>

<div style="clear:left; height:24px;"></div>
<div style="float:center; height:auto; width:500px;background:#3366FF; margin-left:25px; margin-right:5px; border:#CCC 2px solid;">

<h1>J~Net Door Lock</h1>
<form id="relay" action="remote_relay.php" method="GET">
    <input type="button" id="submitOn" style="border:1px solid #000; font-size:40px;" value="Unlock" onClick="relayOn()"/>
  
    <input type="hidden" name="total" id="total" value="">
</form>

<p><br>
</div>

<?php
exec("sudo gpio -g mode 7 out");
$state = $_GET["total"];
if ($state == "on"){
exec("sudo gpio -g write 7 1");
$command = escapeshellcmd('sudo python /home/pi/Documents/scripts/doorlock/open.py');
$output = shell_exec($command);
echo $output;
// dealy 5 secs
//sleep(5);
//exec("sudo gpio -g write 7 0");
header('Location: /remote_relay.php');
}
//if ($state == "off"){
//exec("sudo gpio -g write 7 0");
//}
?>

</body>


Last edited by jamied_uk on 30th August 2016, 12:14; edited 2 times in total

jamied_uk
Admin

Posts : 1972
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: Simple Python Relay Switch For Door Unlock

Post by jamied_uk on 29th August 2016, 20:41

Extra Security (IP White-list)


Code:
<?php
require('sas.php');
// may need to comment out this require line to make this work
// but if it works leave it alone!
// http://www.openhomeautomation.net/control-a-relay-from-anywhere-using-the-raspberry-pi
// doorPin=7
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>J~Net Remote Access Control & Automation System</title>
  <script type="text/javascript" src="script.js"></script>
</head>
<body>
<center>
<STYLE TYPE="text/css">
BODY {
    background-color:#0099ff;
    background-image: url('/images/bg.png');
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: center;
}
h1 {
color:#1aff1a;
}
</STYLE>

<div style="clear:left; height:24px;"></div>
<div style="float:center; height:auto; width:500px;background:#3366FF; margin-left:25px; margin-right:5px; border:#CCC 2px solid;">

<h1>J~Net Door Lock</h1>
<form id="relay" action="remote_relay.php" method="GET">
    <input type="button" id="submitOn" style="border:1px solid #000; font-size:40px;" value="Unlock" onClick="relayOn()"/>
  
    <input type="hidden" name="total" id="total" value="">
</form>

<p><br>
</div>

<?php
$auth=array("192.168.0.1", "192.168.0.3", "192.168.0.4", "192.168.0.5", "192.168.0.9");
$ip="";
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}
if (in_array($ip, $auth)) {
   //  echo "Authorized!";

exec("sudo gpio -g mode 7 out");
$state = $_GET["total"];
if ($state == "on"){
exec("sudo gpio -g write 7 1");
$command = escapeshellcmd('sudo python /home/pi/Documents/scripts/doorlock/open.py');
$output = shell_exec($command);
echo $output;
header('Location: /remote_relay.php');
}
}
?>
</body>

jamied_uk
Admin

Posts : 1972
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: Simple Python Relay Switch For Door Unlock

Post by jamied_uk on 30th August 2016, 12:16

J~Net Trip Wire can be used with this and for that you will need a mail server setup for emails upon unauthorized access attempts!

I used python html js css.

I had better luck allowing www-data sudo access to run the python script from web server!

jamied_uk
Admin

Posts : 1972
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: Simple Python Relay Switch For Door Unlock

Post by jamied_uk on 30th August 2016, 12:18

TripWire


jamied_uk
Admin

Posts : 1972
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

Re: Simple Python Relay Switch For Door Unlock

Post by Sponsored content Today at 16:04


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum