Home smart alarm

Detecting the motion in your house, sending a notification and voice control

IntermediateFull instructions provided391
Home smart alarm

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Male/Male Jumper Wires
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Buzzer
Buzzer
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Resistor 1k ohm
Resistor 1k ohm
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×1
Resistor 2.21k ohm
Resistor 2.21k ohm
×1

Software apps and online services

SiriControl
SiriControl is an open source framework designed with developers in mind. It provides a simple way of using Siri commands by dynamically loading modules created by the user.

Story

Read more

Schematics

homealarm_ZcPje5xbcd.jpg

Hardware Diagram

Code

Project code

Python
import time
import RPi.GPIO as GPIO
import pigpio
import smtplib
import ssl


 
port=465
smtp_server = "smtp.gmail.com"
sender_email= ""
receiver_email= ""
password= ""
message = """\
        Subject: ALARM!!!

        Motion Detected in your house."""



context=ssl.create_default_context()


pi=pigpio.pi()
GPIO.setmode(GPIO.BOARD)
trig=16
echo=18
buzzer=18
button=25
GPIO.setup(trig,GPIO.OUT)
GPIO.setup(echo,GPIO.IN)
pi.set_mode(buzzer,pigpio.OUTPUT)
pi.set_pull_up_down(button,pigpio.PUD_UP)

def calculateDist():
        GPIO.output(trig,GPIO.LOW)
        time.sleep(0.05)
        GPIO.output(trig,GPIO.HIGH)
        time.sleep(0.00001)
        GPIO.output(trig,GPIO.LOW)

        start=time.time()
        stop=time.time()

        while GPIO.input(echo)==0:
            start=time.time()

        while GPIO.input(echo)==1:
            stop=time.time()

        duration=stop-start

        distance=34300/2*duration

        #if distance < 3400:
            #print("Distance = %.2f" % distance)
        return distance 
      

print("The system calibrating...")
i=0
val=0
while i<20:
    val=val+calculateDist()
    i=i+1
    #print(i)
val=val/20
print("Average value: %.2f" % val)

try:
    while True:
        ti=0
        ok=True
        dist=calculateDist()
        eroare=val-dist
        if(eroare<0):
            eroare=eroare*(-1)
        while ok:
            #print("Everything is fine!")
            dist=calculateDist()
            eroare=val-dist
            if(eroare<0):
                eroare=eroare*(-1)
            if(eroare<5*val/100):
                ok=True
            else:
                if(ti==0):
                    ok=True
                    ti=ti+1
                else:
                    ok=False
        pi.hardware_PWM(buzzer,500,500000)
        with smtplib.SMTP_SSL(smtp_server,port,context=context) as server:
            server.login(sender_email,password)
            server.sendmail(sender_email,receiver_email, message)
        i=0
        while pi.read(button)==1:
            if pi.read(buzzer)==0:
                start=time.time()
                while pi.read(buzzer)==0:
                    stop=time.time()
                    print(stop-start)
                    i=i+1
                    if(stop-start>1):
                        break
                if(stop-start>1):
                    break
            #print("ALARMA %d" % ti)
        pi.hardware_PWM(buzzer,0,0)
except KeyboardInterrupt:
    pass

pi.write(buzzer,0)
pi.stop()
GPIO.cleanup()

Module that turn off the sound of the buzzer

Python
#You can import any required modules here
import RPi.GPIO as GPIO
#This can be anything you want
moduleName = "AlarmOff"

#All of the words must be heard in order for this module to be executed
commandWords = ["turn","off","alarm"]

def execute(command):
    #Write anything you want to be executed when the commandWords are heard
    #The 'command' parameter is the command you speaik
    buzzer=12

    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(buzzer,GPIO.OUT)
    GPIO.output(buzzer,GPIO.LOW)
    
    return

SiriControl

Credits

Claudiu Şerban Zălincă

Claudiu Şerban Zălincă

1 project • 2 followers
Andrei.Gurzun

Andrei.Gurzun

1 project • 1 follower
ballan.marius

ballan.marius

1 project • 1 follower
condrea octavian

condrea octavian

1 project • 2 followers

Comments