Fitness Rep Counter IoT with Gmail connection

This project uses a digital display that shows the number of reps done within a physical exercise, with the help of an ultrasonic sensor.

BeginnerFull instructions provided3 hours200
Fitness Rep Counter IoT with Gmail connection

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
Breadboard (generic)
Breadboard (generic)
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
SparkFun 7-Segment Serial Display - Red
SparkFun 7-Segment Serial Display - Red
×1
Female/Female Jumper Wires
Female/Female Jumper Wires
×6
Male/Male Jumper Wires
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×2
Resistor 1k ohm
Resistor 1k ohm
×1
Resistor 2.21k ohm
Resistor 2.21k ohm
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian
Python

Story

Read more

Schematics

Project schematics

Follow this to recreate the project:

Code

proiect_sm.py

Python
The script can be ran using a Python 3 interpreter
import RPi.GPIO as GPIO
import time, os
import tm1637
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

#input/output
GPIO.setmode(GPIO.BCM)
GPIO_TRIGGER = 23
GPIO_ECHO = 24
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)

#check display functionality
Display = tm1637.TM1637(clk=5,dio=4)
Display.show('-ON-')

#distance determination method
def distance():
    GPIO.output(GPIO_TRIGGER, True)
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)

    StartTime = time.time()
    StopTime = time.time()

    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()

    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()

    TimeElapsed = StopTime - StartTime
    #sonic speed(34300 cm/s)
    distance = (TimeElapsed * 34300) / 2

    return distance

#reps check method
def verifyRep(lastRep):
    if (time.time() - lastRep < 1):
        return False
    else:
        return True

#mail section
mail_content = '''Tocmai au fost inregistrate 10 repetari '''

#mail and password to be set
sender_address = '...@gmail.com'
sender_pass = '...'
receiver_address = '...@gmail.com' 

if __name__ == '__main__':

    inBench = True
    firstRep = False
    #false for down, true for up
    dir = False
    repCounter = 0
    lastRep = time.time()
    Display.write([0, 0, 0, 0])
    try:
        while True:
            dist = distance()

            if (dist < 30):
                if (not firstRep):
                    firstRep = True
                    dir = True
                    lastRep = time.time()
                    print(repCounter)
                elif (dir and verifyRep(lastRep)):
                    dir = False
                    lastRep = time.time()
                    print(repCounter)
                elif (verifyRep(lastRep)):
                    repCounter += 1;
                    dir = True
                    lastRep = time.time()
                    print(repCounter)
                    converted_repCounter = str (repCounter)
                    Display.show(converted_repCounter)
                else:
                    print("DEBOUNCE")
                #mail condition
                if(repCounter == 10):
                    #Setup the MIME
                    message = MIMEMultipart()
                    message['From'] = sender_address
                    message['To'] = receiver_address
                    message['Subject'] = 'Mail sent from Raspberry Pi.'
                    #The body and the attachments for the mail
                    message.attach(MIMEText(mail_content, 'plain'))
                    #Create SMTP session for sending the mail
                    session = smtplib.SMTP('smtp.gmail.com', 587)
                    session.starttls() #enable security
                    session.login(sender_address, sender_pass)
                    text = message.as_string()
                    session.sendmail(sender_address, receiver_address, text)
                    session.quit()
                    print('Mail Sent')
            time.sleep(0.05)

    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()

Credits

Dorian Luca

Dorian Luca

1 project • 4 followers
Paul Chiriță

Paul Chiriță

1 project • 5 followers
Andrei Petcu

Andrei Petcu

1 project • 6 followers
Alexandru Manole

Alexandru Manole

1 project • 3 followers

Comments

Add projectSign up / Login