We have made a SmartLock that can be locked and unlocked through the click of a button on a website, using the raspberry pi.
To begin with we had some troubles gaining access to dev/mem and dev/gpiomem. In step 4 below our solution to that problem is shown.
1) We installed Apache and PHP on the Pi.
2) We made sure that Apache supported PHP.
3) We hosted our website on the Raspberry Pi using Apache.
4) We gave “www-data” (profile) access to dev/mem and dev/gpiomem. This is done in the terminal by writing:
chmod 777 dev/mem
chmod 777 dev/gpiomem
5) We accessed the website using the Pi’s IP address.
Proof of concept:
Fritzing:
Code:
move.py
credit to;
https://github.com/christophrumpel/test_stepper/blob/master/stepper_library.py
from time import sleep
import RPi.GPIO as GPIO
class Motor(object):
def __init__(self, pins):
"""Initialise the motor object.
pins -- a list of 4 integers referring to the GPIO pins that the IN1, IN2
IN3 and IN4 pins of the ULN2003 board are wired to
"""
self.P1 = pins[0]
self.P2 = pins[1]
self.P3 = pins[2]
self.P4 = pins[3]
self.deg_per_step = 5.625 / 64 # for half-step drive (mode 3)
self.steps_per_rev = int(360 / self.deg_per_step) # 4096
self.step_angle = 0 # Assume the way it is pointing is zero degrees
for p in pins:
GPIO.setup(p, GPIO.OUT)
GPIO.output(p, 0)
def _set_rpm(self, rpm):
"""Set the turn speed in RPM."""
self._rpm = rpm
# T is the amount of time to stop between signals
self._T = (60.0 / rpm) / self.steps_per_rev
# This means you can set "rpm" as if it is an attribute and
# behind the scenes it sets the _T attribute
rpm = property(lambda self: self._rpm, _set_rpm)
def move_to(self, angle):
"""Take the shortest route to a particular angle (degrees)."""
# Make sure there is a 1:1 mapping between angle and stepper angle
target_step_angle = 8 * (int(angle / self.deg_per_step) / 8)
steps = target_step_angle - self.step_angle
steps = (steps % self.steps_per_rev)
if steps > self.steps_per_rev / 2:
steps -= self.steps_per_rev
print "moving " + `steps` + " steps"
self._move_acw_2(-steps / 8)
else:
print "moving " + `steps` + " steps"
self._move_cw_2(steps / 8)
self.step_angle = target_step_angle
def __clear(self):
GPIO.output(self.P1, 0)
GPIO.output(self.P2, 0)
GPIO.output(self.P3, 0)
GPIO.output(self.P4, 0)
#Counter clockwise
def _move_acw_2(self, big_steps):
self.__clear()
for i in range(big_steps):
GPIO.output(self.P3, 0)
GPIO.output(self.P1, 1)
sleep(self._T * 2)
GPIO.output(self.P2, 0)
GPIO.output(self.P4, 1)
sleep(self._T * 2)
GPIO.output(self.P1, 0)
GPIO.output(self.P3, 1)
sleep(self._T * 2)
GPIO.output(self.P4, 0)
GPIO.output(self.P2, 1)
sleep(self._T * 2)
#Clockwise
def _move_cw_2(self, big_steps):
self.__clear()
for i in range(big_steps):
GPIO.output(self.P4, 0)
GPIO.output(self.P2, 1)
sleep(self._T * 2)
GPIO.output(self.P1, 0)
GPIO.output(self.P3, 1)
sleep(self._T * 2)
GPIO.output(self.P2, 0)
GPIO.output(self.P4, 1)
sleep(self._T * 2)
GPIO.output(self.P3, 0)
GPIO.output(self.P1, 1)
sleep(self._T * 2)
def unlock(self):
self.move_to(-90)
def lock(self):
self.move_to(90)
lock.py
from move import Motor
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
motor = Motor([18,22,24,26])
motor.rpm = 10
motor.lock()
print('locked')
GPIO.cleanup()
lock.php
<?php
exec("python lock.py 2>&1",$out);
foreach($out as $value){
echo $value;
}
?>
unlock.py
from move import Motor
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
motor = Motor([18,22,24,26])
motor.rpm = 10
motor.unlock()
print('unlocked')
GPIO.cleanup()
unlock.php
<?php
exec("python unlock.py 2>&1",$out);
foreach($out as $value){
echo $value;
}
?>
my.js
$(document).ready(function () {
$("#lockBtn").click(function () {
if($(this).text() === 'Lock'){
$(this).text('Unlock');
$.get('unlock.php');
} else {
$(this).text('Lock');
$.get('lock.php');
}
});
});
function lockoutSubmit(button) {
var oldValue = button.value;
button.setAttribute('disabled', true);
button.value = '...processing...';
setTimeout(function(){
button.value = oldValue;
button.removeAttribute('disabled');
}, 3000)
}
Project zip:
IOT_SmartLock_Website
