/heartrate.py Secret
Last active
March 13, 2017 06:57
Star
You must be signed in to star a gist
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 RPi.GPIO as io # import the GPIO library we just installed but call it "io" | |
from ISStreamer.Streamer import Streamer # import the IS Streamer we just installed but call it "Streamer" | |
SAMPLE_COUNT = 10 | |
## name the bucket and individual access_key | |
## the bucket_key will send all of our messages to the same place | |
## the access_key tells Initial State to send the messages to you | |
streamer=Streamer(bucket_name="Heart Rate Monitor",bucket_key="heartrate",access_key="Your_Access_Key_Here") | |
## set GPIO mode to BCM | |
## this takes GPIO number instead of pin number | |
io.setmode(io.BCM) | |
io.setwarnings(False) | |
receiver_in = 23 # this is the GPIO number our receiver is connected to | |
LED_in = 24 # GPIO number the LED is connected to | |
io.setup(receiver_in, io.IN) # initialize receiver GPIO to take input | |
io.setup(LED_in, io.OUT) # initialized LED GPIO to give output | |
io.output(LED_in, io.HIGH) # start with LED off | |
## log that everything is ready to receive the heartbeat signal | |
streamer.log("msg","Waiting for heartbeat") | |
## this try block looks for 1 values (indicate a beat) from the transmitter | |
try: | |
firstBeatTime = time.time() | |
sampleCounter = 0 | |
while True: | |
if sampleCounter == 0: | |
firstBeatTime = time.time() | |
## sample will either be 1 or 0 | |
sample = io.input(receiver_in) | |
if sample == 1: | |
io.output(LED_in, io.LOW) # turn LED on | |
sampleCounter = sampleCounter + 1 | |
if sampleCounter == SAMPLE_COUNT: | |
sampleCounter = 0 # reset the sample counter | |
# calculate beats per minute given the SAMPLE_COUNT | |
sampleMinutes = (time.time() - firstBeatTime)/60 | |
bpm = sampleMinutes * SAMPLE_COUNT | |
# stream | |
streamer.log("bpm", bpm) | |
else: | |
io.output(LED_in, io.HIGH) # turn LED off | |
# Set the previous sample to the current sample so that it can be used to | |
# evaluate if at the front edge of the heartbeat and not count it more than | |
# once if already in the high position | |
previousSample = sample | |
## this allows you to end the script with ctrl+c | |
except KeyboardInterrupt: | |
streamer.log("msg", "Received Interrupt") | |
streamer.close() # safely close the streamer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can i read the data directly from pulse sensor without any adc or any converter to get the values like this. I'm trying that way and am facing problems in it with calculations. If you have any idea on it help me naa....
thank you