This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import time | |
import subprocess | |
import signal | |
import logging | |
from range_sensor import RangeSensor | |
from graceful_kill import GracefulKiller | |
logging.basicConfig(format='%(asctime)s: %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p', filename='/var/log/detect_person.log', filemode='w', level=logging.INFO) | |
r = RangeSensor() | |
gk = GracefulKiller() | |
def turn_tv_on(): | |
subprocess.call(['/home/pi/tv_on.sh']) | |
def turn_tv_off(): | |
subprocess.call(['/home/pi/tv_off.sh']) | |
STATE_OFF=0 | |
STATE_ON=1 | |
thresh = 50 #cm | |
t_start =0 | |
t_on=314 #time to stay on, in seconds | |
if __name__=="__main__": | |
logging.info("Starting up...") | |
distance=r.read() | |
old_distance=distance | |
state = STATE_OFF | |
while True: | |
if gk.killed(): | |
logging.info("Now shutting down, received signal") | |
break | |
old_distance = distance | |
distance= r.read() | |
if state==STATE_OFF: | |
if ((old_distance - distance) > thresh): #only when someone moves closer | |
state=STATE_ON | |
turn_tv_on() | |
t_start = int(time.time()) | |
logging.info("MIRROR ON") | |
logging.info("DISTANCE: %f OLD DISTANCE: %f"%(distance,old_distance)) | |
elif state==STATE_ON: | |
if ((t_start + t_on) < time.time()): | |
logging.info("MIRROR OFF") | |
state=STATE_OFF | |
turn_tv_off() | |
turn_tv_off() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Service to turn on tv when someone is in front of it | |
[Service] | |
Type=simple | |
ExecStart=/home/pi/detect_person.py | |
RemainAfterExit=yes | |
[Install] | |
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import signal | |
import time | |
class GracefulKiller: | |
kill_now=False | |
def __init__(self): | |
signal.signal(signal.SIGINT, self.exit_gracefully) | |
signal.signal(signal.SIGTERM, self.exit_gracefully) | |
def exit_gracefully(self,signum,frame): | |
self.kill_now=True | |
def killed(self): | |
return self.kill_now |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import RPi.GPIO as GPIO | |
import time | |
TRIG = 23 | |
ECHO = 18 | |
class RangeSensor: | |
def read(self): | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(TRIG,GPIO.OUT) | |
GPIO.setup(ECHO,GPIO.IN) | |
GPIO.output(TRIG, False) | |
time.sleep(2) | |
GPIO.output(TRIG, True) | |
time.sleep(0.00001) | |
GPIO.output(TRIG, False) | |
while GPIO.input(ECHO)==0: | |
pulse_start = time.time() | |
while GPIO.input(ECHO)==1: | |
pulse_end = time.time() | |
pulse_duration = pulse_end - pulse_start | |
distance = pulse_duration * 17150 | |
distance = round(distance, 2) | |
GPIO.cleanup() | |
return distance | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Control power to tv | |
[Service] | |
Type=oneshot | |
ExecStart=/home/pi/tv_on.sh | |
ExecStop=/home/pi/tv_off.sh | |
RemainAfterExit=yes | |
[Install] | |
WantedBy=single-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#if tv is on, turn it off | |
POWER_ON=$(/home/pi/tv_read_power_state.sh) | |
if [ "$POWER_ON" = "1" ]; then | |
/home/pi/tv_toggle_power.sh | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#if tv is off, turn it on | |
POWER_ON=$(/home/pi/tv_read_power_state.sh) | |
if [ "$POWER_ON" = "0" ]; then | |
/home/pi/tv_toggle_power.sh | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
gpio -g mode 15 in | |
gpio -g read 15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
gpio -g mode 14 out | |
gpio -g write 14 1 | |
sleep 0.25 | |
gpio -g write 14 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment