Home security with raspberry pi zero, open door sensor, buzzer, 2 cams and pushbullet

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Home security with raspberry pi zero, open door sensor, buzzer, 2 cams and pushbullet

    What the system does?
    It detects open door, makes a snapshot from two cameras (one camera is located outside, the other one is located inside), and sends a push notification to your mobile phone via the pushbullet service and a pushbullet app. The notification includes a message with time when the door was opened and two camera snapshots, the second notification is sent when the door is closed, it also includes the time and two camera images from inside and outside.

    you will need:
    two ip cameras with rtsp video streaming, (in my case i use xiaomi ants cameras)
    install ffmpeg on your raspberry pi, you can get installer from here https://github.com/tgogos/rpi_ffmpeg
    open door sensor,
    rpi zero w, (important it has to have wifi)
    also install pushbullet python library https://github.com/randomchars/pushbullet.py
    you will need it for python3,

    so installation command will look like this:
    Code:
    pip3 install pushbullet.py
    Signup at pushbullet service and get your api key for the code below.

    and here is the python code

    Code:
    #!/usr/bin/env python3
    
    #For use with normally closed (NC) Reed Switch connected to ground & GPIO input
    #If using normally open (NO), simply reverse the booleans.
    from pushbullet import Pushbullet
    import subprocess
    import RPi.GPIO as GPIO
    import time
    import _thread
    from datetime import datetime
    from pytz import timezone  
    ukraine = timezone('Europe/Kiev')
    
    
    buzzer_pin = 21
    
    door_sensor = 12
    
    try:
        need_clean = False
    
        #Message Template
        #Leading '\n' is required for sending an email with ':' (SMS/MMS Gateway)
        MSG  = 'Vyshenka\nDoor was '
        DOOR_MSG = {True:'opened', False:'closed'}
    
        #Setting up connection to SMTP Server for sending email/sms.
        print('Setting up SMS...')
        #Function to call on new thread
        #Because of race conditions, this needs to be done quickly or on diff thread
        def send_msg(opened:bool):
    
            #Compile message string to print and send.
            #Ex: '\nDoor was closed at 5:50:20 PM'
            #This way is used because it is quickest and we have race conditions!
            ua_time = datetime.now(ukraine)
            str_print =''.join([MSG, DOOR_MSG[opened], ' at ',
                                ua_time.strftime('%Y-%m-%d _ %H:%M:%S'), ' Ukrainian time'])
    
            pb = Pushbullet('pushbullet_api_key')
                    #send images from cameras
            if opened:
                subprocess.call('ffmpeg -i rtsp://xiaomi_yi_ants_camera_ip:554/ch0_0.h264 -ss 00:00:01.500 -f image2 -vframes 1 -vsync 2 outside1.jpg -y && ffmpeg -i rtsp://xiaomi_yi_ants_camera_ip:554/ch0_0.h264 -ss 00:00:01.500 -f image2 -vframes 1 -vsync 2 inside1.jpg -y', shell=True)
                with open("inside1.jpg", "rb") as pic:
                    file_data = pb.upload_file(pic, "inside1.jpg")
                push = pb.push_file(**file_data)
                with open("outside1.jpg", "rb") as pic:
                    file_data = pb.upload_file(pic, "outside1.jpg")
                push = pb.push_file(**file_data)
            else:
                subprocess.call('ffmpeg -i rtsp://xiaomi_yi_ants_camera_ip:554/ch0_0.h264 -ss 00:00:01.500 -f image2 -vframes 1 -vsync 2 inside2.jpg -y && ffmpeg -i rtsp://xiaomi_yi_ants_camera_ip:554/ch0_0.h264 -ss 00:00:01.500 -f image2 -vframes 1 -vsync 2 outside2.jpg -y', shell=True)
                with open("inside2.jpg", "rb") as pic:
                    file_data = pb.upload_file(pic, "inside2.jpg")
                push = pb.push_file(**file_data)
                with open("outside2.jpg", "rb") as pic:
                    file_data = pb.upload_file(pic, "outside2.jpg")
                push = pb.push_file(**file_data)
    
            # send a note with time
            push = pb.push_note(str_print, str_print)
            print(push)
    
    
    
        #Initializing GPIO
        print('Setting up hardware...')
    
        #GPIO.setmode(GPIO.BOARD)
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(door_sensor, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    
        GPIO.setup(buzzer_pin, GPIO.IN)
        GPIO.setup(buzzer_pin, GPIO.OUT)
        #next_state to check for to send message
        next_state = True
    
        need_clean = True
        buzzer_on = False
        open_time = 0
    
        #Running actual program
        print('Ready!')
        #Run infinitely
        while True:
            #Check for next state
            if GPIO.input(door_sensor) == next_state:
                if open_time==0:
                    open_time = time.time()
                else:
                    open_time = 0
                #Send message on different thread
                _thread.start_new_thread(send_msg, (next_state,))
                #Negate next_state
                next_state = not next_state
    
            if open_time > 0 and (time.time() - open_time) > 60:
                GPIO.output(buzzer_pin, GPIO.HIGH)
                buzzer_on = True
    
            time.sleep(0.3)
    
            if buzzer_on:
                GPIO.output(buzzer_pin, GPIO.LOW)
    
    except KeyboardInterrupt:
        GPIO.cleanup() #For Keyboard Interrupt exit
        need_clean = False
    
    if need_clean:    
        GPIO.cleanup() #For normal exit
    print('\nEnd!')
    I have used the code for open door sensor from a guy in this video https://www.youtube.com/watch?v=fCUJgj_pb4E and added some own code to it.

    you may have noticed in the code, that the buzzer beeps when you door is opened longer than 60 seconds,
    also change the timezone from Kiev/Ukraine to your timezone,
    you will also probably need to install pytz for python3 with this command:

    Code:
    pip3 install pytz
    the wiring diagram looks like this:
    Click image for larger version  Name:	Screenshot 2018-02-12 12.55.33.png Views:	2 Size:	239.5 KB ID:	154550
    Attached Files
    Advertise your mobile site for FREE with AdTwirl


    #2
    Pushbullet provides limited amount of push notifications, if you would like to send more, you have to purchase their pro service.
    But you could use telegram bot to send push notifications almost same way as pushbullet does, and its free.
    If anyone interested in telegram bot script post a request here i will share my new script.
    Advertise your mobile site for FREE with AdTwirl

    Comment


      #3
      can you please share your telegram script Gums, thanks
      wapZan Mobile site builder - Yours is here



      http://wapzan.com?ref=2wap

      Comment

      Working...
      X