Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Cristian
Published © Apache-2.0

Digital Photo Frame Activated by PIR Motion with a Raspberry

The photo frame turns on when it detects movement and shows nice pictures. Photos are shared with a Samba server.

BeginnerFull instructions provided12 hours4,775

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
HDMI 8" LCD Screen Kit (1024x768)
Pimoroni HDMI 8" LCD Screen Kit (1024x768)
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Audio / Video Cable Assembly, Ultra Slim RedMere HDMI to HDMI
Audio / Video Cable Assembly, Ultra Slim RedMere HDMI to HDMI
×1
Picture frame
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Hand tools and fabrication machines

Tape, Clear
Tape, Clear

Story

Read more

Code

main.py

Python
import RPi.GPIO as GPIO
import subprocess
import datetime
import time
from multiprocessing import Process

GPIO.setmode(GPIO.BCM)
PIR_PIN = 12
GPIO.setup(PIR_PIN, GPIO.IN)

SCREEN_ON = False
MOVEMENT_ON= False
TIMEOUT = 20

def fim(action):
    if action == "kill":
        bashCommand = "killall fim"
    else:
        bashCommand = "fim /home/pi/Selection -a -H -q --slideshow 7 --random -T 1"
    process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
    #process = subprocess.Popen(bashCommand.split())
    output, error = process.communicate()
    if error is not None:
        print("## Error at fim at {}:".format(action))
        print(output)
        print(error)

def turn_tv(action):
    if action == "on":
        bashCommand = "vcgencmd display_power 1"
    else:
        bashCommand = "vcgencmd display_power 0"
    process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
    #process = subprocess.Popen(bashCommand.split())
    output, error = process.communicate()
    if error is not None:
        print("## Error at turning tv: {}".format(action))
        print(output)
        print(error)

def MOTION(PIR_PIN):
    global MOVEMENT_ON
    global SCREEN_ON
    MOVEMENT_ON = True
    if SCREEN_ON == False:
        print("In movement detected - Turning on - {} {}".format(MOVEMENT_ON, SCREEN_ON))
        turn_tv("on")
        time.sleep(2)
        # fim("start")
        p = Process(target=fim, args=("start", ))
        p.start()
        SCREEN_ON = True
    print("Motion detected - {}".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))

print("PIR Module Test (CTRL+C to exit)")
time.sleep(2)
print("Ready")

try:
    GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
    while True:
        #print("Loop, Movement", MOVEMENT_ON, "Screen", SCREEN_ON)
        if (MOVEMENT_ON == False) and (SCREEN_ON == True):
            print("No movement detected - Turning off - {} {}".format(MOVEMENT_ON, SCREEN_ON))
            fim("kill")
            #time.sleep(2)
            turn_tv("off")
            SCREEN_ON = False
        elif (MOVEMENT_ON == True) and (SCREEN_ON == False):
            print("Movement detected - Turning on - {} {}".format(MOVEMENT_ON, SCREEN_ON))
            turn_tv("on")
            #time.sleep(2)
            p = Process(target=fim, args=("start", ))
            p.start()
            SCREEN_ON = True
        elif (MOVEMENT_ON == True) and (SCREEN_ON == True):
            print("Inaction interval - Preparing to turn off - {} {}".format(MOVEMENT_ON, SCREEN_ON))
            MOVEMENT_ON = False
        time.sleep(TIMEOUT)
except KeyboardInterrupt:
    print("Quit")
    fim("kill")
    turn_tv("off")
    GPIO.cleanup()
except Exception as e:
    print(e)

Credits

Cristian

Cristian

7 projects • 9 followers

Comments