I am doing a project where I am tracking my sleep with a fitbit. Basically, when
ID: 3794408 • Letter: I
Question
I am doing a project where I am tracking my sleep with a fitbit. Basically, when a person falls asleep, a tv is going to turn off (if its on). The tv is going to recieve a signal from an arduino. The arduino is going to recieve a command from a raspberry pi to execute. I was able to get the data from the fitbit using python, a variable used in the output window of the python code is minutesOfSleep = 10( it tells me how many minutes the person has fallen sleep).
I do now know how to create a python code that will send a signal to the arduino when minuteOfSleep > 0. I am new to python and i have no idea how to start. Please help
Explanation / Answer
import time from subprocess import call import os import RPi.GPIO as GPIO PIR = 9 # data pin of PIR sensor (in) LED = 7 # positive pin of LED (out) timestamp = '/home/pi/events/motiontime' # file to store last motion detection time (in epoch) SOUND = '/home/pi/events/sounds/Hello.wav' # reaction sound # GPIO setup GPIO.setmode(GPIO.BCM) GPIO.setup(PIR,GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(LED,GPIO.OUT) # function which gets called when motion is reported (sensor includes own delay-until-hot again # and sensibility settings def my_callback(channel): now = time.time() # store current epoch time in variable 'now' f = open(timestamp, "r") then = float(f.readline()) # read last detection time from file difference = now - then # calculate time that has passed call(['/home/pi/bin/kitchenlights.sh', '-1']) # turn light on call(['/home/pi/bin/lighttimer.sh']) # schedule at job to turn lights off if difference > 160: # if more than 160 seconds without activity have passed then... GPIO.output(LED, True) # turn on LED if not os.path.isfile("/home/pi/events/muted"): # check if system is muted, else call(['/usr/bin/mplayer', '-really-quiet', '-noconsolecontrols', SOUND]) # play sound GPIO.output(LED, False) # turn of LED f = open(timestamp, "w") f.write(repr(now)) # update timestamp f.close() else: # when less than 160 seconds have passed do nothing and f = open(timestamp, "w") f.write(repr(now)) # update timestamp (thus increasing the interval of silence) f.close() GPIO.add_event_detect(PIR, GPIO.RISING,callback=my_callback,bouncetime=100) # add rising edge detection on a channel while True: time.sleep(0.2) pass
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.