Files
evaluation/filter_dev/ble.py
2025-04-13 22:55:55 +02:00

48 lines
1.6 KiB
Python

import matplotlib.pyplot as plt
from time import time
import asyncio
from bleak import BleakClient, BleakScanner
import pandas as pd
from argparse import ArgumentParser
from tqdm.auto import tqdm
async def read_ble(reading_time=READING_TIME):
device = await BleakScanner.find_device_by_name("Smaage")
timestamps = []
raw_weights = []
async with BleakClient(device.address) as client:
print(f"Connected to {device}")
# Read the characteristic value
pbar = tqdm(desc="reading data", total=reading_time)
time_start = time()
time_passed = 0
last_time_passed = 0
while time_passed < reading_time:
try:
# Read the sensor data
millis = await client.read_gatt_char(MILLIS_UUID)
millis = int.from_bytes(millis, byteorder='little') # Adjust based on your data format
weight = await client.read_gatt_char(WEIGHT_UUID)
weight = int.from_bytes(weight, byteorder='little') # Adjust based on your data format
timestamps.append(millis)
raw_weights.append(weight)
except Exception as e:
print(f"Error reading data: {e}")
break
time_passed = time() - time_start
time_delta = (time_passed - last_time_passed)
last_time_passed = time_passed
pbar.update(time_delta)
pbar.close()
df = pd.DataFrame({"timestamps": timestamps, "weights": raw_weights})
print(df['weights'].describe())
plt.plot(timestamps, raw_weights)
plt.show()