Frank Burke-Olson
Published

Lane Tech HS - PCL - Internet of Things Door Buzzer

An outdated door buzzer system is upgraded by connecting a Raspberry Pi to enable a user to remotely unlock the door using their phone.

IntermediateFull instructions provided8 hours5,160
Lane Tech HS - PCL - Internet of Things Door Buzzer

Things used in this project

Hardware components

Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
RobotGeek Relay
RobotGeek Relay
This relay board can be used instead of making your own.
×1
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
SparkFun Relay
×1
Android device
Android device
×1

Software apps and online services

Dataplicity
Etcher
IP Webcam App for Android

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Wiring diagram for raspberry pi

Code

control.py

Python
Controls the relay
import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BOARD)

GPIO.setup(12,GPIO.OUT)

GPIO.output(12,0)
GPIO.output(12,1)
time.sleep(0.5)

GPIO.cleanup()

os.system("sudo python image.py &")

image.py

Python
Downloads image from internet link
import urllib
import os
u=urllib.URLopener()
f=u.open("LOCATION OF IMAGE TO BE DOWNLOADED")
open("photo.jpg", "w").write(f.read())

print "image obtained!"

os.system("sudo python testmail.py &")

testmail.py

Python
#!/usr/bin/python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os

gmail_user = "YOUR USERNAME"
gmail_pwd = "YOUR PASSWORD"

def mail(to, subject, text, attach):
   msg = MIMEMultipart()

   msg['From'] = gmail_user
   msg['To'] = to
   msg['Subject'] = subject

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   # Should be mailServer.quit(), but that crashes...
   mailServer.close()

mail("EMAIL TO RECIEVE MESSAGE",
   "Door was used",
   "Yeah",
   "photo.jpg")

print "sent?"

listener.py

Python
Runs constantly in the background, reads for high signal
import RPi.GPIO as GPIO
import time
import os



while True:
        time.sleep(0.5)

        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(10,GPIO.IN)
        state = GPIO.input(10)

        if(state):
                os.system("sudo python /home/dataplicity/actions/action/buzzer/image.py &")
                print "next"
                break
        else:
                GPIO.cleanup()

time.sleep(20)
os.system("sudo python /home/dataplicity/actions/action/buzzer/listener.py &")

dataplicity.actions.conf

SH
Creates the custom actions panel
[section:Buzz Door]

[onoff:Buzz]
on:/home/dataplicity/actions/on-off/leds/control-state red on
off:/home/dataplicity/actions/on-off/leds/control-state red off
get:/home/dataplicity/actions/on-off/leds/get-state red

get-state

SH
Code for getting the state of the pin
#!/bin/bash
#
# DESCRIPTION:
#     This is a Custom Actions program that gets the state in which the LEDs are in.
#
# PRE-REQUISITES:
#     For this script to work the "dataplicity" user has to be added
#     to "gpio" group. This can be done from sudo account such as "pi"
#     with the use of the following command:
#         "sudo usermod -a -G gpio dataplicity"
#

# Check if "dataplicity" user is in "gpio" group
grep gpio /etc/group | grep dataplicity > /dev/null

if [[ $? -eq 1 ]]; then
    echo "Can't run script, Please add \"dataplicity\" user to \"gpio\" group."
    exit 1
fi

# Enforce user to supply one argument
if [[ $# -ne 1 ]]; then
    echo "[$0] Incorrect amount of arguments, 1 required."
	echo "Argument 1 must be: \"red\" or \"green\"."
    exit 1
fi

# Check if LED_PIN argument is correct
case $1 in
    red)
        LED_PIN=23
        ;;
    green)
        LED_PIN=24
        ;;
    *)
        echo "[$0] Incorrect argument - \"red\" or \"green\" expected."
        exit 1
        ;;
esac

# Check if LED GPIO pin was previously initialized
ls /sys/class/gpio/ | grep gpio$LED_PIN > /dev/null

# If not initialized return error, else return current state
if [[ $? -eq 1 ]]; then
    echo "[[[ReturnError:LED Not Initialized]]]"
else
    echo "[[[ReturnOK:$(cat /sys/class/gpio/gpio$LED_PIN/value)]]]"
fi

control-state

SH
Used to control the state of the pin
#!/bin/bash
#
# DESCRIPTION:
#     This is a Custom Actions program that controls LEDs.
#
# PRE-REQUISITES:
#     For this script to work the "dataplicity" user has to be added
#     to "gpio" group. This can be done from sudo account such as "pi"
#     with the use of the following command:
#         "sudo usermod -a -G gpio dataplicity"
#


# Check if "dataplicity" user is in "gpio" group
grep gpio /etc/group | grep dataplicity > /dev/null

if [[ $? -eq 1 ]]; then
    echo "Can't run script, Please add \"dataplicity\" user to \"gpio\" group."
    exit 1
fi


# Enforce user to supply two arguments
if [[ $# -ne 2 ]]; then
    echo "[$0] Incorrect amount of arguments, 2 required."
	echo "Argument 1 must be: \"red\" or \"green\"."
	echo "Argument 2 must be: \"on\" or \"off\"."
    exit 1
fi


# Check if LED_PIN argument is correct
case $1 in
    red)
        LED_PIN=23
        ;;
    green)
        LED_PIN=24
        ;;
    *)
        echo "[$0] Incorrect argument ($1) - \"red\" or \"green\" expected."
        exit 1
        ;;
esac


# Check if CONTROL argument is correct
case $2 in
    on)
        CONTROL=1
        ;;
    off)
        CONTROL=0
        ;;
    *)
        echo "[$0] Incorrect argument ($2) - \"on\" or \"off\" expected."
        exit 1
        ;;
esac


# Check if LED GPIO pin was previously initialized
ls /sys/class/gpio/ | grep gpio$LED_PIN > /dev/null

# Initialize, if not previously initialized
if [[ $? -eq 1 ]]; then
    echo "$LED_PIN" > /sys/class/gpio/export
    sleep 0.5
fi


# Switch LED ON or OFF
echo "out" > /sys/class/gpio/gpio$LED_PIN/direction
echo "$CONTROL" > /sys/class/gpio/gpio$LED_PIN/value


# Custom Action executed succesfully
echo "[[[ReturnOK]]]"

Credits

Frank Burke-Olson

Frank Burke-Olson

2 projects • 10 followers

Comments