-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
63 lines (51 loc) · 2.59 KB
/
main.py
File metadata and controls
63 lines (51 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import serial
import re
import time
from health_metrics import HealthMetrics # Your Calculation Module
from refined_value import RefinedUploader # Your New Uploader Module
# --- CONFIGURATION ---
SERIAL_PORT = 'COM5'
BAUD_RATE = 115200
def run_system_brain():
# 1. Initialize the specialized uploader from refined_value.py
# This automatically connects to MySQL using your credentials
uploader = RefinedUploader()
try:
# 2. Initialize the Serial connection to Arduino
ser = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1)
print(f"--- 2026 Health Monitor Brain Active ---")
print(f"Connected to {SERIAL_PORT}. Waiting for sensor data...")
time.sleep(2) # Stabilize connection
while True:
if ser.in_waiting > 0:
# Read raw line from Arduino
line = ser.readline().decode('utf-8').strip()
# Process only lines containing the averaged data
if "AVG" in line:
# Regex to extract [Samples, BPM, SpO2]
numbers = re.findall(r"\d+\.\d+|\d+", line)
if len(numbers) >= 3:
raw_bpm = float(numbers[1])
raw_spo2 = float(numbers[3])
# STEP 1: CALCULATE (using health_metrics.py)
# Gets Glucose, HRV, PI, RR, and Status
metrics = HealthMetrics.calculate_all(raw_bpm, raw_spo2)
# STEP 2: UPLOAD (using refined_value.py)
# Pushes all 7 parameters to 'refined_health_data' table
uploader.upload_to_db(raw_bpm, raw_spo2, metrics)
# STEP 3: FEEDBACK
print(f"\n[NEW RECORD SAVED]")
print(f"BPM: {raw_bpm} | SpO2: {raw_spo2}% | Status: {metrics['status']}")
print(f"Glucose: {metrics['glucose']} mg/dL | HRV: {metrics['hrv']} ms")
elif "No finger" in line:
print("Status: Waiting for patient finger...")
except serial.SerialException as e:
print(f"Hardware Error: {e}")
except KeyboardInterrupt:
print("\nShutting down system brain...")
finally:
# Safely close database and serial connections
uploader.close()
if 'ser' in locals(): ser.close()
if __name__ == "__main__":
run_system_brain()