HyperChiicken
Published © GPL3+

SmartEdge Gym Buddy: A Workout Tracker

Use Raspberry Pi, SmartEdge Agile, Brainium Portal, and PyQt to visualize motion capture/recognition, in this case exercises, on the fly.

IntermediateFull instructions provided2 hours1,079
SmartEdge Gym Buddy: A Workout Tracker

Things used in this project

Story

Read more

Code

GymBuddyTracker.py

Python
Run this to launch the GUI and keep track of motion recognition alerts
from PyQt5 import QtWidgets, QtGui, QtCore
from GymBuddy_GUI import Ui_MainWindow
import uuid
import paho.mqtt.client as mqtt
import json
import logging
logging.basicConfig(level=logging.DEBUG)

counter1 = 0
counter2 = 0
counter3 = 0

mqtt_user_name = 'oauth2-user'
mqtt_password = '' # copy and paste here external access token from your account
user_id = ''  # copy and paste here your user id
device_id = ''  # copy and paste here your device id

print("user: " + user_id)
print("device: " + device_id)

alerts_topic = '/v1/users/{user_id}/in/alerts'.format(user_id=user_id)
alerts_topic_device_spec = '/v1/users/{user_id}/in/devices/{device_id}/datasources/alerts'.format(user_id=user_id, device_id=device_id)
motion_topic = '/v1/users/{user_id}/in/devices/{device_id}/datasources/MOTION'.format(user_id=user_id, device_id=device_id)
magnetic_field_topic = '/v1/users/{user_id}/in/devices/{device_id}/datasources/MAGNETIC_FIELD_NORM'.format(user_id=user_id, device_id=device_id)

print("alerts topic: " + alerts_topic)
print("alerts topic device specific: " + alerts_topic_device_spec)

ca_cert_path = 'cacert.crt'

def increment1():
    global counter1
    counter1 += 1

def increment2():
    global counter2
    counter2 += 1

def increment3():
    global counter3
    counter3 += 1

def on_connect(client, userdata, flags, rc):
    print('Connected with result code {code}'.format(code=rc))

def on_message(client, userdata, msg):
    print('Msg received from topic={topic}\n{content}'.format(topic=msg.topic, content=str(msg.payload)))

class mywindow(QtWidgets.QMainWindow):
    client_message = QtCore.pyqtSignal(object)

    def __init__(self, mqtt_client):
        super(mywindow, self).__init__()
        self._mqtt_client = mqtt_client    
        self.setup_ui()

    def setup_ui(self):      
        self.ui = Ui_MainWindow()      
        self.ui.setupUi(self)
        
        self.ui.label.setFont(QtGui.QFont('SansSerif', 8)) # change font type and size
        self.ui.label.setText("PUSH-UP") #change label text

        self.ui.label_2.setFont(QtGui.QFont('SansSerif', 8)) # change font type and size
        self.ui.label_2.setText("SQUATS") #change label text       

        self.ui.label_3.setFont(QtGui.QFont('SansSerif', 8)) # change font type and size
        self.ui.label_3.setText("PULL-UP") #change label text

        self.ui.progressBar.setMinimum(0)
        self.ui.progressBar.setMaximum(30)
        self.ui.progressBar.setValue(0)

        self.ui.progressBar_2.setMinimum(0)
        self.ui.progressBar_2.setMaximum(30)
        self.ui.progressBar_2.setValue(0)

        self.ui.progressBar_3.setMinimum(0)
        self.ui.progressBar_3.setMaximum(10)
        self.ui.progressBar_3.setValue(0)

        # Webhook events
        self._mqtt_client.on_connect = self.on_client_connect
        # Get signal to process events in the GUI thread
        self._mqtt_client.on_message = lambda c, d, msg: self.client_message.emit(msg)
        self.client_message.connect(self.on_client_message)
    
    def on_client_message(self, msg):
        print('Msg received from topic={topic}\n{content}'.format(topic=msg.topic, content=str(msg.payload)))
        decoded_msg = json.loads(msg.payload.decode('utf-8'))
        msg_list = decoded_msg[0]

        code = msg_list['code']
        name = msg_list['name']
        receivedAt = msg_list['receivedAt']

        print("Motion Type: " + str(name))
        print("Code: " + str(code))
        print("Received At: " + str(receivedAt))

        # Update GUI LCD and progress bars depending on received motion event
        if code == 0:
            print("0 received")
            increment1()
            self.ui.lcdNumber.display(counter1)
            self.ui.progressBar.setValue(counter1)
        if code == 1:
            print("1 received")
            increment2()
            self.ui.lcdNumber_2.display(counter2)
            self.ui.progressBar_2.setValue(counter2)
        if code == 2:
            print("2 received")
            increment3()
            self.ui.lcdNumber_3.display(counter3)
            self.ui.progressBar_3.setValue(counter3)

    # The callback when the client receives a CONNACK response from the server.
    def on_client_connect(self, client, userdata, flags, rc):
        print("Connected with result code " + str(rc))   

if __name__ == "__main__":
    import sys

    client = mqtt.Client(client_id=str(uuid.uuid4()), transport='websockets')

    app = QtWidgets.QApplication(sys.argv)
    application = mywindow(client)
    application.show()
    
    client.enable_logger()
    client.tls_set(ca_certs=ca_cert_path)
    client.username_pw_set(mqtt_user_name, mqtt_password)
    client.connect('ns01-wss.brainium.com', 443)
    client.subscribe(motion_topic)
    client.loop_start()
    try:
        sys.exit(app.exec_()) 
    finally:
        client.loop_stop()

GymBuddy_GUI.py

Python
Converted .ui file to .py using pyuic5
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'GymBuddy_GUI.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(680, 305)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(17, 50, 55, 16))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(17, 120, 55, 16))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(17, 200, 55, 16))
        self.label_3.setObjectName("label_3")
        self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
        self.progressBar.setGeometry(QtCore.QRect(227, 20, 441, 71))
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName("progressBar")
        self.progressBar_2 = QtWidgets.QProgressBar(self.centralwidget)
        self.progressBar_2.setGeometry(QtCore.QRect(227, 100, 441, 71))
        self.progressBar_2.setProperty("value", 24)
        self.progressBar_2.setObjectName("progressBar_2")
        self.progressBar_3 = QtWidgets.QProgressBar(self.centralwidget)
        self.progressBar_3.setGeometry(QtCore.QRect(227, 180, 441, 71))
        self.progressBar_3.setProperty("value", 24)
        self.progressBar_3.setObjectName("progressBar_3")
        self.lcdNumber = QtWidgets.QLCDNumber(self.centralwidget)
        self.lcdNumber.setGeometry(QtCore.QRect(110, 20, 111, 71))
        self.lcdNumber.setObjectName("lcdNumber")
        self.lcdNumber_2 = QtWidgets.QLCDNumber(self.centralwidget)
        self.lcdNumber_2.setGeometry(QtCore.QRect(110, 100, 111, 71))
        self.lcdNumber_2.setObjectName("lcdNumber_2")
        self.lcdNumber_3 = QtWidgets.QLCDNumber(self.centralwidget)
        self.lcdNumber_3.setGeometry(QtCore.QRect(110, 180, 111, 71))
        self.lcdNumber_3.setObjectName("lcdNumber_3")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 680, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "TextLabel"))
        self.label_2.setText(_translate("MainWindow", "TextLabel"))
        self.label_3.setText(_translate("MainWindow", "TextLabel"))

Credits

HyperChiicken

HyperChiicken

11 projects • 28 followers
I cobble things together :]

Comments